2015-06-21 23:33:46 +08:00
|
|
|
/*
|
2024-01-02 05:15:26 +08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
|
|
|
#include <SDL3/SDL_main.h>
|
|
|
|
|
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#include <stdlib.h>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2024-01-25 01:37:00 +08:00
|
|
|
#if defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_EMSCRIPTEN) || defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_LINUX)
|
2015-06-21 23:33:46 +08:00
|
|
|
#define HAVE_OPENGLES2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENGLES2
|
|
|
|
|
2022-11-27 12:43:38 +08:00
|
|
|
#include <SDL3/SDL_opengles2.h>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
typedef struct GLES2_Context
|
|
|
|
{
|
2023-05-19 19:10:02 +08:00
|
|
|
#define SDL_PROC(ret, func, params) ret (APIENTRY *func) params;
|
2015-06-21 23:33:46 +08:00
|
|
|
#include "../src/render/opengles2/SDL_gles2funcs.h"
|
|
|
|
#undef SDL_PROC
|
|
|
|
} GLES2_Context;
|
|
|
|
|
2022-03-27 01:31:17 +08:00
|
|
|
typedef struct shader_data
|
|
|
|
{
|
|
|
|
GLuint shader_program, shader_frag, shader_vert;
|
|
|
|
|
|
|
|
GLint attr_position;
|
|
|
|
GLint attr_color, attr_mvp;
|
|
|
|
|
|
|
|
int angle_x, angle_y, angle_z;
|
|
|
|
|
|
|
|
GLuint position_buffer;
|
|
|
|
GLuint color_buffer;
|
|
|
|
} shader_data;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2023-06-25 22:18:58 +08:00
|
|
|
typedef enum wait_state
|
|
|
|
{
|
|
|
|
WAIT_STATE_GO = 0,
|
|
|
|
WAIT_STATE_ENTER_SEM,
|
|
|
|
WAIT_STATE_WAITING_ON_SEM,
|
|
|
|
} wait_state;
|
|
|
|
|
2022-08-16 11:51:15 +08:00
|
|
|
typedef struct thread_data
|
|
|
|
{
|
|
|
|
SDL_Thread *thread;
|
2023-06-25 22:18:58 +08:00
|
|
|
SDL_Semaphore *suspend_sem;
|
|
|
|
SDL_AtomicInt suspended;
|
2022-08-16 11:51:15 +08:00
|
|
|
int done;
|
|
|
|
int index;
|
|
|
|
} thread_data;
|
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
static SDLTest_CommonState *state;
|
|
|
|
static SDL_GLContext *context = NULL;
|
|
|
|
static int depth = 16;
|
2023-06-25 22:18:58 +08:00
|
|
|
static SDL_bool suspend_when_occluded;
|
2015-06-21 23:33:46 +08:00
|
|
|
static GLES2_Context ctx;
|
2024-07-27 19:06:54 +08:00
|
|
|
static shader_data *datas;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
static int LoadContext(GLES2_Context *data)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2023-03-30 05:49:01 +08:00
|
|
|
#ifdef SDL_VIDEO_DRIVER_UIKIT
|
2015-06-21 23:33:46 +08:00
|
|
|
#define __SDL_NOGETPROCADDR__
|
2023-03-30 05:49:01 +08:00
|
|
|
#elif defined(SDL_VIDEO_DRIVER_ANDROID)
|
2015-06-21 23:33:46 +08:00
|
|
|
#define __SDL_NOGETPROCADDR__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined __SDL_NOGETPROCADDR__
|
2022-12-01 04:51:59 +08:00
|
|
|
#define SDL_PROC(ret, func, params) data->func = func;
|
2015-06-21 23:33:46 +08:00
|
|
|
#else
|
2022-12-01 04:51:59 +08:00
|
|
|
#define SDL_PROC(ret, func, params) \
|
|
|
|
do { \
|
2023-01-10 06:55:12 +08:00
|
|
|
data->func = (ret (APIENTRY *) params)SDL_GL_GetProcAddress(#func); \
|
2022-12-01 04:51:59 +08:00
|
|
|
if (!data->func) { \
|
2017-03-27 03:00:19 +08:00
|
|
|
return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
|
2022-12-01 04:51:59 +08:00
|
|
|
} \
|
|
|
|
} while (0);
|
2015-12-25 20:41:23 +08:00
|
|
|
#endif /* __SDL_NOGETPROCADDR__ */
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
#include "../src/render/opengles2/SDL_gles2funcs.h"
|
|
|
|
#undef SDL_PROC
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2024-07-27 19:06:54 +08:00
|
|
|
SDL_free(datas);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (context) {
|
2015-06-21 23:33:46 +08:00
|
|
|
for (i = 0; i < state->num_windows; i++) {
|
|
|
|
if (context[i]) {
|
2024-07-11 02:50:57 +08:00
|
|
|
SDL_GL_DestroyContext(context[i]);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_free(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLTest_CommonQuit(state);
|
2023-04-12 17:07:28 +08:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
#define GL_CHECK(x) \
|
|
|
|
x; \
|
|
|
|
{ \
|
|
|
|
GLenum glError = ctx.glGetError(); \
|
|
|
|
if (glError != GL_NO_ERROR) { \
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
|
2022-12-01 04:51:59 +08:00
|
|
|
quit(1); \
|
|
|
|
} \
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2022-12-01 04:51:59 +08:00
|
|
|
* Simulates desktop's glRotatef. The matrix is returned in column-major
|
|
|
|
* order.
|
2015-06-21 23:33:46 +08:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rotate_matrix(float angle, float x, float y, float z, float *r)
|
|
|
|
{
|
|
|
|
float radians, c, s, c1, u[3], length;
|
|
|
|
int i, j;
|
|
|
|
|
2022-11-26 03:35:24 +08:00
|
|
|
radians = (angle * SDL_PI_F) / 180.0f;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
c = SDL_cosf(radians);
|
|
|
|
s = SDL_sinf(radians);
|
|
|
|
|
|
|
|
c1 = 1.0f - SDL_cosf(radians);
|
|
|
|
|
|
|
|
length = (float)SDL_sqrt(x * x + y * y + z * z);
|
|
|
|
|
|
|
|
u[0] = x / length;
|
|
|
|
u[1] = y / length;
|
|
|
|
u[2] = z / length;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
r[i] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r[15] = 1.0;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
|
|
|
|
r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2022-12-01 04:51:59 +08:00
|
|
|
* Simulates gluPerspectiveMatrix
|
2015-06-21 23:33:46 +08:00
|
|
|
*/
|
2022-12-01 04:51:59 +08:00
|
|
|
static void
|
2015-06-21 23:33:46 +08:00
|
|
|
perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float f;
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
f = 1.0f / SDL_tanf(fovy * 0.5f);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
r[i] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r[0] = f / aspect;
|
|
|
|
r[5] = f;
|
|
|
|
r[10] = (znear + zfar) / (znear - zfar);
|
|
|
|
r[11] = -1.0f;
|
|
|
|
r[14] = (2.0f * znear * zfar) / (znear - zfar);
|
|
|
|
r[15] = 0.0f;
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2015-06-21 23:33:46 +08:00
|
|
|
* Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
|
|
|
|
* major. In-place multiplication is supported.
|
|
|
|
*/
|
|
|
|
static void
|
2022-12-02 05:07:03 +08:00
|
|
|
multiply_matrix(const float *lhs, const float *rhs, float *r)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2015-11-26 04:39:28 +08:00
|
|
|
int i, j, k;
|
2015-06-21 23:33:46 +08:00
|
|
|
float tmp[16];
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
tmp[j * 4 + i] = 0.0;
|
|
|
|
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
r[i] = tmp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2015-06-21 23:33:46 +08:00
|
|
|
* Create shader, load in source, compile, dump debug as necessary.
|
|
|
|
*
|
|
|
|
* shader: Pointer to return created shader ID.
|
|
|
|
* source: Passed-in shader source code.
|
|
|
|
* shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
|
|
|
|
*/
|
2022-03-27 01:31:17 +08:00
|
|
|
static void
|
2022-12-01 04:51:59 +08:00
|
|
|
process_shader(GLuint *shader, const char *source, GLint shader_type)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
GLint status = GL_FALSE;
|
|
|
|
const char *shaders[1] = { NULL };
|
|
|
|
char buffer[1024];
|
2022-03-27 01:31:17 +08:00
|
|
|
GLsizei length = 0;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Create shader and load into GL. */
|
|
|
|
*shader = GL_CHECK(ctx.glCreateShader(shader_type));
|
|
|
|
|
|
|
|
shaders[0] = source;
|
|
|
|
|
|
|
|
GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
|
|
|
|
|
|
|
|
/* Clean up shader source. */
|
|
|
|
shaders[0] = NULL;
|
|
|
|
|
|
|
|
/* Try compiling the shader. */
|
|
|
|
GL_CHECK(ctx.glCompileShader(*shader));
|
|
|
|
GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
|
|
|
|
|
|
|
|
/* Dump debug info (source and log) if compilation failed. */
|
2022-11-28 00:38:43 +08:00
|
|
|
if (status != GL_TRUE) {
|
2022-03-27 01:31:17 +08:00
|
|
|
ctx.glGetShaderInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
|
2015-06-21 23:33:46 +08:00
|
|
|
buffer[length] = '\0';
|
2022-03-27 01:31:17 +08:00
|
|
|
SDL_Log("Shader compilation failed: %s", buffer);
|
2015-06-21 23:33:46 +08:00
|
|
|
quit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 01:31:17 +08:00
|
|
|
static void
|
|
|
|
link_program(struct shader_data *data)
|
|
|
|
{
|
|
|
|
GLint status = GL_FALSE;
|
|
|
|
char buffer[1024];
|
|
|
|
GLsizei length = 0;
|
|
|
|
|
|
|
|
GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
|
|
|
|
GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
|
|
|
|
GL_CHECK(ctx.glLinkProgram(data->shader_program));
|
|
|
|
GL_CHECK(ctx.glGetProgramiv(data->shader_program, GL_LINK_STATUS, &status));
|
|
|
|
|
2022-11-28 00:38:43 +08:00
|
|
|
if (status != GL_TRUE) {
|
2022-12-01 04:51:59 +08:00
|
|
|
ctx.glGetProgramInfoLog(data->shader_program, sizeof(buffer), &length, &buffer[0]);
|
|
|
|
buffer[length] = '\0';
|
|
|
|
SDL_Log("Program linking failed: %s", buffer);
|
|
|
|
quit(-1);
|
2022-03-27 01:31:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
/* 3D data. Vertex range -0.5..0.5 in all axes.
|
2022-12-01 04:51:59 +08:00
|
|
|
* Z -0.5 is near, 0.5 is far. */
|
2023-03-08 23:12:45 +08:00
|
|
|
static const float g_vertices[] = {
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Front face. */
|
|
|
|
/* Bottom left */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top right */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Left face */
|
|
|
|
/* Bottom left */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top right */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top face */
|
|
|
|
/* Bottom left */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top right */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Right face */
|
|
|
|
/* Bottom left */
|
2022-12-01 04:51:59 +08:00
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top right */
|
2022-12-01 04:51:59 +08:00
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Back face */
|
|
|
|
/* Bottom left */
|
2022-12-01 04:51:59 +08:00
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top right */
|
2022-12-01 04:51:59 +08:00
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Bottom face */
|
|
|
|
/* Bottom left */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Top right */
|
2022-12-01 04:51:59 +08:00
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
|
|
|
-0.5,
|
|
|
|
0.5,
|
2015-06-21 23:33:46 +08:00
|
|
|
};
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static const float g_colors[] = {
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Front face */
|
|
|
|
/* Bottom left */
|
|
|
|
1.0, 0.0, 0.0, /* red */
|
|
|
|
0.0, 0.0, 1.0, /* blue */
|
|
|
|
0.0, 1.0, 0.0, /* green */
|
|
|
|
/* Top right */
|
|
|
|
1.0, 0.0, 0.0, /* red */
|
|
|
|
1.0, 1.0, 0.0, /* yellow */
|
|
|
|
0.0, 0.0, 1.0, /* blue */
|
|
|
|
/* Left face */
|
|
|
|
/* Bottom left */
|
|
|
|
1.0, 1.0, 1.0, /* white */
|
|
|
|
0.0, 1.0, 0.0, /* green */
|
|
|
|
0.0, 1.0, 1.0, /* cyan */
|
|
|
|
/* Top right */
|
|
|
|
1.0, 1.0, 1.0, /* white */
|
|
|
|
1.0, 0.0, 0.0, /* red */
|
|
|
|
0.0, 1.0, 0.0, /* green */
|
|
|
|
/* Top face */
|
|
|
|
/* Bottom left */
|
|
|
|
1.0, 1.0, 1.0, /* white */
|
|
|
|
1.0, 1.0, 0.0, /* yellow */
|
|
|
|
1.0, 0.0, 0.0, /* red */
|
|
|
|
/* Top right */
|
|
|
|
1.0, 1.0, 1.0, /* white */
|
|
|
|
0.0, 0.0, 0.0, /* black */
|
|
|
|
1.0, 1.0, 0.0, /* yellow */
|
|
|
|
/* Right face */
|
|
|
|
/* Bottom left */
|
|
|
|
1.0, 1.0, 0.0, /* yellow */
|
|
|
|
1.0, 0.0, 1.0, /* magenta */
|
|
|
|
0.0, 0.0, 1.0, /* blue */
|
|
|
|
/* Top right */
|
|
|
|
1.0, 1.0, 0.0, /* yellow */
|
|
|
|
0.0, 0.0, 0.0, /* black */
|
|
|
|
1.0, 0.0, 1.0, /* magenta */
|
|
|
|
/* Back face */
|
|
|
|
/* Bottom left */
|
|
|
|
0.0, 0.0, 0.0, /* black */
|
|
|
|
0.0, 1.0, 1.0, /* cyan */
|
|
|
|
1.0, 0.0, 1.0, /* magenta */
|
|
|
|
/* Top right */
|
|
|
|
0.0, 0.0, 0.0, /* black */
|
|
|
|
1.0, 1.0, 1.0, /* white */
|
|
|
|
0.0, 1.0, 1.0, /* cyan */
|
|
|
|
/* Bottom face */
|
|
|
|
/* Bottom left */
|
|
|
|
0.0, 1.0, 0.0, /* green */
|
|
|
|
1.0, 0.0, 1.0, /* magenta */
|
|
|
|
0.0, 1.0, 1.0, /* cyan */
|
|
|
|
/* Top right */
|
|
|
|
0.0, 1.0, 0.0, /* green */
|
|
|
|
0.0, 0.0, 1.0, /* blue */
|
|
|
|
1.0, 0.0, 1.0, /* magenta */
|
|
|
|
};
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static const char *g_shader_vert_src =
|
2022-12-01 04:51:59 +08:00
|
|
|
" attribute vec4 av4position; "
|
|
|
|
" attribute vec3 av3color; "
|
|
|
|
" uniform mat4 mvp; "
|
|
|
|
" varying vec3 vv3color; "
|
|
|
|
" void main() { "
|
|
|
|
" vv3color = av3color; "
|
|
|
|
" gl_Position = mvp * av4position; "
|
|
|
|
" } ";
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static const char *g_shader_frag_src =
|
2022-12-01 04:51:59 +08:00
|
|
|
" precision lowp float; "
|
|
|
|
" varying vec3 vv3color; "
|
|
|
|
" void main() { "
|
|
|
|
" gl_FragColor = vec4(vv3color, 1.0); "
|
|
|
|
" } ";
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
static void
|
2022-12-01 04:51:59 +08:00
|
|
|
Render(unsigned int width, unsigned int height, shader_data *data)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
/*
|
|
|
|
* Do some rotation with Euler angles. It is not a fixed axis as
|
|
|
|
* quaterions would be, but the effect is cool.
|
|
|
|
*/
|
2015-06-21 23:33:46 +08:00
|
|
|
rotate_matrix((float)data->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
|
|
|
|
rotate_matrix((float)data->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
|
|
|
|
|
|
|
|
multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
|
|
|
|
|
|
|
|
rotate_matrix((float)data->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
|
|
|
|
|
|
|
|
multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
|
|
|
|
|
|
|
|
/* Pull the camera back from the cube */
|
2024-06-04 05:33:29 +08:00
|
|
|
matrix_modelview[14] -= 2.5f;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
perspective_matrix(45.0f, (float)width / height, 0.01f, 100.0f, matrix_perspective);
|
2015-06-21 23:33:46 +08:00
|
|
|
multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
|
|
|
|
|
|
|
|
GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
|
|
|
|
|
|
|
|
data->angle_x += 3;
|
|
|
|
data->angle_y += 2;
|
|
|
|
data->angle_z += 1;
|
|
|
|
|
2022-11-28 00:38:43 +08:00
|
|
|
if (data->angle_x >= 360) {
|
|
|
|
data->angle_x -= 360;
|
|
|
|
}
|
|
|
|
if (data->angle_x < 0) {
|
|
|
|
data->angle_x += 360;
|
|
|
|
}
|
|
|
|
if (data->angle_y >= 360) {
|
|
|
|
data->angle_y -= 360;
|
|
|
|
}
|
|
|
|
if (data->angle_y < 0) {
|
|
|
|
data->angle_y += 360;
|
|
|
|
}
|
|
|
|
if (data->angle_z >= 360) {
|
|
|
|
data->angle_z -= 360;
|
|
|
|
}
|
|
|
|
if (data->angle_z < 0) {
|
|
|
|
data->angle_z += 360;
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2022-08-16 11:51:15 +08:00
|
|
|
GL_CHECK(ctx.glViewport(0, 0, width, height));
|
2015-06-21 23:33:46 +08:00
|
|
|
GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
|
|
|
|
GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static int done;
|
|
|
|
static Uint32 frames;
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifndef SDL_PLATFORM_EMSCRIPTEN
|
2023-03-08 23:12:45 +08:00
|
|
|
static thread_data *threads;
|
2023-03-09 00:58:26 +08:00
|
|
|
#endif
|
2022-08-16 11:51:15 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
render_window(int index)
|
|
|
|
{
|
2024-08-23 08:33:49 +08:00
|
|
|
int w, h;
|
2022-08-16 11:51:15 +08:00
|
|
|
|
|
|
|
if (!state->windows[index]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-23 08:33:49 +08:00
|
|
|
if (!SDL_GL_MakeCurrent(state->windows[index], context[index])) {
|
2022-08-16 11:51:15 +08:00
|
|
|
SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-29 10:43:03 +08:00
|
|
|
SDL_GetWindowSizeInPixels(state->windows[index], &w, &h);
|
2022-08-16 11:51:15 +08:00
|
|
|
Render(w, h, &datas[index]);
|
|
|
|
SDL_GL_SwapWindow(state->windows[index]);
|
|
|
|
++frames;
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifndef SDL_PLATFORM_EMSCRIPTEN
|
2022-08-16 11:51:15 +08:00
|
|
|
static int SDLCALL
|
2022-12-01 04:51:59 +08:00
|
|
|
render_thread_fn(void *render_ctx)
|
2022-08-16 11:51:15 +08:00
|
|
|
{
|
|
|
|
thread_data *thread = render_ctx;
|
|
|
|
|
|
|
|
while (!done && !thread->done && state->windows[thread->index]) {
|
2024-01-18 19:39:55 +08:00
|
|
|
if (SDL_AtomicCompareAndSwap(&thread->suspended, WAIT_STATE_ENTER_SEM, WAIT_STATE_WAITING_ON_SEM)) {
|
2023-06-25 22:18:58 +08:00
|
|
|
SDL_WaitSemaphore(thread->suspend_sem);
|
|
|
|
}
|
2022-08-16 11:51:15 +08:00
|
|
|
render_window(thread->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(state->windows[thread->index], NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-25 22:18:58 +08:00
|
|
|
static thread_data *GetThreadDataForWindow(SDL_WindowID id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SDL_Window *window = SDL_GetWindowFromID(id);
|
|
|
|
if (window) {
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
if (window == state->windows[i]) {
|
|
|
|
return &threads[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:51:15 +08:00
|
|
|
static void
|
2023-03-08 23:12:45 +08:00
|
|
|
loop_threaded(void)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
2023-06-25 22:18:58 +08:00
|
|
|
thread_data *tdata;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2022-08-16 11:51:15 +08:00
|
|
|
/* Wait for events */
|
|
|
|
while (SDL_WaitEvent(&event) && !done) {
|
2023-06-25 22:18:58 +08:00
|
|
|
if (suspend_when_occluded && event.type == SDL_EVENT_WINDOW_OCCLUDED) {
|
|
|
|
tdata = GetThreadDataForWindow(event.window.windowID);
|
|
|
|
if (tdata) {
|
2024-01-18 19:39:55 +08:00
|
|
|
SDL_AtomicCompareAndSwap(&tdata->suspended, WAIT_STATE_GO, WAIT_STATE_ENTER_SEM);
|
2023-06-25 22:18:58 +08:00
|
|
|
}
|
|
|
|
} else if (suspend_when_occluded && event.type == SDL_EVENT_WINDOW_EXPOSED) {
|
|
|
|
tdata = GetThreadDataForWindow(event.window.windowID);
|
|
|
|
if (tdata) {
|
|
|
|
if (SDL_AtomicSet(&tdata->suspended, WAIT_STATE_GO) == WAIT_STATE_WAITING_ON_SEM) {
|
2024-07-25 03:39:30 +08:00
|
|
|
SDL_SignalSemaphore(tdata->suspend_sem);
|
2023-06-25 22:18:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
|
|
|
|
tdata = GetThreadDataForWindow(event.window.windowID);
|
|
|
|
if (tdata) {
|
|
|
|
/* Stop the render thread when the window is closed */
|
|
|
|
tdata->done = 1;
|
|
|
|
if (tdata->thread) {
|
|
|
|
SDL_AtomicSet(&tdata->suspended, WAIT_STATE_GO);
|
2024-07-25 03:39:30 +08:00
|
|
|
SDL_SignalSemaphore(tdata->suspend_sem);
|
2023-06-25 22:18:58 +08:00
|
|
|
SDL_WaitThread(tdata->thread, NULL);
|
|
|
|
tdata->thread = NULL;
|
|
|
|
SDL_DestroySemaphore(tdata->suspend_sem);
|
2022-08-16 11:51:15 +08:00
|
|
|
}
|
2023-06-25 22:18:58 +08:00
|
|
|
break;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
}
|
2022-08-16 11:51:15 +08:00
|
|
|
}
|
2022-11-01 20:10:46 +08:00
|
|
|
#endif
|
2022-08-16 11:51:15 +08:00
|
|
|
|
|
|
|
static void
|
2023-03-08 23:12:45 +08:00
|
|
|
loop(void)
|
2022-08-16 11:51:15 +08:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
int i;
|
2023-06-25 22:18:58 +08:00
|
|
|
int active_windows = 0;
|
2022-08-16 11:51:15 +08:00
|
|
|
|
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event) && !done) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
if (!done) {
|
2022-08-16 11:51:15 +08:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2023-06-25 22:18:58 +08:00
|
|
|
if (state->windows[i] == NULL ||
|
|
|
|
(suspend_when_occluded && (SDL_GetWindowFlags(state->windows[i]) & SDL_WINDOW_OCCLUDED))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++active_windows;
|
2022-08-16 11:51:15 +08:00
|
|
|
render_window(i);
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
else {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
2023-06-25 22:18:58 +08:00
|
|
|
|
|
|
|
/* If all windows are occluded, throttle event polling to 15hz. */
|
|
|
|
if (!done && !active_windows) {
|
|
|
|
SDL_DelayNS(SDL_NS_PER_SECOND / 15);
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2022-08-16 11:51:15 +08:00
|
|
|
int fsaa, accel, threaded;
|
2015-06-21 23:33:46 +08:00
|
|
|
int value;
|
|
|
|
int i;
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 13:23:14 +08:00
|
|
|
const SDL_DisplayMode *mode;
|
2022-12-02 17:17:17 +08:00
|
|
|
Uint64 then, now;
|
2015-06-21 23:33:46 +08:00
|
|
|
shader_data *data;
|
|
|
|
|
|
|
|
/* Initialize parameters */
|
|
|
|
fsaa = 0;
|
|
|
|
accel = 0;
|
2022-08-16 11:51:15 +08:00
|
|
|
threaded = 0;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!state) {
|
2015-06-21 23:33:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (consumed == 0) {
|
|
|
|
if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
|
|
|
|
++fsaa;
|
|
|
|
consumed = 1;
|
|
|
|
} else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
|
|
|
|
++accel;
|
|
|
|
consumed = 1;
|
2022-08-16 11:51:15 +08:00
|
|
|
} else if (SDL_strcasecmp(argv[i], "--threaded") == 0) {
|
|
|
|
++threaded;
|
|
|
|
consumed = 1;
|
2023-06-25 22:18:58 +08:00
|
|
|
} else if(SDL_strcasecmp(argv[i], "--suspend-when-occluded") == 0) {
|
|
|
|
suspend_when_occluded = SDL_TRUE;
|
|
|
|
consumed = 1;
|
2015-06-21 23:33:46 +08:00
|
|
|
} else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
|
|
|
|
i++;
|
|
|
|
if (!argv[i]) {
|
|
|
|
consumed = -1;
|
|
|
|
} else {
|
2023-03-17 07:25:39 +08:00
|
|
|
char *endptr = NULL;
|
|
|
|
depth = (int)SDL_strtol(argv[i], &endptr, 0);
|
|
|
|
if (endptr != argv[i] && *endptr == '\0') {
|
|
|
|
consumed = 1;
|
|
|
|
} else {
|
|
|
|
consumed = -1;
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
consumed = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed < 0) {
|
2023-06-25 22:18:58 +08:00
|
|
|
static const char *options[] = { "[--fsaa]", "[--accel]", "[--zdepth %d]", "[--threaded]", "[--suspend-when-occluded]",NULL };
|
2019-05-29 05:39:13 +08:00
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
2015-06-21 23:33:46 +08:00
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set OpenGL parameters */
|
2022-08-16 11:51:15 +08:00
|
|
|
state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
|
2015-06-21 23:33:46 +08:00
|
|
|
state->gl_red_size = 5;
|
|
|
|
state->gl_green_size = 5;
|
|
|
|
state->gl_blue_size = 5;
|
|
|
|
state->gl_depth_size = depth;
|
|
|
|
state->gl_major_version = 2;
|
|
|
|
state->gl_minor_version = 0;
|
|
|
|
state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
|
|
|
|
|
|
|
|
if (fsaa) {
|
2022-12-01 04:51:59 +08:00
|
|
|
state->gl_multisamplebuffers = 1;
|
|
|
|
state->gl_multisamplesamples = fsaa;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
if (accel) {
|
2022-12-01 04:51:59 +08:00
|
|
|
state->gl_accelerated = 1;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
if (!SDLTest_CommonInit(state)) {
|
|
|
|
quit(2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:55:49 +08:00
|
|
|
context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(*context));
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!context) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("Out of memory!\n");
|
|
|
|
quit(2);
|
|
|
|
}
|
2022-12-01 04:51:59 +08:00
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Create OpenGL ES contexts */
|
|
|
|
for (i = 0; i < state->num_windows; i++) {
|
|
|
|
context[i] = SDL_GL_CreateContext(state->windows[i]);
|
|
|
|
if (!context[i]) {
|
|
|
|
SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Important: call this *after* creating the context */
|
|
|
|
if (LoadContext(&ctx) < 0) {
|
|
|
|
SDL_Log("Could not load GLES2 functions\n");
|
|
|
|
quit(2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-14 01:31:55 +08:00
|
|
|
SDL_GL_SetSwapInterval(state->render_vsync);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 13:23:14 +08:00
|
|
|
mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
|
2022-08-16 11:51:15 +08:00
|
|
|
SDL_Log("Threaded : %s\n", threaded ? "yes" : "no");
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 13:23:14 +08:00
|
|
|
if (mode) {
|
|
|
|
SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode->format));
|
|
|
|
SDL_Log("\n");
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
|
|
|
|
SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
|
|
|
|
SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
|
|
|
|
SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
|
|
|
|
SDL_Log("\n");
|
|
|
|
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
|
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_RED_SIZE: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
|
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_GREEN_SIZE: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
|
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_BLUE_SIZE: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
|
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_DEPTH_SIZE: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
|
|
|
if (fsaa) {
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
|
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
|
2022-12-01 04:51:59 +08:00
|
|
|
value);
|
2015-06-21 23:33:46 +08:00
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accel) {
|
2024-08-23 08:33:49 +08:00
|
|
|
if (SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
|
|
|
|
} else {
|
2022-12-01 04:51:59 +08:00
|
|
|
SDL_Log("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:31:32 +08:00
|
|
|
datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Set rendering settings for each context */
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
|
|
|
|
int w, h;
|
2024-08-23 08:33:49 +08:00
|
|
|
if (!SDL_GL_MakeCurrent(state->windows[i], context[i])) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
|
|
|
|
|
|
|
|
/* Continue for next window */
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-29 10:43:03 +08:00
|
|
|
SDL_GetWindowSizeInPixels(state->windows[i], &w, &h);
|
2015-06-21 23:33:46 +08:00
|
|
|
ctx.glViewport(0, 0, w, h);
|
|
|
|
|
|
|
|
data = &datas[i];
|
2022-12-01 04:51:59 +08:00
|
|
|
data->angle_x = 0;
|
|
|
|
data->angle_y = 0;
|
|
|
|
data->angle_z = 0;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Shader Initialization */
|
2022-12-30 05:58:16 +08:00
|
|
|
process_shader(&data->shader_vert, g_shader_vert_src, GL_VERTEX_SHADER);
|
|
|
|
process_shader(&data->shader_frag, g_shader_frag_src, GL_FRAGMENT_SHADER);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Create shader_program (ready to attach shaders) */
|
|
|
|
data->shader_program = GL_CHECK(ctx.glCreateProgram());
|
|
|
|
|
|
|
|
/* Attach shaders and link shader_program */
|
2022-03-27 01:31:17 +08:00
|
|
|
link_program(data);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Get attribute locations of non-fixed attributes like color and texture coordinates. */
|
|
|
|
data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
|
|
|
|
data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
|
|
|
|
|
|
|
|
/* Get uniform locations */
|
|
|
|
data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
|
|
|
|
|
|
|
|
GL_CHECK(ctx.glUseProgram(data->shader_program));
|
|
|
|
|
|
|
|
/* Enable attributes for position, color and texture coordinates etc. */
|
|
|
|
GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
|
|
|
|
GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
|
|
|
|
|
|
|
|
/* Populate attributes for position, color and texture coordinates etc. */
|
2022-01-25 11:32:58 +08:00
|
|
|
|
|
|
|
GL_CHECK(ctx.glGenBuffers(1, &data->position_buffer));
|
|
|
|
GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, data->position_buffer));
|
2022-12-30 05:58:16 +08:00
|
|
|
GL_CHECK(ctx.glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertices), g_vertices, GL_STATIC_DRAW));
|
2022-01-25 11:32:58 +08:00
|
|
|
GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, 0));
|
|
|
|
GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, 0));
|
|
|
|
|
|
|
|
GL_CHECK(ctx.glGenBuffers(1, &data->color_buffer));
|
|
|
|
GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, data->color_buffer));
|
2022-12-30 05:58:16 +08:00
|
|
|
GL_CHECK(ctx.glBufferData(GL_ARRAY_BUFFER, sizeof(g_colors), g_colors, GL_STATIC_DRAW));
|
2022-01-25 11:32:58 +08:00
|
|
|
GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, 0));
|
|
|
|
GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, 0));
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
GL_CHECK(ctx.glEnable(GL_CULL_FACE));
|
|
|
|
GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
|
2022-08-16 11:51:15 +08:00
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(state->windows[i], NULL);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Main render loop */
|
|
|
|
frames = 0;
|
|
|
|
then = SDL_GetTicks();
|
|
|
|
done = 0;
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
2022-08-16 11:51:15 +08:00
|
|
|
if (threaded) {
|
2022-12-01 04:51:59 +08:00
|
|
|
threads = (thread_data *)SDL_calloc(state->num_windows, sizeof(thread_data));
|
2022-08-16 11:51:15 +08:00
|
|
|
|
|
|
|
/* Start a render thread for each window */
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
threads[i].index = i;
|
2023-06-25 22:18:58 +08:00
|
|
|
SDL_AtomicSet(&threads[i].suspended, 0);
|
|
|
|
threads[i].suspend_sem = SDL_CreateSemaphore(0);
|
2022-08-16 11:51:15 +08:00
|
|
|
threads[i].thread = SDL_CreateThread(render_thread_fn, "RenderThread", &threads[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
loop_threaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Join the remaining render threads (if any) */
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
threads[i].done = 1;
|
|
|
|
if (threads[i].thread) {
|
|
|
|
SDL_WaitThread(threads[i].thread, NULL);
|
|
|
|
}
|
|
|
|
}
|
2024-07-27 19:06:54 +08:00
|
|
|
SDL_free(threads);
|
2022-08-16 11:51:15 +08:00
|
|
|
} else {
|
|
|
|
while (!done) {
|
|
|
|
loop();
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Print out some timing information */
|
|
|
|
now = SDL_GetTicks();
|
|
|
|
if (now > then) {
|
|
|
|
SDL_Log("%2.2f frames per second\n",
|
2022-12-01 04:51:59 +08:00
|
|
|
((double)frames * 1000) / (now - then));
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifndef SDL_PLATFORM_ANDROID
|
2015-06-21 23:33:46 +08:00
|
|
|
quit(0);
|
2022-11-23 01:10:47 +08:00
|
|
|
#endif
|
2015-06-21 23:33:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_OPENGLES2 */
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
SDL_Log("No OpenGL ES support on this system\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_OPENGLES2 */
|