SDL  2.0
SDL_shaders_gles2.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
24 
25 #include "SDL_video.h"
26 #include "SDL_opengles2.h"
27 #include "SDL_shaders_gles2.h"
28 #include "SDL_stdinc.h"
29 
30 /*************************************************************************************************
31  * Vertex/fragment shader source *
32  *************************************************************************************************/
33 
34 static const Uint8 GLES2_VertexSrc_Default_[] = " \
35  uniform mat4 u_projection; \
36  attribute vec2 a_position; \
37  attribute vec2 a_texCoord; \
38  attribute float a_angle; \
39  attribute vec2 a_center; \
40  varying vec2 v_texCoord; \
41  \
42  void main() \
43  { \
44  float angle = radians(a_angle); \
45  float c = cos(angle); \
46  float s = sin(angle); \
47  mat2 rotationMatrix = mat2(c, -s, s, c); \
48  vec2 position = rotationMatrix * (a_position - a_center) + a_center; \
49  v_texCoord = a_texCoord; \
50  gl_Position = u_projection * vec4(position, 0.0, 1.0);\
51  gl_PointSize = 1.0; \
52  } \
53 ";
54 
55 static const Uint8 GLES2_FragmentSrc_SolidSrc_[] = " \
56  precision mediump float; \
57  uniform vec4 u_color; \
58  \
59  void main() \
60  { \
61  gl_FragColor = u_color; \
62  } \
63 ";
64 
65 static const Uint8 GLES2_FragmentSrc_TextureABGRSrc_[] = " \
66  precision mediump float; \
67  uniform sampler2D u_texture; \
68  uniform vec4 u_modulation; \
69  varying vec2 v_texCoord; \
70  \
71  void main() \
72  { \
73  gl_FragColor = texture2D(u_texture, v_texCoord); \
74  gl_FragColor *= u_modulation; \
75  } \
76 ";
77 
78 /* ARGB to ABGR conversion */
79 static const Uint8 GLES2_FragmentSrc_TextureARGBSrc_[] = " \
80  precision mediump float; \
81  uniform sampler2D u_texture; \
82  uniform vec4 u_modulation; \
83  varying vec2 v_texCoord; \
84  \
85  void main() \
86  { \
87  vec4 abgr = texture2D(u_texture, v_texCoord); \
88  gl_FragColor = abgr; \
89  gl_FragColor.r = abgr.b; \
90  gl_FragColor.b = abgr.r; \
91  gl_FragColor *= u_modulation; \
92  } \
93 ";
94 
95 /* RGB to ABGR conversion */
96 static const Uint8 GLES2_FragmentSrc_TextureRGBSrc_[] = " \
97  precision mediump float; \
98  uniform sampler2D u_texture; \
99  uniform vec4 u_modulation; \
100  varying vec2 v_texCoord; \
101  \
102  void main() \
103  { \
104  vec4 abgr = texture2D(u_texture, v_texCoord); \
105  gl_FragColor = abgr; \
106  gl_FragColor.r = abgr.b; \
107  gl_FragColor.b = abgr.r; \
108  gl_FragColor.a = 1.0; \
109  gl_FragColor *= u_modulation; \
110  } \
111 ";
112 
113 /* BGR to ABGR conversion */
114 static const Uint8 GLES2_FragmentSrc_TextureBGRSrc_[] = " \
115  precision mediump float; \
116  uniform sampler2D u_texture; \
117  uniform vec4 u_modulation; \
118  varying vec2 v_texCoord; \
119  \
120  void main() \
121  { \
122  vec4 abgr = texture2D(u_texture, v_texCoord); \
123  gl_FragColor = abgr; \
124  gl_FragColor.a = 1.0; \
125  gl_FragColor *= u_modulation; \
126  } \
127 ";
128 
129 /* YUV to ABGR conversion */
130 static const Uint8 GLES2_FragmentSrc_TextureYUVSrc_[] = " \
131  precision mediump float; \
132  uniform sampler2D u_texture; \
133  uniform sampler2D u_texture_u; \
134  uniform sampler2D u_texture_v; \
135  uniform vec4 u_modulation; \
136  varying vec2 v_texCoord; \
137  \
138  void main() \
139  { \
140  mediump vec3 yuv; \
141  lowp vec3 rgb; \
142  yuv.x = texture2D(u_texture, v_texCoord).r; \
143  yuv.y = texture2D(u_texture_u, v_texCoord).r - 0.5; \
144  yuv.z = texture2D(u_texture_v, v_texCoord).r - 0.5; \
145  rgb = mat3( 1, 1, 1, \
146  0, -0.39465, 2.03211, \
147  1.13983, -0.58060, 0) * yuv; \
148  gl_FragColor = vec4(rgb, 1); \
149  gl_FragColor *= u_modulation; \
150  } \
151 ";
152 
153 /* NV12 to ABGR conversion */
154 static const Uint8 GLES2_FragmentSrc_TextureNV12Src_[] = " \
155  precision mediump float; \
156  uniform sampler2D u_texture; \
157  uniform sampler2D u_texture_u; \
158  uniform vec4 u_modulation; \
159  varying vec2 v_texCoord; \
160  \
161  void main() \
162  { \
163  mediump vec3 yuv; \
164  lowp vec3 rgb; \
165  yuv.x = texture2D(u_texture, v_texCoord).r; \
166  yuv.yz = texture2D(u_texture_u, v_texCoord).ra - 0.5; \
167  rgb = mat3( 1, 1, 1, \
168  0, -0.39465, 2.03211, \
169  1.13983, -0.58060, 0) * yuv; \
170  gl_FragColor = vec4(rgb, 1); \
171  gl_FragColor *= u_modulation; \
172  } \
173 ";
174 
175 /* NV21 to ABGR conversion */
176 static const Uint8 GLES2_FragmentSrc_TextureNV21Src_[] = " \
177  precision mediump float; \
178  uniform sampler2D u_texture; \
179  uniform sampler2D u_texture_u; \
180  uniform vec4 u_modulation; \
181  varying vec2 v_texCoord; \
182  \
183  void main() \
184  { \
185  mediump vec3 yuv; \
186  lowp vec3 rgb; \
187  yuv.x = texture2D(u_texture, v_texCoord).r; \
188  yuv.yz = texture2D(u_texture_u, v_texCoord).ar - 0.5; \
189  rgb = mat3( 1, 1, 1, \
190  0, -0.39465, 2.03211, \
191  1.13983, -0.58060, 0) * yuv; \
192  gl_FragColor = vec4(rgb, 1); \
193  gl_FragColor *= u_modulation; \
194  } \
195 ";
196 
197 static const GLES2_ShaderInstance GLES2_VertexSrc_Default = {
199  GLES2_SOURCE_SHADER,
200  sizeof(GLES2_VertexSrc_Default_),
201  GLES2_VertexSrc_Default_
202 };
203 
204 static const GLES2_ShaderInstance GLES2_FragmentSrc_SolidSrc = {
206  GLES2_SOURCE_SHADER,
207  sizeof(GLES2_FragmentSrc_SolidSrc_),
208  GLES2_FragmentSrc_SolidSrc_
209 };
210 
211 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureABGRSrc = {
213  GLES2_SOURCE_SHADER,
214  sizeof(GLES2_FragmentSrc_TextureABGRSrc_),
215  GLES2_FragmentSrc_TextureABGRSrc_
216 };
217 
218 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureARGBSrc = {
220  GLES2_SOURCE_SHADER,
221  sizeof(GLES2_FragmentSrc_TextureARGBSrc_),
222  GLES2_FragmentSrc_TextureARGBSrc_
223 };
224 
225 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureRGBSrc = {
227  GLES2_SOURCE_SHADER,
228  sizeof(GLES2_FragmentSrc_TextureRGBSrc_),
229  GLES2_FragmentSrc_TextureRGBSrc_
230 };
231 
232 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureBGRSrc = {
234  GLES2_SOURCE_SHADER,
235  sizeof(GLES2_FragmentSrc_TextureBGRSrc_),
236  GLES2_FragmentSrc_TextureBGRSrc_
237 };
238 
239 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureYUVSrc = {
241  GLES2_SOURCE_SHADER,
242  sizeof(GLES2_FragmentSrc_TextureYUVSrc_),
243  GLES2_FragmentSrc_TextureYUVSrc_
244 };
245 
246 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureNV12Src = {
248  GLES2_SOURCE_SHADER,
249  sizeof(GLES2_FragmentSrc_TextureNV12Src_),
250  GLES2_FragmentSrc_TextureNV12Src_
251 };
252 
253 static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureNV21Src = {
255  GLES2_SOURCE_SHADER,
256  sizeof(GLES2_FragmentSrc_TextureNV21Src_),
257  GLES2_FragmentSrc_TextureNV21Src_
258 };
259 
260 
261 /*************************************************************************************************
262  * Vertex/fragment shader definitions *
263  *************************************************************************************************/
264 
265 static GLES2_Shader GLES2_VertexShader_Default = {
266  1,
267  {
268  &GLES2_VertexSrc_Default
269  }
270 };
271 
272 static GLES2_Shader GLES2_FragmentShader_SolidSrc = {
273  1,
274  {
275  &GLES2_FragmentSrc_SolidSrc
276  }
277 };
278 
279 static GLES2_Shader GLES2_FragmentShader_TextureABGRSrc = {
280  1,
281  {
282  &GLES2_FragmentSrc_TextureABGRSrc
283  }
284 };
285 
286 static GLES2_Shader GLES2_FragmentShader_TextureARGBSrc = {
287  1,
288  {
289  &GLES2_FragmentSrc_TextureARGBSrc
290  }
291 };
292 
293 static GLES2_Shader GLES2_FragmentShader_TextureRGBSrc = {
294  1,
295  {
296  &GLES2_FragmentSrc_TextureRGBSrc
297  }
298 };
299 
300 static GLES2_Shader GLES2_FragmentShader_TextureBGRSrc = {
301  1,
302  {
303  &GLES2_FragmentSrc_TextureBGRSrc
304  }
305 };
306 
307 static GLES2_Shader GLES2_FragmentShader_TextureYUVSrc = {
308  1,
309  {
310  &GLES2_FragmentSrc_TextureYUVSrc
311  }
312 };
313 
314 static GLES2_Shader GLES2_FragmentShader_TextureNV12Src = {
315  1,
316  {
317  &GLES2_FragmentSrc_TextureNV12Src
318  }
319 };
320 
321 static GLES2_Shader GLES2_FragmentShader_TextureNV21Src = {
322  1,
323  {
324  &GLES2_FragmentSrc_TextureNV21Src
325  }
326 };
327 
328 
329 /*************************************************************************************************
330  * Shader selector *
331  *************************************************************************************************/
332 
333 const GLES2_Shader *GLES2_GetShader(GLES2_ShaderType type)
334 {
335  switch (type) {
336  case GLES2_SHADER_VERTEX_DEFAULT:
337  return &GLES2_VertexShader_Default;
338  case GLES2_SHADER_FRAGMENT_SOLID_SRC:
339  return &GLES2_FragmentShader_SolidSrc;
340  case GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC:
341  return &GLES2_FragmentShader_TextureABGRSrc;
342  case GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC:
343  return &GLES2_FragmentShader_TextureARGBSrc;
344  case GLES2_SHADER_FRAGMENT_TEXTURE_RGB_SRC:
345  return &GLES2_FragmentShader_TextureRGBSrc;
346  case GLES2_SHADER_FRAGMENT_TEXTURE_BGR_SRC:
347  return &GLES2_FragmentShader_TextureBGRSrc;
348  case GLES2_SHADER_FRAGMENT_TEXTURE_YUV_SRC:
349  return &GLES2_FragmentShader_TextureYUVSrc;
350  case GLES2_SHADER_FRAGMENT_TEXTURE_NV12_SRC:
351  return &GLES2_FragmentShader_TextureNV12Src;
352  case GLES2_SHADER_FRAGMENT_TEXTURE_NV21_SRC:
353  return &GLES2_FragmentShader_TextureNV21Src;
354  default:
355  return NULL;
356  }
357 }
358 
359 #endif /* SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED */
360 
361 /* vi: set ts=4 sw=4 expandtab: */
#define GL_VERTEX_SHADER
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:153
#define GL_FRAGMENT_SHADER
#define NULL
Definition: begin_code.h:164
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571