Skip to content

Commit c0a2ae2

Browse files
committed
opengles2: fixed swapped colors when using indexed textures
1 parent 7388054 commit c0a2ae2

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

src/render/opengles2/SDL_render_gles2.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ static bool GLES2_SelectProgram(GLES2_RenderData *data, SDL_Texture *texture, GL
628628
GLES2_ShaderType vtype, ftype;
629629
GLES2_ProgramCacheEntry *program;
630630
GLES2_TextureData *tdata = texture ? (GLES2_TextureData *)texture->internal : NULL;
631+
const bool colorswap = (data->drawstate.target && (data->drawstate.target->format == SDL_PIXELFORMAT_BGRA32 || data->drawstate.target->format == SDL_PIXELFORMAT_BGRX32));
631632
const float *shader_params = NULL;
632633
int shader_params_len = 0;
633634

@@ -640,15 +641,27 @@ static bool GLES2_SelectProgram(GLES2_RenderData *data, SDL_Texture *texture, GL
640641
case GLES2_IMAGESOURCE_TEXTURE_INDEX8:
641642
switch (scale_mode) {
642643
case SDL_SCALEMODE_NEAREST:
643-
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_NEAREST;
644+
if (colorswap) {
645+
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_NEAREST_COLORSWAP;
646+
} else {
647+
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_NEAREST;
648+
}
644649
break;
645650
case SDL_SCALEMODE_LINEAR:
646-
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_LINEAR;
651+
if (colorswap) {
652+
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_LINEAR_COLORSWAP;
653+
} else {
654+
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_LINEAR;
655+
}
647656
shader_params = tdata->texel_size;
648657
shader_params_len = 4 * sizeof(float);
649658
break;
650659
case SDL_SCALEMODE_PIXELART:
651-
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART;
660+
if (colorswap) {
661+
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART_COLORSWAP;
662+
} else {
663+
ftype = GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART;
664+
}
652665
shader_params = tdata->texel_size;
653666
shader_params_len = 4 * sizeof(float);
654667
break;

src/render/opengles2/SDL_shaders_gles2.c

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,20 @@ static const char GLES2_Fragment_TexturePalette_Nearest[] =
167167
"\n"
168168
"void main()\n"
169169
"{\n"
170-
" gl_FragColor = SamplePaletteNearest(v_texCoord);\n"
170+
" mediump vec4 color = SamplePaletteNearest(v_texCoord);\n"
171+
" gl_FragColor = color;\n"
172+
" gl_FragColor *= v_color;\n"
173+
"}\n"
174+
;
175+
176+
static const char GLES2_Fragment_TexturePalette_Nearest_Colorswap[] =
177+
PALETTE_SHADER_PROLOGUE
178+
PALETTE_SHADER_FUNCTIONS
179+
"\n"
180+
"void main()\n"
181+
"{\n"
182+
" mediump vec4 color = SamplePaletteNearest(v_texCoord);\n"
183+
" gl_FragColor = vec4(color.b, color.g, color.r, color.a);\n"
171184
" gl_FragColor *= v_color;\n"
172185
"}\n"
173186
;
@@ -178,7 +191,20 @@ static const char GLES2_Fragment_TexturePalette_Linear[] =
178191
"\n"
179192
"void main()\n"
180193
"{\n"
181-
" gl_FragColor = SamplePaletteLinear(v_texCoord);\n"
194+
" mediump vec4 color = SamplePaletteLinear(v_texCoord);\n"
195+
" gl_FragColor = color;\n"
196+
" gl_FragColor *= v_color;\n"
197+
"}\n"
198+
;
199+
200+
static const char GLES2_Fragment_TexturePalette_Linear_Colorswap[] =
201+
PALETTE_SHADER_PROLOGUE
202+
PALETTE_SHADER_FUNCTIONS
203+
"\n"
204+
"void main()\n"
205+
"{\n"
206+
" mediump vec4 color = SamplePaletteLinear(v_texCoord);\n"
207+
" gl_FragColor = vec4(color.b, color.g, color.r, color.a);\n"
182208
" gl_FragColor *= v_color;\n"
183209
"}\n"
184210
;
@@ -191,10 +217,28 @@ static const char GLES2_Fragment_TexturePalette_PixelArt[] =
191217
"void main()\n"
192218
"{\n"
193219
#ifdef OPENGLES_300
194-
" gl_FragColor = SamplePaletteLinear(GetPixelArtUV(v_texCoord));\n"
220+
" mediump vec4 color = SamplePaletteLinear(GetPixelArtUV(v_texCoord));\n"
195221
#else
196-
" gl_FragColor = SamplePaletteNearest(v_texCoord);\n"
222+
" mediump vec4 color = SamplePaletteNearest(v_texCoord);\n"
197223
#endif
224+
" gl_FragColor = color;\n"
225+
" gl_FragColor *= v_color;\n"
226+
"}\n"
227+
;
228+
229+
static const char GLES2_Fragment_TexturePalette_PixelArt_Colorswap[] =
230+
PALETTE_SHADER_PROLOGUE
231+
PALETTE_SHADER_FUNCTIONS
232+
PIXELART_SHADER_FUNCTIONS
233+
"\n"
234+
"void main()\n"
235+
"{\n"
236+
#ifdef OPENGLES_300
237+
" mediump vec4 color = SamplePaletteLinear(GetPixelArtUV(v_texCoord));\n"
238+
#else
239+
" mediump vec4 color = SamplePaletteNearest(v_texCoord);\n"
240+
#endif
241+
" gl_FragColor = vec4(color.b, color.g, color.r, color.a);\n"
198242
" gl_FragColor *= v_color;\n"
199243
"}\n"
200244
;
@@ -206,10 +250,7 @@ static const char GLES2_Fragment_TextureRGB[] =
206250
"void main()\n"
207251
"{\n"
208252
" mediump vec4 color = texture2D(u_texture, v_texCoord);\n"
209-
" gl_FragColor = color;\n"
210-
" gl_FragColor.r = color.b;\n"
211-
" gl_FragColor.b = color.r;\n"
212-
" gl_FragColor.a = 1.0;\n"
253+
" gl_FragColor = vec4(color.b, color.g, color.r, 1.0);\n"
213254
" gl_FragColor *= v_color;\n"
214255
"}\n"
215256
;
@@ -222,10 +263,7 @@ static const char GLES2_Fragment_TextureRGB_PixelArt[] =
222263
"void main()\n"
223264
"{\n"
224265
" mediump vec4 color = GetPixelArtSample(v_texCoord);\n"
225-
" gl_FragColor = color;\n"
226-
" gl_FragColor.r = color.b;\n"
227-
" gl_FragColor.b = color.r;\n"
228-
" gl_FragColor.a = 1.0;\n"
266+
" gl_FragColor = vec4(color.b, color.g, color.r, 1.0);\n"
229267
" gl_FragColor *= v_color;\n"
230268
"}\n"
231269
;
@@ -237,8 +275,7 @@ static const char GLES2_Fragment_TextureBGR[] =
237275
"void main()\n"
238276
"{\n"
239277
" mediump vec4 color = texture2D(u_texture, v_texCoord);\n"
240-
" gl_FragColor = color;\n"
241-
" gl_FragColor.a = 1.0;\n"
278+
" gl_FragColor = vec4(color.r, color.g, color.b, 1.0);\n"
242279
" gl_FragColor *= v_color;\n"
243280
"}\n"
244281
;
@@ -251,8 +288,7 @@ static const char GLES2_Fragment_TextureBGR_PixelArt[] =
251288
"void main()\n"
252289
"{\n"
253290
" mediump vec4 color = GetPixelArtSample(v_texCoord);\n"
254-
" gl_FragColor = color;\n"
255-
" gl_FragColor.a = 1.0;\n"
291+
" gl_FragColor = vec4(color.r, color.g, color.b, 1.0);\n"
256292
" gl_FragColor *= v_color;\n"
257293
"}\n"
258294
;
@@ -264,9 +300,7 @@ static const char GLES2_Fragment_TextureARGB[] =
264300
"void main()\n"
265301
"{\n"
266302
" mediump vec4 color = texture2D(u_texture, v_texCoord);\n"
267-
" gl_FragColor = color;\n"
268-
" gl_FragColor.r = color.b;\n"
269-
" gl_FragColor.b = color.r;\n"
303+
" gl_FragColor = vec4(color.b, color.g, color.r, color.a);\n"
270304
" gl_FragColor *= v_color;\n"
271305
"}\n"
272306
;
@@ -279,9 +313,7 @@ static const char GLES2_Fragment_TextureARGB_PixelArt[] =
279313
"void main()\n"
280314
"{\n"
281315
" mediump vec4 color = GetPixelArtSample(v_texCoord);\n"
282-
" gl_FragColor = color;\n"
283-
" gl_FragColor.r = color.b;\n"
284-
" gl_FragColor.b = color.r;\n"
316+
" gl_FragColor = vec4(color.b, color.g, color.r, color.a);\n"
285317
" gl_FragColor *= v_color;\n"
286318
"}\n"
287319
;
@@ -524,6 +556,12 @@ const char *GLES2_GetShader(GLES2_ShaderType type)
524556
return GLES2_Fragment_TexturePalette_Linear;
525557
case GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART:
526558
return GLES2_Fragment_TexturePalette_PixelArt;
559+
case GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_NEAREST_COLORSWAP:
560+
return GLES2_Fragment_TexturePalette_Nearest_Colorswap;
561+
case GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_LINEAR_COLORSWAP:
562+
return GLES2_Fragment_TexturePalette_Linear_Colorswap;
563+
case GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART_COLORSWAP:
564+
return GLES2_Fragment_TexturePalette_PixelArt_Colorswap;
527565
case GLES2_SHADER_FRAGMENT_TEXTURE_RGB:
528566
return GLES2_Fragment_TextureRGB;
529567
case GLES2_SHADER_FRAGMENT_TEXTURE_RGB_PIXELART:

src/render/opengles2/SDL_shaders_gles2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ typedef enum
4242
GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_NEAREST,
4343
GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_LINEAR,
4444
GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART,
45+
GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_NEAREST_COLORSWAP,
46+
GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_LINEAR_COLORSWAP,
47+
GLES2_SHADER_FRAGMENT_TEXTURE_PALETTE_PIXELART_COLORSWAP,
4548
GLES2_SHADER_FRAGMENT_TEXTURE_RGB,
4649
GLES2_SHADER_FRAGMENT_TEXTURE_RGB_PIXELART,
4750
GLES2_SHADER_FRAGMENT_TEXTURE_BGR,

0 commit comments

Comments
 (0)