Pretty print shaders for debugging purposes

This commit is contained in:
Sam Lantinga 2022-09-15 07:41:29 -07:00
parent bc57d3e35c
commit 2970710b5d
2 changed files with 140 additions and 119 deletions

View File

@ -521,6 +521,22 @@ GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type, GLenum shader_t
SDL_assert(num_src <= SDL_arraysize(shader_src_list)); SDL_assert(num_src <= SDL_arraysize(shader_src_list));
#ifdef DEBUG_PRINT_SHADERS
{
int i;
char *message = NULL;
SDL_asprintf(&message, "Compiling shader:\n");
for (i = 0; i < num_src; ++i) {
char *last_message = message;
SDL_asprintf(&message, "%s%s", last_message, shader_src_list[i]);
SDL_free(last_message);
}
SDL_Log("%s\n", message);
SDL_free(message);
}
#endif
/* Compile */ /* Compile */
id = data->glCreateShader(shader_type); id = data->glCreateShader(shader_type);
data->glShaderSource(id, num_src, shader_src_list, NULL); data->glShaderSource(id, num_src, shader_src_list, NULL);
@ -541,10 +557,10 @@ GLES2_CacheShader(GLES2_RenderData *data, GLES2_ShaderType type, GLenum shader_t
} }
} }
if (info) { if (info) {
SDL_SetError("Failed to load the shader: %s", info); SDL_SetError("Failed to load the shader %d: %s", type, info);
SDL_small_free(info, isstack); SDL_small_free(info, isstack);
} else { } else {
SDL_SetError("Failed to load the shader"); SDL_SetError("Failed to load the shader %d", type);
} }
data->glDeleteShader(id); data->glDeleteShader(id);
return 0; return 0;

View File

@ -32,117 +32,122 @@
* Vertex/fragment shader source * * Vertex/fragment shader source *
*************************************************************************************************/ *************************************************************************************************/
static const char GLES2_Fragment_Include_Best_Texture_Precision[] = "\n\ static const char GLES2_Fragment_Include_Best_Texture_Precision[] = \
#ifdef GL_FRAGMENT_PRECISION_HIGH\n\ "#ifdef GL_FRAGMENT_PRECISION_HIGH\n" \
#define SDL_TEXCOORD_PRECISION highp\n\ "#define SDL_TEXCOORD_PRECISION highp\n" \
#else\n\ "#else\n" \
#define SDL_TEXCOORD_PRECISION mediump\n\ "#define SDL_TEXCOORD_PRECISION mediump\n" \
#endif\n\ "#endif\n" \
precision mediump float;\n\ "\n" \
"; "precision mediump float;\n" \
"\n" \
;
static const char GLES2_Fragment_Include_Medium_Texture_Precision[] = "\n\ static const char GLES2_Fragment_Include_Medium_Texture_Precision[] = \
#define SDL_TEXCOORD_PRECISION mediump\n\ "#define SDL_TEXCOORD_PRECISION mediump\n" \
precision mediump float;\n\ "precision mediump float;\n" \
"; "\n" \
;
static const char GLES2_Fragment_Include_High_Texture_Precision[] = "\n\ static const char GLES2_Fragment_Include_High_Texture_Precision[] = \
#define SDL_TEXCOORD_PRECISION highp\n\ "#define SDL_TEXCOORD_PRECISION highp\n" \
precision mediump float;\n\ "precision mediump float;\n" \
"; "\n" \
;
static const char GLES2_Fragment_Include_Undef_Precision[] = "\n\ static const char GLES2_Fragment_Include_Undef_Precision[] = \
#define mediump\n\ "#define mediump\n" \
#define highp\n\ "#define highp\n" \
#define lowp\n\ "#define lowp\n" \
#define SDL_TEXCOORD_PRECISION\n\ "#define SDL_TEXCOORD_PRECISION\n" \
"; "\n" \
;
static const char GLES2_Vertex_Default[] = " \ static const char GLES2_Vertex_Default[] = \
uniform mat4 u_projection; \ "uniform mat4 u_projection;\n" \
attribute vec2 a_position; \ "attribute vec2 a_position;\n" \
attribute vec4 a_color; \ "attribute vec4 a_color;\n" \
attribute vec2 a_texCoord; \ "attribute vec2 a_texCoord;\n" \
varying vec2 v_texCoord; \ "varying vec2 v_texCoord;\n" \
varying vec4 v_color; \ "varying vec4 v_color;\n" \
\ "\n" \
void main() \ "void main()\n" \
{ \ "{\n" \
v_texCoord = a_texCoord; \ " v_texCoord = a_texCoord;\n" \
gl_Position = u_projection * vec4(a_position, 0.0, 1.0);\ " gl_Position = u_projection * vec4(a_position, 0.0, 1.0);\n" \
gl_PointSize = 1.0; \ " gl_PointSize = 1.0;\n" \
v_color = a_color; \ " v_color = a_color;\n" \
} \ "}\n" \
"; ;
static const char GLES2_Fragment_Solid[] = " \ static const char GLES2_Fragment_Solid[] = \
varying mediump vec4 v_color; \ "varying mediump vec4 v_color;\n" \
\ "\n" \
void main() \ "void main()\n" \
{ \ "{\n" \
gl_FragColor = v_color; \ " gl_FragColor = v_color;\n" \
} \ "}\n" \
"; ;
static const char GLES2_Fragment_TextureABGR[] = " \ static const char GLES2_Fragment_TextureABGR[] = \
uniform sampler2D u_texture; \ "uniform sampler2D u_texture;\n" \
varying mediump vec4 v_color;\n\ "varying mediump vec4 v_color;\n" \
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\ "varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n" \
\ "\n" \
void main() \ "void main()\n" \
{ \ "{\n" \
gl_FragColor = texture2D(u_texture, v_texCoord); \ " gl_FragColor = texture2D(u_texture, v_texCoord);\n" \
gl_FragColor *= v_color; \ " gl_FragColor *= v_color;\n" \
} \ "}\n" \
"; ;
/* ARGB to ABGR conversion */ /* ARGB to ABGR conversion */
static const char GLES2_Fragment_TextureARGB[] = " \ static const char GLES2_Fragment_TextureARGB[] = \
uniform sampler2D u_texture; \ "uniform sampler2D u_texture;\n" \
varying mediump vec4 v_color;\n\ "varying mediump vec4 v_color;\n" \
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\ "varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n" \
\ "\n" \
void main() \ "void main()\n" \
{ \ "{\n" \
mediump vec4 abgr = texture2D(u_texture, v_texCoord); \ " mediump vec4 abgr = texture2D(u_texture, v_texCoord);\n" \
gl_FragColor = abgr; \ " gl_FragColor = abgr;\n" \
gl_FragColor.r = abgr.b; \ " gl_FragColor.r = abgr.b;\n" \
gl_FragColor.b = abgr.r; \ " gl_FragColor.b = abgr.r;\n" \
gl_FragColor *= v_color; \ " gl_FragColor *= v_color;\n" \
} \ "}\n" \
"; ;
/* RGB to ABGR conversion */ /* RGB to ABGR conversion */
static const char GLES2_Fragment_TextureRGB[] = " \ static const char GLES2_Fragment_TextureRGB[] = \
uniform sampler2D u_texture; \ "uniform sampler2D u_texture;\n" \
varying mediump vec4 v_color;\n\ "varying mediump vec4 v_color;\n" \
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\ "varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n" \
\ "\n" \
void main() \ "void main()\n" \
{ \ "{\n" \
mediump vec4 abgr = texture2D(u_texture, v_texCoord); \ " mediump vec4 abgr = texture2D(u_texture, v_texCoord);\n" \
gl_FragColor = abgr; \ " gl_FragColor = abgr;\n" \
gl_FragColor.r = abgr.b; \ " gl_FragColor.r = abgr.b;\n" \
gl_FragColor.b = abgr.r; \ " gl_FragColor.b = abgr.r;\n" \
gl_FragColor.a = 1.0; \ " gl_FragColor.a = 1.0;\n" \
gl_FragColor *= v_color; \ " gl_FragColor *= v_color;\n" \
} \ "}\n" \
"; ;
/* BGR to ABGR conversion */ /* BGR to ABGR conversion */
static const char GLES2_Fragment_TextureBGR[] = " \ static const char GLES2_Fragment_TextureBGR[] = \
uniform sampler2D u_texture; \ "uniform sampler2D u_texture;\n" \
varying mediump vec4 v_color;\n\ "varying mediump vec4 v_color;\n" \
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n\ "varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n" \
\ "\n" \
void main() \ "void main()\n" \
{ \ "{\n" \
mediump vec4 abgr = texture2D(u_texture, v_texCoord); \ " mediump vec4 abgr = texture2D(u_texture, v_texCoord);\n" \
gl_FragColor = abgr; \ " gl_FragColor = abgr;\n" \
gl_FragColor.a = 1.0; \ " gl_FragColor.a = 1.0;\n" \
gl_FragColor *= v_color; \ " gl_FragColor *= v_color;\n" \
} \ "}\n" \
"; ;
#if SDL_HAVE_YUV #if SDL_HAVE_YUV
@ -154,6 +159,7 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"const mat3 matrix = mat3( 1, 1, 1,\n" \ "const mat3 matrix = mat3( 1, 1, 1,\n" \
" 0, -0.3441, 1.772,\n" \ " 0, -0.3441, 1.772,\n" \
" 1.402, -0.7141, 0);\n" \ " 1.402, -0.7141, 0);\n" \
"\n" \
#define BT601_SHADER_CONSTANTS \ #define BT601_SHADER_CONSTANTS \
"// YUV offset \n" \ "// YUV offset \n" \
@ -163,6 +169,7 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"const mat3 matrix = mat3( 1.1644, 1.1644, 1.1644,\n" \ "const mat3 matrix = mat3( 1.1644, 1.1644, 1.1644,\n" \
" 0, -0.3918, 2.0172,\n" \ " 0, -0.3918, 2.0172,\n" \
" 1.596, -0.813, 0);\n" \ " 1.596, -0.813, 0);\n" \
"\n" \
#define BT709_SHADER_CONSTANTS \ #define BT709_SHADER_CONSTANTS \
"// YUV offset \n" \ "// YUV offset \n" \
@ -172,6 +179,7 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"const mat3 matrix = mat3( 1.1644, 1.1644, 1.1644,\n" \ "const mat3 matrix = mat3( 1.1644, 1.1644, 1.1644,\n" \
" 0, -0.2132, 2.1124,\n" \ " 0, -0.2132, 2.1124,\n" \
" 1.7927, -0.5329, 0);\n" \ " 1.7927, -0.5329, 0);\n" \
"\n" \
#define YUV_SHADER_PROLOGUE \ #define YUV_SHADER_PROLOGUE \
@ -183,7 +191,6 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"\n" \ "\n" \
#define YUV_SHADER_BODY \ #define YUV_SHADER_BODY \
"\n" \
"void main()\n" \ "void main()\n" \
"{\n" \ "{\n" \
" mediump vec3 yuv;\n" \ " mediump vec3 yuv;\n" \
@ -204,7 +211,6 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"}" \ "}" \
#define NV12_RA_SHADER_BODY \ #define NV12_RA_SHADER_BODY \
"\n" \
"void main()\n" \ "void main()\n" \
"{\n" \ "{\n" \
" mediump vec3 yuv;\n" \ " mediump vec3 yuv;\n" \
@ -224,7 +230,6 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"}" \ "}" \
#define NV12_RG_SHADER_BODY \ #define NV12_RG_SHADER_BODY \
"\n" \
"void main()\n" \ "void main()\n" \
"{\n" \ "{\n" \
" mediump vec3 yuv;\n" \ " mediump vec3 yuv;\n" \
@ -244,7 +249,6 @@ static const char GLES2_Fragment_TextureBGR[] = " \
"}" \ "}" \
#define NV21_SHADER_BODY \ #define NV21_SHADER_BODY \
"\n" \
"void main()\n" \ "void main()\n" \
"{\n" \ "{\n" \
" mediump vec3 yuv;\n" \ " mediump vec3 yuv;\n" \
@ -326,20 +330,21 @@ static const char GLES2_Fragment_TextureNV21BT709[] = \
#endif #endif
/* Custom Android video format texture */ /* Custom Android video format texture */
static const char GLES2_Fragment_TextureExternalOES_Prologue[] = " \ static const char GLES2_Fragment_TextureExternalOES_Prologue[] = \
#extension GL_OES_EGL_image_external : require\n\ "#extension GL_OES_EGL_image_external : require\n" \
"; "\n" \
static const char GLES2_Fragment_TextureExternalOES[] = " \ ;
uniform samplerExternalOES u_texture; \ static const char GLES2_Fragment_TextureExternalOES[] = \
varying mediump vec4 v_color; \ "uniform samplerExternalOES u_texture;\n" \
varying SDL_TEXCOORD_PRECISION vec2 v_texCoord; \ "varying mediump vec4 v_color;\n" \
\ "varying SDL_TEXCOORD_PRECISION vec2 v_texCoord;\n" \
void main() \ "\n" \
{ \ "void main()\n" \
gl_FragColor = texture2D(u_texture, v_texCoord); \ "{\n" \
gl_FragColor *= v_color; \ " gl_FragColor = texture2D(u_texture, v_texCoord);\n" \
} \ " gl_FragColor *= v_color;\n" \
"; "}\n" \
;
/************************************************************************************************* /*************************************************************************************************