├── test ├── all.png ├── img.png ├── img2.png ├── stone.png ├── stone_n.png ├── pixel_art.png ├── color_mode │ ├── ref_rendering │ │ ├── ADD_blue.png │ │ ├── ADD_red.png │ │ ├── MOD_blue.png │ │ ├── MOD_red.png │ │ ├── NONE_red.png │ │ ├── ADD_green.png │ │ ├── ADD_white.png │ │ ├── BLEND_blue.png │ │ ├── BLEND_red.png │ │ ├── MOD_green.png │ │ ├── MOD_white.png │ │ ├── NONE_blue.png │ │ ├── NONE_green.png │ │ ├── NONE_white.png │ │ ├── BLEND_green.png │ │ ├── BLEND_white.png │ │ ├── ADD_semitransp.png │ │ ├── MOD_semitransp.png │ │ ├── NONE_semitransp.png │ │ └── BLEND_semitransp.png │ └── main.c ├── context_switch │ ├── ref_rendering │ │ └── ref.png │ └── main.c ├── vertex_buffer │ ├── ref_rendering │ │ └── ref.png │ └── main.c ├── all_the_shaders │ ├── ref_rendering │ │ ├── do_nothing.png │ │ └── greyscale.png │ └── main.c ├── shaders │ ├── normal_map │ │ ├── normal_map.gl.vert │ │ ├── normal_map.gles2.vert │ │ ├── normal_map.gl.frag │ │ ├── normal_map.gles2.frag │ │ └── normal_map.d3d.hlsl │ ├── sepia │ │ ├── sepia.gles2.vert │ │ ├── sepia.gl.vert │ │ ├── sepia.gl.frag │ │ ├── sepia.gles2.frag │ │ └── sepia.d3d.hlsl │ ├── multitex │ │ ├── multitex.gles2.vert │ │ ├── multitex.gl.vert │ │ ├── multitex.gl.frag │ │ ├── multitex.gles2.frag │ │ └── multitex.d3d.hlsl │ ├── scale2x │ │ ├── scale2x.gl.vert │ │ ├── scale2x.gles2.vert │ │ ├── scale2x.gles2.frag │ │ ├── scale2x.gl.frag │ │ └── scale2x.d3d.hlsl │ ├── do_nothing │ │ ├── do_nothing.gles2.vert │ │ ├── do_nothing.gl.vert │ │ ├── do_nothing.gl.frag │ │ ├── do_nothing.gles2.frag │ │ └── do_nothing.d3d.hlsl │ └── greyscale │ │ ├── greyscale.gles2.vert │ │ ├── greyscale.gl.vert │ │ ├── greyscale.gl.frag │ │ ├── greyscale.gles2.frag │ │ └── greyscale.d3d.hlsl ├── normal_map │ └── main.c └── scale2x │ └── main.c ├── src ├── test │ ├── SDL_test_font.c │ └── SDL_test_font.h ├── d3d │ ├── SDL_D3D_RenderStructs.h │ ├── SDL_D3D_shader.h │ ├── SDL_D3D_SDL_internals.c │ └── SDL_D3D_shader.c ├── opengl │ ├── SDL_GL_shader.h │ ├── SDL_GL_RenderStructs.h │ └── SDL_GL_shader.c ├── opengles2 │ ├── SDL_GLES2_shader.h │ ├── SDL_gles2funcs.h │ ├── SDL_GLES2_RenderStructs.h │ └── SDL_GLES2_shader.c ├── SDL_shader.h ├── SDL_SYS_RenderStructs.h └── SDL_shader.c ├── linux.mk ├── win64.mk ├── macos.mk ├── LICENSE ├── common.mk └── README.md /test/all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/all.png -------------------------------------------------------------------------------- /test/img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/img.png -------------------------------------------------------------------------------- /test/img2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/img2.png -------------------------------------------------------------------------------- /test/stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/stone.png -------------------------------------------------------------------------------- /test/stone_n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/stone_n.png -------------------------------------------------------------------------------- /test/pixel_art.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/pixel_art.png -------------------------------------------------------------------------------- /src/test/SDL_test_font.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/src/test/SDL_test_font.c -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/ADD_blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/ADD_blue.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/ADD_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/ADD_red.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/MOD_blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/MOD_blue.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/MOD_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/MOD_red.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/NONE_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/NONE_red.png -------------------------------------------------------------------------------- /test/context_switch/ref_rendering/ref.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/context_switch/ref_rendering/ref.png -------------------------------------------------------------------------------- /test/vertex_buffer/ref_rendering/ref.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/vertex_buffer/ref_rendering/ref.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/ADD_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/ADD_green.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/ADD_white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/ADD_white.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/BLEND_blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/BLEND_blue.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/BLEND_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/BLEND_red.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/MOD_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/MOD_green.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/MOD_white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/MOD_white.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/NONE_blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/NONE_blue.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/NONE_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/NONE_green.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/NONE_white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/NONE_white.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/BLEND_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/BLEND_green.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/BLEND_white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/BLEND_white.png -------------------------------------------------------------------------------- /test/all_the_shaders/ref_rendering/do_nothing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/all_the_shaders/ref_rendering/do_nothing.png -------------------------------------------------------------------------------- /test/all_the_shaders/ref_rendering/greyscale.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/all_the_shaders/ref_rendering/greyscale.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/ADD_semitransp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/ADD_semitransp.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/MOD_semitransp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/MOD_semitransp.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/NONE_semitransp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/NONE_semitransp.png -------------------------------------------------------------------------------- /test/color_mode/ref_rendering/BLEND_semitransp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powertomato/SDL2_shader/HEAD/test/color_mode/ref_rendering/BLEND_semitransp.png -------------------------------------------------------------------------------- /test/shaders/normal_map/normal_map.gl.vert: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec4 color; 7 | attribute vec2 texCoords; 8 | 9 | void main(void){ 10 | gl_FrontColor = color; 11 | gl_Position = world_view_projection * position; 12 | gl_TexCoord[0] = vec4(texCoords,1,1); 13 | } 14 | -------------------------------------------------------------------------------- /test/shaders/sepia/sepia.gles2.vert: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec2 texCoords; 7 | attribute vec4 color; 8 | 9 | varying vec4 col_out; 10 | varying vec2 texCoord_out; 11 | 12 | void main(){ 13 | col_out = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_out = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/multitex/multitex.gles2.vert: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec2 texCoords; 7 | attribute vec4 color; 8 | 9 | varying vec4 col_out; 10 | varying vec2 texCoord_out; 11 | 12 | void main(){ 13 | col_out = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_out = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/scale2x/scale2x.gl.vert: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec4 color; 7 | attribute vec2 texCoords; 8 | 9 | varying vec4 color_v2f; 10 | varying vec2 texCoord_v2f; 11 | 12 | void main(void){ 13 | color_v2f = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_v2f = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/scale2x/scale2x.gles2.vert: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec2 texCoords; 7 | attribute vec4 color; 8 | 9 | varying vec4 col_out; 10 | varying vec2 texCoord_out; 11 | 12 | void main(){ 13 | col_out = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_out = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/do_nothing/do_nothing.gles2.vert: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec2 texCoords; 7 | attribute vec4 color; 8 | 9 | varying vec4 col_out; 10 | varying vec2 texCoord_out; 11 | 12 | void main(){ 13 | col_out = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_out = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/greyscale/greyscale.gles2.vert: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec2 texCoords; 7 | attribute vec4 color; 8 | 9 | varying vec4 col_out; 10 | varying vec2 texCoord_out; 11 | 12 | void main(){ 13 | col_out = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_out = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/normal_map/normal_map.gles2.vert: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | uniform mat4 world_view_projection; 4 | 5 | attribute vec4 position; 6 | attribute vec2 texCoords; 7 | attribute vec4 color; 8 | 9 | varying vec4 col_out; 10 | varying vec2 texCoord_out; 11 | 12 | void main(){ 13 | col_out = color; 14 | gl_Position = world_view_projection * position; 15 | texCoord_out = texCoords; 16 | } 17 | -------------------------------------------------------------------------------- /test/shaders/sepia/sepia.gl.vert: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | uniform mat4 world_view_projection; 4 | attribute vec4 position; 5 | attribute vec2 texCoords; 6 | attribute vec4 color; 7 | void main(void){ 8 | gl_FrontColor = color; 9 | //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 10 | gl_Position = world_view_projection * position; 11 | gl_TexCoord[0] = vec4(texCoords,1,1); //gl_MultiTexCoord0; 12 | } 13 | -------------------------------------------------------------------------------- /test/shaders/do_nothing/do_nothing.gl.vert: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | uniform mat4 world_view_projection; 4 | attribute vec4 position; 5 | attribute vec4 color; 6 | attribute vec2 texCoords; 7 | void main(void){ 8 | gl_FrontColor = color; 9 | //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 10 | gl_Position = world_view_projection * position; 11 | gl_TexCoord[0] = vec4(texCoords,1,1); //gl_MultiTexCoord0; 12 | } 13 | -------------------------------------------------------------------------------- /test/shaders/greyscale/greyscale.gl.vert: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | uniform mat4 world_view_projection; 4 | attribute vec4 position; 5 | attribute vec2 texCoords; 6 | attribute vec4 color; 7 | void main(void){ 8 | gl_FrontColor = color; 9 | //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 10 | gl_Position = world_view_projection * position; 11 | gl_TexCoord[0] = vec4(texCoords,1,1); //gl_MultiTexCoord0; 12 | } 13 | -------------------------------------------------------------------------------- /test/shaders/multitex/multitex.gl.vert: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | uniform mat4 world_view_projection; 4 | attribute vec4 position; 5 | attribute vec4 color; 6 | attribute vec2 texCoords; 7 | void main(void){ 8 | gl_FrontColor = color; 9 | //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 10 | gl_Position = world_view_projection * position; 11 | gl_TexCoord[0] = vec4(texCoords,1,1); //gl_MultiTexCoord0; 12 | } 13 | -------------------------------------------------------------------------------- /test/shaders/do_nothing/do_nothing.gl.frag: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | vec4 convertColor(int type, vec4 color); 10 | uniform int color_mode; 11 | 12 | uniform sampler2D colorMap; 13 | 14 | void main(void){ 15 | vec4 texcol = texture2D(colorMap, gl_TexCoord[0].st); 16 | texcol = convertColor(color_mode,texcol); 17 | gl_FragColor = texcol * gl_Color; 18 | } 19 | -------------------------------------------------------------------------------- /test/shaders/do_nothing/do_nothing.gles2.frag: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | precision mediump float; 10 | 11 | varying vec4 col_out; 12 | varying vec2 texCoord_out; 13 | 14 | vec4 convertColor(int type, vec4 color); 15 | uniform int color_mode; 16 | 17 | uniform sampler2D colorMap; 18 | 19 | void main(void){ 20 | vec4 texcol = texture2D(colorMap, texCoord_out.st); 21 | texcol = convertColor(color_mode, texcol); 22 | gl_FragColor = texcol * col_out; 23 | } 24 | -------------------------------------------------------------------------------- /linux.mk: -------------------------------------------------------------------------------- 1 | DEFINES+=-DSDL_SHADER_OPENGL \ 2 | -DSDL_SHADER_OPENGLES2 \ 3 | -DGL_GLEXT_PROTOTYPES 4 | 5 | TEST_CASES=color_mode \ 6 | context_switch \ 7 | all_the_shaders \ 8 | vertex_buffer \ 9 | normal_map \ 10 | scale2x 11 | 12 | GCC=gcc 13 | GPP=g++ 14 | AR=ar 15 | RM=rm -rf 16 | MKDIR_LIST=mkdir -p 17 | CPPFLAGS=-g3 --pedantic -Wall -std=c++11 $(DEFINES) -fno-rtti 18 | CFLAGS=-g3 --pedantic -Wall -std=c99 $(DEFINES) 19 | LDFLAGS=-lSDL2 -lSDL2_image \ 20 | -ldl -lGL -lm 21 | EXE_EXT= 22 | RUN=#optirun 23 | 24 | include common.mk 25 | 26 | .PHONY: install 27 | install: 28 | cp libSDL2_shader.a /usr/lib 29 | cp src/SDL_shader.h /usr/include 30 | -------------------------------------------------------------------------------- /test/shaders/greyscale/greyscale.gl.frag: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | vec4 convertColor(int type, vec4 color); 10 | uniform int color_mode; 11 | 12 | uniform sampler2D colorMap; 13 | 14 | void main(void){ 15 | vec4 texcol = texture2D(colorMap, gl_TexCoord[0].st); 16 | texcol = convertColor(color_mode,texcol); 17 | texcol = texcol * gl_Color; 18 | 19 | float grey = 0.2126 *texcol.r + 0.7152 *texcol.g + 0.0722 *texcol.b; 20 | gl_FragColor = vec4(grey,grey,grey,texcol.a); 21 | } 22 | -------------------------------------------------------------------------------- /win64.mk: -------------------------------------------------------------------------------- 1 | DEFINES+=-DSDL_SHADER_D3D \ 2 | -DSDL_SHADER_OPENGL \ 3 | #-D SDL_SHADER_D3D11 4 | 5 | TEST_CASES=color_mode \ 6 | context_switch \ 7 | all_the_shaders \ 8 | vertex_buffer \ 9 | normal_map \ 10 | scale2x 11 | 12 | GCC=x86_64-w64-mingw32-gcc 13 | GPP=x86_64-w64-mingw32-g++ 14 | AR=x86_64-w64-mingw32-ar 15 | RM=rm -rf 16 | MKDIR_LIST=mkdir -p 17 | CPPFLAGS=-g3 --pedantic -Wall -std=c++11 $(DEFINES) -fno-rtti 18 | CFLAGS=-g3 --pedantic -Wall -std=c99 $(DEFINES) 19 | LDFLAGS= -luuid -lSDL2 -lSDL2_image -ld3dcompiler_43\ 20 | -lws2_32 -lstdc++ -ld3dx9_43 -ld3dx11_43\ 21 | -lopengl32 -lglew32 22 | EXE_EXT=.exe 23 | RUN=wine 24 | 25 | include common.mk 26 | -------------------------------------------------------------------------------- /test/shaders/multitex/multitex.gl.frag: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | vec4 convertColor(int type, vec4 color); 10 | uniform int color_mode; 11 | 12 | uniform sampler2D color_map; 13 | uniform sampler2D color_map2; 14 | 15 | void main(void){ 16 | vec4 texcol = texture2D(color_map, gl_TexCoord[0].st); 17 | texcol = convertColor(color_mode,texcol); 18 | vec4 texcol2 = texture2D(color_map2, gl_TexCoord[0].st); 19 | texcol2 = convertColor(color_mode,texcol2); 20 | 21 | gl_FragColor = mix(texcol,texcol2,0.5) * gl_Color; 22 | } 23 | -------------------------------------------------------------------------------- /macos.mk: -------------------------------------------------------------------------------- 1 | DEFINES+=-DSDL_SHADER_OPENGL \ 2 | -DGL_GLEXT_PROTOTYPES 3 | 4 | TEST_CASES=color_mode \ 5 | context_switch \ 6 | all_the_shaders \ 7 | vertex_buffer \ 8 | normal_map \ 9 | scale2x 10 | 11 | GCC=clang 12 | GPP=clang++ 13 | AR=ar 14 | RM=rm -rf 15 | FRAMEWORKS= -F/Library/Frameworks 16 | #-F/Library/Frameworks/SDL2_Image.framework/ -framework SDL2 17 | CPPFLAGS=-g3 -pedantic -Wall -std=c++11 $(DEFINES) $(FRAMEWORKS) 18 | CFLAGS=-g3 -pedantic -Wall -std=c99 $(DEFINES) $(FRAMEWORKS) 19 | LDFLAGS=-framework SDL2\ 20 | -framework SDL2_Image\ 21 | -framework OpenGL 22 | 23 | EXE_EXT= 24 | RUN= 25 | 26 | include common.mk 27 | 28 | .PHONY: install 29 | install: 30 | echo "No install target yet" 31 | #TODO 32 | -------------------------------------------------------------------------------- /test/shaders/scale2x/scale2x.gles2.frag: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | precision mediump float; 10 | 11 | varying vec4 col_out; 12 | varying vec2 texCoord_out; 13 | 14 | vec4 convertColor(int type, vec4 color); 15 | uniform int color_mode; 16 | 17 | uniform sampler2D colorMap; 18 | 19 | void main(void){ 20 | if ( mod(int(gl_FragCoord.y), 2) == 0.0 ) { 21 | vec4 texcol = texture2D(colorMap, texCoord_out.st); 22 | texcol = convertColor(color_mode,texcol); 23 | gl_FragColor = texcol * col_out; 24 | } else { 25 | gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/shaders/greyscale/greyscale.gles2.frag: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | precision mediump float; 10 | 11 | varying vec4 col_out; 12 | varying vec2 texCoord_out; 13 | 14 | vec4 convertColor(int type, vec4 color); 15 | uniform int color_mode; 16 | 17 | uniform sampler2D colorMap; 18 | 19 | void main(void){ 20 | vec4 texcol = texture2D(colorMap, texCoord_out.st); 21 | texcol = convertColor(color_mode, texcol); 22 | texcol = texcol * col_out; 23 | 24 | float grey = 0.2126 *texcol.r + 0.7152 *texcol.g + 0.0722 *texcol.b; 25 | gl_FragColor = vec4(grey,grey,grey,texcol.a); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /test/shaders/multitex/multitex.gles2.frag: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | precision mediump float; 10 | 11 | varying vec4 col_out; 12 | varying vec2 texCoord_out; 13 | 14 | vec4 convertColor(int type, vec4 color); 15 | uniform int color_mode; 16 | 17 | uniform sampler2D color_map; 18 | uniform sampler2D color_map2; 19 | 20 | void main(void){ 21 | vec4 texcol = texture2D(color_map, texCoord_out.st); 22 | texcol = convertColor(color_mode, texcol); 23 | 24 | vec4 texcol2 = texture2D(color_map2, texCoord_out.st); 25 | texcol2 = convertColor(color_mode, texcol2); 26 | 27 | gl_FragColor = mix(texcol,texcol2,0.5) * col_out; 28 | } 29 | -------------------------------------------------------------------------------- /test/shaders/sepia/sepia.gl.frag: -------------------------------------------------------------------------------- 1 | #version 120 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | vec4 convertColor(int type, vec4 color); 10 | uniform int color_mode; 11 | 12 | uniform sampler2D colorMap; 13 | 14 | void main(void){ 15 | vec4 texcol = texture2D(colorMap, gl_TexCoord[0].st); 16 | texcol = convertColor(color_mode,texcol); 17 | texcol = texcol * gl_Color; 18 | 19 | /* http://www.techrepublic.com/blog/how-do-i/ 20 | how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/# */ 21 | gl_FragColor.a = texcol.a; 22 | gl_FragColor.r = texcol.r * 0.393 + texcol.g * 0.769 + texcol.b * 0.189; 23 | gl_FragColor.g = texcol.r * 0.349 + texcol.g * 0.686 + texcol.b * 0.168; 24 | gl_FragColor.b = texcol.r * 0.272 + texcol.g * 0.534 + texcol.b * 0.131; 25 | } 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | SDL2 shader 2 | Copyright (C) 2014 Stefan Krulj (powertomato) 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the authors be held liable for any damages 6 | arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any purpose, 9 | including commercial applications, and to alter it and redistribute it 10 | freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must not 13 | claim that you wrote the original software. If you use this software 14 | in a product, an acknowledgment in the product documentation would be 15 | appreciated but is not required. 16 | 2. Altered source versions must be plainly marked as such, and must not be 17 | misrepresented as being the original software. 18 | 3. This notice may not be removed or altered from any source distribution. 19 | -------------------------------------------------------------------------------- /test/shaders/sepia/sepia.gles2.frag: -------------------------------------------------------------------------------- 1 | #version 100 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | precision mediump float; 10 | 11 | varying vec4 col_out; 12 | varying vec2 texCoord_out; 13 | 14 | vec4 convertColor(int type, vec4 color); 15 | uniform int color_mode; 16 | 17 | uniform sampler2D colorMap; 18 | 19 | void main(void){ 20 | vec4 texcol = texture2D(colorMap, texCoord_out.st); 21 | texcol = convertColor(color_mode, texcol); 22 | texcol = texcol * col_out; 23 | 24 | /* http://www.techrepublic.com/blog/how-do-i/ 25 | how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/# */ 26 | gl_FragColor.a = texcol.a; 27 | gl_FragColor.r = texcol.r * 0.393 + texcol.g * 0.769 + texcol.b * 0.189; 28 | gl_FragColor.g = texcol.r * 0.349 + texcol.g * 0.686 + texcol.b * 0.168; 29 | gl_FragColor.b = texcol.r * 0.272 + texcol.g * 0.534 + texcol.b * 0.131; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /test/shaders/do_nothing/do_nothing.d3d.hlsl: -------------------------------------------------------------------------------- 1 | 2 | texture xColoredTexture; 3 | float4x4 world_view_projection : WORLDVIEWPROJECTION; 4 | 5 | struct AppToVertex { 6 | float4 pos : POSITION; 7 | float2 texcoord : TEXCOORD0; 8 | float4 color : COLOR0; 9 | }; 10 | 11 | struct VertexToPixel { 12 | float4 pos : POSITION; 13 | float2 texcoord : TEXCOORD0; 14 | float4 color : COLOR0; 15 | }; 16 | 17 | struct PixelToFrame { 18 | float4 color : COLOR0; 19 | }; 20 | 21 | 22 | 23 | sampler ColoredTextureSampler = sampler_state { 24 | texture = ; 25 | magfilter = LINEAR; 26 | minfilter = LINEAR; 27 | mipfilter = LINEAR; 28 | AddressU = mirror; 29 | AddressV = mirror; 30 | }; 31 | 32 | VertexToPixel VertexShaderMain( AppToVertex vs_in ) { 33 | VertexToPixel vs_out = (VertexToPixel)0; 34 | vs_out.pos = mul(world_view_projection, vs_in.pos); 35 | vs_out.texcoord = vs_in.texcoord; 36 | vs_out.color = vs_in.color; 37 | 38 | return vs_out; 39 | } 40 | 41 | PixelToFrame PixelShaderMain(VertexToPixel ps_in) { 42 | PixelToFrame ps_out = (PixelToFrame)0; 43 | float4 color = tex2D(ColoredTextureSampler, ps_in.texcoord ); 44 | ps_out.color = color * ps_in.color; 45 | return ps_out; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /test/shaders/greyscale/greyscale.d3d.hlsl: -------------------------------------------------------------------------------- 1 | 2 | texture xColoredTexture; 3 | float4x4 world_view_projection : WORLDVIEWPROJECTION; 4 | 5 | struct AppToVertex { 6 | float4 pos : POSITION; 7 | float2 texcoord : TEXCOORD0; 8 | float4 color : COLOR0; 9 | }; 10 | 11 | struct VertexToPixel { 12 | float4 pos : POSITION; 13 | float2 texcoord : TEXCOORD0; 14 | float4 color : COLOR0; 15 | }; 16 | 17 | struct PixelToFrame { 18 | float4 color : COLOR0; 19 | }; 20 | 21 | 22 | 23 | sampler ColoredTextureSampler = sampler_state { 24 | texture = ; 25 | magfilter = LINEAR; 26 | minfilter = LINEAR; 27 | mipfilter = LINEAR; 28 | AddressU = mirror; 29 | AddressV = mirror; 30 | }; 31 | 32 | VertexToPixel VertexShaderMain( AppToVertex vs_in ) { 33 | VertexToPixel vs_out = (VertexToPixel)0; 34 | vs_out.pos = mul(world_view_projection, vs_in.pos); 35 | vs_out.texcoord = vs_in.texcoord; 36 | vs_out.color = vs_in.color; 37 | 38 | return vs_out; 39 | } 40 | 41 | PixelToFrame PixelShaderMain(VertexToPixel ps_in) { 42 | PixelToFrame ps_out = (PixelToFrame)0; 43 | float4 color = tex2D(ColoredTextureSampler, ps_in.texcoord ); 44 | color = color * ps_in.color; 45 | float grey = 0.2126 *color.r + 0.7152 *color.g + 0.0722 *color.b; 46 | 47 | ps_out.color = float4(grey, grey, grey, color.a); 48 | return ps_out; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /test/shaders/multitex/multitex.d3d.hlsl: -------------------------------------------------------------------------------- 1 | 2 | texture xColoredTexture; 3 | float4x4 world_view_projection : WORLDVIEWPROJECTION; 4 | 5 | struct AppToVertex { 6 | float4 pos : POSITION; 7 | float2 texcoord : TEXCOORD0; 8 | float4 color : COLOR0; 9 | }; 10 | 11 | struct VertexToPixel { 12 | float4 pos : POSITION; 13 | float2 texcoord : TEXCOORD0; 14 | float4 color : COLOR0; 15 | }; 16 | 17 | struct PixelToFrame { 18 | float4 color : COLOR0; 19 | }; 20 | 21 | 22 | 23 | sampler color_map = sampler_state { 24 | texture = ; 25 | magfilter = LINEAR; 26 | minfilter = LINEAR; 27 | mipfilter = LINEAR; 28 | AddressU = mirror; 29 | AddressV = mirror; 30 | }; 31 | 32 | sampler color_map2 = sampler_state { 33 | texture = ; 34 | magfilter = LINEAR; 35 | minfilter = LINEAR; 36 | mipfilter = LINEAR; 37 | AddressU = mirror; 38 | AddressV = mirror; 39 | }; 40 | 41 | VertexToPixel VertexShaderMain( AppToVertex vs_in ) { 42 | VertexToPixel vs_out = (VertexToPixel)0; 43 | vs_out.pos = mul(world_view_projection, vs_in.pos); 44 | vs_out.texcoord = vs_in.texcoord; 45 | vs_out.color = vs_in.color; 46 | 47 | return vs_out; 48 | } 49 | 50 | PixelToFrame PixelShaderMain(VertexToPixel ps_in) { 51 | PixelToFrame ps_out = (PixelToFrame)0; 52 | float4 color = tex2D( color_map, ps_in.texcoord ); 53 | float4 color2 = tex2D( color_map2, ps_in.texcoord ); 54 | ps_out.color = lerp(color,color2,0.5) * ps_in.color; 55 | return ps_out; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /test/shaders/sepia/sepia.d3d.hlsl: -------------------------------------------------------------------------------- 1 | 2 | texture xColoredTexture; 3 | float4x4 world_view_projection : WORLDVIEWPROJECTION; 4 | 5 | struct AppToVertex { 6 | float4 pos : POSITION; 7 | float2 texcoord : TEXCOORD0; 8 | float4 color : COLOR0; 9 | }; 10 | 11 | struct VertexToPixel { 12 | float4 pos : POSITION; 13 | float2 texcoord : TEXCOORD0; 14 | float4 color : COLOR0; 15 | }; 16 | 17 | struct PixelToFrame { 18 | float4 color : COLOR0; 19 | }; 20 | 21 | 22 | 23 | sampler ColoredTextureSampler = sampler_state { 24 | texture = ; 25 | magfilter = LINEAR; 26 | minfilter = LINEAR; 27 | mipfilter = LINEAR; 28 | AddressU = mirror; 29 | AddressV = mirror; 30 | }; 31 | 32 | VertexToPixel VertexShaderMain( AppToVertex vs_in ) { 33 | VertexToPixel vs_out = (VertexToPixel)0; 34 | vs_out.pos = mul(world_view_projection, vs_in.pos); 35 | vs_out.texcoord = vs_in.texcoord; 36 | vs_out.color = vs_in.color; 37 | 38 | return vs_out; 39 | } 40 | 41 | PixelToFrame PixelShaderMain(VertexToPixel ps_in) { 42 | PixelToFrame ps_out = (PixelToFrame)0; 43 | float4 color = tex2D(ColoredTextureSampler, ps_in.texcoord ); 44 | color = color * ps_in.color; 45 | 46 | /* http://www.techrepublic.com/blog/how-do-i/ 47 | how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/# */ 48 | ps_out.color.a = color.a; 49 | ps_out.color.r = color.r * 0.393 + color.g * 0.769 + color.b * 0.189; 50 | ps_out.color.g = color.r * 0.349 + color.g * 0.686 + color.b * 0.168; 51 | ps_out.color.b = color.r * 0.272 + color.g * 0.534 + color.b * 0.131; 52 | return ps_out; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /common.mk: -------------------------------------------------------------------------------- 1 | .PHONY: all clean test runtest clean_test flags builddirs 2 | 3 | # paths 4 | SRCPATH=src 5 | TESTPATH=test 6 | TESTBIN=test/bin 7 | OBJPATH=obj 8 | BINPATH=bin 9 | 10 | # objects to be included in the library 11 | OBJ=SDL_shader.o \ 12 | opengl/SDL_GL_shader.o \ 13 | d3d/SDL_D3D_shader.o \ 14 | opengles2/SDL_GLES2_shader.o 15 | #d3d11/SDL_D3D11_shader.o \ 16 | 17 | 18 | DIRS=opengl/ \ 19 | d3d \ 20 | d3d11 \ 21 | opengles2 \ 22 | test 23 | 24 | # objects needed for the test cases 25 | TEST_OBJ=test/SDL_test_font.o 26 | TEST_BIN=$(TEST_CASES:%=$(TESTBIN)/test_%$(EXE_EXT)) 27 | TEST_LDFLAGS=-L. -lSDL2_shader 28 | 29 | 30 | INCLUDE= 31 | OBJECTS=$(OBJ:%=$(OBJPATH)/%) 32 | TEST_OBJECTS=$(TEST_OBJ:%=$(OBJPATH)/%) 33 | BUILDDIRS=$(DIRS:%=$(OBJPATH)/%) 34 | 35 | ifdef NO_COLOR 36 | CLR= 37 | GREEN= 38 | CYAN= 39 | RED= 40 | YELLOW= 41 | else 42 | CLR="\x1b[0m" 43 | GREEN="\x1b[32;01m" 44 | CYAN="\x1b[36;01m" 45 | RED="\x1b[31;01m" 46 | YELLOW="\x1b[33;01m" 47 | endif 48 | 49 | 50 | all: libSDL2_shader.a flags 51 | 52 | libSDL2_shader.a: builddirs $(OBJECTS) 53 | $(AR) rcs libSDL2_shader.a $(OBJECTS) 54 | 55 | flags: 56 | @echo -e $(YELLOW)CFLAGS = $(CFLAGS)$(CLR) 57 | @echo -e $(YELLOW)CPPFLAGS = $(CPPFLAGS)$(CLR) 58 | 59 | builddirs: 60 | @$(MKDIR_LIST) $(BUILDDIRS) 61 | 62 | test: libSDL2_shader.a $(TEST_OBJECTS) runtest 63 | 64 | runtest: $(TEST_BIN) 65 | @$(foreach TEST_CASE,$(TEST_CASES), \ 66 | echo -e $(GREEN)"### Testing: $(TEST_CASE) ###"$(CLR); \ 67 | cd $(TESTBIN); \ 68 | $(RUN) ./test_$(TEST_CASE)$(EXE_EXT); \ 69 | cd ../.. ;) 70 | 71 | $(OBJPATH)/%.o : $(SRCPATH)/%.cpp 72 | @echo "$(GPP) (CPPFLAGS) -c $< -o $@" 73 | @$(GPP) $(CPPFLAGS) $(INCLUDE) -c $< -o $@ 74 | 75 | $(OBJPATH)/%.o : $(SRCPATH)/%.c 76 | @echo "$(GCC) (CFLAGS) -c $< -o $@" 77 | @$(GCC) $(CFLAGS) $(INCLUDE) -c $< -o $@ 78 | 79 | $(TESTBIN)/test_%$(EXE_EXT) : $(TESTPATH)/%/main.c libSDL2_shader.a 80 | @echo "$(GCC) (CFLAGS) (OBJECTS) $< -o $@" 81 | @$(GCC) $(CFLAGS) $(INCLUDE) -I$(SRCPATH) $(TEST_OBJECTS) \ 82 | $< $(LDFLAGS) $(TEST_LDFLAGS) -o $@ 83 | clean_test: 84 | $(RM) $(TEST_BIN) 85 | 86 | clean: clean_test 87 | $(RM) $(OBJECTS) $(MAIN_OBJECT) $(COMPILE_OBJECT) $(TEST_OBJECTS) 88 | 89 | -------------------------------------------------------------------------------- /test/shaders/normal_map/normal_map.gl.frag: -------------------------------------------------------------------------------- 1 | #version 120 2 | /* based on https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6 */ 3 | 4 | 5 | #ifdef GL_ARB_texture_rectangle 6 | #extension GL_ARB_texture_rectangle : enable 7 | #define sampler2D sampler2DRect 8 | #define texture2D texture2DRect 9 | #endif 10 | 11 | vec4 convertColor(int type, vec4 color); 12 | uniform int color_mode; 13 | 14 | //attributes from vertex shader 15 | // gl_Color, gl_TexCoord[0].st 16 | 17 | //our texture samplers 18 | uniform sampler2D u_texture; //diffuse map 19 | uniform sampler2D u_normals; //normal map 20 | 21 | //values used for shading algorithm... 22 | uniform vec2 Resolution; //resolution of screen 23 | uniform vec3 LightPos; //light position, normalized 24 | uniform vec4 LightColor; //light RGBA -- alpha is intensity 25 | uniform vec4 AmbientColor; //ambient RGBA -- alpha is intensity 26 | uniform vec3 Falloff; //attenuation coefficients 27 | 28 | void main() { 29 | //RGBA of our diffuse color 30 | vec4 DiffuseColor = texture2D(u_texture, gl_TexCoord[0].st); 31 | 32 | //RGB of our normal map 33 | vec3 NormalMap = texture2D(u_normals, gl_TexCoord[0].st).rgb; 34 | 35 | //The delta position of light 36 | vec3 LightDir = vec3(LightPos.xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z); 37 | 38 | //Correct for aspect ratio 39 | LightDir.x *= Resolution.x / Resolution.y; 40 | 41 | //Determine distance (used for attenuation) BEFORE we normalize our LightDir 42 | float D = length(LightDir); 43 | 44 | //normalize our vectors 45 | vec3 N = normalize(NormalMap * 2.0 - 1.0); 46 | vec3 L = normalize(LightDir); 47 | 48 | //Pre-multiply light color with intensity 49 | //Then perform "N dot L" to determine our diffuse term 50 | vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0); 51 | 52 | //pre-multiply ambient color with intensity 53 | vec3 Ambient = AmbientColor.rgb * AmbientColor.a; 54 | 55 | //calculate attenuation 56 | float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) ); 57 | 58 | //the calculation which brings it all together 59 | vec3 Intensity = Ambient + Diffuse * Attenuation; 60 | vec3 FinalColor = DiffuseColor.rgb * Intensity; 61 | gl_FragColor = gl_Color * vec4(FinalColor, DiffuseColor.a); 62 | } 63 | -------------------------------------------------------------------------------- /test/shaders/normal_map/normal_map.gles2.frag: -------------------------------------------------------------------------------- 1 | #version 100 2 | /* based on https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6 */ 3 | 4 | 5 | #ifdef GL_ARB_texture_rectangle 6 | #extension GL_ARB_texture_rectangle : enable 7 | #define sampler2D sampler2DRect 8 | #define texture2D texture2DRect 9 | #endif 10 | 11 | precision mediump float; 12 | 13 | 14 | vec4 convertColor(int type, vec4 color); 15 | uniform int color_mode; 16 | 17 | //attributes from vertex shader 18 | // gl_Color, gl_TexCoord[0].st 19 | varying vec4 col_out; 20 | varying vec2 texCoord_out; 21 | 22 | //our texture samplers 23 | uniform sampler2D u_texture; //diffuse map 24 | uniform sampler2D u_normals; //normal map 25 | 26 | //values used for shading algorithm... 27 | uniform vec2 Resolution; //resolution of screen 28 | uniform vec3 LightPos; //light position, normalized 29 | uniform vec4 LightColor; //light RGBA -- alpha is intensity 30 | uniform vec4 AmbientColor; //ambient RGBA -- alpha is intensity 31 | uniform vec3 Falloff; //attenuation coefficients 32 | 33 | void main() { 34 | //RGBA of our diffuse color 35 | vec4 DiffuseColor = texture2D(u_texture, texCoord_out.st); 36 | 37 | //RGB of our normal map 38 | vec3 NormalMap = texture2D(u_normals, texCoord_out.st).rgb; 39 | 40 | //The delta position of light 41 | vec3 LightDir = vec3(LightPos.xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z); 42 | 43 | //Correct for aspect ratio 44 | LightDir.x *= Resolution.x / Resolution.y; 45 | 46 | //Determine distance (used for attenuation) BEFORE we normalize our LightDir 47 | float D = length(LightDir); 48 | 49 | //normalize our vectors 50 | vec3 N = normalize(NormalMap * 2.0 - 1.0); 51 | vec3 L = normalize(LightDir); 52 | 53 | //Pre-multiply light color with intensity 54 | //Then perform "N dot L" to determine our diffuse term 55 | vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0); 56 | 57 | //pre-multiply ambient color with intensity 58 | vec3 Ambient = AmbientColor.rgb * AmbientColor.a; 59 | 60 | //calculate attenuation 61 | float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) ); 62 | 63 | //the calculation which brings it all together 64 | vec3 Intensity = Ambient + Diffuse * Attenuation; 65 | vec3 FinalColor = DiffuseColor.rgb * Intensity; 66 | gl_FragColor = col_out * vec4(FinalColor, DiffuseColor.a); 67 | } 68 | -------------------------------------------------------------------------------- /src/d3d/SDL_D3D_RenderStructs.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | 10 | /* 11 | Simple DirectMedia Layer 12 | Copyright (C) 1997-2014 Sam Lantinga 13 | 14 | This software is provided 'as-is', without any express or implied 15 | warranty. In no event will the authors be held liable for any damages 16 | arising from the use of this software. 17 | 18 | Permission is granted to anyone to use this software for any purpose, 19 | including commercial applications, and to alter it and redistribute it 20 | freely, subject to the following restrictions: 21 | 22 | 1. The origin of this software must not be misrepresented; you must not 23 | claim that you wrote the original software. If you use this software 24 | in a product, an acknowledgment in the product documentation would be 25 | appreciated but is not required. 26 | 2. Altered source versions must be plainly marked as such, and must not be 27 | misrepresented as being the original software. 28 | 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | 31 | #ifndef _SDL_D3D_RenderStructs_h 32 | #define _SDL_D3D_RenderStructs_h 33 | 34 | #include "../SDL_SYS_RenderStructs.h" 35 | 36 | typedef struct 37 | { 38 | void* d3dDLL; 39 | IDirect3D9 *d3d; 40 | IDirect3DDevice9 *device; 41 | UINT adapter; 42 | D3DPRESENT_PARAMETERS pparams; 43 | SDL_bool updateSize; 44 | SDL_bool beginScene; 45 | SDL_bool enableSeparateAlphaBlend; 46 | D3DTEXTUREFILTERTYPE scaleMode[8]; 47 | IDirect3DSurface9 *defaultRenderTarget; 48 | IDirect3DSurface9 *currentRenderTarget; 49 | void* d3dxDLL; 50 | LPDIRECT3DPIXELSHADER9 ps_yuv; 51 | } D3D_RenderData; 52 | 53 | typedef struct 54 | { 55 | IDirect3DTexture9 *texture; 56 | D3DTEXTUREFILTERTYPE scaleMode; 57 | 58 | /* YV12 texture support */ 59 | SDL_bool yuv; 60 | IDirect3DTexture9 *utexture; 61 | IDirect3DTexture9 *vtexture; 62 | Uint8 *pixels; 63 | int pitch; 64 | SDL_Rect locked_rect; 65 | } D3D_TextureData; 66 | 67 | typedef struct 68 | { 69 | float x, y, z; 70 | DWORD color; 71 | float u, v; 72 | } Vertex; 73 | 74 | #endif /* _SDL_D3D_RenderStructs_h */ 75 | 76 | -------------------------------------------------------------------------------- /src/test/SDL_test_font.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | /* 10 | Simple DirectMedia Layer 11 | Copyright (C) 1997-2013 Sam Lantinga 12 | 13 | This software is provided 'as-is', without any express or implied 14 | warranty. In no event will the authors be held liable for any damages 15 | arising from the use of this software. 16 | 17 | Permission is granted to anyone to use this software for any purpose, 18 | including commercial applications, and to alter it and redistribute it 19 | freely, subject to the following restrictions: 20 | 21 | 1. The origin of this software must not be misrepresented; you must not 22 | claim that you wrote the original software. If you use this software 23 | in a product, an acknowledgment in the product documentation would be 24 | appreciated but is not required. 25 | 2. Altered source versions must be plainly marked as such, and must not be 26 | misrepresented as being the original software. 27 | 3. This notice may not be removed or altered from any source distribution. 28 | */ 29 | 30 | /** 31 | * \file SDL_test_font.h 32 | * 33 | * Include file for SDL test framework. 34 | * 35 | * This code is a part of the SDL2_test library, not the main SDL library. 36 | */ 37 | 38 | #ifndef _SDL_test_font_h 39 | #define _SDL_test_font_h 40 | 41 | /* Set up for C function definitions, even when using C++ */ 42 | #ifdef __cplusplus 43 | /* *INDENT-OFF* */ 44 | extern "C" { 45 | /* *INDENT-ON* */ 46 | #endif 47 | 48 | /* Function prototypes */ 49 | 50 | /** 51 | * \brief Draw a string in the currently set font. 52 | * 53 | * \param renderer The renderer to draw on. 54 | * \param x The X coordinate of the upper left corner of the string. 55 | * \param y The Y coordinate of the upper left corner of the string. 56 | * \param s The string to draw. 57 | * 58 | * \returns Returns 0 on success, -1 on failure. 59 | */ 60 | int SDLTest_DrawString(SDL_Renderer * renderer, int x, int y, const char *s); 61 | 62 | 63 | /* Ends C function definitions when using C++ */ 64 | #ifdef __cplusplus 65 | /* *INDENT-OFF* */ 66 | } 67 | /* *INDENT-ON* */ 68 | #endif 69 | 70 | #endif /* _SDL_test_font_h */ 71 | 72 | /* vi: set ts=4 sw=4 expandtab: */ 73 | -------------------------------------------------------------------------------- /src/opengl/SDL_GL_shader.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #ifdef SDL_SHADER_OPENGL 23 | 24 | #ifndef _SDL_GL_shader_h 25 | #define _SDL_GL_shader_h 26 | 27 | /* Set up for C function definitions, even when using C++ */ 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | void SDL_GL_hint(sdl_shader_hint flag, void* value); 33 | SDL_Shader* SDL_GL_createShader( SDL_Renderer* renderer, SDL_ShaderStream* str ); 34 | int SDL_GL_destroyShader( SDL_Shader* shader ); 35 | int SDL_GL_bindShader( SDL_Shader* shader ); 36 | int SDL_GL_unbindShader( SDL_Shader* shader ); 37 | int SDL_GL_renderCopyShd(SDL_Shader* shader, SDL_Texture** textures, 38 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert); 39 | 40 | SDL_Uniform* SDL_GL_createUniform( SDL_Shader* shader, const char* name ); 41 | int SDL_GL_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ); 42 | 43 | int SDL_GL_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ); 44 | int SDL_GL_setUniform_f ( SDL_Uniform* uniform, float a ); 45 | int SDL_GL_setUniform_f2( SDL_Uniform* uniform, float a, float b ); 46 | int SDL_GL_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ); 47 | int SDL_GL_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ); 48 | 49 | int SDL_GL_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ); 50 | int SDL_GL_setUniform_i ( SDL_Uniform* uniform, int a ); 51 | int SDL_GL_setUniform_i2( SDL_Uniform* uniform, int a,int b ); 52 | int SDL_GL_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ); 53 | int SDL_GL_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | #endif /* _SDL_GL_shader_h */ 60 | 61 | #endif /* SDL_SHADER_OPENGL */ 62 | -------------------------------------------------------------------------------- /src/d3d/SDL_D3D_shader.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #ifdef SDL_SHADER_D3D 23 | #ifndef _SDL_D3D_shader_h 24 | #define _SDL_D3D_shader_h 25 | 26 | /* Set up for C function definitions, even when using C++ */ 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | void SDL_D3D_hint(sdl_shader_hint flag, void* value); 32 | SDL_Shader* SDL_D3D_createShader( SDL_Renderer* renderer, 33 | SDL_ShaderStream* shdstream ); 34 | int SDL_D3D_destroyShader( SDL_Shader* shader ); 35 | int SDL_D3D_bindShader( SDL_Shader* shader ); 36 | int SDL_D3D_unbindShader( SDL_Shader* shader ); 37 | int SDL_D3D_renderCopyShd(SDL_Shader* shader, SDL_Texture** textures, 38 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert); 39 | 40 | 41 | SDL_Uniform* SDL_D3D_createUniform( SDL_Shader* shader, const char* name ); 42 | int SDL_D3D_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ); 43 | 44 | int SDL_D3D_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ); 45 | int SDL_D3D_setUniform_f( SDL_Uniform* uniform, float a ); 46 | int SDL_D3D_setUniform_f2( SDL_Uniform* uniform, float a, float b ); 47 | int SDL_D3D_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ); 48 | int SDL_D3D_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ); 49 | 50 | 51 | int SDL_D3D_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ); 52 | int SDL_D3D_setUniform_i ( SDL_Uniform* uniform, int a ); 53 | int SDL_D3D_setUniform_i2( SDL_Uniform* uniform, int a, int b ); 54 | int SDL_D3D_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ); 55 | int SDL_D3D_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* _SDL_D3D_shader_h */ 62 | 63 | #endif /* SDL_SHADER_D3D */ 64 | -------------------------------------------------------------------------------- /src/opengles2/SDL_GLES2_shader.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #ifdef SDL_SHADER_OPENGLES2 23 | 24 | #ifndef _SDL_GLES2_shader_h 25 | #define _SDL_GLES2_shader_h 26 | 27 | /* Set up for C function definitions, even when using C++ */ 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | void SDL_GLES2_hint(sdl_shader_hint flag, void* value); 33 | SDL_Shader* SDL_GLES2_createShader( SDL_Renderer* renderer, 34 | SDL_ShaderStream *shdstream ); 35 | int SDL_GLES2_destroyShader( SDL_Shader* shader ); 36 | int SDL_GLES2_bindShader( SDL_Shader* shader ); 37 | int SDL_GLES2_unbindShader( SDL_Shader* shader ); 38 | int SDL_GLES2_renderCopyShd(SDL_Shader* shader, SDL_Texture** textures, 39 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert); 40 | 41 | SDL_Uniform* SDL_GLES2_createUniform( SDL_Shader* shader, const char* name ); 42 | int SDL_GLES2_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ); 43 | 44 | int SDL_GLES2_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ); 45 | int SDL_GLES2_setUniform_f ( SDL_Uniform* uniform, float a ); 46 | int SDL_GLES2_setUniform_f2( SDL_Uniform* uniform, float a, float b ); 47 | int SDL_GLES2_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ); 48 | int SDL_GLES2_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ); 49 | 50 | int SDL_GLES2_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ); 51 | int SDL_GLES2_setUniform_i ( SDL_Uniform* uniform, int a ); 52 | int SDL_GLES2_setUniform_i2( SDL_Uniform* uniform, int a,int b ); 53 | int SDL_GLES2_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ); 54 | int SDL_GLES2_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* _SDL_GLES2_shader_h */ 61 | 62 | #endif /* SDL_SHADER_OPENGLES2 */ 63 | -------------------------------------------------------------------------------- /test/shaders/scale2x/scale2x.gl.frag: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | #ifdef GL_ARB_texture_rectangle 4 | #extension GL_ARB_texture_rectangle : enable 5 | #define sampler2D sampler2DRect 6 | #define texture2D texture2DRect 7 | #endif 8 | 9 | vec4 convertColor(int type, vec4 color); 10 | uniform int color_mode; 11 | 12 | varying vec4 color_v2f; 13 | varying vec2 texCoord_v2f; 14 | 15 | uniform sampler2D colorMap; 16 | uniform int scaler; 17 | 18 | bool col_compare( vec4 a, vec4 b) { 19 | return a==b; 20 | } 21 | 22 | 23 | // A --\ a' b' 24 | // C P B --/ c' d' 25 | // D 26 | // a=P; b=P; c=P; d=P; 27 | // IF C==A AND C!=D AND A!=B => a'=A 28 | // IF A==B AND A!=C AND B!=D => b'=B 29 | // IF D==C AND D!=B AND C!=A => c'=C 30 | // IF B==D AND B!=A AND D!=C => d'=D 31 | void scale2x( ){ 32 | vec4 A,B,C,D,P; 33 | bool a_eq_b, b_eq_d, d_eq_c, c_eq_a; 34 | bool y_even = int(gl_FragCoord.y) % 2 == 0; 35 | bool x_even = int(gl_FragCoord.x) % 2 == 0; 36 | 37 | /* note: if you know the color-mode you don't have to do any conversion 38 | that would likely speed up the shader */ 39 | P = texture2D(colorMap, texCoord_v2f.st); 40 | P = convertColor(color_mode,P); 41 | 42 | A = texture2D(colorMap, texCoord_v2f.st - vec2(0,1) ); 43 | A = convertColor(color_mode,A); 44 | B = texture2D(colorMap, texCoord_v2f.st + vec2(1,0) ); 45 | B = convertColor(color_mode,B); 46 | C = texture2D(colorMap, texCoord_v2f.st - vec2(1,0) ); 47 | C = convertColor(color_mode,C); 48 | D = texture2D(colorMap, texCoord_v2f.st + vec2(0,1) ); 49 | D = convertColor(color_mode,D); 50 | 51 | a_eq_b = col_compare(A,B); 52 | b_eq_d = col_compare(B,D); 53 | d_eq_c = col_compare(D,C); 54 | c_eq_a = col_compare(C,A); 55 | 56 | if ( x_even && y_even ) { 57 | // a' 58 | if( c_eq_a && !d_eq_c && !a_eq_b ) { 59 | gl_FragColor = A * color_v2f; 60 | return; 61 | } 62 | } else 63 | if ( !x_even && y_even ) { 64 | // b' 65 | if( a_eq_b && !c_eq_a && !b_eq_d ) { 66 | gl_FragColor = B * color_v2f; 67 | return; 68 | } 69 | } else 70 | if ( x_even && !y_even ) { 71 | // c' 72 | if( d_eq_c && !b_eq_d && !c_eq_a ) { 73 | gl_FragColor = C * color_v2f; 74 | return; 75 | } 76 | } else 77 | if ( !x_even && !y_even ) { 78 | // d' 79 | if( b_eq_d && !a_eq_b && !d_eq_c ) { 80 | gl_FragColor = D * color_v2f; 81 | return; 82 | } 83 | } 84 | gl_FragColor = P * color_v2f; 85 | } 86 | 87 | void main(void){ 88 | 89 | if ( scaler == 0 ) { 90 | // no scaler 91 | vec4 P; 92 | P = texture2D(colorMap, texCoord_v2f.st); 93 | P = convertColor(color_mode,P); 94 | gl_FragColor = P * color_v2f; 95 | return; 96 | } else 97 | if ( scaler == 1 ) { 98 | // use scale2x/Mame2x 99 | scale2x(); 100 | return; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /test/shaders/scale2x/scale2x.d3d.hlsl: -------------------------------------------------------------------------------- 1 | 2 | texture xColoredTexture; 3 | float4x4 world_view_projection : WORLDVIEWPROJECTION; 4 | int scaler; 5 | 6 | struct AppToVertex { 7 | float4 pos : POSITION; 8 | float2 texcoord : TEXCOORD0; 9 | float4 color : COLOR0; 10 | }; 11 | 12 | struct VertexToPixel { 13 | float4 pos : POSITION; 14 | float2 texcoord : TEXCOORD0; 15 | float4 color : COLOR0; 16 | }; 17 | 18 | struct PixelToFrame { 19 | float4 color : COLOR0; 20 | }; 21 | 22 | 23 | 24 | sampler colorMap = sampler_state { 25 | texture = ; 26 | magfilter = LINEAR; 27 | minfilter = LINEAR; 28 | mipfilter = LINEAR; 29 | AddressU = mirror; 30 | AddressV = mirror; 31 | }; 32 | 33 | 34 | bool col_compare( float4 a, float4 b) { 35 | return !any(a-b); 36 | } 37 | 38 | // A --\ a' b' 39 | // C P B --/ c' d' 40 | // D 41 | // a=P; b=P; c=P; d=P; 42 | // IF C==A AND C!=D AND A!=B => a'=A 43 | // IF A==B AND A!=C AND B!=D => b'=B 44 | // IF D==C AND D!=B AND C!=A => c'=C 45 | // IF B==D AND B!=A AND D!=C => d'=D 46 | float4 scale2x( VertexToPixel ps_in ){ 47 | float4 A,B,C,D,P; 48 | bool a_eq_b, b_eq_d, d_eq_c, c_eq_a; 49 | bool y_even = int( ps_in.texcoord.y * 600 ) % 2 == 0; 50 | bool x_even = int( ps_in.texcoord.x * 800 ) % 2 == 0; 51 | 52 | P = tex2D(colorMap, ps_in.texcoord ); 53 | 54 | A = tex2D( colorMap, ps_in.texcoord - float2(0,1)/300 ); 55 | B = tex2D( colorMap, ps_in.texcoord + float2(1,0)/400 ); 56 | C = tex2D( colorMap, ps_in.texcoord - float2(1,0)/400 ); 57 | D = tex2D( colorMap, ps_in.texcoord + float2(0,1)/300 ); 58 | 59 | a_eq_b = col_compare(A,B); 60 | b_eq_d = col_compare(B,D); 61 | d_eq_c = col_compare(D,C); 62 | c_eq_a = col_compare(C,A); 63 | 64 | if ( x_even && y_even ) { 65 | // a' 66 | if( c_eq_a && !d_eq_c && !a_eq_b ) { 67 | return A; 68 | } 69 | } else 70 | if ( !x_even && y_even ) { 71 | // b' 72 | if( a_eq_b && !c_eq_a && !b_eq_d ) { 73 | return B; 74 | } 75 | } else 76 | if ( x_even && !y_even ) { 77 | // c' 78 | if( d_eq_c && !b_eq_d && !c_eq_a ) { 79 | return C; 80 | } 81 | } else 82 | if ( !x_even && !y_even ) { 83 | // d' 84 | if( b_eq_d && !a_eq_b && !d_eq_c ) { 85 | return D; 86 | } 87 | } 88 | return P; 89 | } 90 | 91 | VertexToPixel VertexShaderMain( AppToVertex vs_in ) { 92 | VertexToPixel vs_out = (VertexToPixel)0; 93 | vs_out.pos = mul(world_view_projection, vs_in.pos); 94 | vs_out.texcoord = vs_in.texcoord; 95 | vs_out.color = vs_in.color; 96 | 97 | return vs_out; 98 | } 99 | 100 | PixelToFrame PixelShaderMain(VertexToPixel ps_in) { 101 | PixelToFrame ps_out = (PixelToFrame)0; 102 | float4 color; 103 | if ( scaler == 0 ) { 104 | color = tex2D(colorMap, ps_in.texcoord ); 105 | } else 106 | if ( scaler == 1 ) { 107 | color = scale2x( ps_in ); 108 | } 109 | 110 | ps_out.color = color * ps_in.color; 111 | return ps_out; 112 | } 113 | 114 | -------------------------------------------------------------------------------- /test/shaders/normal_map/normal_map.d3d.hlsl: -------------------------------------------------------------------------------- 1 | /* based on https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6 */ 2 | 3 | texture xColoredTexture; 4 | float4x4 world_view_projection : WORLDVIEWPROJECTION; 5 | 6 | float2 Resolution; //resolution of screen 7 | float3 LightPos; //light position, normalized 8 | float4 LightColor; //light RGBA -- alpha is intensity 9 | float4 AmbientColor; //ambient RGBA -- alpha is intensity 10 | float3 Falloff; //attenuation coefficients 11 | 12 | struct AppToVertex { 13 | float4 pos : POSITION; 14 | float2 texcoord : TEXCOORD0; 15 | float4 color : COLOR0; 16 | }; 17 | 18 | struct VertexToPixel { 19 | float4 pos : POSITION; 20 | float2 texcoord : TEXCOORD0; 21 | float4 color : COLOR0; 22 | }; 23 | 24 | struct PixelToFrame { 25 | float4 color : COLOR0; 26 | }; 27 | 28 | 29 | 30 | sampler u_texture = sampler_state { 31 | texture = ; 32 | magfilter = LINEAR; 33 | minfilter = LINEAR; 34 | mipfilter = LINEAR; 35 | AddressU = mirror; 36 | AddressV = mirror; 37 | }; 38 | 39 | sampler u_normals = sampler_state { 40 | texture = ; 41 | magfilter = LINEAR; 42 | minfilter = LINEAR; 43 | mipfilter = LINEAR; 44 | AddressU = mirror; 45 | AddressV = mirror; 46 | }; 47 | 48 | 49 | VertexToPixel VertexShaderMain( AppToVertex vs_in ) { 50 | VertexToPixel vs_out = (VertexToPixel)0; 51 | vs_out.pos = mul(world_view_projection, vs_in.pos); 52 | vs_out.texcoord = vs_in.texcoord; 53 | vs_out.color = vs_in.color; 54 | 55 | return vs_out; 56 | } 57 | 58 | PixelToFrame PixelShaderMain(VertexToPixel ps_in) { 59 | PixelToFrame ps_out = (PixelToFrame)0; 60 | 61 | //RGBA of our diffuse color 62 | float4 DiffuseColor = tex2D(u_texture, ps_in.texcoord ); 63 | 64 | //RGB of our normal map 65 | float3 NormalMap = tex2D(u_normals, ps_in.texcoord).rgb; 66 | 67 | //The delta position of light 68 | ps_in.texcoord.y = 1-ps_in.texcoord.y; 69 | float3 LightDir = float3(LightPos.xy - (ps_in.texcoord ), LightPos.z); 70 | 71 | //Correct for aspect ratio 72 | LightDir.x *= Resolution.x / Resolution.y; 73 | 74 | //Determine distance (used for attenuation) BEFORE we normalize our LightDir 75 | float D = length(LightDir); 76 | 77 | //normalize our vectors 78 | float3 N = normalize(NormalMap * 2.0 - 1.0); 79 | float3 L = normalize(LightDir); 80 | 81 | //Pre-multiply light color with intensity 82 | //Then perform "N dot L" to determine our diffuse term 83 | float3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0); 84 | 85 | //pre-multiply ambient color with intensity 86 | float3 Ambient = AmbientColor.rgb * AmbientColor.a; 87 | 88 | //calculate attenuation 89 | float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) ); 90 | 91 | //the calculation which brings it all together 92 | float3 Intensity = Ambient + Diffuse * Attenuation; 93 | float3 FinalColor = DiffuseColor.rgb * Intensity; 94 | 95 | ps_out.color = float4(FinalColor, DiffuseColor.a) * ps_in.color; 96 | return ps_out; 97 | } 98 | 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | SDL2_shader is an inofficial SDL2 extension library intended to allow the 4 | use of shaders with a number of SDL2 rendering backends. Currently opengl 5 | opengles2 and direct3d are supported and direct3d11 (WinRT) is in 6 | development. 7 | 8 | License 9 | ======= 10 | SDL2_shader is developed under a liberal zlib license. For details see 11 | [this wikipedia article](http://en.wikipedia.org/wiki/Zlib_License) or LICENSE 12 | file. 13 | 14 | Usage 15 | ===== 16 | SDL2_shader is in early alpha stage. For now it shouldn't be used in production 17 | environments if you don't want to get your hands dirty: Testcases and reference 18 | renderings are in the repository and need to be tested on as many plattforms 19 | and by as many users as possible. 20 | 21 | The best option for now is droping the whole codebase in your project, since 22 | there are no official builds. There are a few macros which to choose the 23 | desired backends: 24 | * SDL_SHADER_D3D - enables the direct3d backend 25 | * SDL_SHADER_D3D11 - enables the direct3d backend (not yet) 26 | * SDL_SHADER_OPENGL - enables the opengl backend 27 | * SDL_SHADER_OPENGLES2 - enables the opengles2 backend 28 | 29 | Code sample 30 | ----------- 31 | 32 | Using SDL_Shader is very easy: 33 | ~~~C 34 | SDL_Shader *shader = SDL_createShader( renderer, "path/to/shader" ); 35 | ... 36 | SDL_renderCopyShd( shader, textures[i], NULL, &dst ); 37 | ~~~ 38 | 39 | The above code code compiles and assembles the shaders. Depending on the 40 | concrete backend it looks for the following: 41 | * shader.d3d.hlsl - for the direct3d shaders 42 | * shader.gles2.frag - for the opengles2 fragment shader 43 | * shader.gles2.vert - for the opengles2 vertex shader 44 | * shader.gl.frag - for the opengles2 fragment shader 45 | * shader.gl.vert - for the opengl vertex shader 46 | 47 | This is the bad news: for every backend you want to support, you have to port 48 | your shader to. You leave the nice world of SDL where everything is abstracted 49 | away. There are several solutions to this problem, SDL2_shader is not (yet?) 50 | one of them. 51 | 52 | Writing shaders 53 | -------------- 54 | If you write shaders you will be restricted a little bit. You have to use 55 | predetermined variable names and texture lookups may need color conversion. 56 | Take a look the "do_nothing" shader from the test-directory as a template. 57 | 58 | Contributing 59 | ============ 60 | The easiest way to contribute is by forking the repo on 61 | [github](https://github.com/powertomato/sdl2_shader) make some changes and make 62 | a pull request. If you don't have (or don't want) a github accout you also may 63 | to drop me an email with a patch file or instructions. If you choose the latter 64 | also don't forget to write which (nick)name you want to show up in the 65 | repository. 66 | 67 | Anyway please make sure not to follow these guidelines to make maintaining 68 | a bit easier: 69 | 70 | * Don't do any unnecessary changes (e.g. fix typos in unrelated files) 71 | * Don't change the interface i.e. change the way the library is used 72 | * Keep your code K&R-ish 73 | * Use tabs for identiation (spaces may be used in comments), assume a tab- 74 | length of 4 75 | 76 | Dev. Roadmap 77 | ============ 78 | 79 | If you want to contribute here is the list of TODOs which has to be done. A * 80 | denotes the feature is beeing worked on. 81 | 82 | Beta milestones 83 | --------------- 84 | This is the stuff which needs to be done for a beta release. 85 | * Doxygen-documentation 86 | * Testcase for SDL_uniform* functions 87 | * integrate a testing framework 88 | * testcase automation 89 | * Makefiles for iOS and android 90 | * An other pass of code cleanup 91 | 92 | High priority 93 | ------------- 94 | * direct3d11 backend* 95 | 96 | Lower priority 97 | ------------ 98 | * integrate a build system like cmake or automake 99 | * official builds 100 | * a 2D-lightning shader like [this](https://www.youtube.com/watch?v=Xmn6zhDJGLE) 101 | or [this](http://www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/) 102 | 103 | Nice to have 104 | ------------ 105 | * more backends 106 | * IDEs projects 107 | * Common shader language parser 108 | 109 | -------------------------------------------------------------------------------- /src/opengles2/SDL_gles2funcs.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | 10 | /* 11 | Simple DirectMedia Layer 12 | Copyright (C) 1997-2014 Sam Lantinga 13 | 14 | This software is provided 'as-is', without any express or implied 15 | warranty. In no event will the authors be held liable for any damages 16 | arising from the use of this software. 17 | 18 | Permission is granted to anyone to use this software for any purpose, 19 | including commercial applications, and to alter it and redistribute it 20 | freely, subject to the following restrictions: 21 | 22 | 1. The origin of this software must not be misrepresented; you must not 23 | claim that you wrote the original software. If you use this software 24 | in a product, an acknowledgment in the product documentation would be 25 | appreciated but is not required. 26 | 2. Altered source versions must be plainly marked as such, and must not be 27 | misrepresented as being the original software. 28 | 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | 31 | SDL_PROC(void, glActiveTexture, (GLenum)) 32 | SDL_PROC(void, glAttachShader, (GLuint, GLuint)) 33 | SDL_PROC(void, glBindAttribLocation, (GLuint, GLuint, const char *)) 34 | SDL_PROC(void, glBindTexture, (GLenum, GLuint)) 35 | SDL_PROC(void, glBlendFuncSeparate, (GLenum, GLenum, GLenum, GLenum)) 36 | SDL_PROC(void, glClear, (GLbitfield)) 37 | SDL_PROC(void, glClearColor, (GLclampf, GLclampf, GLclampf, GLclampf)) 38 | SDL_PROC(void, glCompileShader, (GLuint)) 39 | SDL_PROC(GLuint, glCreateProgram, (void)) 40 | SDL_PROC(GLuint, glCreateShader, (GLenum)) 41 | SDL_PROC(void, glDeleteProgram, (GLuint)) 42 | SDL_PROC(void, glDeleteShader, (GLuint)) 43 | SDL_PROC(void, glDeleteTextures, (GLsizei, const GLuint *)) 44 | SDL_PROC(void, glDisable, (GLenum)) 45 | SDL_PROC(void, glDisableVertexAttribArray, (GLuint)) 46 | SDL_PROC(void, glDrawArrays, (GLenum, GLint, GLsizei)) 47 | SDL_PROC(void, glEnable, (GLenum)) 48 | SDL_PROC(void, glEnableVertexAttribArray, (GLuint)) 49 | SDL_PROC(void, glFinish, (void)) 50 | SDL_PROC(void, glGenFramebuffers, (GLsizei, GLuint *)) 51 | SDL_PROC(void, glGenTextures, (GLsizei, GLuint *)) 52 | SDL_PROC(void, glGetBooleanv, (GLenum, GLboolean *)) 53 | SDL_PROC(const GLubyte *, glGetString, (GLenum)) 54 | SDL_PROC(GLenum, glGetError, (void)) 55 | SDL_PROC(void, glGetIntegerv, (GLenum, GLint *)) 56 | SDL_PROC(void, glGetProgramiv, (GLuint, GLenum, GLint *)) 57 | SDL_PROC(void, glGetShaderInfoLog, (GLuint, GLsizei, GLsizei *, char *)) 58 | SDL_PROC(void, glGetShaderiv, (GLuint, GLenum, GLint *)) 59 | SDL_PROC(GLint, glGetUniformLocation, (GLuint, const char *)) 60 | SDL_PROC(void, glLinkProgram, (GLuint)) 61 | SDL_PROC(void, glPixelStorei, (GLenum, GLint)) 62 | SDL_PROC(void, glReadPixels, (GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, GLvoid*)) 63 | SDL_PROC(void, glScissor, (GLint, GLint, GLsizei, GLsizei)) 64 | SDL_PROC(void, glShaderBinary, (GLsizei, const GLuint *, GLenum, const void *, GLsizei)) 65 | SDL_PROC(void, glShaderSource, (GLuint, GLsizei, const char **, const GLint *)) 66 | SDL_PROC(void, glTexImage2D, (GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const void *)) 67 | SDL_PROC(void, glTexParameteri, (GLenum, GLenum, GLint)) 68 | SDL_PROC(void, glTexSubImage2D, (GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const GLvoid *)) 69 | SDL_PROC(void, glUniform1i, (GLint, GLint)) 70 | SDL_PROC(void, glUniform4f, (GLint, GLfloat, GLfloat, GLfloat, GLfloat)) 71 | SDL_PROC(void, glUniformMatrix4fv, (GLint, GLsizei, GLboolean, const GLfloat *)) 72 | SDL_PROC(void, glUseProgram, (GLuint)) 73 | SDL_PROC(void, glVertexAttribPointer, (GLuint, GLint, GLenum, GLboolean, GLsizei, const void *)) 74 | SDL_PROC(void, glViewport, (GLint, GLint, GLsizei, GLsizei)) 75 | SDL_PROC(void, glBindFramebuffer, (GLenum, GLuint)) 76 | SDL_PROC(void, glFramebufferTexture2D, (GLenum, GLenum, GLenum, GLuint, GLint)) 77 | SDL_PROC(GLenum, glCheckFramebufferStatus, (GLenum)) 78 | SDL_PROC(void, glDeleteFramebuffers, (GLsizei, const GLuint *)) 79 | 80 | #if SDL_VERSION_ATLEAST(2,0,2) 81 | SDL_PROC(GLint, glGetAttribLocation, (GLuint, const GLchar *)) 82 | #endif 83 | 84 | -------------------------------------------------------------------------------- /test/all_the_shaders/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #ifdef __MACOSX__ 6 | #include 7 | #else 8 | #include 9 | #endif 10 | #include "../src/test/SDL_test_font.h" 11 | #include "../src/SDL_shader.h" 12 | #include "../src/SDL_SYS_RenderStructs.h" 13 | 14 | 15 | static char *shader_names[] = { 16 | "do_nothing", 17 | "greyscale", 18 | "sepia", 19 | }; 20 | static SDL_Shader** shaders; 21 | static int current_shader; 22 | #define NUM_OF_SHADERS (sizeof(shader_names)/sizeof(char*)) 23 | 24 | int mod(int a, int b) 25 | { 26 | int r = a % b; 27 | return r < 0 ? r + b : r; 28 | } 29 | 30 | #ifdef _WIN32 31 | //prevents displaying the black console window 32 | //on windows systems 33 | //depending on the toolchain (settings) this is also required 34 | #include 35 | int WINAPI WinMain (HINSTANCE hThisInstance, 36 | HINSTANCE hPrevInstance, 37 | LPSTR lpszArgument, 38 | int nCmdShow) 39 | #else 40 | int main(int argc, char** argv) 41 | #endif 42 | { 43 | #ifdef _WIN32 44 | LPSTR *argv = __argv; 45 | int argc = __argc; 46 | (void) argv; 47 | (void) argc; 48 | #endif 49 | SDL_Window *screen; 50 | SDL_Renderer *renderer; 51 | 52 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ 53 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 54 | return 1; 55 | } 56 | 57 | SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); 58 | 59 | int width = 500; 60 | int height = 400; 61 | screen = SDL_CreateWindow("Caption", 62 | SDL_WINDOWPOS_CENTERED, 63 | SDL_WINDOWPOS_CENTERED, 64 | width, height, 65 | SDL_WINDOW_RESIZABLE); 66 | // SDL_WINDOW_FULLSCREEN_DESKTOP ); 67 | if ( screen == NULL ) { 68 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 69 | return 2; 70 | } 71 | renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); 72 | //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); 73 | if ( renderer == NULL ) { 74 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 75 | return 3; 76 | } 77 | 78 | SDL_RenderSetLogicalSize(renderer, width, height); 79 | 80 | SDL_SetWindowTitle( screen, renderer->info.name ); 81 | 82 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 83 | SDL_RenderClear(renderer); 84 | 85 | shaders = (SDL_Shader**) malloc( sizeof(SDL_Shader*)*NUM_OF_SHADERS ); 86 | int i; 87 | for ( i=0; iw, tex->h }; 121 | ret = SDL_renderCopyShd( shaders[current_shader], tex, NULL, &dst ); 122 | if ( ret!=0 ){ 123 | fprintf(stderr,"Err: %s\n", SDL_GetError()); 124 | } 125 | 126 | while (SDL_PollEvent(&e)){ 127 | switch ( e.type ) { 128 | case SDL_QUIT: 129 | quit = 1; 130 | break; 131 | case SDL_KEYDOWN: 132 | switch ( e.key.keysym.sym ) { 133 | case SDLK_SPACE: 134 | current_shader= mod( (current_shader-1), (NUM_OF_SHADERS)); 135 | break; 136 | case SDLK_TAB: 137 | current_shader= mod( (current_shader+1), (NUM_OF_SHADERS)); 138 | break; 139 | } 140 | break; 141 | case SDL_WINDOWEVENT: 142 | for( int i=0; i 3 | #include 4 | #include 5 | #ifdef __MACOSX__ 6 | #include 7 | #else 8 | #include 9 | #endif 10 | #include "../src/test/SDL_test_font.h" 11 | #include "../src/SDL_shader.h" 12 | #include "../src/SDL_SYS_RenderStructs.h" 13 | #include 14 | #ifndef M_PI 15 | #define M_PI 3.14159265358979323846 16 | #endif /*M_PI*/ 17 | 18 | 19 | /*static char *shader_names[] = { 20 | "do_nothing", 21 | "greyscale", 22 | "sepia", 23 | }; 24 | static SDL_Shader** shaders; 25 | static int current_shader; 26 | #define NUM_OF_SHADERS (sizeof(shader_names)/sizeof(char*))*/ 27 | 28 | int mod(int a, int b) 29 | { 30 | int r = a % b; 31 | return r < 0 ? r + b : r; 32 | } 33 | 34 | #ifdef _WIN32 35 | //prevents displaying the black console window 36 | //on windows systems 37 | //depending on the toolchain (settings) this is also required 38 | #include 39 | int WINAPI WinMain (HINSTANCE hThisInstance, 40 | HINSTANCE hPrevInstance, 41 | LPSTR lpszArgument, 42 | int nCmdShow) 43 | #else 44 | int main(int argc, char** argv) 45 | #endif 46 | { 47 | #ifdef _WIN32 48 | LPSTR *argv = __argv; 49 | int argc = __argc; 50 | (void) argv; 51 | (void) argc; 52 | #endif 53 | SDL_Window *screen; 54 | SDL_Renderer *renderer; 55 | 56 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ 57 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 58 | return 1; 59 | } 60 | 61 | SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); 62 | 63 | int width = 500; 64 | int height = 400; 65 | screen = SDL_CreateWindow("Caption", 66 | SDL_WINDOWPOS_CENTERED, 67 | SDL_WINDOWPOS_CENTERED, 68 | width, height, 69 | SDL_WINDOW_RESIZABLE); 70 | // SDL_WINDOW_FULLSCREEN_DESKTOP ); 71 | if ( screen == NULL ) { 72 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 73 | return 2; 74 | } 75 | renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); 76 | //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); 77 | if ( renderer == NULL ) { 78 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 79 | return 3; 80 | } 81 | 82 | SDL_RenderSetLogicalSize(renderer, width, height); 83 | 84 | SDL_SetWindowTitle( screen, renderer->info.name ); 85 | 86 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 87 | SDL_RenderClear(renderer); 88 | 89 | // a5 90 | // ,-'\, 91 | // a4 ,-'' \, 92 | // | \, 93 | // | +- - - :-a3 94 | // | /' 95 | // a2'-. /' 96 | // `'--./' 97 | // a1 98 | 99 | SDL_shader_hint(SDL_GL_TEX0_NAME,"color_map" ); 100 | SDL_shader_hint(SDL_GL_TEX1_NAME,"color_map2" ); 101 | SDL_Shader* shader = SDL_createShader( renderer, "../shaders/multitex/multitex" ); 102 | SDL_Vertex *vertices = shader->createVertexBuffer( 5 ); 103 | float angles[] = { 2*M_PI/5*4, 2*M_PI/5*3, 0, 2*M_PI/5*2, 2*M_PI/5*1 }; 104 | for ( int i=0; i<5; i++ ){ 105 | vertices->setVertexColor( vertices,i,1,255,255,255,255 ); 106 | vertices->setVertexPosition( vertices,i,1, 107 | 150*cos(angles[i])+250, 150*sin(angles[i])+200,0.0f ); 108 | vertices->setVertexTexCoord( vertices,i,1, 109 | (cos(angles[i])/2+0.5), (sin(angles[i])/2+0.5) ); 110 | } 111 | 112 | SDL_Texture* tex[2]; 113 | SDL_Surface* srf; 114 | char* names[] = {"../img.png", "../img2.png"}; 115 | for( int i=0; i<2; i++ ){ 116 | srf = IMG_Load( names[i] ); 117 | 118 | SDL_PixelFormat* fmt = SDL_AllocFormat( SDL_PIXELFORMAT_ARGB8888 ); 119 | SDL_assert(fmt != NULL); 120 | SDL_Surface* srf2 = SDL_ConvertSurface(srf, fmt, 0); 121 | SDL_assert(srf2 != NULL); 122 | 123 | if ( !srf ) { 124 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 125 | return 5; 126 | } 127 | tex[i] = SDL_CreateTextureFromSurface(renderer, srf2 ); 128 | 129 | SDL_FreeSurface(srf); 130 | SDL_FreeSurface( srf2 ); 131 | } 132 | 133 | SDL_SetRenderTarget( renderer, NULL ); 134 | SDL_Event e; 135 | SDL_SetRenderDrawColor(renderer, 0,0,0,1); 136 | int ret = 0; 137 | int quit = 0; 138 | while ( !quit ) { 139 | //SDLTest_DrawString( renderer, 8, 8, shader_names[current_shader] ); 140 | ret = SDL_renderVertexBuffer( shader, vertices, tex, 2 ); 141 | if ( ret!=0 ){ 142 | fprintf(stderr,"Err: %s\n", SDL_GetError()); 143 | } 144 | 145 | while (SDL_PollEvent(&e)){ 146 | switch ( e.type ) { 147 | case SDL_QUIT: 148 | quit = 1; 149 | break; 150 | case SDL_KEYDOWN: 151 | switch ( e.key.keysym.sym ) { 152 | case SDLK_SPACE: 153 | break; 154 | case SDLK_TAB: 155 | break; 156 | } 157 | break; 158 | //case SDL_WINDOWEVENT: 159 | // SDL_updateViewport( shader ); 160 | } 161 | } 162 | 163 | SDL_RenderPresent(renderer); 164 | 165 | SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); 166 | SDL_RenderClear(renderer); 167 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 168 | 169 | } 170 | SDL_destroyShader( shader ); 171 | SDL_DestroyTexture( tex[0] ); 172 | SDL_DestroyTexture( tex[1] ); 173 | SDL_DestroyRenderer( renderer ); 174 | SDL_DestroyWindow( screen ); 175 | SDL_Quit(); 176 | return 0; 177 | } 178 | -------------------------------------------------------------------------------- /src/opengles2/SDL_GLES2_RenderStructs.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | 10 | /* 11 | Simple DirectMedia Layer 12 | Copyright (C) 1997-2014 Sam Lantinga 13 | 14 | This software is provided 'as-is', without any express or implied 15 | warranty. In no event will the authors be held liable for any damages 16 | arising from the use of this software. 17 | 18 | Permission is granted to anyone to use this software for any purpose, 19 | including commercial applications, and to alter it and redistribute it 20 | freely, subject to the following restrictions: 21 | 22 | 1. The origin of this software must not be misrepresented; you must not 23 | claim that you wrote the original software. If you use this software 24 | in a product, an acknowledgment in the product documentation would be 25 | appreciated but is not required. 26 | 2. Altered source versions must be plainly marked as such, and must not be 27 | misrepresented as being the original software. 28 | 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | 31 | #include "../SDL_SYS_RenderStructs.h" 32 | 33 | typedef struct GLES2_ShaderInstance 34 | { 35 | GLenum type; 36 | GLenum format; 37 | int length; 38 | const void *data; 39 | } GLES2_ShaderInstance; 40 | 41 | typedef enum 42 | { 43 | GLES2_ATTRIBUTE_POSITION = 0, 44 | GLES2_ATTRIBUTE_TEXCOORD = 1, 45 | GLES2_ATTRIBUTE_ANGLE = 2, 46 | GLES2_ATTRIBUTE_CENTER = 3, 47 | GLES2_ATTRIBUTE_COLOR = 4, 48 | } GLES2_Shader_Attribute; 49 | 50 | typedef enum 51 | { 52 | GLES2_SHADER_VERTEX_DEFAULT, 53 | GLES2_SHADER_FRAGMENT_SOLID_SRC, 54 | GLES2_SHADER_FRAGMENT_TEXTURE_ABGR_SRC, 55 | GLES2_SHADER_FRAGMENT_TEXTURE_ARGB_SRC, 56 | GLES2_SHADER_FRAGMENT_TEXTURE_BGR_SRC, 57 | GLES2_SHADER_FRAGMENT_TEXTURE_RGB_SRC 58 | } GLES2_ShaderType; 59 | 60 | typedef struct GLES2_FBOList GLES2_FBOList; 61 | struct GLES2_FBOList 62 | { 63 | Uint32 w, h; 64 | GLuint FBO; 65 | GLES2_FBOList *next; 66 | }; 67 | 68 | typedef struct GLES2_TextureData 69 | { 70 | GLenum texture; 71 | GLenum texture_type; 72 | GLenum pixel_format; 73 | GLenum pixel_type; 74 | void *pixel_data; 75 | size_t pitch; 76 | GLES2_FBOList *fbo; 77 | } GLES2_TextureData; 78 | 79 | typedef struct GLES2_ShaderCacheEntry 80 | { 81 | GLuint id; 82 | GLES2_ShaderType type; 83 | const GLES2_ShaderInstance *instance; 84 | int references; 85 | Uint8 modulation_r, modulation_g, modulation_b, modulation_a; 86 | struct GLES2_ShaderCacheEntry *prev; 87 | struct GLES2_ShaderCacheEntry *next; 88 | } GLES2_ShaderCacheEntry; 89 | 90 | typedef struct GLES2_ShaderCache 91 | { 92 | int count; 93 | GLES2_ShaderCacheEntry *head; 94 | } GLES2_ShaderCache; 95 | 96 | typedef struct GLES2_ProgramCacheEntry 97 | { 98 | GLuint id; 99 | SDL_BlendMode blend_mode; 100 | GLES2_ShaderCacheEntry *vertex_shader; 101 | GLES2_ShaderCacheEntry *fragment_shader; 102 | GLuint uniform_locations[16]; 103 | Uint8 color_r, color_g, color_b, color_a; 104 | Uint8 modulation_r, modulation_g, modulation_b, modulation_a; 105 | GLfloat projection[4][4]; 106 | struct GLES2_ProgramCacheEntry *prev; 107 | struct GLES2_ProgramCacheEntry *next; 108 | } GLES2_ProgramCacheEntry; 109 | 110 | typedef struct GLES2_ProgramCache 111 | { 112 | int count; 113 | GLES2_ProgramCacheEntry *head; 114 | GLES2_ProgramCacheEntry *tail; 115 | } GLES2_ProgramCache; 116 | 117 | 118 | typedef struct GLES2_DriverContext 119 | { 120 | SDL_GLContext *context; 121 | 122 | SDL_bool debug_enabled; 123 | 124 | struct { 125 | int blendMode; 126 | SDL_bool tex_coords; 127 | } current; 128 | 129 | #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params; 130 | #include "SDL_gles2funcs.h" 131 | #undef SDL_PROC 132 | GLES2_FBOList *framebuffers; 133 | GLuint window_framebuffer; 134 | 135 | int shader_format_count; 136 | GLenum *shader_formats; 137 | GLES2_ShaderCache shader_cache; 138 | GLES2_ProgramCache program_cache; 139 | GLES2_ProgramCacheEntry *current_program; 140 | Uint8 clear_r, clear_g, clear_b, clear_a; 141 | } GLES2_DriverContext; 142 | 143 | #if (GL_TEXTURE0+1)!=(GL_TEXTURE1) 144 | #error "GL_TEXTUREx sequence static assertion" 145 | #endif 146 | #if (GL_TEXTURE0+2)!=(GL_TEXTURE2) 147 | #error "GL_TEXTUREx sequence static assertion" 148 | #endif 149 | #if (GL_TEXTURE0+3)!=(GL_TEXTURE3) 150 | #error "GL_TEXTUREx sequence static assertion" 151 | #endif 152 | #if (GL_TEXTURE0+4)!=(GL_TEXTURE4) 153 | #error "GL_TEXTUREx sequence static assertion" 154 | #endif 155 | #if (GL_TEXTURE0+5)!=(GL_TEXTURE5) 156 | #error "GL_TEXTUREx sequence static assertion" 157 | #endif 158 | #if (GL_TEXTURE0+6)!=(GL_TEXTURE6) 159 | #error "GL_TEXTUREx sequence static assertion" 160 | #endif 161 | #if (GL_TEXTURE0+7)!=(GL_TEXTURE7) 162 | #error "GL_TEXTUREx sequence static assertion" 163 | #endif 164 | -------------------------------------------------------------------------------- /src/d3d/SDL_D3D_SDL_internals.c: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | 10 | /* 11 | Simple DirectMedia Layer 12 | Copyright (C) 1997-2014 Sam Lantinga 13 | 14 | This software is provided 'as-is', without any express or implied 15 | warranty. In no event will the authors be held liable for any damages 16 | arising from the use of this software. 17 | 18 | Permission is granted to anyone to use this software for any purpose, 19 | including commercial applications, and to alter it and redistribute it 20 | freely, subject to the following restrictions: 21 | 22 | 1. The origin of this software must not be misrepresented; you must not 23 | claim that you wrote the original software. If you use this software 24 | in a product, an acknowledgment in the product documentation would be 25 | appreciated but is not required. 26 | 2. Altered source versions must be plainly marked as such, and must not be 27 | misrepresented as being the original software. 28 | 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | 31 | static D3DFORMAT PixelFormatToD3DFMT(Uint32 format) { 32 | switch (format) { 33 | case SDL_PIXELFORMAT_RGB565: 34 | return D3DFMT_R5G6B5; 35 | case SDL_PIXELFORMAT_RGB888: 36 | return D3DFMT_X8R8G8B8; 37 | case SDL_PIXELFORMAT_ARGB8888: 38 | return D3DFMT_A8R8G8B8; 39 | case SDL_PIXELFORMAT_YV12: 40 | case SDL_PIXELFORMAT_IYUV: 41 | return D3DFMT_L8; 42 | default: 43 | return D3DFMT_UNKNOWN; 44 | } 45 | } 46 | 47 | static int D3D_ActivateRenderer(SDL_Renderer * renderer) 48 | { 49 | D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata; 50 | HRESULT result; 51 | 52 | if (data->updateSize) { 53 | SDL_Window *window = renderer->window; 54 | int w, h; 55 | 56 | SDL_GetWindowSize(window, &w, &h); 57 | data->pparams.BackBufferWidth = w; 58 | data->pparams.BackBufferHeight = h; 59 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) { 60 | data->pparams.BackBufferFormat = 61 | PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window)); 62 | } else { 63 | data->pparams.BackBufferFormat = D3DFMT_UNKNOWN; 64 | } 65 | /* XXX if (D3D_Reset(renderer) < 0) { 66 | return -1; 67 | }*/ 68 | 69 | data->updateSize = SDL_FALSE; 70 | } 71 | if (data->beginScene) { 72 | result = IDirect3DDevice9_BeginScene(data->device); 73 | if (result == D3DERR_DEVICELOST) { 74 | /* XXX if (D3D_Reset(renderer) < 0) { 75 | return -1./include/GL/glext.h; 76 | }*/ 77 | result = IDirect3DDevice9_BeginScene(data->device); 78 | } 79 | if (FAILED(result)) { 80 | return SDL_SetError("SDL_Shader: D3D BeginScene() failed, errno %d\n", result); 81 | } 82 | data->beginScene = SDL_FALSE; 83 | } 84 | return 0; 85 | } 86 | 87 | 88 | static void D3D_UpdateTextureScaleMode(D3D_RenderData *data, D3D_TextureData *texturedata, unsigned index) 89 | { 90 | if (texturedata->scaleMode != data->scaleMode[index]) { 91 | IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MINFILTER, 92 | texturedata->scaleMode); 93 | IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MAGFILTER, 94 | texturedata->scaleMode); 95 | data->scaleMode[index] = texturedata->scaleMode; 96 | } 97 | } 98 | 99 | static void 100 | D3D_SetBlendMode(D3D_RenderData * data, int blendMode) 101 | { 102 | switch (blendMode) { 103 | case SDL_BLENDMODE_NONE: 104 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, 105 | FALSE); 106 | break; 107 | case SDL_BLENDMODE_BLEND: 108 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, 109 | TRUE); 110 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND, 111 | D3DBLEND_SRCALPHA); 112 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND, 113 | D3DBLEND_INVSRCALPHA); 114 | if (data->enableSeparateAlphaBlend) { 115 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA, 116 | D3DBLEND_ONE); 117 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA, 118 | D3DBLEND_INVSRCALPHA); 119 | } 120 | break; 121 | case SDL_BLENDMODE_ADD: 122 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, 123 | TRUE); 124 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND, 125 | D3DBLEND_SRCALPHA); 126 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND, 127 | D3DBLEND_ONE); 128 | if (data->enableSeparateAlphaBlend) { 129 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA, 130 | D3DBLEND_ZERO); 131 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA, 132 | D3DBLEND_ONE); 133 | } 134 | break; 135 | case SDL_BLENDMODE_MOD: 136 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE, 137 | TRUE); 138 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND, 139 | D3DBLEND_ZERO); 140 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND, 141 | D3DBLEND_SRCCOLOR); 142 | if (data->enableSeparateAlphaBlend) { 143 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA, 144 | D3DBLEND_ZERO); 145 | IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA, 146 | D3DBLEND_ONE); 147 | } 148 | break; 149 | } 150 | } 151 | 152 | -------------------------------------------------------------------------------- /src/opengl/SDL_GL_RenderStructs.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | 10 | /* 11 | Simple DirectMedia Layer 12 | Copyright (C) 1997-2014 Sam Lantinga 13 | 14 | This software is provided 'as-is', without any express or implied 15 | warranty. In no event will the authors be held liable for any damages 16 | arising from the use of this software. 17 | 18 | Permission is granted to anyone to use this software for any purpose, 19 | including commercial applications, and to alter it and redistribute it 20 | freely, subject to the following restrictions: 21 | 22 | 1. The origin of this software must not be misrepresented; you must not 23 | claim that you wrote the original software. If you use this software 24 | in a product, an acknowledgment in the product documentation would be 25 | appreciated but is not required. 26 | 2. Altered source versions must be plainly marked as such, and must not be 27 | misrepresented as being the original software. 28 | 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | #ifndef _SDL_GL_RenderStructs_h 31 | #define _SDL_GL_RenderStructs_h 32 | 33 | #include "../SDL_SYS_RenderStructs.h" 34 | #include 35 | 36 | typedef enum { 37 | SHADER_NONE, 38 | SHADER_SOLID, 39 | SHADER_RGB, 40 | SHADER_YV12, 41 | NUM_SHADERS 42 | } GL_Shader; 43 | 44 | 45 | typedef enum 46 | { 47 | GL_ATTRIBUTE_POSITION = 0, 48 | GL_ATTRIBUTE_TEXCOORD = 1, 49 | GL_ATTRIBUTE_COLOR = 2 50 | } GL_Shader_Attribute; 51 | 52 | typedef struct 53 | { 54 | GLhandleARB program; 55 | GLhandleARB vert_shader; 56 | GLhandleARB frag_shader; 57 | } GL_ShaderData; 58 | 59 | typedef struct GL_ShaderContext GL_ShaderContext; 60 | struct GL_ShaderContext 61 | { 62 | GLenum (*glGetError)(void); 63 | 64 | PFNGLATTACHOBJECTARBPROC glAttachObjectARB; 65 | PFNGLCOMPILESHADERARBPROC glCompileShaderARB; 66 | PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB; 67 | PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB; 68 | PFNGLDELETEOBJECTARBPROC glDeleteObjectARB; 69 | PFNGLGETINFOLOGARBPROC glGetInfoLogARB; 70 | PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB; 71 | PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB; 72 | PFNGLLINKPROGRAMARBPROC glLinkProgramARB; 73 | PFNGLSHADERSOURCEARBPROC glShaderSourceARB; 74 | PFNGLUNIFORM1IARBPROC glUniform1iARB; 75 | PFNGLUNIFORM1FARBPROC glUniform1fARB; 76 | PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB; 77 | 78 | SDL_bool GL_ARB_texture_rectangle_supported; 79 | 80 | GL_ShaderData shaders[NUM_SHADERS]; 81 | }; 82 | 83 | typedef struct GL_FBOList GL_FBOList; 84 | struct GL_FBOList 85 | { 86 | Uint32 w, h; 87 | GLuint FBO; 88 | GL_FBOList *next; 89 | }; 90 | 91 | typedef struct 92 | { 93 | SDL_GLContext context; 94 | 95 | SDL_bool debug_enabled; 96 | SDL_bool GL_ARB_debug_output_supported; 97 | int errors; 98 | char **error_messages; 99 | GLDEBUGPROCARB next_error_callback; 100 | GLvoid *next_error_userparam; 101 | 102 | SDL_bool GL_ARB_texture_rectangle_supported; 103 | struct { 104 | GL_Shader shader; 105 | Uint32 color; 106 | int blendMode; 107 | } current; 108 | 109 | SDL_bool GL_EXT_framebuffer_object_supported; 110 | GL_FBOList *framebuffers; 111 | 112 | /* OpenGL functions */ 113 | #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params; 114 | #include "SDL_glfuncs.h" 115 | #undef SDL_PROC 116 | 117 | /* Multitexture support */ 118 | SDL_bool GL_ARB_multitexture_supported; 119 | PFNGLACTIVETEXTUREARBPROC glActiveTextureARB; 120 | GLint num_texture_units; 121 | 122 | PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT; 123 | PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT; 124 | PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT; 125 | PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT; 126 | PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT; 127 | 128 | /* Shader support */ 129 | GL_ShaderContext *shaders; 130 | 131 | } GL_RenderData; 132 | 133 | typedef struct 134 | { 135 | GLuint texture; 136 | GLenum type; 137 | GLfloat texw; 138 | GLfloat texh; 139 | GLenum format; 140 | GLenum formattype; 141 | void *pixels; 142 | int pitch; 143 | SDL_Rect locked_rect; 144 | 145 | /* YV12 texture support */ 146 | SDL_bool yuv; 147 | GLuint utexture; 148 | GLuint vtexture; 149 | 150 | GL_FBOList *fbo; 151 | } GL_TextureData; 152 | #if (GL_TEXTURE0_ARB+1)!=(GL_TEXTURE1_ARB) 153 | #error "GL_TEXTUREx_ARB sequence static assertion" 154 | #endif 155 | #if (GL_TEXTURE0_ARB+2)!=(GL_TEXTURE2_ARB) 156 | #error "GL_TEXTUREx_ARB sequence static assertion" 157 | #endif 158 | #if (GL_TEXTURE0_ARB+3)!=(GL_TEXTURE3_ARB) 159 | #error "GL_TEXTUREx_ARB sequence static assertion" 160 | #endif 161 | #if (GL_TEXTURE0_ARB+4)!=(GL_TEXTURE4_ARB) 162 | #error "GL_TEXTUREx_ARB sequence static assertion" 163 | #endif 164 | #if (GL_TEXTURE0_ARB+5)!=(GL_TEXTURE5_ARB) 165 | #error "GL_TEXTUREx_ARB sequence static assertion" 166 | #endif 167 | #if (GL_TEXTURE0_ARB+6)!=(GL_TEXTURE6_ARB) 168 | #error "GL_TEXTUREx_ARB sequence static assertion" 169 | #endif 170 | #if (GL_TEXTURE0_ARB+7)!=(GL_TEXTURE7_ARB) 171 | #error "GL_TEXTUREx_ARB sequence static assertion" 172 | #endif 173 | #endif /*_SDL_GL_RenderStructs_h */ 174 | 175 | -------------------------------------------------------------------------------- /test/normal_map/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #ifdef __MACOSX__ 6 | #include 7 | #else 8 | #include 9 | #endif 10 | #include "../src/test/SDL_test_font.h" 11 | #include "../src/SDL_shader.h" 12 | #include "../src/SDL_SYS_RenderStructs.h" 13 | #include 14 | #ifndef M_PI 15 | #define M_PI 3.14159265358979323846 16 | #endif /*M_PI*/ 17 | 18 | int mod(int a, int b) 19 | { 20 | int r = a % b; 21 | return r < 0 ? r + b : r; 22 | } 23 | 24 | #ifdef _WIN32 25 | //prevents displaying the black console window 26 | //on windows systems 27 | //depending on the toolchain (settings) this is also required 28 | #include 29 | int WINAPI WinMain (HINSTANCE hThisInstance, 30 | HINSTANCE hPrevInstance, 31 | LPSTR lpszArgument, 32 | int nCmdShow) 33 | #else 34 | int main(int argc, char** argv) 35 | #endif 36 | { 37 | #ifdef _WIN32 38 | LPSTR *argv = __argv; 39 | int argc = __argc; 40 | (void) argv; 41 | (void) argc; 42 | #endif 43 | SDL_Window *screen; 44 | SDL_Renderer *renderer; 45 | 46 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ 47 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 48 | return 1; 49 | } 50 | 51 | SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); 52 | 53 | int width = 800; 54 | int height = 600; 55 | screen = SDL_CreateWindow("Caption", 56 | SDL_WINDOWPOS_CENTERED, 57 | SDL_WINDOWPOS_CENTERED, 58 | width, height, 59 | SDL_WINDOW_RESIZABLE); 60 | // SDL_WINDOW_FULLSCREEN_DESKTOP ); 61 | if ( screen == NULL ) { 62 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 63 | return 2; 64 | } 65 | renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); 66 | //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); 67 | if ( renderer == NULL ) { 68 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 69 | return 3; 70 | } 71 | 72 | SDL_RenderSetLogicalSize(renderer, width, height); 73 | 74 | SDL_SetWindowTitle( screen, renderer->info.name ); 75 | 76 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 77 | SDL_RenderClear(renderer); 78 | 79 | SDL_shader_hint(SDL_GL_TEX0_NAME,"u_texture" ); 80 | SDL_shader_hint(SDL_GL_TEX1_NAME,"u_normals" ); 81 | SDL_Shader* shader = SDL_createShader( renderer, "../shaders/normal_map/normal_map" ); 82 | 83 | if ( shader == NULL ) { 84 | fprintf( stderr, "Error: %s \n", SDL_GetError() ); 85 | } 86 | SDL_assert(shader != NULL); 87 | 88 | SDL_Uniform* resolution = SDL_createUniform( shader, "Resolution" ); 89 | SDL_Uniform* lightPos = SDL_createUniform( shader, "LightPos" ); 90 | SDL_Uniform* lightColor = SDL_createUniform( shader, "LightColor" ); 91 | SDL_Uniform* ambientColor = SDL_createUniform( shader, "AmbientColor" ); 92 | SDL_Uniform* falloff = SDL_createUniform( shader, "Falloff" ); 93 | 94 | SDL_assert(resolution != NULL); 95 | SDL_assert(lightPos != NULL); 96 | SDL_assert(lightColor != NULL); 97 | SDL_assert(ambientColor != NULL); 98 | SDL_assert(falloff != NULL); 99 | 100 | SDL_setUniform_f2( resolution, 800, 600 ); 101 | SDL_setUniform_f3( lightPos, 0.5,0.5,0.075f ); 102 | SDL_setUniform_f4( lightColor, 1, 0.7f, 0.7f, 7 ); 103 | SDL_setUniform_f4( ambientColor, 1, 1, 1, 0.3f ); 104 | SDL_setUniform_f3( falloff, 0.4f, 3, 20 ); 105 | 106 | SDL_Texture* tex[2]; 107 | SDL_Surface* srf; 108 | char* names[] = {"../stone.png", "../stone_n.png"}; 109 | for( int i=0; i<2; i++ ){ 110 | srf = IMG_Load( names[i] ); 111 | 112 | SDL_PixelFormat* fmt = SDL_AllocFormat( SDL_PIXELFORMAT_ARGB8888 ); 113 | SDL_assert(fmt != NULL); 114 | SDL_Surface* srf2 = SDL_ConvertSurface(srf, fmt, 0); 115 | SDL_assert(srf2 != NULL); 116 | 117 | if ( !srf ) { 118 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 119 | return 5; 120 | } 121 | tex[i] = SDL_CreateTextureFromSurface(renderer, srf2 ); 122 | 123 | SDL_FreeSurface(srf); 124 | SDL_FreeSurface(srf2); 125 | } 126 | 127 | SDL_SetRenderTarget( renderer, NULL ); 128 | SDL_Event e; 129 | SDL_SetRenderDrawColor(renderer, 0,0,0,1); 130 | int ret = 0; 131 | int quit = 0; 132 | while ( !quit ) { 133 | //SDLTest_DrawString( renderer, 8, 8, shader_names[current_shader] ); 134 | SDL_Rect dst = {0,0,800,600}; 135 | ret = SDL_renderCopyShdArray( shader, tex,NULL,&dst,2 ); 136 | if ( ret!=0 ){ 137 | fprintf(stderr,"Err: %s\n", SDL_GetError()); 138 | } 139 | 140 | while (SDL_PollEvent(&e)){ 141 | switch ( e.type ) { 142 | case SDL_QUIT: 143 | quit = 1; 144 | break; 145 | case SDL_KEYDOWN: 146 | switch ( e.key.keysym.sym ) { 147 | case SDLK_SPACE: 148 | break; 149 | case SDLK_TAB: 150 | break; 151 | } 152 | break; 153 | case SDL_MOUSEMOTION: 154 | shader->bindShader( shader ); 155 | SDL_setUniform_f3( lightPos, 156 | (float) e.motion.x/800.0f, 157 | (float) 1.0f-e.motion.y/600.0f, 158 | 0.450f ); 159 | break; 160 | //case SDL_WINDOWEVENT: 161 | // SDL_updateViewport( shader ); 162 | } 163 | } 164 | 165 | SDL_RenderPresent(renderer); 166 | 167 | SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); 168 | SDL_RenderClear(renderer); 169 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 170 | 171 | } 172 | 173 | 174 | SDL_destroyUniform(shader, resolution); 175 | SDL_destroyUniform(shader, lightPos); 176 | SDL_destroyUniform(shader, lightColor); 177 | SDL_destroyUniform(shader, ambientColor); 178 | SDL_destroyUniform(shader, falloff); 179 | 180 | SDL_destroyShader( shader ); 181 | SDL_DestroyTexture( tex[0] ); 182 | SDL_DestroyTexture( tex[1] ); 183 | SDL_DestroyRenderer( renderer ); 184 | SDL_DestroyWindow( screen ); 185 | SDL_Quit(); 186 | return 0; 187 | } 188 | -------------------------------------------------------------------------------- /test/scale2x/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #ifdef __MACOSX__ 6 | #include 7 | #else 8 | #include 9 | #endif 10 | #include "../src/test/SDL_test_font.h" 11 | #include "../src/SDL_shader.h" 12 | #include "../src/SDL_SYS_RenderStructs.h" 13 | #include 14 | #ifndef M_PI 15 | #define M_PI 3.14159265358979323846 16 | #endif /*M_PI*/ 17 | 18 | char* scaler_names[] = { 19 | "No scaler", 20 | "scale2x/MAME2x" 21 | }; 22 | 23 | int mod(int a, int b) 24 | { 25 | int r = a % b; 26 | return r < 0 ? r + b : r; 27 | } 28 | 29 | #ifdef _WIN32 30 | //prevents displaying the black console window 31 | //on windows systems 32 | //depending on the toolchain (settings) this is also required 33 | #include 34 | int WINAPI WinMain (HINSTANCE hThisInstance, 35 | HINSTANCE hPrevInstance, 36 | LPSTR lpszArgument, 37 | int nCmdShow) 38 | #else 39 | int main(int argc, char** argv) 40 | #endif 41 | { 42 | #ifdef _WIN32 43 | LPSTR *argv = __argv; 44 | int argc = __argc; 45 | (void) argv; 46 | (void) argc; 47 | #endif 48 | SDL_Window *screen; 49 | SDL_Renderer *renderer; 50 | 51 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ 52 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 53 | return 1; 54 | } 55 | 56 | SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); 57 | 58 | int width = 800; 59 | int height = 600; 60 | screen = SDL_CreateWindow("Caption", 61 | SDL_WINDOWPOS_CENTERED, 62 | SDL_WINDOWPOS_CENTERED, 63 | width, height, 64 | SDL_WINDOW_RESIZABLE); 65 | // SDL_WINDOW_FULLSCREEN_DESKTOP ); 66 | if ( screen == NULL ) { 67 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 68 | return 2; 69 | } 70 | renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); 71 | //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); 72 | if ( renderer == NULL ) { 73 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 74 | return 3; 75 | } 76 | 77 | SDL_RenderSetLogicalSize(renderer, width, height); 78 | 79 | SDL_SetWindowTitle( screen, renderer->info.name ); 80 | 81 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 82 | SDL_RenderClear(renderer); 83 | 84 | 85 | int val = 3; 86 | SDL_shader_hint( SDL_D3D_PS_MAJOR_VERSION, &val ); 87 | val = 0; 88 | SDL_shader_hint( SDL_D3D_VS_MINOR_VERSION, &val ); 89 | 90 | SDL_Shader* shader = SDL_createShader( renderer, "../shaders/scale2x/scale2x" ); 91 | if ( shader == NULL ) { 92 | fprintf( stderr, "Error: %s \n", SDL_GetError() ); 93 | } 94 | SDL_assert(shader != NULL); 95 | 96 | SDL_Uniform* scaler = SDL_createUniform( shader, "scaler" ); 97 | SDL_assert(scaler); 98 | SDL_setUniform_i( scaler, 0 ); 99 | 100 | SDL_Texture *image; 101 | SDL_Texture *target_ld, *target_hd; 102 | SDL_Surface *srf; 103 | 104 | srf = IMG_Load( "../pixel_art.png" ); 105 | 106 | SDL_PixelFormat* fmt = SDL_AllocFormat( SDL_PIXELFORMAT_ARGB8888 ); 107 | SDL_assert(fmt != NULL); 108 | SDL_Surface* srf2 = SDL_ConvertSurface(srf, fmt, 0); 109 | SDL_assert(srf2 != NULL); 110 | 111 | if ( !srf ) { 112 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 113 | return 5; 114 | } 115 | image = SDL_CreateTextureFromSurface(renderer, srf2 ); 116 | 117 | SDL_FreeSurface(srf); 118 | SDL_FreeSurface(srf2); 119 | 120 | 121 | target_ld = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, 122 | SDL_TEXTUREACCESS_TARGET, 400, 300 ); 123 | target_hd = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, 124 | SDL_TEXTUREACCESS_TARGET, 800, 600 ); 125 | 126 | SDL_SetRenderTarget( renderer, NULL ); 127 | SDL_Event e; 128 | SDL_SetRenderDrawColor(renderer, 0,0,0,255); 129 | int ret = 0; 130 | int quit = 0; 131 | while ( !quit ) { 132 | static int scaler_num=0; 133 | // Step 1: 134 | // Render something to the low-res texture 135 | SDL_SetRenderTarget( renderer, target_ld ); 136 | SDL_RenderCopy( renderer, image, NULL, NULL ); 137 | SDLTest_DrawString( renderer, 8, 8, scaler_names[scaler_num] ); 138 | // Step 2: 139 | // Copy low-res texture to the high-res texture, using scale shader 140 | SDL_SetRenderTarget( renderer, target_hd ); 141 | SDL_Rect dst = {0,0,800,600}; 142 | ret = SDL_renderCopyShd( shader, target_ld ,NULL,&dst); 143 | //ret = SDL_RenderCopy( renderer, target_ld, NULL, NULL ); 144 | if ( ret!=0 ){ 145 | fprintf(stderr,"Err: %s\n", SDL_GetError()); 146 | } 147 | // Step 3: 148 | // Render something to high-res texture 149 | 150 | // Step 4: 151 | // Copy the high-res texture to the screen 152 | SDL_SetRenderTarget( renderer, NULL ); 153 | //SDL_renderCopyShd( shader, target_hd, NULL, &dst ); 154 | SDL_RenderCopy( renderer, target_hd, NULL, NULL ); 155 | 156 | while (SDL_PollEvent(&e)){ 157 | switch ( e.type ) { 158 | case SDL_QUIT: 159 | quit = 1; 160 | break; 161 | case SDL_KEYDOWN: 162 | switch ( e.key.keysym.sym ) { 163 | case SDLK_SPACE: 164 | scaler_num = mod(scaler_num+1, 2); 165 | SDL_bindShader( shader ); 166 | SDL_setUniform_i( scaler, scaler_num ); 167 | break; 168 | case SDLK_TAB: 169 | scaler_num = mod(scaler_num-1, 2); 170 | SDL_bindShader( shader ); 171 | SDL_setUniform_i( scaler, scaler_num ); 172 | break; 173 | } 174 | break; 175 | //case SDL_WINDOWEVENT: 176 | // SDL_updateViewport( shader ); 177 | } 178 | } 179 | 180 | SDL_RenderPresent(renderer); 181 | 182 | SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); 183 | SDL_RenderClear(renderer); 184 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 185 | 186 | } 187 | 188 | 189 | SDL_destroyShader( shader ); 190 | SDL_DestroyTexture( image ); 191 | SDL_DestroyRenderer( renderer ); 192 | SDL_DestroyWindow( screen ); 193 | SDL_Quit(); 194 | return 0; 195 | } 196 | -------------------------------------------------------------------------------- /src/SDL_shader.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #ifndef _SDL_shader_h 23 | #define _SDL_shader_h 24 | 25 | /* Set up for C function definitions, even when using C++ */ 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef enum { 31 | SDL_GLSL_VERSION, 32 | SDL_D3D_VS_MAJOR_VERSION, 33 | SDL_D3D_VS_MINOR_VERSION, 34 | SDL_D3D_PS_MAJOR_VERSION, 35 | SDL_D3D_PS_MINOR_VERSION, 36 | 37 | SDL_GL_TEX0_NAME, 38 | SDL_GL_TEX1_NAME, 39 | SDL_GL_TEX2_NAME, 40 | SDL_GL_TEX3_NAME, 41 | SDL_GL_TEX4_NAME, 42 | SDL_GL_TEX5_NAME, 43 | SDL_GL_TEX6_NAME, 44 | SDL_GL_TEX7_NAME, 45 | } sdl_shader_hint; 46 | 47 | typedef struct { 48 | char* vshader; 49 | char* pshader; 50 | } SDL_ShaderFileNames; 51 | 52 | typedef struct { 53 | SDL_RWops* vshader; 54 | SDL_RWops* pshader; 55 | } SDL_ShaderStream; 56 | 57 | typedef struct SDL_Shader_t SDL_Shader; 58 | typedef struct SDL_Uniform_t SDL_Uniform; 59 | struct SDL_Uniform_t{ 60 | SDL_Shader* shader; 61 | 62 | int (*setUniform_fv)( SDL_Uniform*, float* vector, int num ); 63 | int (*setUniform_iv)( SDL_Uniform*, int* vector, int num ); 64 | 65 | int (*setUniform_f )( SDL_Uniform* uniform, float a ); 66 | int (*setUniform_f2)( SDL_Uniform* uniform, float a, float b ); 67 | int (*setUniform_f3)( SDL_Uniform* uniform, float a, float b, float c ); 68 | int (*setUniform_f4)( SDL_Uniform* uniform, float a, float b, float c, float d ); 69 | 70 | int (*setUniform_i )( SDL_Uniform* uniform, int a ); 71 | int (*setUniform_i2)( SDL_Uniform* uniform, int a, int b ); 72 | int (*setUniform_i3)( SDL_Uniform* uniform, int a, int b, int c ); 73 | int (*setUniform_i4)( SDL_Uniform* uniform, int a, int b, int c, int d ); 74 | 75 | void* driver_data; 76 | }; 77 | 78 | typedef struct SDL_Vertex_t SDL_Vertex; 79 | struct SDL_Vertex_t { 80 | unsigned size; 81 | void* vertexBuffer; 82 | void (*setVertexColor)(SDL_Vertex* vertices, unsigned from, 83 | unsigned num, uint8_t r, uint8_t g, uint8_t b, uint8_t a ); 84 | void (*setVertexPosition)(SDL_Vertex* vertices, unsigned from, 85 | unsigned num, float x, float y, float z ); 86 | void (*setVertexTexCoord)(SDL_Vertex* vertices, unsigned from, 87 | unsigned num, float s, float t ); 88 | void (*getVertex)( SDL_Vertex* vertices, unsigned num, 89 | uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a, 90 | float *x, float *y, float *z, 91 | float *tex_s, float *tex_t ); 92 | }; 93 | 94 | struct SDL_Shader_t { 95 | SDL_Renderer* renderer; 96 | 97 | SDL_Shader* (*create_shader)( SDL_Renderer *renderer, SDL_ShaderStream* str ); 98 | 99 | int (*bindShader)( SDL_Shader* shader ); 100 | int (*unbindShader)( SDL_Shader* shader ); 101 | int (*destroyShader)( SDL_Shader* shader ); 102 | 103 | int (*renderCopyShd)(SDL_Shader* shader, SDL_Texture** textures, 104 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert); 105 | void (*updateViewport)( SDL_Shader *shader ); 106 | 107 | SDL_Uniform* (*createUniform)( SDL_Shader* shader, const char* name ); 108 | int (*destroyUniform)( SDL_Shader* shader, SDL_Uniform* uniform ); 109 | 110 | SDL_Vertex* (*createVertexBuffer)( unsigned size ); 111 | int (*destroyVertexBuffer)( SDL_Vertex* buff ); 112 | 113 | void* driver_data; 114 | }; 115 | 116 | 117 | 118 | void SDL_shader_hint(sdl_shader_hint flag, void* value); 119 | 120 | SDL_ShaderFileNames SDL_getShaderFileNames( SDL_Renderer* renderer, 121 | const char* name ); 122 | SDL_ShaderStream SDL_getShaderStream( SDL_ShaderFileNames* names, int delete_ ); 123 | 124 | SDL_Shader* SDL_createShader( SDL_Renderer *renderer, const char* name ); 125 | SDL_Shader* SDL_createShader_RW( SDL_Renderer *renderer, 126 | SDL_ShaderStream* shdstream, int close ); 127 | int SDL_destroyShader( SDL_Shader* shader ); 128 | int SDL_bindShader( SDL_Shader* shader ); 129 | int SDL_renderCopyShd( SDL_Shader* shader, SDL_Texture* texture, 130 | const SDL_Rect * srcrect, const SDL_Rect * dstrect); 131 | int SDL_renderCopyShdArray( SDL_Shader* shader, SDL_Texture** textures, 132 | const SDL_Rect * srcrect, const SDL_Rect * dstrect, unsigned num); 133 | int SDL_renderVertexBuffer( SDL_Shader* shader, SDL_Vertex *vertices, 134 | SDL_Texture** textures, unsigned num_of_tex); 135 | void SDL_updateViewport( SDL_Shader *shader ); 136 | 137 | SDL_Uniform* SDL_createUniform( SDL_Shader* shader, const char* name ); 138 | int SDL_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ); 139 | 140 | int SDL_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ); 141 | int SDL_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ); 142 | 143 | int SDL_setUniform_f ( SDL_Uniform* uniform, float a ); 144 | int SDL_setUniform_f2( SDL_Uniform* uniform, float a, float b ); 145 | int SDL_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ); 146 | int SDL_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ); 147 | 148 | int SDL_setUniform_i ( SDL_Uniform* uniform, int a ); 149 | int SDL_setUniform_i2( SDL_Uniform* uniform, int a, int b ); 150 | int SDL_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ); 151 | int SDL_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ); 152 | 153 | #ifdef __cplusplus 154 | } 155 | #endif 156 | 157 | #endif /* _SDL_shader_h */ 158 | 159 | -------------------------------------------------------------------------------- /test/context_switch/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #ifdef __MACOSX__ 6 | #include 7 | #else 8 | #include 9 | #endif 10 | #include "../src/test/SDL_test_font.h" 11 | #include "../src/SDL_shader.h" 12 | #include "../src/SDL_SYS_RenderStructs.h" 13 | 14 | #define NUM_OF_SHADERS (sizeof(shader_names)/sizeof(char*)) 15 | 16 | 17 | #ifdef _WIN32 18 | //prevents displaying the black console window 19 | //on windows systems 20 | //depending on the toolchain (settings) this is also required 21 | #include 22 | int WINAPI WinMain (HINSTANCE hThisInstance, 23 | HINSTANCE hPrevInstance, 24 | LPSTR lpszArgument, 25 | int nCmdShow) 26 | #else 27 | int main(int argc, char** argv) 28 | #endif 29 | { 30 | #ifdef _WIN32 31 | LPSTR *argv = __argv; 32 | int argc = __argc; 33 | (void) argv; 34 | (void) argc; 35 | #endif 36 | SDL_Window *screen; 37 | SDL_Renderer *renderer; 38 | 39 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ 40 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 41 | return 1; 42 | } 43 | 44 | SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); 45 | 46 | int width = 800; 47 | int height = 600; 48 | screen = SDL_CreateWindow("Caption", 49 | SDL_WINDOWPOS_CENTERED, 50 | SDL_WINDOWPOS_CENTERED, 51 | width, height, 52 | //SDL_WINDOW_RESIZABLE); 53 | SDL_WINDOW_FULLSCREEN_DESKTOP ); 54 | if ( screen == NULL ) { 55 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 56 | return 2; 57 | } 58 | renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); 59 | //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); 60 | if ( renderer == NULL ) { 61 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 62 | return 3; 63 | } 64 | 65 | SDL_RenderSetLogicalSize(renderer, width, height); 66 | 67 | SDL_SetWindowTitle( screen, renderer->info.name ); 68 | 69 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 70 | SDL_RenderClear(renderer); 71 | 72 | SDL_Shader *shader; 73 | shader = SDL_createShader( renderer, "../shaders/do_nothing/do_nothing" ); 74 | 75 | SDL_Texture* tex; 76 | SDL_Texture* tex2; 77 | SDL_Surface* srf; 78 | srf = IMG_Load( "../img.png" ); 79 | if ( !srf ) { 80 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 81 | return 5; 82 | } 83 | tex = SDL_CreateTextureFromSurface(renderer, srf ); 84 | tex2 = SDL_CreateTextureFromSurface(renderer, srf ); 85 | SDL_SetTextureAlphaMod( tex2, 127 ); 86 | SDL_FreeSurface(srf); 87 | 88 | SDL_SetRenderTarget( renderer, NULL ); 89 | SDL_Event e; 90 | SDL_SetRenderDrawColor(renderer, 0,0,0,1); 91 | int ret = 0; 92 | int quit = 0; 93 | while ( !quit ) { 94 | SDL_Rect dst; 95 | SDLTest_DrawString( renderer, 25, 10, "Shd -> Copy -> Shd" ); 96 | SDLTest_DrawString( renderer, 25, 85, "Copy -> Shd -> Copy" ); 97 | SDLTest_DrawString( renderer, 25, 160, "Draw -> Shd -> Draw" ); 98 | SDLTest_DrawString( renderer, 25, 235, "Shd -> Draw -> Shd" ); 99 | 100 | // Shd -> Copy -> Shd 101 | SDL_SetTextureColorMod(tex, 255,255,255); 102 | dst = (SDL_Rect) { 25, 25, tex->w, tex->h }; 103 | ret = SDL_renderCopyShd( shader, tex, NULL, &dst ); 104 | 105 | SDL_SetTextureColorMod(tex2, 255,0,0); 106 | dst = (SDL_Rect){ 100, 25, tex->w, tex->h }; 107 | ret = SDL_RenderCopy( renderer, tex2, NULL, &dst ); 108 | 109 | SDL_SetTextureColorMod(tex, 255,255,255); 110 | dst = (SDL_Rect){ 175, 25, tex->w, tex->h }; 111 | ret = SDL_renderCopyShd( shader, tex, NULL, &dst ); 112 | 113 | // Copy -> Shd -> Copy 114 | SDL_SetTextureColorMod(tex2, 255,255,255); 115 | dst = (SDL_Rect){ 25, 100, tex->w, tex->h }; 116 | ret = SDL_RenderCopy( renderer, tex2, NULL, &dst ); 117 | 118 | SDL_SetTextureColorMod(tex, 0,255,0); 119 | dst = (SDL_Rect){ 100, 100, tex->w, tex->h }; 120 | ret = SDL_renderCopyShd( shader, tex, NULL, &dst ); 121 | 122 | SDL_SetTextureColorMod(tex2, 255,255,255); 123 | dst = (SDL_Rect){ 175, 100, tex->w, tex->h }; 124 | ret = SDL_RenderCopy( renderer, tex2, NULL, &dst ); 125 | 126 | // Draw -> Shd -> Draw 127 | SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); 128 | dst = (SDL_Rect){ 25, 175, tex->w, tex->h }; 129 | ret = SDL_RenderFillRect( renderer, &dst ); 130 | 131 | SDL_SetTextureColorMod(tex, 255,0,255); 132 | dst = (SDL_Rect){ 100, 175, tex->w, tex->h }; 133 | ret = SDL_renderCopyShd( shader, tex, NULL, &dst ); 134 | 135 | SDL_SetTextureColorMod(tex, 255,0,255); 136 | dst = (SDL_Rect){ -1, -1, 1, 1 }; 137 | SDL_RenderCopy( renderer, tex2, NULL, &dst ); 138 | ret = SDL_RenderFillRect( renderer, &dst ); 139 | SDL_RenderCopy( renderer, tex2, NULL, &dst ); 140 | 141 | SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); 142 | dst = (SDL_Rect){ 175, 175, tex->w, tex->h }; 143 | ret = SDL_RenderFillRect( renderer, &dst ); 144 | 145 | // Shd -> Draw -> Shd 146 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 147 | SDL_SetTextureColorMod(tex, 0,255,255); 148 | dst = (SDL_Rect){ 25, 250, tex->w, tex->h }; 149 | ret = SDL_renderCopyShd( shader, tex, NULL, &dst ); 150 | 151 | dst = (SDL_Rect){ 100, 250, tex->w, tex->h }; 152 | ret = SDL_RenderFillRect( renderer, &dst ); 153 | 154 | SDL_SetTextureColorMod(tex, 255,255,0); 155 | dst = (SDL_Rect){ 175, 250, tex->w, tex->h }; 156 | ret = SDL_renderCopyShd( shader, tex, NULL, &dst ); 157 | 158 | 159 | if ( ret!=0 ){ 160 | fprintf(stderr,"Err: %s\n", SDL_GetError()); 161 | } 162 | 163 | while (SDL_PollEvent(&e)){ 164 | switch ( e.type ) { 165 | case SDL_QUIT: 166 | quit = 1; 167 | break; 168 | case SDL_KEYDOWN: 169 | switch ( e.key.keysym.sym ) { 170 | case SDLK_SPACE: 171 | break; 172 | case SDLK_TAB: 173 | break; 174 | } 175 | break; 176 | //case SDL_WINDOWEVENT: 177 | //SDL_updateViewport( shader ); 178 | } 179 | } 180 | 181 | SDL_RenderPresent(renderer); 182 | 183 | SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); 184 | SDL_RenderClear(renderer); 185 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 186 | 187 | } 188 | 189 | SDL_DestroyTexture( tex ); 190 | SDL_DestroyTexture( tex2 ); 191 | SDL_DestroyRenderer( renderer ); 192 | SDL_DestroyWindow( screen ); 193 | SDL_Quit(); 194 | 195 | return 0; 196 | } 197 | -------------------------------------------------------------------------------- /src/SDL_SYS_RenderStructs.h: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 4 | 5 | Thes code is internally defined by SDL2 and copied from the SDL2-2.0.3 6 | source release. Minor changes or additions (if any) are provided by the 7 | license below. 8 | */ 9 | 10 | /* 11 | Simple DirectMedia Layer 12 | Copyright (C) 1997-2014 Sam Lantinga 13 | 14 | This software is provided 'as-is', without any express or implied 15 | warranty. In no event will the authors be held liable for any damages 16 | arising from the use of this software. 17 | 18 | Permission is granted to anyone to use this software for any purpose, 19 | including commercial applications, and to alter it and redistribute it 20 | freely, subject to the following restrictions: 21 | 22 | 1. The origin of this software must not be misrepresented; you must not 23 | claim that you wrote the original software. If you use this software 24 | in a product, an acknowledgment in the product documentation would be 25 | appreciated but is not required. 26 | 2. Altered source versions must be plainly marked as such, and must not be 27 | misrepresented as being the original software. 28 | 3. This notice may not be removed or altered from any source distribution. 29 | */ 30 | 31 | #ifndef _SDL_SYS_RenderStructs_h 32 | #define _SDL_SYS_RenderStructs_h 33 | 34 | typedef struct { 35 | float x; 36 | float y; 37 | } SDL_FPoint; 38 | 39 | typedef struct { 40 | float x; 41 | float y; 42 | float w; 43 | float h; 44 | } SDL_FRect; 45 | 46 | typedef void SDL_SW_YUVTexture; /*TODO*/ 47 | struct SDL_Texture 48 | { 49 | const void *magic; 50 | Uint32 format; /**< The pixel format of the texture */ 51 | int access; /**< SDL_TextureAccess */ 52 | int w; /**< The width of the texture */ 53 | int h; /**< The height of the texture */ 54 | int modMode; /**< The texture modulation mode */ 55 | SDL_BlendMode blendMode; /**< The texture blend mode */ 56 | Uint8 r, g, b, a; /**< Texture modulation values */ 57 | 58 | SDL_Renderer *renderer; 59 | 60 | /* Support for formats not supported directly by the renderer */ 61 | SDL_Texture *native; 62 | SDL_SW_YUVTexture *yuv; 63 | void *pixels; 64 | int pitch; 65 | SDL_Rect locked_rect; 66 | 67 | void *driverdata; /**< Driver specific texture representation */ 68 | 69 | SDL_Texture *prev; 70 | SDL_Texture *next; 71 | }; 72 | 73 | struct SDL_Renderer 74 | { 75 | const void *magic; 76 | 77 | void (*WindowEvent) (SDL_Renderer * renderer, const SDL_WindowEvent *event); 78 | int (*GetOutputSize) (SDL_Renderer * renderer, int *w, int *h); 79 | int (*CreateTexture) (SDL_Renderer * renderer, SDL_Texture * texture); 80 | int (*SetTextureColorMod) (SDL_Renderer * renderer, 81 | SDL_Texture * texture); 82 | int (*SetTextureAlphaMod) (SDL_Renderer * renderer, 83 | SDL_Texture * texture); 84 | int (*SetTextureBlendMode) (SDL_Renderer * renderer, 85 | SDL_Texture * texture); 86 | int (*UpdateTexture) (SDL_Renderer * renderer, SDL_Texture * texture, 87 | const SDL_Rect * rect, const void *pixels, 88 | int pitch); 89 | int (*UpdateTextureYUV) (SDL_Renderer * renderer, SDL_Texture * texture, 90 | const SDL_Rect * rect, 91 | const Uint8 *Yplane, int Ypitch, 92 | const Uint8 *Uplane, int Upitch, 93 | const Uint8 *Vplane, int Vpitch); 94 | int (*LockTexture) (SDL_Renderer * renderer, SDL_Texture * texture, 95 | const SDL_Rect * rect, void **pixels, int *pitch); 96 | void (*UnlockTexture) (SDL_Renderer * renderer, SDL_Texture * texture); 97 | int (*SetRenderTarget) (SDL_Renderer * renderer, SDL_Texture * texture); 98 | int (*UpdateViewport) (SDL_Renderer * renderer); 99 | int (*UpdateClipRect) (SDL_Renderer * renderer); 100 | int (*RenderClear) (SDL_Renderer * renderer); 101 | int (*RenderDrawPoints) (SDL_Renderer * renderer, const SDL_FPoint * points, 102 | int count); 103 | int (*RenderDrawLines) (SDL_Renderer * renderer, const SDL_FPoint * points, 104 | int count); 105 | int (*RenderFillRects) (SDL_Renderer * renderer, const SDL_FRect * rects, 106 | int count); 107 | int (*RenderCopy) (SDL_Renderer * renderer, SDL_Texture * texture, 108 | const SDL_Rect * srcrect, const SDL_FRect * dstrect); 109 | int (*RenderCopyEx) (SDL_Renderer * renderer, SDL_Texture * texture, 110 | const SDL_Rect * srcquad, const SDL_FRect * dstrect, 111 | const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip); 112 | int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect, 113 | Uint32 format, void * pixels, int pitch); 114 | void (*RenderPresent) (SDL_Renderer * renderer); 115 | void (*DestroyTexture) (SDL_Renderer * renderer, SDL_Texture * texture); 116 | 117 | void (*DestroyRenderer) (SDL_Renderer * renderer); 118 | 119 | int (*GL_BindTexture) (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh); 120 | int (*GL_UnbindTexture) (SDL_Renderer * renderer, SDL_Texture *texture); 121 | 122 | /* The current renderer info */ 123 | SDL_RendererInfo info; 124 | 125 | /* The window associated with the renderer */ 126 | SDL_Window *window; 127 | SDL_bool hidden; 128 | 129 | /* The logical resolution for rendering */ 130 | int logical_w; 131 | int logical_h; 132 | int logical_w_backup; 133 | int logical_h_backup; 134 | 135 | /* The drawable area within the window */ 136 | SDL_Rect viewport; 137 | SDL_Rect viewport_backup; 138 | 139 | /* The clip rectangle within the window */ 140 | SDL_Rect clip_rect; 141 | SDL_Rect clip_rect_backup; 142 | 143 | /* The render output coordinate scale */ 144 | SDL_FPoint scale; 145 | SDL_FPoint scale_backup; 146 | 147 | /* The list of textures */ 148 | SDL_Texture *textures; 149 | SDL_Texture *target; 150 | 151 | Uint8 r, g, b, a; /**< Color for drawing operations values */ 152 | SDL_BlendMode blendMode; /**< The drawing blend mode */ 153 | 154 | void *driverdata; 155 | }; 156 | 157 | #endif /* _SDL_SYS_RenderStructs_h */ 158 | 159 | 160 | -------------------------------------------------------------------------------- /test/color_mode/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #ifdef __MACOSX__ 6 | #include 7 | #else 8 | #include 9 | #endif 10 | #include "../src/test/SDL_test_font.h" 11 | #include "../src/SDL_shader.h" 12 | #include "../src/SDL_SYS_RenderStructs.h" 13 | 14 | #define NUM_OF_TEXTURES (sizeof(formats)/sizeof(Uint32)) 15 | 16 | #ifdef _WIN32 17 | //prevents displaying the black console window 18 | //on windows systems 19 | //depending on the toolchain (settings) this is also required 20 | #include 21 | int WINAPI WinMain (HINSTANCE hThisInstance, 22 | HINSTANCE hPrevInstance, 23 | LPSTR lpszArgument, 24 | int nCmdShow) 25 | #else 26 | int main(int argc, char** argv) 27 | #endif 28 | { 29 | #ifdef _WIN32 30 | LPSTR *argv = __argv; 31 | int argc = __argc; 32 | (void) argv; 33 | (void) argc; 34 | #endif 35 | SDL_Window *screen; 36 | SDL_Renderer *renderer; 37 | SDL_Shader *shader; 38 | 39 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ 40 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 41 | return 1; 42 | } 43 | 44 | SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 ); 45 | SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 ); 46 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); 47 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 ); 48 | 49 | SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); 50 | 51 | int width = 500; 52 | int height = 700; 53 | screen = SDL_CreateWindow("Caption", 54 | SDL_WINDOWPOS_CENTERED, 55 | SDL_WINDOWPOS_CENTERED, 56 | width, height, 57 | SDL_WINDOW_RESIZABLE); 58 | // SDL_WINDOW_FULLSCREEN_DESKTOP ); 59 | if ( screen == NULL ) { 60 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 61 | return 2; 62 | } 63 | renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); 64 | //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); 65 | if ( renderer == NULL ) { 66 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 67 | return 3; 68 | } 69 | 70 | SDL_RenderSetLogicalSize(renderer, width, height); 71 | 72 | SDL_SetWindowTitle( screen, renderer->info.name ); 73 | 74 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 75 | SDL_RenderClear(renderer); 76 | 77 | 78 | printf("renderer name: %s\n" , renderer->info.name ); 79 | printf("SDL_PIXELFORMAT_UNKNOWN= %d\n",SDL_PIXELFORMAT_UNKNOWN); 80 | printf("SDL_PIXELFORMAT_INDEX1LSB= %d\n",SDL_PIXELFORMAT_INDEX1LSB); 81 | printf("SDL_PIXELFORMAT_INDEX1MSB= %d\n",SDL_PIXELFORMAT_INDEX1MSB); 82 | printf("SDL_PIXELFORMAT_INDEX4LSB= %d\n",SDL_PIXELFORMAT_INDEX4LSB); 83 | printf("SDL_PIXELFORMAT_INDEX4MSB= %d\n",SDL_PIXELFORMAT_INDEX4MSB); 84 | printf("SDL_PIXELFORMAT_INDEX8= %d\n",SDL_PIXELFORMAT_INDEX8); 85 | printf("SDL_PIXELFORMAT_RGB332= %d\n",SDL_PIXELFORMAT_RGB332); 86 | printf("SDL_PIXELFORMAT_RGB444= %d\n",SDL_PIXELFORMAT_RGB444); 87 | printf("SDL_PIXELFORMAT_RGB555= %d\n",SDL_PIXELFORMAT_RGB555); 88 | printf("SDL_PIXELFORMAT_BGR555= %d\n",SDL_PIXELFORMAT_BGR555); 89 | printf("SDL_PIXELFORMAT_ARGB4444= %d\n",SDL_PIXELFORMAT_ARGB4444); 90 | printf("SDL_PIXELFORMAT_RGBA4444= %d\n",SDL_PIXELFORMAT_RGBA4444); 91 | printf("SDL_PIXELFORMAT_ABGR4444= %d\n",SDL_PIXELFORMAT_ABGR4444); 92 | printf("SDL_PIXELFORMAT_BGRA4444= %d\n",SDL_PIXELFORMAT_BGRA4444); 93 | printf("SDL_PIXELFORMAT_ARGB1555= %d\n",SDL_PIXELFORMAT_ARGB1555); 94 | printf("SDL_PIXELFORMAT_RGBA5551= %d\n",SDL_PIXELFORMAT_RGBA5551); 95 | printf("SDL_PIXELFORMAT_ABGR1555= %d\n",SDL_PIXELFORMAT_ABGR1555); 96 | printf("SDL_PIXELFORMAT_BGRA5551= %d\n",SDL_PIXELFORMAT_BGRA5551); 97 | printf("SDL_PIXELFORMAT_RGB565= %d\n",SDL_PIXELFORMAT_RGB565); 98 | printf("SDL_PIXELFORMAT_BGR565= %d\n",SDL_PIXELFORMAT_BGR565); 99 | printf("SDL_PIXELFORMAT_RGB24= %d\n",SDL_PIXELFORMAT_RGB24); 100 | printf("SDL_PIXELFORMAT_BGR24= %d\n",SDL_PIXELFORMAT_BGR24); 101 | printf("SDL_PIXELFORMAT_RGB888= %d\n",SDL_PIXELFORMAT_RGB888); 102 | printf("SDL_PIXELFORMAT_RGBX8888= %d\n",SDL_PIXELFORMAT_RGBX8888); 103 | printf("SDL_PIXELFORMAT_BGR888= %d\n",SDL_PIXELFORMAT_BGR888); 104 | printf("SDL_PIXELFORMAT_BGRX8888= %d\n",SDL_PIXELFORMAT_BGRX8888); 105 | printf("SDL_PIXELFORMAT_ARGB8888= %d\n",SDL_PIXELFORMAT_ARGB8888); 106 | printf("SDL_PIXELFORMAT_RGBA8888= %d\n",SDL_PIXELFORMAT_RGBA8888); 107 | printf("SDL_PIXELFORMAT_ABGR8888= %d\n",SDL_PIXELFORMAT_ABGR8888); 108 | printf("SDL_PIXELFORMAT_BGRA8888= %d\n",SDL_PIXELFORMAT_BGRA8888); 109 | printf("SDL_PIXELFORMAT_ARGB2101010=%d\n",SDL_PIXELFORMAT_ARGB2101010); 110 | 111 | shader = SDL_createShader( renderer, "../shaders/do_nothing/do_nothing" ); 112 | if ( shader == NULL ){ 113 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 114 | return 4; 115 | } 116 | 117 | SDL_Surface* srf; 118 | srf = IMG_Load( "../img.png" ); 119 | if ( !srf ) { 120 | fprintf(stderr, "Error: %s \n", SDL_GetError()); 121 | return 5; 122 | } 123 | 124 | int i; 125 | Uint32 formats[] = { 126 | /* Indexed formats and YUV are not supported */ 127 | SDL_PIXELFORMAT_RGB332, 128 | SDL_PIXELFORMAT_RGB444, 129 | SDL_PIXELFORMAT_RGB555, 130 | SDL_PIXELFORMAT_BGR555, 131 | SDL_PIXELFORMAT_ARGB4444, 132 | SDL_PIXELFORMAT_RGBA4444, 133 | SDL_PIXELFORMAT_ABGR4444, 134 | SDL_PIXELFORMAT_BGRA4444, 135 | SDL_PIXELFORMAT_ARGB1555, 136 | SDL_PIXELFORMAT_RGBA5551, 137 | SDL_PIXELFORMAT_ABGR1555, 138 | SDL_PIXELFORMAT_BGRA5551, 139 | SDL_PIXELFORMAT_RGB565, 140 | SDL_PIXELFORMAT_BGR565, 141 | SDL_PIXELFORMAT_RGB24, 142 | SDL_PIXELFORMAT_BGR24, 143 | SDL_PIXELFORMAT_RGB888, 144 | SDL_PIXELFORMAT_RGBX8888, 145 | SDL_PIXELFORMAT_BGR888, 146 | SDL_PIXELFORMAT_BGRX8888, 147 | SDL_PIXELFORMAT_ARGB8888, 148 | SDL_PIXELFORMAT_RGBA8888, 149 | SDL_PIXELFORMAT_ABGR8888, 150 | SDL_PIXELFORMAT_BGRA8888, 151 | SDL_PIXELFORMAT_ARGB2101010 152 | }; 153 | 154 | char fmt_names[][NUM_OF_TEXTURES] = { 155 | "RGB332", 156 | "RGB444", 157 | "RGB555", 158 | "BGR555", 159 | "ARGB4444", 160 | "RGBA4444", 161 | "ABGR4444", 162 | "BGRA4444", 163 | "ARGB1555", 164 | "RGBA5551", 165 | "ABGR1555", 166 | "BGRA5551", 167 | "RGB565", 168 | "BGR565", 169 | "RGB24", 170 | "BGR24", 171 | "RGB888", 172 | "RGBX8888", 173 | "BGR888", 174 | "BGRX8888", 175 | "ARGB8888", 176 | "RGBA8888", 177 | "ABGR8888", 178 | "BGRA8888", 179 | "ARGB2101010" 180 | }; 181 | 182 | SDL_BlendMode blendModes[] = { 183 | SDL_BLENDMODE_NONE, 184 | SDL_BLENDMODE_BLEND, 185 | SDL_BLENDMODE_ADD, 186 | SDL_BLENDMODE_MOD 187 | }; 188 | char* blendNames[] = { 189 | "NONE", 190 | "BLEND", 191 | "ADD", 192 | "MOD" 193 | }; 194 | int current_blend = 0; 195 | 196 | int colors[][4] = { 197 | {255,255,255,255}, 198 | {255, 0, 0,255}, 199 | { 0,255, 0,255}, 200 | { 0, 0,255,255}, 201 | {255,255,255,127} 202 | }; 203 | char* colorNames[] = { 204 | "white", 205 | "red", 206 | "green", 207 | "blue", 208 | "semi transp." 209 | }; 210 | int current_color = 0; 211 | 212 | SDL_Texture* textures[ NUM_OF_TEXTURES ]; 213 | 214 | for ( i=0; i< (int)(NUM_OF_TEXTURES); i++) { 215 | SDL_PixelFormat* fmt = SDL_AllocFormat( formats[i] ); 216 | SDL_assert(fmt != NULL); 217 | SDL_Surface* srf2 = SDL_ConvertSurface(srf, fmt, 0); 218 | SDL_assert(srf2 != NULL); 219 | textures[i] = SDL_CreateTextureFromSurface(renderer, srf2 ); 220 | 221 | SDL_SetTextureBlendMode(textures[i], blendModes[current_blend]); 222 | SDL_FreeSurface( srf2 ); 223 | SDL_FreeFormat(fmt); 224 | 225 | if ( !textures[i] ){ 226 | return 1000 + i; 227 | } 228 | 229 | if ( textures[i]->native ){ 230 | printf("native_tex: %d \n",textures[i]->native->format ); 231 | }else{ 232 | printf("SDL_tex: %d \n",textures[i]->format ); 233 | } 234 | 235 | } 236 | SDL_FreeSurface(srf); 237 | 238 | SDL_SetRenderTarget( renderer, NULL ); 239 | SDL_Event e; 240 | SDL_SetRenderDrawColor(renderer, 0,0,0,1); 241 | int ret = 0; 242 | int quit = 0; 243 | 244 | while ( !quit ) { 245 | SDLTest_DrawString( renderer, 8, 8, blendNames[current_blend] ); 246 | SDLTest_DrawString( renderer, 108, 8, colorNames[current_color] ); 247 | for ( i=0; i< (int)(NUM_OF_TEXTURES); i++) { 248 | int x=30+(i%8)*55; 249 | int y=30+(i/8)*75; 250 | SDL_Rect dst = {x,y,50,50}; 251 | ret = SDL_renderCopyShd( shader, textures[i], NULL, &dst ); 252 | if ( ret!=0 ){ 253 | fprintf(stderr,"Err: %s\n", SDL_GetError()); 254 | } 255 | SDLTest_DrawString( renderer, dst.x,dst.y+55 + (i%2==0 ? 10 : 0), fmt_names[i] ); 256 | 257 | dst.y += (NUM_OF_TEXTURES)/8 * 75 + 85; 258 | SDL_RenderCopy( renderer, textures[i], NULL, &dst ); 259 | SDLTest_DrawString( renderer, dst.x,dst.y+55 + (i%2==0 ? 10 : 0), fmt_names[i] ); 260 | 261 | } 262 | while (SDL_PollEvent(&e)){ 263 | switch ( e.type ) { 264 | case SDL_QUIT: 265 | quit = 1; 266 | break; 267 | case SDL_KEYDOWN: 268 | switch ( e.key.keysym.sym ) { 269 | case SDLK_SPACE: 270 | current_blend = (current_blend+1)%4; 271 | for ( i=0; i< (int)(NUM_OF_TEXTURES); i++) { 272 | SDL_SetTextureBlendMode(textures[i], blendModes[current_blend]); 273 | } 274 | break; 275 | case SDLK_TAB: 276 | current_color = (current_color+1)%5; 277 | for ( i=0; i< (int)(NUM_OF_TEXTURES); i++) { 278 | SDL_SetTextureColorMod(textures[i], 279 | colors[current_color][0], 280 | colors[current_color][1], 281 | colors[current_color][2]); 282 | SDL_SetTextureAlphaMod(textures[i], colors[current_color][3] ); 283 | } 284 | break; 285 | } 286 | break; 287 | case SDL_WINDOWEVENT: 288 | SDL_updateViewport( shader ); 289 | } 290 | } 291 | 292 | SDL_RenderPresent(renderer); 293 | 294 | SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); 295 | SDL_RenderClear(renderer); 296 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 297 | 298 | } 299 | for ( i=0; i<(int)NUM_OF_TEXTURES; i++ ) { 300 | SDL_DestroyTexture( textures[i] ); 301 | textures[i] = NULL; 302 | } 303 | SDL_destroyShader( shader ); 304 | SDL_DestroyRenderer( renderer ); 305 | SDL_DestroyWindow( screen ); 306 | SDL_Quit(); 307 | 308 | return 0; 309 | } 310 | -------------------------------------------------------------------------------- /src/SDL_shader.c: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #include 23 | #include 24 | #include "SDL_SYS_RenderStructs.h" 25 | #include "SDL_shader.h" 26 | #include "opengles2/SDL_GLES2_shader.h" 27 | #include "opengl/SDL_GL_shader.h" 28 | #include "d3d/SDL_D3D_shader.h" 29 | //#include "d3d11/SDL_D3D11_shader.h" 30 | 31 | 32 | #include 33 | SDL_Shader* SDL_createShader( SDL_Renderer *renderer, const char* name ) { 34 | 35 | SDL_version linked; 36 | SDL_GetVersion(&linked); 37 | if ( !( linked.major >= 2 && linked.minor >= 0 && linked.patch >= 1) ) { 38 | SDL_SetError("SDL_Shader: Linked SDL_version must be atleast 2.0.1\n"); 39 | return NULL; 40 | } 41 | 42 | if ( linked.major >= 2 && linked.minor >= 0 && linked.patch >= 4 ) { 43 | fprintf(stderr,"WARN: [SDL_Shader] Linked SDL_version is atleast 2.0.4, this is untested\n"); 44 | } 45 | 46 | SDL_ShaderFileNames names = SDL_getShaderFileNames( renderer, name ); 47 | SDL_ShaderStream stream = SDL_getShaderStream( &names, 1 ); 48 | return SDL_createShader_RW( renderer, &stream, 1 ); 49 | } 50 | 51 | SDL_Shader* SDL_createShader_RW( SDL_Renderer *renderer, 52 | SDL_ShaderStream* shdstream, int close ) 53 | { 54 | SDL_Shader* shader = NULL; 55 | SDL_SetError("SDL_Shader: No shading available for renderer '%s'\n", renderer->info.name); 56 | 57 | 58 | #ifdef SDL_SHADER_OPENGL 59 | if( strcmp( renderer->info.name, "opengl" )==0 ) { 60 | shader = SDL_GL_createShader( renderer, shdstream ); 61 | } 62 | #endif 63 | 64 | /* opengles1.x didn't support shaders */ 65 | 66 | #ifdef SDL_SHADER_OPENGLES2 67 | if( strcmp( renderer->info.name, "opengles2" )==0 ) { 68 | shader = SDL_GLES2_createShader( renderer, shdstream ); 69 | } 70 | #endif 71 | 72 | #ifdef SDL_SHADER_D3D 73 | if( strcmp( renderer->info.name, "direct3d" )==0 ) { 74 | shader = SDL_D3D_createShader( renderer, shdstream ); 75 | } 76 | #endif 77 | 78 | #ifdef SDL_SHADER_D3D11 79 | if( strcmp( renderer->info.name, "direct3d11" )==0 ) { 80 | shader = SDL_D3D11_createShader( renderer, shdstream ); 81 | } 82 | #endif 83 | 84 | return shader; 85 | 86 | } 87 | 88 | void SDL_shader_hint( sdl_shader_hint flag, void* value ){ 89 | #ifdef SDL_SHADER_OPENGL 90 | SDL_GL_hint( flag, value ); 91 | #endif 92 | #ifdef SDL_SHADER_OPENGLES2 93 | SDL_GLES2_hint( flag, value ); 94 | #endif 95 | #ifdef SDL_SHADER_D3D 96 | SDL_D3D_hint( flag, value ); 97 | #endif 98 | #ifdef SDL_SHADER_D3D11 99 | SDL_D3D11_hint( flag, value ); 100 | #endif 101 | } 102 | 103 | SDL_ShaderFileNames SDL_getShaderFileNames( SDL_Renderer* renderer, 104 | const char* name ) 105 | { 106 | 107 | char* vshd_ext; 108 | char* pshd_ext; 109 | int vshd_ext_len=-1; 110 | int pshd_ext_len; 111 | #ifdef SDL_SHADER_OPENGL 112 | char vshd_ext_gl[] = ".gl.vert"; 113 | char pshd_ext_gl[] = ".gl.frag"; 114 | if( strcmp( renderer->info.name, "opengl" )==0 ) { 115 | vshd_ext_len = sizeof( vshd_ext_gl ); 116 | vshd_ext = vshd_ext_gl; 117 | pshd_ext_len = sizeof( pshd_ext_gl ); 118 | pshd_ext = pshd_ext_gl; 119 | } 120 | #endif 121 | #ifdef SDL_SHADER_OPENGLES2 122 | char vshd_ext_gles2[] = ".gles2.vert"; 123 | char pshd_ext_gles2[] = ".gles2.frag"; 124 | if( strcmp( renderer->info.name, "opengles2" )==0 ) { 125 | vshd_ext_len = sizeof( vshd_ext_gles2 ); 126 | vshd_ext = vshd_ext_gles2; 127 | pshd_ext_len = sizeof( pshd_ext_gles2 ); 128 | pshd_ext = pshd_ext_gles2; 129 | } 130 | #endif 131 | #ifdef SDL_SHADER_D3D 132 | char both_shd_ext_d3d[] = ".d3d.hlsl"; /* HLSL uses a single file */ 133 | if( strcmp( renderer->info.name, "direct3d" )==0 ) { 134 | vshd_ext_len = sizeof( both_shd_ext_d3d ); 135 | vshd_ext = both_shd_ext_d3d; 136 | pshd_ext_len = 0; 137 | pshd_ext = NULL; 138 | } 139 | #endif 140 | #ifdef SDL_SHADER_D3D11 141 | 142 | #endif 143 | SDL_ShaderFileNames ret = {NULL,NULL}; 144 | if ( vshd_ext_len == -1 ) { 145 | return ret; 146 | } 147 | int name_len = strlen( name ); 148 | 149 | ret.vshader = malloc(name_len + vshd_ext_len + 1 ); 150 | if( !ret.vshader ){ 151 | SDL_OutOfMemory(); 152 | return ret; 153 | } 154 | if( pshd_ext ) { 155 | ret.pshader = malloc(name_len + pshd_ext_len + 1 ); 156 | if( !ret.pshader ){ 157 | free( ret.vshader ); 158 | ret.vshader = NULL; 159 | SDL_OutOfMemory(); 160 | return ret; 161 | } 162 | } 163 | ret.vshader[ name_len + vshd_ext_len ] = '\0'; 164 | strncpy( ret.vshader, name, name_len ); 165 | strncpy( ret.vshader + name_len, vshd_ext, vshd_ext_len ); 166 | 167 | if( pshd_ext ){ 168 | ret.pshader[ name_len + pshd_ext_len ] = '\0'; 169 | strncpy( ret.pshader, name, name_len ); 170 | strncpy( ret.pshader + name_len, pshd_ext, pshd_ext_len ); 171 | } 172 | 173 | return ret; 174 | } 175 | 176 | SDL_ShaderStream SDL_getShaderStream( SDL_ShaderFileNames* names, int delete ){ 177 | SDL_ShaderStream ret; 178 | ret.vshader = SDL_RWFromFile( names->vshader, "rb" ); 179 | ret.pshader = SDL_RWFromFile( names->pshader, "rb" ); 180 | if( delete ){ 181 | if( names->vshader ) { 182 | free( names->vshader); 183 | } 184 | if( names->pshader ) { 185 | free( names->pshader); 186 | } 187 | } 188 | return ret; 189 | } 190 | 191 | int SDL_destroyShader( SDL_Shader* shader ){ 192 | return shader->destroyShader( shader ); 193 | } 194 | 195 | int SDL_bindShader( SDL_Shader* shader ){ 196 | return shader->bindShader( shader ); 197 | } 198 | void SDL_updateViewport( SDL_Shader *shader ){ 199 | shader->updateViewport( shader ); 200 | } 201 | int SDL_renderVertexBuffer( SDL_Shader* shader, SDL_Vertex *vertices, 202 | SDL_Texture** textures, unsigned num_of_tex) 203 | { 204 | unsigned i; 205 | SDL_Renderer* renderer = shader->renderer; 206 | 207 | if ( num_of_tex==0 ) 208 | return 0; 209 | if ( num_of_tex >= 8 ) 210 | return SDL_SetError("SDL_Shader: only 8 textures are supported"); 211 | for ( i=0; iw != textures[0]->w || 213 | textures[i]->h != textures[0]->h ) && i!=0 ) 214 | { 215 | return SDL_SetError("SDL_Shader: Textures must have the same size"); 216 | } 217 | if (( textures[i]->format != textures[0]->format) && i!=0 ) { 218 | return SDL_SetError("SDL_Shader: Textures must have the same format"); 219 | } 220 | if (renderer != textures[i]->renderer) { 221 | return SDL_SetError("SDL_Shader: Texture was not created with this renderer"); 222 | } 223 | } 224 | 225 | return shader->renderCopyShd( shader, textures, num_of_tex, 226 | vertices, vertices->size ); 227 | } 228 | 229 | int SDL_renderCopyShdArray( SDL_Shader* shader, SDL_Texture** textures, 230 | const SDL_Rect * srcrect, const SDL_Rect * dstrect, unsigned num_of_tex) 231 | { 232 | 233 | SDL_Renderer *renderer = shader->renderer; 234 | 235 | if (renderer->hidden) { 236 | return 0; 237 | } 238 | 239 | SDL_Rect real_srcrect = { 0, 0, 0, 0 }; 240 | SDL_Rect real_dstrect = { 0, 0, 0, 0 }; 241 | unsigned i; 242 | 243 | real_srcrect.x = 0; 244 | real_srcrect.y = 0; 245 | real_srcrect.w = textures[0]->w; 246 | real_srcrect.h = textures[0]->h; 247 | if (srcrect) { 248 | if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) { 249 | return 0; 250 | } 251 | } 252 | 253 | //XXX bug here 254 | //SDL_RenderGetViewport(renderer, &real_dstrect); 255 | if (dstrect) { 256 | /*if (!SDL_HasIntersection(dstrect, &real_dstrect)) { 257 | return 0; 258 | }*/ 259 | real_dstrect = *dstrect; 260 | } 261 | 262 | for( i=0; inative) { 264 | textures[i] = textures[i]->native; 265 | } 266 | } 267 | 268 | /*real_dstrect.x *= renderer->scale.x; 269 | real_dstrect.y *= renderer->scale.y; 270 | real_dstrect.w *= renderer->scale.x; 271 | real_dstrect.h *= renderer->scale.y;*/ 272 | 273 | SDL_Vertex* vertices = shader->createVertexBuffer(4); 274 | 275 | int r,g,b,a; 276 | r = textures[0]->r; 277 | g = textures[0]->g; 278 | b = textures[0]->b; 279 | a = textures[0]->a; 280 | vertices->setVertexColor( vertices, 0,4, r,g,b,a); 281 | 282 | vertices->setVertexPosition( vertices, 0,1, 283 | real_dstrect.x, real_dstrect.y, 0.0f); 284 | vertices->setVertexPosition( vertices, 1,1, 285 | (real_dstrect.x + real_dstrect.w), real_dstrect.y, 0.0f); 286 | vertices->setVertexPosition( vertices, 2,1, 287 | real_dstrect.x, (real_dstrect.y + real_dstrect.h), 0.0f); 288 | vertices->setVertexPosition( vertices, 3,1, 289 | (real_dstrect.x + real_dstrect.w), 290 | (real_dstrect.y + real_dstrect.h), 0.0f); 291 | 292 | float minu, maxu, minv, maxv; 293 | minu = real_srcrect.x / textures[0]->w; 294 | maxu = (real_srcrect.x + real_srcrect.w) / textures[0]->w; 295 | minv = real_srcrect.y / textures[0]->h; 296 | maxv = (real_srcrect.y + real_srcrect.h) / textures[0]->h; 297 | vertices->setVertexTexCoord( vertices, 0,1, minu, minv); 298 | vertices->setVertexTexCoord( vertices, 1,1, maxu, minv); 299 | vertices->setVertexTexCoord( vertices, 2,1, minu, maxv); 300 | vertices->setVertexTexCoord( vertices, 3,1, maxu, maxv); 301 | 302 | int res = SDL_renderVertexBuffer( shader, vertices, textures, num_of_tex ); 303 | shader->destroyVertexBuffer( vertices ); 304 | return res; 305 | } 306 | 307 | 308 | int SDL_renderCopyShd( SDL_Shader* shader, SDL_Texture* texture, 309 | const SDL_Rect * srcrect, const SDL_Rect * dstrect) 310 | { 311 | SDL_Texture *textures[] = {texture}; 312 | return SDL_renderCopyShdArray( shader, textures, srcrect, dstrect, 1 ); 313 | } 314 | 315 | char* SDL_Shader_readRW( SDL_RWops* rwop ) { 316 | Sint64 fsize = rwop->size(rwop); 317 | char *string = malloc(fsize + 1); 318 | if( !string ){ 319 | return string; 320 | } 321 | 322 | rwop->read(rwop, string, fsize, 1); 323 | string[fsize] = '\0'; 324 | 325 | return string; 326 | } 327 | 328 | SDL_Uniform* SDL_createUniform( SDL_Shader* shader, const char* name ) { 329 | return shader->createUniform( shader, name ); 330 | } 331 | int SDL_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ) { 332 | return shader->destroyUniform( shader, uniform ); 333 | } 334 | int SDL_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ) { 335 | return uniform->setUniform_fv( uniform, vector, num ); 336 | } 337 | int SDL_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ) { 338 | return uniform->setUniform_iv( uniform, vector, num ); 339 | } 340 | 341 | int SDL_setUniform_f( SDL_Uniform* uniform, float a ){ 342 | return uniform->setUniform_f( uniform, a ); 343 | } 344 | int SDL_setUniform_f2( SDL_Uniform* uniform, float a, float b ){ 345 | return uniform->setUniform_f2( uniform, a, b ); 346 | } 347 | int SDL_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ){ 348 | return uniform->setUniform_f3( uniform, a, b, c ); 349 | } 350 | int SDL_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ){ 351 | return uniform->setUniform_f4( uniform, a, b, c, d ); 352 | } 353 | 354 | int SDL_setUniform_i ( SDL_Uniform* uniform, int a ){ 355 | return uniform->setUniform_i( uniform, a ); 356 | } 357 | int SDL_setUniform_i2( SDL_Uniform* uniform, int a, int b ){ 358 | return uniform->setUniform_i2( uniform, a, b ); 359 | } 360 | int SDL_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ){ 361 | return uniform->setUniform_i3( uniform, a, b, c ); 362 | } 363 | int SDL_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ){ 364 | return uniform->setUniform_i4( uniform, a, b, c, d ); 365 | } 366 | 367 | -------------------------------------------------------------------------------- /src/d3d/SDL_D3D_shader.c: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #ifdef SDL_SHADER_D3D 23 | 24 | #include 25 | #include 26 | 27 | #define D3D_DEBUG_INFO 28 | #define COBJMACROS 29 | #include 30 | #include 31 | #include 32 | #undef D3D_DEBUG_INFO 33 | #undef COBJMACROS 34 | 35 | #include "../SDL_shader.h" 36 | #include "SDL_D3D_shader.h" 37 | 38 | #include "SDL_D3D_RenderStructs.h" 39 | #include "SDL_D3D_SDL_internals.c" 40 | 41 | #define SAFE_RELEASE(X) if ((X)) { IUnknown_Release(SDL_static_cast(IUnknown*, X)); X = NULL; } 42 | typedef struct { 43 | LPDIRECT3DPIXELSHADER9 pixl_shader; 44 | LPDIRECT3DVERTEXSHADER9 vert_shader; 45 | 46 | LPD3DXCONSTANTTABLE pixl_symtable; 47 | LPD3DXCONSTANTTABLE vert_symtable; 48 | 49 | SDL_Uniform* color; 50 | SDL_Uniform* vport; 51 | SDL_Uniform* color_mode; 52 | } SDL_D3D_ShaderData; 53 | 54 | typedef struct { 55 | LPD3DXCONSTANTTABLE table; 56 | D3DXHANDLE handle; 57 | } SDL_D3D_UniformData; 58 | 59 | typedef Vertex SDL_D3D_Vertex_t; 60 | 61 | void SDL_D3D_getVertex( SDL_Vertex* vertices, unsigned num, 62 | uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a, 63 | float *x, float *y, float *z, 64 | float *tex_s, float *tex_t ) 65 | { 66 | SDL_D3D_Vertex_t* vbuff = (SDL_D3D_Vertex_t*) vertices->vertexBuffer; 67 | DWORD color = vbuff[num].color; 68 | if( a ) (*a) = (uint8_t) ((color & 0xFF000000)>>24); 69 | if( r ) (*r) = (uint8_t) ((color & 0x00FF0000)>>16); 70 | if( g ) (*g) = (uint8_t) ((color & 0x0000FF00)>> 8); 71 | if( b ) (*b) = (uint8_t) (color & 0x000000FF); 72 | 73 | if( x ) (*x) = (vbuff[num].x - 0.5); 74 | if( y ) (*y) = (vbuff[num].y - 0.5); 75 | if( z ) (*z) = (vbuff[num].z - 0.5); 76 | 77 | if( tex_s ) (*tex_s) = (vbuff[num].u ); 78 | if( tex_t ) (*tex_t) = (vbuff[num].v ); 79 | } 80 | 81 | void SDL_D3D_setVertexColor(SDL_Vertex* vertices, unsigned from, unsigned num, 82 | uint8_t r, uint8_t g, uint8_t b, uint8_t a ) 83 | { 84 | SDL_D3D_Vertex_t* vbuff = (SDL_D3D_Vertex_t*) vertices->vertexBuffer; 85 | unsigned i; 86 | for ( i=from; ivertexBuffer; 94 | unsigned i; 95 | for ( i=from; ivertexBuffer; 105 | unsigned i; 106 | for ( i=from; ivertexBuffer = malloc( sizeof(SDL_D3D_Vertex_t)*size ); 118 | if ( !ret->vertexBuffer ) { 119 | free( ret ); 120 | SDL_OutOfMemory(); 121 | return NULL; 122 | } 123 | ret->size = size; 124 | ret->getVertex = SDL_D3D_getVertex; 125 | ret->setVertexColor = SDL_D3D_setVertexColor; 126 | ret->setVertexPosition = SDL_D3D_setVertexPosition; 127 | ret->setVertexTexCoord = SDL_D3D_setVertexTexCoord; 128 | return ret; 129 | } 130 | int SDL_D3D_destroyVertexBuffer( SDL_Vertex* buff ) { 131 | free( buff->vertexBuffer ); 132 | free( buff ); 133 | return 0; 134 | } 135 | 136 | static int vs_version_major = 1; 137 | static int vs_version_minor = 1; 138 | static int ps_version_major = 2; 139 | static int ps_version_minor = 0; 140 | 141 | void SDL_D3D_hint(sdl_shader_hint flag, void* value) { 142 | int tmp; 143 | switch(flag) { 144 | case SDL_D3D_VS_MAJOR_VERSION: 145 | tmp = *((int*)value); 146 | vs_version_major = tmp >= 0 && tmp <= 9 ? tmp : vs_version_major; 147 | break; 148 | case SDL_D3D_VS_MINOR_VERSION: 149 | tmp = *((int*)value); 150 | vs_version_minor = tmp >= 0 && tmp <= 9 ? tmp : vs_version_minor; 151 | break; 152 | case SDL_D3D_PS_MAJOR_VERSION: 153 | tmp = *((int*)value); 154 | ps_version_major = tmp >= 0 && tmp <= 9 ? tmp : ps_version_major; 155 | break; 156 | case SDL_D3D_PS_MINOR_VERSION: 157 | tmp = *((int*)value); 158 | ps_version_minor = tmp >= 0 && tmp <= 9 ? tmp : ps_version_minor; 159 | break; 160 | default: /* nothing */ break; 161 | } 162 | 163 | } 164 | 165 | static int SDL_D3D_setUniform_matrix( SDL_Uniform* uniform, D3DXMATRIX* matix ); 166 | static void SDL_D3D_updateViewport( SDL_Shader* shader ) { 167 | SDL_Renderer* renderer = shader->renderer; 168 | SDL_D3D_ShaderData *shader_data = (SDL_D3D_ShaderData*) shader->driver_data; 169 | int w,h; 170 | SDL_RenderGetLogicalSize( renderer, &w, &h ); 171 | if ( w && h && shader_data->vport ) 172 | { 173 | D3DXMATRIX matrix; 174 | matrix.m[0][0] = 2.0f / w; 175 | matrix.m[1][0] = 0.0f; 176 | matrix.m[2][0] = 0.0f; 177 | matrix.m[3][0] = 0.0f; 178 | 179 | matrix.m[0][1] = 0.0f; 180 | matrix.m[1][1] =-2.0f / h; 181 | matrix.m[2][1] = 0.0f; 182 | matrix.m[3][1] = 0.0f; 183 | 184 | matrix.m[0][2] = 0.0f; 185 | matrix.m[1][2] = 0.0f; 186 | matrix.m[2][2] = 0.0f; 187 | matrix.m[3][2] = 0.0f; 188 | 189 | matrix.m[0][3] =-1.0f; 190 | matrix.m[1][3] = 1.0f; 191 | matrix.m[2][3] = 0.0f; 192 | matrix.m[3][3] = 1.0f; 193 | 194 | SDL_D3D_setUniform_matrix( shader_data->vport, &matrix ); 195 | } 196 | } 197 | 198 | extern char* SDL_Shader_readRW( SDL_RWops* rwop ); 199 | SDL_Shader* SDL_D3D_createShader( SDL_Renderer* renderer, 200 | SDL_ShaderStream* shdstream ) 201 | { 202 | 203 | SDL_Shader *shader; 204 | 205 | shader = (SDL_Shader*) malloc( sizeof(SDL_Shader) ); 206 | if ( !shader ) { 207 | SDL_OutOfMemory(); 208 | return NULL; 209 | } 210 | shader->driver_data = malloc( sizeof(SDL_D3D_ShaderData ) ); 211 | if ( !shader->driver_data ) { 212 | free( shader ); 213 | SDL_OutOfMemory(); 214 | return NULL; 215 | } 216 | SDL_D3D_ShaderData *shader_data = (SDL_D3D_ShaderData*) shader->driver_data; 217 | D3D_RenderData *render_data = (D3D_RenderData*) renderer->driverdata; 218 | 219 | shader->renderer = renderer; 220 | shader->create_shader = SDL_D3D_createShader; 221 | shader->destroyShader = SDL_D3D_destroyShader; 222 | shader->bindShader = SDL_D3D_bindShader; 223 | shader->unbindShader = SDL_D3D_unbindShader; 224 | shader->renderCopyShd = SDL_D3D_renderCopyShd; 225 | shader->updateViewport = SDL_D3D_updateViewport; 226 | 227 | shader->createUniform = SDL_D3D_createUniform; 228 | shader->destroyUniform = SDL_D3D_destroyUniform; 229 | 230 | shader->createVertexBuffer = SDL_D3D_createVertexBuffer; 231 | shader->destroyVertexBuffer = SDL_D3D_destroyVertexBuffer; 232 | 233 | shader_data->pixl_shader = NULL; 234 | shader_data->vert_shader = NULL; 235 | shader_data->pixl_symtable = NULL; 236 | shader_data->vert_symtable = NULL; 237 | 238 | HRESULT result; 239 | 240 | LPD3DXBUFFER code,error; 241 | char shader_version[8] = "vs_x_x"; 242 | 243 | 244 | char* buff = SDL_Shader_readRW( shdstream->vshader ); 245 | if( !buff ){ 246 | SDL_OutOfMemory(); 247 | return NULL; 248 | } 249 | shader_version[3] = '0'+vs_version_major; 250 | shader_version[5] = '0'+vs_version_minor; 251 | result = D3DXCompileShader(buff, strlen(buff), NULL, NULL, "VertexShaderMain", 252 | shader_version, 0, &code, &error, &(shader_data->vert_symtable)); 253 | if ( FAILED(result)){ 254 | SDL_SetError("SDL_Shader: D3D CompilelShader() failed: \n--\n%s--\n", ID3DXBuffer_GetBufferPointer( error )); 255 | SAFE_RELEASE( code ); 256 | SAFE_RELEASE( error ); 257 | return NULL; 258 | } 259 | result = IDirect3DDevice9_CreateVertexShader(render_data->device, 260 | (DWORD*) ID3DXBuffer_GetBufferPointer( code ), &shader_data->vert_shader); 261 | SAFE_RELEASE( code ); 262 | SAFE_RELEASE( error ); 263 | if (FAILED(result)) { 264 | shader->destroyShader(shader); 265 | SDL_SetError("SDL_Shader: D3D CreateVertexShader() failed, errno %d\n", result); 266 | return NULL; 267 | } 268 | shader_version[0] = 'p'; 269 | shader_version[3] = '0'+ps_version_major; 270 | shader_version[5] = '0'+ps_version_minor; 271 | result = D3DXCompileShader(buff, strlen(buff), NULL, NULL, "PixelShaderMain", 272 | shader_version, 0, &code, &error, &(shader_data->pixl_symtable)); 273 | if ( FAILED(result)){ 274 | shader->destroyShader(shader); 275 | SDL_SetError("SDL_Shader: D3D CompilelShader() failed: \n--\n%s--\n", ID3DXBuffer_GetBufferPointer( error )); 276 | SAFE_RELEASE( code ); 277 | SAFE_RELEASE( error ); 278 | return NULL; 279 | } 280 | result = IDirect3DDevice9_CreatePixelShader(render_data->device, 281 | (DWORD*) ID3DXBuffer_GetBufferPointer( code ), &shader_data->pixl_shader); 282 | SAFE_RELEASE( code ); 283 | SAFE_RELEASE( error ); 284 | free( buff ); 285 | if (FAILED(result)) { 286 | shader->destroyShader(shader); 287 | SDL_SetError("SDL_Shader: D3D CreatePixelShader() failed, errno %d\n", result); 288 | return NULL; 289 | } 290 | 291 | shader_data->vport = SDL_createUniform(shader, "world_view_projection"); 292 | 293 | SDL_D3D_updateViewport(shader); 294 | 295 | shader_data->color = SDL_createUniform(shader,"color"); 296 | shader_data->color_mode = SDL_createUniform(shader,"color_mode"); 297 | return shader; 298 | } 299 | 300 | int SDL_D3D_bindShader( SDL_Shader* shader ) { 301 | SDL_D3D_ShaderData *shader_data = (SDL_D3D_ShaderData*) shader->driver_data; 302 | D3D_RenderData *render_data = (D3D_RenderData*) shader->renderer->driverdata; 303 | HRESULT result = IDirect3DDevice9_SetPixelShader(render_data->device, shader_data->pixl_shader); 304 | if (FAILED(result)) { 305 | return SDL_SetError("SDL_Shader: D3D SetPixelShader() failed, errno %d\n", result); 306 | } 307 | 308 | result = IDirect3DDevice9_SetVertexShader(render_data->device, shader_data->vert_shader); 309 | if (FAILED(result)) { 310 | return SDL_SetError("SDL_Shader: D3D SetVertexShader() failed, errno %d\n", result); 311 | } 312 | 313 | return 0; 314 | } 315 | 316 | int SDL_D3D_unbindShader( SDL_Shader* shader ) { 317 | D3D_RenderData *render_data = (D3D_RenderData*) shader->renderer->driverdata; 318 | 319 | HRESULT result = IDirect3DDevice9_SetPixelShader(render_data->device, NULL); 320 | if (FAILED(result)) { 321 | return SDL_SetError("SDL_Shader: D3D SetPixelShader() failed, errno %d\n", result); 322 | } 323 | 324 | result = IDirect3DDevice9_SetVertexShader(render_data->device, NULL); 325 | if (FAILED(result)) { 326 | return SDL_SetError("SDL_Shader: D3D SetVertexShader() failed, errno %d\n", result); 327 | } 328 | 329 | return 0; 330 | } 331 | 332 | int SDL_D3D_destroyShader( SDL_Shader* shader ) { 333 | SDL_D3D_ShaderData *shader_data = (SDL_D3D_ShaderData*) shader->driver_data; 334 | SAFE_RELEASE( shader_data->vert_shader ); 335 | SAFE_RELEASE( shader_data->pixl_shader ); 336 | SAFE_RELEASE( shader_data->vert_symtable ); 337 | SAFE_RELEASE( shader_data->pixl_symtable ); 338 | shader->unbindShader( shader ); 339 | free( shader->driver_data ); 340 | free( shader ); 341 | return 0; 342 | } 343 | 344 | int SDL_D3D_renderCopyShd(SDL_Shader* shader, SDL_Texture** textures, 345 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert) 346 | { 347 | 348 | SDL_Renderer* renderer; 349 | D3D_RenderData *data; 350 | D3D_TextureData *texturedata; 351 | int i; 352 | HRESULT result; 353 | 354 | 355 | renderer = shader->renderer; 356 | data = (D3D_RenderData *) renderer->driverdata; 357 | 358 | if (D3D_ActivateRenderer(renderer) < 0) { 359 | return -1; 360 | } 361 | 362 | for ( i=0; idriverdata; 364 | if (!texturedata) { 365 | return SDL_SetError("SDL_Shader: D3D Texture is not currently available"); 366 | } 367 | if (texturedata->yuv) { 368 | return SDL_SetError("SDL_Shader: D3D YUV-textures not supported\n"); 369 | } 370 | 371 | result = IDirect3DDevice9_SetTexture(data->device, i, 372 | (IDirect3DBaseTexture9 *) texturedata->texture); 373 | if (FAILED(result)) { 374 | return SDL_SetError("SDL_Shader: D3D SetTexture() failed, errno %d\n", result); 375 | } 376 | } 377 | 378 | D3D_SetBlendMode(data, textures[0]->blendMode); 379 | D3D_UpdateTextureScaleMode(data, texturedata, 0); 380 | 381 | int r = shader->bindShader(shader); 382 | if ( r ) return r; 383 | 384 | SDL_D3D_Vertex_t *vbuff = (SDL_D3D_Vertex_t*) vertices->vertexBuffer; 385 | result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLESTRIP, 386 | num_of_vert-2, vbuff, sizeof(Vertex)); 387 | if (FAILED(result)) { 388 | return SDL_SetError("SDL_Shader: D3D DrawPrimitiveUP() " 389 | "failed, errno %d\n", result); 390 | } 391 | r = shader->unbindShader(shader); 392 | if ( r ) return r; 393 | 394 | return 0; 395 | } 396 | 397 | SDL_Uniform* SDL_D3D_createUniformFromHandle( SDL_Shader* shader, D3DXHANDLE handle, LPD3DXCONSTANTTABLE table ) { 398 | // No validation checks, handle must be valid 399 | SDL_Uniform* uniform = (SDL_Uniform*) malloc( sizeof(SDL_Uniform) ); 400 | if ( !uniform ) { 401 | SDL_OutOfMemory(); 402 | return NULL; 403 | } 404 | 405 | uniform->setUniform_iv = SDL_D3D_setUniform_iv; 406 | uniform->setUniform_i = SDL_D3D_setUniform_i; 407 | uniform->setUniform_i2 = SDL_D3D_setUniform_i2; 408 | uniform->setUniform_i3 = SDL_D3D_setUniform_i3; 409 | uniform->setUniform_i4 = SDL_D3D_setUniform_i4; 410 | 411 | uniform->setUniform_fv = SDL_D3D_setUniform_fv; 412 | uniform->setUniform_f = SDL_D3D_setUniform_f; 413 | uniform->setUniform_f2 = SDL_D3D_setUniform_f2; 414 | uniform->setUniform_f3 = SDL_D3D_setUniform_f3; 415 | uniform->setUniform_f4 = SDL_D3D_setUniform_f4; 416 | 417 | SDL_D3D_UniformData* udata = malloc( sizeof(SDL_D3D_UniformData) ); 418 | if ( !udata ) { 419 | free( uniform ); 420 | SDL_OutOfMemory(); 421 | return NULL; 422 | } 423 | udata->handle = handle; 424 | udata->table = table; 425 | uniform->driver_data = udata; 426 | uniform->shader = shader; 427 | return uniform; 428 | } 429 | 430 | SDL_Uniform* SDL_D3D_createUniform( SDL_Shader* shader, const char* name ) { 431 | D3DXHANDLE handle; 432 | SDL_D3D_ShaderData *shader_data = (SDL_D3D_ShaderData*) shader->driver_data; 433 | 434 | shader->bindShader( shader ); 435 | handle = ID3DXConstantTable_GetConstantByName( shader_data->vert_symtable, NULL, name ); 436 | if ( handle == NULL ){ 437 | handle = ID3DXConstantTable_GetConstantByName( shader_data->pixl_symtable, NULL, name ); 438 | if ( handle == NULL ){ 439 | SDL_SetError("SDL_Shader: D3D GetParameterByName() failed\n" ); 440 | return NULL; 441 | } else { 442 | return SDL_D3D_createUniformFromHandle( shader, handle, shader_data->pixl_symtable ); 443 | } 444 | } 445 | return SDL_D3D_createUniformFromHandle( shader, handle, shader_data->vert_symtable ); 446 | } 447 | 448 | int SDL_D3D_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ) { 449 | (void) shader; 450 | free(uniform->driver_data); 451 | free(uniform); 452 | return 0; 453 | } 454 | 455 | int SDL_D3D_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ) { 456 | HRESULT result; 457 | SDL_Renderer* renderer = uniform->shader->renderer; 458 | D3D_RenderData *render_data = (D3D_RenderData*) renderer->driverdata; 459 | uniform->shader->bindShader(uniform->shader); 460 | SDL_D3D_UniformData* udata = (SDL_D3D_UniformData*) uniform->driver_data; 461 | 462 | result = ID3DXConstantTable_SetFloatArray( udata->table, render_data->device, udata->handle, vector, num ); 463 | 464 | if (FAILED(result)){ 465 | return SDL_SetError("SDL_Shader: D3D SetFloatArray() failed, errno %d\n", result); 466 | } 467 | return 0; 468 | } 469 | int SDL_D3D_setUniform_f ( SDL_Uniform* uniform, float a ){ 470 | float tmp[1] = {a}; 471 | return SDL_D3D_setUniform_fv( uniform, tmp, 1); 472 | } 473 | int SDL_D3D_setUniform_f2( SDL_Uniform* uniform, float a, float b ){ 474 | float tmp[2] = {a,b}; 475 | return SDL_D3D_setUniform_fv( uniform, tmp, 2); 476 | 477 | } 478 | int SDL_D3D_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ){ 479 | float tmp[3] = {a,b,c}; 480 | return SDL_D3D_setUniform_fv( uniform, tmp, 3); 481 | 482 | } 483 | int SDL_D3D_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ){ 484 | float tmp[4] = {a,b,c,d}; 485 | return SDL_D3D_setUniform_fv( uniform, tmp, 4); 486 | } 487 | 488 | int SDL_D3D_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ) { 489 | HRESULT result; 490 | SDL_Renderer* renderer = uniform->shader->renderer; 491 | D3D_RenderData *render_data = (D3D_RenderData*) renderer->driverdata; 492 | uniform->shader->bindShader(uniform->shader); 493 | SDL_D3D_UniformData* udata = (SDL_D3D_UniformData*) uniform->driver_data; 494 | 495 | result = ID3DXConstantTable_SetIntArray( udata->table, render_data->device, udata->handle, vector, num ); 496 | 497 | if (FAILED(result)) { 498 | return SDL_SetError("SDL_Shader: D3D SetIntArray() failed, errno %d\n", result); 499 | } 500 | return 0; 501 | } 502 | int SDL_D3D_setUniform_i ( SDL_Uniform* uniform, int a ){ 503 | int tmp[1] = {a}; 504 | return SDL_D3D_setUniform_iv( uniform, tmp, 1); 505 | } 506 | int SDL_D3D_setUniform_i2( SDL_Uniform* uniform, int a, int b ){ 507 | int tmp[2] = {a,b}; 508 | return SDL_D3D_setUniform_iv( uniform, tmp, 2); 509 | } 510 | int SDL_D3D_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ){ 511 | int tmp[3] = {a,b,c}; 512 | return SDL_D3D_setUniform_iv( uniform, tmp, 3); 513 | } 514 | int SDL_D3D_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ){ 515 | int tmp[4] = {a,b,c,d}; 516 | return SDL_D3D_setUniform_iv( uniform, tmp, 4); 517 | } 518 | 519 | static int SDL_D3D_setUniform_matrix( SDL_Uniform* uniform, D3DXMATRIX* matrix ){ 520 | HRESULT result; 521 | SDL_Renderer* renderer = uniform->shader->renderer; 522 | D3D_RenderData *render_data = (D3D_RenderData*) renderer->driverdata; 523 | uniform->shader->bindShader(uniform->shader); 524 | SDL_D3D_UniformData* udata = (SDL_D3D_UniformData*) uniform->driver_data; 525 | 526 | result = ID3DXConstantTable_SetMatrix( udata->table, render_data->device, udata->handle, matrix ); 527 | if( FAILED(result) ){ 528 | return SDL_SetError("SDL_Shader: D3D SetMatrix() failed, errno %d\n", result); 529 | } 530 | return 0; 531 | } 532 | 533 | #else 534 | extern void __suppress_a_warning__(); 535 | #endif /* SDL_SHADER_D3D */ 536 | -------------------------------------------------------------------------------- /src/opengles2/SDL_GLES2_shader.c: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | 23 | #ifdef SDL_SHADER_OPENGLES2 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "../SDL_shader.h" 30 | #include "SDL_GLES2_shader.h" 31 | #include "SDL_GLES2_RenderStructs.h" 32 | 33 | typedef struct { 34 | GLuint p; 35 | 36 | SDL_Uniform* vport; 37 | SDL_Uniform* color_mode; 38 | Uint32 is_target; 39 | } SDL_GLES2_ShaderData; 40 | extern char* SDL_Shader_readRW( SDL_RWops* rwop ); 41 | 42 | typedef struct { 43 | GLuint p; 44 | GLint loc; 45 | } SDL_GLES2_UniformData; 46 | 47 | typedef struct { 48 | float x, y, z; 49 | float r, g, b, a; 50 | float tex_s, tex_t; 51 | } SDL_GLES2_Vertex_t; 52 | 53 | static const float inv255f = 1.0f / 255.0f; 54 | static char texture_names[][32] = {"tex0","tex1","tex2","tex3","tex4","tex5","tex6","tex7"}; 55 | static char color_conv[1024]; 56 | static int is_init = 0; 57 | 58 | void SDL_GLES2_getVertex( SDL_Vertex* vertices, unsigned num, 59 | uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a, 60 | float *x, float *y, float *z, 61 | float *tex_s, float *tex_t ) 62 | { 63 | SDL_GLES2_Vertex_t* vbuff = (SDL_GLES2_Vertex_t*) vertices->vertexBuffer; 64 | if( r ) (*r) = (uint8_t) (vbuff[num].r * 255); 65 | if( g ) (*g) = (uint8_t) (vbuff[num].g * 255); 66 | if( b ) (*b) = (uint8_t) (vbuff[num].b * 255); 67 | if( a ) (*a) = (uint8_t) (vbuff[num].a * 255); 68 | 69 | if( x ) (*x) = (vbuff[num].x ); 70 | if( y ) (*y) = (vbuff[num].y ); 71 | if( z ) (*z) = (vbuff[num].z ); 72 | 73 | if( tex_s ) (*tex_s) = (vbuff[num].tex_s ); 74 | if( tex_t ) (*tex_t) = (vbuff[num].tex_t ); 75 | } 76 | 77 | void SDL_GLES2_setVertexColor(SDL_Vertex* vertices, unsigned from, unsigned num, 78 | uint8_t r, uint8_t g, uint8_t b, uint8_t a ) 79 | { 80 | SDL_GLES2_Vertex_t* vbuff = (SDL_GLES2_Vertex_t*) vertices->vertexBuffer; 81 | unsigned i; 82 | for ( i=from; ivertexBuffer; 93 | unsigned i; 94 | for ( i=from; ivertexBuffer; 104 | unsigned i; 105 | for ( i=from; ivertexBuffer = malloc( sizeof(SDL_GLES2_Vertex_t)*size ); 117 | if ( !ret->vertexBuffer ) { 118 | free( ret ); 119 | SDL_OutOfMemory(); 120 | return NULL; 121 | } 122 | ret->size = size; 123 | ret->getVertex = SDL_GLES2_getVertex; 124 | ret->setVertexColor = SDL_GLES2_setVertexColor; 125 | ret->setVertexPosition = SDL_GLES2_setVertexPosition; 126 | ret->setVertexTexCoord = SDL_GLES2_setVertexTexCoord; 127 | return ret; 128 | } 129 | int SDL_GLES2_destroyVertexBuffer( SDL_Vertex* buff ) { 130 | free( buff->vertexBuffer ); 131 | free( buff ); 132 | return 0; 133 | } 134 | 135 | 136 | void SDL_GLES2_init() { 137 | char color_conv_fmt[] = 138 | "vec4 convertColor(int type, vec4 color) {\n" 139 | " vec4 ret = vec4(color.b, color.g, color.r, color.a);\n" 140 | " if( type==%d || /*ABGR4444*/\n" //BGR formats 141 | " type==%d || /*BGR555*/\n" 142 | " type==%d || /*BGRA4444*/\n" 143 | " type==%d || /*ABGR1555*/\n" 144 | " type==%d || /*BGRA5551*/\n" 145 | " type==%d || /*BGR565*/\n" 146 | " type==%d || /*BGR24*/\n" 147 | " type==%d || /*BGR888*/\n" 148 | " type==%d || /*BGRX8888*/\n" 149 | " type==%d || /*ABGR8888*/\n" 150 | " type==%d /*BGRA8888*/\n" 151 | " ){\n" 152 | " ret.r = color.r;\n" 153 | " ret.b = color.b;\n" 154 | " }\n" 155 | " if( type==%d || /*BGR555*/\n" //Formats without alpha 156 | " type==%d || /*BGR565*/\n" 157 | " type==%d || /*BGR24*/\n" 158 | " type==%d || /*BGR888*/\n" 159 | " type==%d || /*RGB332*/\n" 160 | " type==%d || /*RGB444*/\n" 161 | " type==%d || /*RGB555*/\n" 162 | " type==%d || /*RGB565*/\n" 163 | " type==%d || /*RGB24*/\n" 164 | " type==%d /*RGB888*/\n" 165 | " ){\n" 166 | " ret.a = 1.0;\n" 167 | " }\n" 168 | " return ret;\n" 169 | "}\n"; 170 | 171 | snprintf(color_conv, 1024, color_conv_fmt, 172 | SDL_PIXELFORMAT_ABGR4444, 173 | SDL_PIXELFORMAT_BGR555, 174 | SDL_PIXELFORMAT_BGRA4444, 175 | SDL_PIXELFORMAT_ABGR1555, 176 | SDL_PIXELFORMAT_BGRA5551, 177 | SDL_PIXELFORMAT_BGR565, 178 | SDL_PIXELFORMAT_BGR24, 179 | SDL_PIXELFORMAT_BGR888, 180 | SDL_PIXELFORMAT_BGRX8888, 181 | SDL_PIXELFORMAT_ABGR8888, 182 | SDL_PIXELFORMAT_BGRA8888, 183 | 184 | SDL_PIXELFORMAT_BGR555, 185 | SDL_PIXELFORMAT_BGR565, 186 | SDL_PIXELFORMAT_BGR24, 187 | SDL_PIXELFORMAT_BGR888, 188 | SDL_PIXELFORMAT_RGB332, 189 | SDL_PIXELFORMAT_RGB444, 190 | SDL_PIXELFORMAT_RGB555, 191 | SDL_PIXELFORMAT_RGB565, 192 | SDL_PIXELFORMAT_RGB24, 193 | SDL_PIXELFORMAT_RGB888 194 | ); 195 | is_init = 1; 196 | } 197 | 198 | void SDL_GLES2_hint(sdl_shader_hint flag, void* value) { 199 | switch(flag) { 200 | case SDL_GL_TEX0_NAME: strncpy( texture_names[0], (char*) value, 32 ); break; 201 | case SDL_GL_TEX1_NAME: strncpy( texture_names[1], (char*) value, 32 ); break; 202 | case SDL_GL_TEX2_NAME: strncpy( texture_names[2], (char*) value, 32 ); break; 203 | case SDL_GL_TEX3_NAME: strncpy( texture_names[3], (char*) value, 32 ); break; 204 | case SDL_GL_TEX4_NAME: strncpy( texture_names[4], (char*) value, 32 ); break; 205 | case SDL_GL_TEX5_NAME: strncpy( texture_names[5], (char*) value, 32 ); break; 206 | case SDL_GL_TEX6_NAME: strncpy( texture_names[6], (char*) value, 32 ); break; 207 | case SDL_GL_TEX7_NAME: strncpy( texture_names[7], (char*) value, 32 ); break; 208 | default: /* nothing */ break; 209 | } 210 | } 211 | 212 | int SDL_GLES2_CompileSuccessful(int obj) { 213 | int status; 214 | glGetShaderiv(obj, GL_COMPILE_STATUS, &status); 215 | return status == GL_TRUE; 216 | } 217 | 218 | int SDL_GLES2_LinkSuccessful(int obj) { 219 | int status; 220 | glGetProgramiv(obj, GL_LINK_STATUS, &status); 221 | return status == GL_TRUE; 222 | } 223 | 224 | int SDL_GLES2_setUniform_matrix( SDL_Uniform* uniform, GLfloat* mat ); 225 | static void SDL_GLES2_updateViewport( SDL_Shader* shader ) { 226 | SDL_Renderer* renderer = shader->renderer; 227 | SDL_GLES2_ShaderData *shader_data = (SDL_GLES2_ShaderData*) shader->driver_data; 228 | int w,h; 229 | SDL_RenderGetLogicalSize( renderer, &w, &h ); 230 | if ( w && h && shader_data->vport ) { 231 | GLfloat projection[4][4]; 232 | /* Prepare an orthographic projection */ 233 | projection[0][0] = 2.0f / w; 234 | projection[0][1] = 0.0f; 235 | projection[0][2] = 0.0f; 236 | projection[0][3] = 0.0f; 237 | projection[1][0] = 0.0f; 238 | if (renderer->target) { 239 | projection[1][1] = 2.0f / h; 240 | } else { 241 | projection[1][1] = -2.0f / h; 242 | } 243 | projection[1][2] = 0.0f; 244 | projection[1][3] = 0.0f; 245 | projection[2][0] = 0.0f; 246 | projection[2][1] = 0.0f; 247 | projection[2][2] = 0.0f; 248 | projection[2][3] = 0.0f; 249 | projection[3][0] = -1.0f; 250 | if (renderer->target) { 251 | projection[3][1] = -1.0f; 252 | } else { 253 | projection[3][1] = 1.0f; 254 | } 255 | projection[3][2] = 0.0f; 256 | projection[3][3] = 1.0f; 257 | SDL_GLES2_setUniform_matrix( shader_data->vport, (GLfloat*) projection ); 258 | } 259 | } 260 | 261 | 262 | SDL_Shader* SDL_GLES2_createShader( SDL_Renderer* renderer, 263 | SDL_ShaderStream *shdstream ) 264 | { 265 | 266 | SDL_Shader *shader; 267 | GLuint v; 268 | GLuint f; 269 | char *vs,*fs; 270 | 271 | if( !is_init ){ 272 | SDL_GLES2_init(); 273 | } 274 | 275 | shader = (SDL_Shader*) malloc( sizeof(SDL_Shader) ); 276 | if ( !shader ) { 277 | SDL_OutOfMemory(); 278 | return NULL; 279 | } 280 | shader->driver_data = malloc( sizeof(SDL_GLES2_ShaderData ) ); 281 | if ( !shader->driver_data ) { 282 | free( shader ); 283 | SDL_OutOfMemory(); 284 | return NULL; 285 | } 286 | SDL_GLES2_ShaderData *shader_data = (SDL_GLES2_ShaderData*) shader->driver_data; 287 | 288 | shader->renderer = renderer; 289 | shader->create_shader = SDL_GLES2_createShader; 290 | shader->destroyShader = SDL_GLES2_destroyShader; 291 | shader->bindShader = SDL_GLES2_bindShader; 292 | shader->unbindShader = SDL_GLES2_unbindShader; 293 | shader->renderCopyShd = SDL_GLES2_renderCopyShd; 294 | shader->updateViewport = SDL_GLES2_updateViewport; 295 | 296 | shader->createUniform = SDL_GLES2_createUniform; 297 | shader->destroyUniform = SDL_GLES2_destroyUniform; 298 | 299 | shader->createVertexBuffer = SDL_GLES2_createVertexBuffer; 300 | shader->destroyVertexBuffer = SDL_GLES2_destroyVertexBuffer; 301 | 302 | shader_data->p = 0; 303 | v = glCreateShader( GL_VERTEX_SHADER ); 304 | f = glCreateShader( GL_FRAGMENT_SHADER ); 305 | shader_data->is_target = renderer->target != NULL; 306 | shader_data->vport = NULL; 307 | shader_data->color_mode = NULL; 308 | 309 | vs = SDL_Shader_readRW( shdstream->vshader ); 310 | if( vs==NULL ){ 311 | shader->destroyShader(shader); 312 | SDL_OutOfMemory(); 313 | return NULL; 314 | } 315 | 316 | fs = SDL_Shader_readRW( shdstream->pshader ); 317 | if( fs==NULL ){ 318 | free(fs); 319 | shader->destroyShader(shader); 320 | SDL_OutOfMemory(); 321 | return NULL; 322 | } 323 | 324 | const char* vss = (const char*) vs; 325 | const char *fss[] = {fs, color_conv}; 326 | glShaderSource(v, 1, &vss,NULL); 327 | glShaderSource(f, 2, fss,NULL); 328 | 329 | free(vs); 330 | free(fs); 331 | 332 | glCompileShader( v ); 333 | if( !SDL_GLES2_CompileSuccessful(v) ){ 334 | 335 | GLchar buff[512]; 336 | GLsizei len; 337 | glGetShaderInfoLog(v, 512, &len, buff); 338 | 339 | SDL_SetError("SDL_Shader[GLES2]: Could not compile vertex shader: \n-------\n%s\n-------\n", buff ); 340 | shader->destroyShader(shader); 341 | return NULL; 342 | } 343 | glCompileShader( f ); 344 | if( !SDL_GLES2_CompileSuccessful(f) ){ 345 | 346 | GLchar buff[512]; 347 | GLsizei len; 348 | glGetShaderInfoLog(f, 512, &len, buff); 349 | 350 | SDL_SetError("SDL_Shader[GLES2]: Could not compile fragment shader: \n-------\n%s\n-------\n", buff ); 351 | shader->destroyShader(shader); 352 | return NULL; 353 | } 354 | 355 | shader_data->p = glCreateProgram(); 356 | 357 | glAttachShader( shader_data->p,v ); 358 | glAttachShader( shader_data->p,f ); 359 | 360 | glBindAttribLocation( shader_data->p, GLES2_ATTRIBUTE_POSITION, "position"); 361 | glBindAttribLocation( shader_data->p, GLES2_ATTRIBUTE_TEXCOORD, "texCoords"); 362 | glBindAttribLocation( shader_data->p, GLES2_ATTRIBUTE_COLOR, "color"); 363 | 364 | glLinkProgram( shader_data->p ); 365 | if( !SDL_GLES2_LinkSuccessful(shader_data->p) ){ 366 | 367 | GLchar buff[512]; 368 | GLsizei len; 369 | glGetProgramInfoLog(shader_data->p, 512, &len, buff); 370 | 371 | SDL_SetError("SDL_Shader[GLES2]: OpenGL Could not link shader: \n-------\n%s\n-------\n", buff ); 372 | shader->destroyShader(shader); 373 | return NULL; 374 | } 375 | 376 | shader->bindShader(shader); 377 | 378 | glDeleteShader( v ); 379 | glDeleteShader( f ); 380 | 381 | shader_data->vport = SDL_createUniform(shader,"world_view_projection"); 382 | SDL_GLES2_updateViewport( shader ); 383 | 384 | shader_data->color_mode = SDL_createUniform(shader,"color_mode"); 385 | glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION); 386 | glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD); 387 | glEnableVertexAttribArray(GLES2_ATTRIBUTE_COLOR); 388 | 389 | for( int i=0; i<8; i++ ) { 390 | SDL_Uniform* texN; 391 | texN = SDL_createUniform( shader,texture_names[i] ); 392 | if( texN ) { 393 | SDL_GLES2_setUniform_i( texN,i ); 394 | SDL_destroyUniform( shader, texN ); 395 | } 396 | } 397 | 398 | return shader; 399 | } 400 | 401 | int SDL_GLES2_bindShader( SDL_Shader* shader ) { 402 | SDL_GLES2_ShaderData *shader_data = (SDL_GLES2_ShaderData*) shader->driver_data; 403 | glUseProgram( shader_data->p ); 404 | return glGetError(); 405 | } 406 | 407 | int SDL_GLES2_unbindShader( SDL_Shader* shader ) { 408 | GLES2_DriverContext *data = (GLES2_DriverContext *) shader->renderer->driverdata; 409 | if ( data->current_program ) 410 | glUseProgram( data->current_program->id ); 411 | else 412 | glUseProgram( 0 ); 413 | 414 | return glGetError(); 415 | } 416 | 417 | int SDL_GLES2_destroyShader( SDL_Shader* shader ) { 418 | SDL_GLES2_ShaderData *shader_data = (SDL_GLES2_ShaderData*) shader->driver_data; 419 | if (shader_data->color_mode ) 420 | shader->destroyUniform( shader, shader_data->color_mode ); 421 | if( shader_data->vport ) 422 | shader->destroyUniform( shader, shader_data->vport ); 423 | if ( shader_data->p ) 424 | glDeleteShader( shader_data->p ); 425 | free( shader->driver_data ); 426 | free( shader ); 427 | return 0; 428 | } 429 | 430 | 431 | int SDL_GLES2_renderCopyShd(SDL_Shader* shader, SDL_Texture** textures, 432 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert) 433 | { 434 | GLES2_DriverContext *data; 435 | SDL_GLES2_ShaderData *shader_data; 436 | int i; 437 | 438 | data = (GLES2_DriverContext *) shader->renderer->driverdata; 439 | shader_data = (SDL_GLES2_ShaderData*) shader->driver_data; 440 | 441 | SDL_GL_MakeCurrent(shader->renderer->window, data->context); 442 | 443 | shader->bindShader(shader); 444 | if ( (shader->renderer->target == NULL && shader_data->is_target) || 445 | (shader->renderer->target != NULL && !shader_data->is_target) ) { 446 | SDL_GLES2_updateViewport(shader); 447 | shader_data->is_target = shader->renderer->target != NULL; 448 | } 449 | 450 | for ( i=num_of_tex; i<8; i++ ) { 451 | data->glActiveTexture( GL_TEXTURE0 + i ); 452 | glDisable(GL_TEXTURE_2D); 453 | } 454 | 455 | for ( i=0; iglActiveTexture( GL_TEXTURE0 + i ); 457 | SDL_GL_BindTexture(textures[i], NULL, NULL); 458 | glEnable(GL_TEXTURE_2D); 459 | } 460 | 461 | if ( shader_data->is_target ) { 462 | if ( shader_data->color_mode ) { 463 | SDL_GLES2_setUniform_i( shader_data->color_mode, shader->renderer->target->format); 464 | } 465 | } else { 466 | if ( shader_data->color_mode ) { 467 | SDL_GLES2_setUniform_i( shader_data->color_mode, textures[0]->format); 468 | } 469 | } 470 | 471 | if (textures[0]->blendMode != data->current.blendMode) { 472 | switch (textures[0]->blendMode) { 473 | case SDL_BLENDMODE_NONE: 474 | //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 475 | glDisable(GL_BLEND); 476 | break; 477 | case SDL_BLENDMODE_BLEND: 478 | //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 479 | glEnable(GL_BLEND); 480 | glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 481 | break; 482 | case SDL_BLENDMODE_ADD: 483 | //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 484 | glEnable(GL_BLEND); 485 | glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE); 486 | break; 487 | case SDL_BLENDMODE_MOD: 488 | //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 489 | glEnable(GL_BLEND); 490 | glBlendFuncSeparate(GL_ZERO, GL_SRC_COLOR, GL_ZERO, GL_ONE); 491 | break; 492 | } 493 | data->current.blendMode = textures[0]->blendMode; 494 | } 495 | if( !data->current.tex_coords ){ 496 | data->glEnableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD); 497 | data->current.tex_coords = SDL_TRUE; 498 | } 499 | 500 | SDL_GLES2_Vertex_t *vbuff = (SDL_GLES2_Vertex_t*) vertices->vertexBuffer; 501 | data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 502 | sizeof(SDL_GLES2_Vertex_t), vbuff); 503 | data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 504 | sizeof(SDL_GLES2_Vertex_t), &(vbuff[0].tex_s) ); 505 | data->glVertexAttribPointer(GLES2_ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, 506 | sizeof(SDL_GLES2_Vertex_t), &(vbuff[0].r) ); 507 | 508 | data->glDrawArrays(GL_TRIANGLE_STRIP, 0, num_of_vert); 509 | 510 | shader->unbindShader(shader); 511 | 512 | return glGetError(); 513 | } 514 | 515 | SDL_Uniform* SDL_GLES2_createUniform( SDL_Shader* shader, const char* name ) { 516 | GLint loc; 517 | SDL_GLES2_ShaderData* shader_data = (SDL_GLES2_ShaderData*) shader->driver_data; 518 | loc = glGetUniformLocation(shader_data->p, name); 519 | if( loc == -1 ) 520 | return NULL; 521 | 522 | SDL_Uniform* uniform = (SDL_Uniform*) malloc( sizeof(SDL_Uniform) ); 523 | if ( !uniform ) { 524 | SDL_OutOfMemory(); 525 | return NULL; 526 | } 527 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*) malloc( sizeof(SDL_GLES2_UniformData) ); 528 | if ( !udata ) { 529 | free( uniform ); 530 | SDL_OutOfMemory(); 531 | return NULL; 532 | } 533 | 534 | uniform->shader = shader; 535 | 536 | uniform->setUniform_iv = SDL_GLES2_setUniform_iv; 537 | uniform->setUniform_i = SDL_GLES2_setUniform_i; 538 | uniform->setUniform_i2 = SDL_GLES2_setUniform_i2; 539 | uniform->setUniform_i3 = SDL_GLES2_setUniform_i3; 540 | uniform->setUniform_i4 = SDL_GLES2_setUniform_i4; 541 | 542 | uniform->setUniform_fv = SDL_GLES2_setUniform_fv; 543 | uniform->setUniform_f = SDL_GLES2_setUniform_f; 544 | uniform->setUniform_f2 = SDL_GLES2_setUniform_f2; 545 | uniform->setUniform_f3 = SDL_GLES2_setUniform_f3; 546 | uniform->setUniform_f4 = SDL_GLES2_setUniform_f4; 547 | 548 | udata->p = shader_data->p; 549 | udata->loc = loc; 550 | 551 | uniform->driver_data = udata; 552 | return uniform; 553 | 554 | } 555 | int SDL_GLES2_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ){ 556 | if( !uniform ) 557 | return 0; 558 | free(uniform->driver_data); 559 | free(uniform); 560 | return 0; 561 | } 562 | 563 | int SDL_GLES2_setUniform_matrix( SDL_Uniform* uniform, GLfloat* mat ) { 564 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 565 | glUniformMatrix4fv( udata->loc, 1, GL_FALSE, mat ); 566 | return glGetError(); 567 | } 568 | 569 | int SDL_GLES2_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ) { 570 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 571 | glUniform1fv( udata->loc, num, vector); 572 | return glGetError(); 573 | } 574 | int SDL_GLES2_setUniform_f( SDL_Uniform* uniform, float a ) { 575 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 576 | glUniform1f(udata->loc, a ); 577 | return glGetError(); 578 | } 579 | int SDL_GLES2_setUniform_f2( SDL_Uniform* uniform, float a, float b ){ 580 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 581 | glUniform2f(udata->loc, a,b ); 582 | return glGetError(); 583 | } 584 | int SDL_GLES2_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ){ 585 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 586 | glUniform3f(udata->loc, a,b,c ); 587 | return glGetError(); 588 | } 589 | int SDL_GLES2_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ){ 590 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 591 | glUniform4f(udata->loc, a,b,c,d ); 592 | return glGetError(); 593 | } 594 | 595 | 596 | int SDL_GLES2_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ) { 597 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 598 | glUniform1iv( udata->loc, num, vector); 599 | return glGetError(); 600 | } 601 | int SDL_GLES2_setUniform_i( SDL_Uniform* uniform, int a ) { 602 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 603 | glUniform1i(udata->loc, a ); 604 | return glGetError(); 605 | } 606 | int SDL_GLES2_setUniform_i2( SDL_Uniform* uniform, int a, int b ){ 607 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 608 | glUniform2i(udata->loc, a,b ); 609 | return glGetError(); 610 | } 611 | int SDL_GLES2_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ){ 612 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 613 | glUniform3i(udata->loc, a,b,c ); 614 | return glGetError(); 615 | } 616 | int SDL_GLES2_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ){ 617 | SDL_GLES2_UniformData* udata = (SDL_GLES2_UniformData*)uniform->driver_data; 618 | glUniform4i(udata->loc, a,b,c,d ); 619 | return glGetError(); 620 | } 621 | 622 | #else 623 | extern void __suppress_a_warning__(); 624 | #endif /* SDL_SHADER_OPENGLES2 */ 625 | -------------------------------------------------------------------------------- /src/opengl/SDL_GL_shader.c: -------------------------------------------------------------------------------- 1 | /* 2 | SDL2 shader 3 | Copyright (C) 2014 Stefan Krulj (powertomato) 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 | 22 | #ifdef SDL_SHADER_OPENGL 23 | 24 | #include 25 | #ifdef _WIN32 26 | #include 27 | #include 28 | 29 | #define SDL_PROC(ret,func,params) typedef ret (APIENTRY * func##_type) params; func##_type func; 30 | SDL_PROC(void, glUniform1f, (GLint, GLfloat) ) 31 | SDL_PROC(void, glUniform2f, (GLint, GLfloat, GLfloat) ) 32 | SDL_PROC(void, glUniform3f, (GLint, GLfloat, GLfloat, GLfloat) ) 33 | SDL_PROC(void, glUniform4f, (GLint, GLfloat, GLfloat, GLfloat, GLfloat) ) 34 | SDL_PROC(void, glUniform1fv, (GLint, GLsizei, const GLfloat*) ) 35 | 36 | SDL_PROC(void, glUniform1i, (GLint, GLint) ) 37 | SDL_PROC(void, glUniform2i, (GLint, GLint, GLint) ) 38 | SDL_PROC(void, glUniform3i, (GLint, GLint, GLint, GLint) ) 39 | SDL_PROC(void, glUniform4i, (GLint, GLint, GLint, GLint, GLint) ) 40 | SDL_PROC(void, glUniform1iv, (GLint, GLsizei, const GLint*) ) 41 | SDL_PROC(void, glGetShaderiv, (GLuint, GLenum, GLint*) ) 42 | SDL_PROC(void, glGetProgramiv, (GLuint, GLenum, GLint*) ) 43 | SDL_PROC(GLuint, glCreateShader, (GLenum) ) 44 | SDL_PROC(void, glShaderSource, (GLuint, GLsizei, const GLchar* const*, const GLint*) ) 45 | SDL_PROC(void, glCompileShader, (GLuint) ) 46 | SDL_PROC(void, glGetShaderInfoLog, (GLuint, GLsizei, GLsizei*, GLchar*) ) 47 | SDL_PROC(void, glBindAttribLocation, (GLuint, GLuint, const GLchar*) ) 48 | SDL_PROC(GLuint, glCreateProgram, (void) ) 49 | SDL_PROC(void, glAttachShader, (GLuint, GLuint) ) 50 | SDL_PROC(void, glLinkProgram, (GLuint) ) 51 | SDL_PROC(void, glGetProgramInfoLog, (GLuint, GLsizei, GLsizei*, GLchar*) ) 52 | SDL_PROC(void, glDeleteShader, (GLuint) ) 53 | SDL_PROC(void, glEnableVertexAttribArray, (GLuint) ) 54 | SDL_PROC(void, glVertexAttribPointer, (GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid* ) ) 55 | SDL_PROC(void, glUniformMatrix4fv, (GLint, GLsizei, GLboolean, const GLfloat*) ) 56 | SDL_PROC(GLint, glGetUniformLocation, (GLuint, const GLchar*) ) 57 | SDL_PROC(void, glUseProgram, (GLuint) ) 58 | SDL_PROC(void, glActiveTextureARB, (GLenum) ) 59 | #undef SDL_PROC 60 | 61 | #undef GL_GLEXT_PROTOTYPES 62 | #endif 63 | #include 64 | #include 65 | 66 | #ifdef __MACOSX__ 67 | #include 68 | #else 69 | #include 70 | #endif 71 | 72 | #include "../SDL_shader.h" 73 | #include "SDL_GL_shader.h" 74 | #include "SDL_GL_RenderStructs.h" 75 | 76 | static const float inv255f = 1.0f / 255.0f; 77 | static char texture_names[][32] = {"tex0","tex1","tex2","tex3","tex4","tex5","tex6","tex7"}; 78 | 79 | typedef struct { 80 | GLuint p; 81 | 82 | SDL_Uniform* vport; 83 | SDL_Uniform* color_mode; 84 | Uint32 is_target; 85 | } SDL_GL_ShaderData; 86 | 87 | typedef struct { 88 | float x, y, z; 89 | float r, g, b, a; 90 | float tex_s, tex_t; 91 | } SDL_GL_Vertex_t; 92 | 93 | extern char* SDL_Shader_readRW( SDL_RWops* rwop ); 94 | 95 | typedef struct { 96 | GLuint p; 97 | GLint loc; 98 | } SDL_GL_UniformData; 99 | 100 | static char color_conv[1024]; 101 | static int is_init = 0; 102 | 103 | void SDL_GL_getVertex( SDL_Vertex* vertices, unsigned num, 104 | uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a, 105 | float *x, float *y, float *z, 106 | float *tex_s, float *tex_t ) 107 | { 108 | SDL_GL_Vertex_t* vbuff = (SDL_GL_Vertex_t*) vertices->vertexBuffer; 109 | if( r ) (*r) = (uint8_t) (vbuff[num].r * 255); 110 | if( g ) (*g) = (uint8_t) (vbuff[num].g * 255); 111 | if( b ) (*b) = (uint8_t) (vbuff[num].b * 255); 112 | if( a ) (*a) = (uint8_t) (vbuff[num].a * 255); 113 | 114 | if( x ) (*x) = (vbuff[num].x ); 115 | if( y ) (*y) = (vbuff[num].y ); 116 | if( z ) (*z) = (vbuff[num].z ); 117 | 118 | if( tex_s ) (*tex_s) = (vbuff[num].tex_s ); 119 | if( tex_t ) (*tex_t) = (vbuff[num].tex_t ); 120 | } 121 | 122 | void SDL_GL_setVertexColor(SDL_Vertex* vertices, unsigned from, unsigned num, 123 | uint8_t r, uint8_t g, uint8_t b, uint8_t a ) 124 | { 125 | SDL_GL_Vertex_t* vbuff = (SDL_GL_Vertex_t*) vertices->vertexBuffer; 126 | unsigned i; 127 | for ( i=from; ivertexBuffer; 138 | unsigned i; 139 | for ( i=from; ivertexBuffer; 149 | unsigned i; 150 | for ( i=from; ivertexBuffer = malloc( sizeof(SDL_GL_Vertex_t)*size ); 162 | if ( !ret->vertexBuffer ) { 163 | free( ret ); 164 | SDL_OutOfMemory(); 165 | return NULL; 166 | } 167 | ret->size = size; 168 | ret->getVertex = SDL_GL_getVertex; 169 | ret->setVertexColor = SDL_GL_setVertexColor; 170 | ret->setVertexPosition = SDL_GL_setVertexPosition; 171 | ret->setVertexTexCoord = SDL_GL_setVertexTexCoord; 172 | return ret; 173 | } 174 | int SDL_GL_destroyVertexBuffer( SDL_Vertex* buff ) { 175 | free( buff->vertexBuffer ); 176 | free( buff ); 177 | return 0; 178 | } 179 | 180 | 181 | void SDL_GL_init() { 182 | char color_conv_fmt[] = 183 | "vec4 convertColor(int type, vec4 color) {\n" 184 | " vec4 ret = color;\n" 185 | " if( type==%d || /*ABGR4444*/\n" /*BGR formats*/ 186 | " type==%d || /*BGR555*/\n" 187 | " type==%d || /*BGRA4444*/\n" 188 | " type==%d || /*ABGR1555*/\n" 189 | " type==%d || /*BGRA5551*/\n" 190 | " type==%d || /*BGR565*/\n" 191 | " type==%d || /*BGR24*/\n" 192 | " type==%d || /*BGR888*/\n" 193 | " type==%d || /*BGRX8888*/\n" 194 | " type==%d || /*ABGR8888*/\n" 195 | " type==%d /*BGRA8888*/\n" 196 | " ){\n" 197 | " ret.r = color.b;\n" 198 | " ret.b = color.r;\n" 199 | " }\n" 200 | " if( type==%d || /*BGR555*/\n" /*Formats without alpha*/ 201 | " type==%d || /*BGR565*/\n" 202 | " type==%d || /*BGR24*/\n" 203 | " type==%d || /*BGR888*/\n" 204 | " type==%d || /*RGB332*/\n" 205 | " type==%d || /*RGB444*/\n" 206 | " type==%d || /*RGB555*/\n" 207 | " type==%d || /*RGB565*/\n" 208 | " type==%d || /*RGB24*/\n" 209 | " type==%d /*RGB888*/\n" 210 | " ){\n" 211 | " ret.a = 1.0;\n" 212 | " }\n" 213 | " return ret;\n" 214 | "}\n"; 215 | 216 | snprintf(color_conv, 1024, color_conv_fmt, 217 | SDL_PIXELFORMAT_ABGR4444, 218 | SDL_PIXELFORMAT_BGR555, 219 | SDL_PIXELFORMAT_BGRA4444, 220 | SDL_PIXELFORMAT_ABGR1555, 221 | SDL_PIXELFORMAT_BGRA5551, 222 | SDL_PIXELFORMAT_BGR565, 223 | SDL_PIXELFORMAT_BGR24, 224 | SDL_PIXELFORMAT_BGR888, 225 | SDL_PIXELFORMAT_BGRX8888, 226 | SDL_PIXELFORMAT_ABGR8888, 227 | SDL_PIXELFORMAT_BGRA8888, 228 | 229 | SDL_PIXELFORMAT_BGR555, 230 | SDL_PIXELFORMAT_BGR565, 231 | SDL_PIXELFORMAT_BGR24, 232 | SDL_PIXELFORMAT_BGR888, 233 | SDL_PIXELFORMAT_RGB332, 234 | SDL_PIXELFORMAT_RGB444, 235 | SDL_PIXELFORMAT_RGB555, 236 | SDL_PIXELFORMAT_RGB565, 237 | SDL_PIXELFORMAT_RGB24, 238 | SDL_PIXELFORMAT_RGB888 239 | ); 240 | is_init = 1; 241 | 242 | } 243 | 244 | void SDL_GL_hint(sdl_shader_hint flag, void* value) { 245 | switch(flag) { 246 | case SDL_GL_TEX0_NAME: strncpy( texture_names[0], (char*) value, 32 ); break; 247 | case SDL_GL_TEX1_NAME: strncpy( texture_names[1], (char*) value, 32 ); break; 248 | case SDL_GL_TEX2_NAME: strncpy( texture_names[2], (char*) value, 32 ); break; 249 | case SDL_GL_TEX3_NAME: strncpy( texture_names[3], (char*) value, 32 ); break; 250 | case SDL_GL_TEX4_NAME: strncpy( texture_names[4], (char*) value, 32 ); break; 251 | case SDL_GL_TEX5_NAME: strncpy( texture_names[5], (char*) value, 32 ); break; 252 | case SDL_GL_TEX6_NAME: strncpy( texture_names[6], (char*) value, 32 ); break; 253 | case SDL_GL_TEX7_NAME: strncpy( texture_names[7], (char*) value, 32 ); break; 254 | default: /* nothing */ break; 255 | } 256 | } 257 | 258 | static int SDL_GL_CompileSuccessful(int obj) { 259 | int status; 260 | glGetShaderiv(obj, GL_COMPILE_STATUS, &status); 261 | return status == GL_TRUE; 262 | } 263 | 264 | static int SDL_GL_LinkSuccessful(int obj) { 265 | int status; 266 | glGetProgramiv(obj, GL_LINK_STATUS, &status); 267 | return status == GL_TRUE; 268 | } 269 | 270 | int SDL_GL_setUniform_matrix( SDL_Uniform* uniform, GLfloat* mat ); 271 | static void SDL_GL_updateViewport( SDL_Shader* shader ) { 272 | SDL_Renderer* renderer = shader->renderer; 273 | SDL_GL_ShaderData *shader_data = (SDL_GL_ShaderData*) shader->driver_data; 274 | int w,h; 275 | SDL_RenderGetLogicalSize( renderer, &w, &h ); 276 | if ( w && h && shader_data->vport ) { 277 | GLfloat projection[4][4]; 278 | /* Prepare an orthographic projection */ 279 | projection[0][0] = 2.0f / w; 280 | projection[0][1] = 0.0f; 281 | projection[0][2] = 0.0f; 282 | projection[0][3] = 0.0f; 283 | 284 | projection[1][0] = 0.0f; 285 | if (renderer->target) { 286 | projection[1][1] = 2.0f / h; 287 | } else { 288 | projection[1][1] = -2.0f / h; 289 | } 290 | projection[1][2] = 0.0f; 291 | projection[1][3] = 0.0f; 292 | 293 | projection[2][0] = 0.0f; 294 | projection[2][1] = 0.0f; 295 | projection[2][2] = 0.0f; 296 | projection[2][3] = 0.0f; 297 | 298 | projection[3][0] = -1.0f; 299 | if (renderer->target) { 300 | projection[3][1] = -1.0f; 301 | } else { 302 | projection[3][1] = 1.0f; 303 | } 304 | projection[3][2] = 0.0f; 305 | projection[3][3] = 1.0f; 306 | SDL_GL_setUniform_matrix( shader_data->vport, (GLfloat*) projection ); 307 | } 308 | } 309 | 310 | SDL_Shader* SDL_GL_createShader( SDL_Renderer* renderer, 311 | SDL_ShaderStream* shdstream ) 312 | { 313 | 314 | SDL_Shader *shader; 315 | GLuint v; 316 | GLuint f; 317 | 318 | char *vs,*fs; 319 | if( !is_init ){ 320 | SDL_GL_init(); 321 | } 322 | 323 | shader = (SDL_Shader*) malloc( sizeof(SDL_Shader) ); 324 | if( !shader ){ 325 | SDL_OutOfMemory(); 326 | return NULL; 327 | } 328 | shader->driver_data = malloc( sizeof(SDL_GL_ShaderData ) ); 329 | if ( !shader->driver_data ) { 330 | free( shader->driver_data ); 331 | SDL_OutOfMemory(); 332 | return NULL; 333 | } 334 | SDL_GL_ShaderData *shader_data = (SDL_GL_ShaderData*) shader->driver_data; 335 | 336 | shader->renderer = renderer; 337 | shader->create_shader = SDL_GL_createShader; 338 | shader->destroyShader = SDL_GL_destroyShader; 339 | shader->bindShader = SDL_GL_bindShader; 340 | shader->unbindShader = SDL_GL_unbindShader; 341 | shader->renderCopyShd = SDL_GL_renderCopyShd; 342 | shader->updateViewport = SDL_GL_updateViewport; 343 | 344 | shader->createUniform = SDL_GL_createUniform; 345 | shader->destroyUniform = SDL_GL_destroyUniform; 346 | 347 | shader->createVertexBuffer = SDL_GL_createVertexBuffer; 348 | shader->destroyVertexBuffer = SDL_GL_destroyVertexBuffer; 349 | 350 | shader_data->p = 0; 351 | 352 | #ifdef _WIN32 353 | glUniform1f = (glUniform1f_type) SDL_GL_GetProcAddress("glUniform1f"); 354 | glUniform2f = (glUniform2f_type) SDL_GL_GetProcAddress("glUniform2f"); 355 | glUniform3f = (glUniform3f_type) SDL_GL_GetProcAddress("glUniform3f"); 356 | glUniform4f = (glUniform4f_type) SDL_GL_GetProcAddress("glUniform4f"); 357 | glUniform1fv = (glUniform1fv_type) SDL_GL_GetProcAddress("glUniform1fv"); 358 | 359 | glUniform1i = (glUniform1i_type) SDL_GL_GetProcAddress("glUniform1i"); 360 | glUniform2i = (glUniform2i_type) SDL_GL_GetProcAddress("glUniform2i"); 361 | glUniform3i = (glUniform3i_type) SDL_GL_GetProcAddress("glUniform3i"); 362 | glUniform4i = (glUniform4i_type) SDL_GL_GetProcAddress("glUniform4i"); 363 | glUniform1iv = (glUniform1iv_type) SDL_GL_GetProcAddress("glUniform1iv"); 364 | glGetShaderiv = (glGetShaderiv_type) SDL_GL_GetProcAddress("glGetShaderiv"); 365 | glGetProgramiv = (glGetProgramiv_type) SDL_GL_GetProcAddress("glGetProgramiv"); 366 | glCreateShader = (glCreateShader_type) SDL_GL_GetProcAddress("glCreateShader"); 367 | glShaderSource = (glShaderSource_type) SDL_GL_GetProcAddress("glShaderSource"); 368 | glCompileShader = (glCompileShader_type) SDL_GL_GetProcAddress("glCompileShader"); 369 | glGetShaderInfoLog = (glGetShaderInfoLog_type) SDL_GL_GetProcAddress("glGetShaderInfoLog"); 370 | glBindAttribLocation = (glBindAttribLocation_type) SDL_GL_GetProcAddress("glBindAttribLocation"); 371 | glCreateProgram = (glCreateProgram_type) SDL_GL_GetProcAddress("glCreateProgram"); 372 | glAttachShader = (glAttachShader_type) SDL_GL_GetProcAddress("glAttachShader"); 373 | glLinkProgram = (glLinkProgram_type) SDL_GL_GetProcAddress("glLinkProgram"); 374 | glGetProgramInfoLog = (glGetProgramInfoLog_type) SDL_GL_GetProcAddress("glGetProgramInfoLog"); 375 | glDeleteShader = (glDeleteShader_type) SDL_GL_GetProcAddress("glDeleteShader"); 376 | glEnableVertexAttribArray = (glEnableVertexAttribArray_type) SDL_GL_GetProcAddress("glEnableVertexAttribArray"); 377 | glVertexAttribPointer = (glVertexAttribPointer_type) SDL_GL_GetProcAddress("glVertexAttribPointer"); 378 | glUniformMatrix4fv = (glUniformMatrix4fv_type) SDL_GL_GetProcAddress("glUniformMatrix4fv"); 379 | glGetUniformLocation = (glGetUniformLocation_type) SDL_GL_GetProcAddress("glGetUniformLocation"); 380 | glUseProgram = (glUseProgram_type) SDL_GL_GetProcAddress("glUseProgram"); 381 | glActiveTextureARB = (glActiveTextureARB_type) SDL_GL_GetProcAddress("glActiveTextureARB"); 382 | #endif 383 | 384 | v = glCreateShader( GL_VERTEX_SHADER ); 385 | f = glCreateShader( GL_FRAGMENT_SHADER ); 386 | shader_data->is_target = renderer->target != NULL; 387 | shader_data->vport = NULL; 388 | shader_data->color_mode = NULL; 389 | 390 | vs = SDL_Shader_readRW( shdstream->vshader ); 391 | if( vs==NULL ){ 392 | shader->destroyShader(shader); 393 | SDL_OutOfMemory(); 394 | return NULL; 395 | } 396 | 397 | fs = SDL_Shader_readRW( shdstream->pshader ); 398 | if( fs==NULL ){ 399 | free(fs); 400 | shader->destroyShader(shader); 401 | SDL_OutOfMemory(); 402 | return NULL; 403 | } 404 | 405 | const char* vss = (const char*) vs; 406 | const char *fss[] = {fs, color_conv}; 407 | glShaderSource(v, 1, &vss,NULL); 408 | glShaderSource(f, 2, fss,NULL); 409 | 410 | free(vs); 411 | free(fs); 412 | 413 | glCompileShader( v ); 414 | if( !SDL_GL_CompileSuccessful(v) ){ 415 | 416 | GLchar buff[512]; 417 | GLsizei len; 418 | glGetShaderInfoLog(v, 512, &len, buff); 419 | 420 | SDL_SetError("SDL_Shader[GL]: Could not compile vertex shader: \n-------\n%s\n-------\n", buff ); 421 | shader->destroyShader(shader); 422 | return NULL; 423 | } 424 | glCompileShader( f ); 425 | if( !SDL_GL_CompileSuccessful(f) ){ 426 | 427 | GLchar buff[512]; 428 | GLsizei len; 429 | glGetShaderInfoLog(f, 512, &len, buff); 430 | 431 | SDL_SetError("SDL_Shader[GL]: Could not compile fragment shader: \n-------\n%s\n-------\n", buff ); 432 | shader->destroyShader(shader); 433 | return NULL; 434 | } 435 | 436 | shader_data->p = glCreateProgram(); 437 | 438 | glAttachShader( shader_data->p,v ); 439 | glAttachShader( shader_data->p,f ); 440 | 441 | glBindAttribLocation( shader_data->p, GL_ATTRIBUTE_POSITION, "position"); 442 | glBindAttribLocation( shader_data->p, GL_ATTRIBUTE_TEXCOORD, "texCoords"); 443 | glBindAttribLocation( shader_data->p, GL_ATTRIBUTE_COLOR, "color"); 444 | 445 | glLinkProgram( shader_data->p ); 446 | if( !SDL_GL_LinkSuccessful(shader_data->p) ){ 447 | 448 | GLchar buff[512]; 449 | GLsizei len; 450 | glGetProgramInfoLog(shader_data->p, 512, &len, buff); 451 | 452 | SDL_SetError("SDL_Shader[GL]: Could not link shader: \n-------\n%s\n-------\n", buff ); 453 | shader->destroyShader(shader); 454 | return NULL; 455 | } 456 | 457 | shader->bindShader(shader); 458 | 459 | glDeleteShader( v ); 460 | glDeleteShader( f ); 461 | 462 | shader_data->vport = SDL_createUniform(shader,"world_view_projection"); 463 | SDL_GL_updateViewport(shader); 464 | 465 | shader_data->color_mode = SDL_createUniform(shader,"color_mode"); 466 | glEnableVertexAttribArray(GL_ATTRIBUTE_POSITION); 467 | glEnableVertexAttribArray(GL_ATTRIBUTE_TEXCOORD); 468 | glEnableVertexAttribArray(GL_ATTRIBUTE_COLOR); 469 | 470 | for( int i=0; i<8; i++ ) { 471 | SDL_Uniform* texN; 472 | texN = SDL_createUniform( shader,texture_names[i] ); 473 | if( texN ) { 474 | SDL_GL_setUniform_i( texN,i ); 475 | SDL_destroyUniform( shader, texN ); 476 | } 477 | } 478 | 479 | return shader; 480 | } 481 | 482 | int SDL_GL_bindShader( SDL_Shader* shader ) { 483 | SDL_GL_ShaderData *shader_data = (SDL_GL_ShaderData*) shader->driver_data; 484 | glUseProgram( shader_data->p ); 485 | return glGetError(); 486 | } 487 | 488 | int SDL_GL_unbindShader( SDL_Shader* shader ) { 489 | glUseProgram( 0 ); 490 | return glGetError(); 491 | } 492 | 493 | int SDL_GL_destroyShader( SDL_Shader* shader ) { 494 | // SDL_GL_ShaderData *shader_data = (SDL_GL_ShaderData*) shader->driver_data; 495 | if( !shader ) 496 | return 0; 497 | SDL_GL_ShaderData *shader_data = (SDL_GL_ShaderData*) shader->driver_data; 498 | if( shader_data->color_mode ) shader->destroyUniform(shader, shader_data->color_mode ); 499 | if( shader_data->vport ) shader->destroyUniform(shader, shader_data->vport ); 500 | if( shader_data->p ) glDeleteShader( shader_data->p ); 501 | shader->unbindShader( shader ); 502 | free( shader->driver_data ); 503 | free( shader ); 504 | return 0; 505 | } 506 | 507 | 508 | int SDL_GL_renderCopyShd(SDL_Shader* shader, SDL_Texture** textures, 509 | unsigned num_of_tex, SDL_Vertex* vertices, unsigned num_of_vert) 510 | { 511 | int i; 512 | GL_TextureData *texturedata; 513 | SDL_GL_ShaderData *shader_data; 514 | GL_RenderData *data; 515 | 516 | for ( i=0; idriverdata; 518 | if ( ( texturedata->texw != texturedata->texw || 519 | texturedata->texh != texturedata->texh) && i!=0 ) 520 | { 521 | return SDL_SetError("SDL_Shader[GL]: Textures must have the same size, " 522 | "internal representation missmatch"); 523 | } 524 | if (texturedata->yuv) { 525 | return SDL_SetError("SDL_Shader[GL]: YUV-textures not supported"); 526 | } 527 | } 528 | /* FIXME sampler2DRect needs texture coordinates in range (0..w)x(0..h) not (0..1)² */ 529 | /* the size is scaled here and then scaled back at the end of the function */ 530 | for ( i=0; igetVertex( vertices, i, 533 | NULL, NULL, NULL, NULL, /*color*/ 534 | NULL, NULL, NULL, /*position*/ 535 | &tex_s, &tex_t); 536 | vertices->setVertexTexCoord( vertices, i, 1, 537 | tex_s * texturedata->texw, tex_t * texturedata->texh ); 538 | } 539 | 540 | data = (GL_RenderData *) shader->renderer->driverdata; 541 | shader_data = (SDL_GL_ShaderData *) shader->driver_data; 542 | 543 | SDL_GL_MakeCurrent(shader->renderer->window, data->context); 544 | 545 | shader->bindShader(shader); 546 | if ( (shader->renderer->target == NULL && shader_data->is_target) || 547 | (shader->renderer->target != NULL && !shader_data->is_target) ) { 548 | SDL_GL_updateViewport(shader); 549 | shader_data->is_target = shader->renderer->target != NULL; 550 | } 551 | 552 | for ( i=num_of_tex; i<8; i++ ) { 553 | glActiveTextureARB( GL_TEXTURE0_ARB + i ); 554 | glDisable(GL_TEXTURE_2D); 555 | } 556 | for ( i=0; icurrent.shader = SHADER_NONE; 565 | 566 | if ( shader_data->color_mode ) { 567 | SDL_GL_setUniform_i( shader_data->color_mode, textures[0]->format); 568 | } 569 | 570 | data->current.shader = SHADER_NONE; 571 | if (textures[0]->blendMode != data->current.blendMode) { 572 | switch (textures[0]->blendMode) { 573 | case SDL_BLENDMODE_NONE: 574 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 575 | glDisable(GL_BLEND); 576 | break; 577 | case SDL_BLENDMODE_BLEND: 578 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 579 | glEnable(GL_BLEND); 580 | data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 581 | break; 582 | case SDL_BLENDMODE_ADD: 583 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 584 | glEnable(GL_BLEND); 585 | data->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE); 586 | break; 587 | case SDL_BLENDMODE_MOD: 588 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 589 | glEnable(GL_BLEND); 590 | data->glBlendFuncSeparate(GL_ZERO, GL_SRC_COLOR, GL_ZERO, GL_ONE); 591 | break; 592 | } 593 | data->current.blendMode = textures[0]->blendMode; 594 | } 595 | 596 | SDL_GL_Vertex_t *vbuff = (SDL_GL_Vertex_t*) vertices->vertexBuffer; 597 | glVertexAttribPointer(GL_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 598 | sizeof(SDL_GL_Vertex_t), vbuff ); 599 | glVertexAttribPointer(GL_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 600 | sizeof(SDL_GL_Vertex_t), &(vbuff[0].tex_s) ); 601 | glVertexAttribPointer(GL_ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, 602 | sizeof(SDL_GL_Vertex_t), &(vbuff[0].r) ); 603 | 604 | glDrawArrays(GL_TRIANGLE_STRIP, 0, num_of_vert); 605 | 606 | shader->unbindShader(shader); 607 | /* FIXME */ 608 | for ( i=0; igetVertex( vertices, i, 611 | NULL, NULL, NULL, NULL, /*color*/ 612 | NULL, NULL, NULL, /*position*/ 613 | &tex_s, &tex_t); 614 | vertices->setVertexTexCoord( vertices, i, 1, 615 | tex_s / texturedata->texw, tex_t / texturedata->texh ); 616 | } 617 | 618 | return glGetError(); 619 | } 620 | 621 | SDL_Uniform* SDL_GL_createUniform( SDL_Shader* shader, const char* name ) { 622 | GLint loc; 623 | SDL_GL_ShaderData* shader_data = (SDL_GL_ShaderData*) shader->driver_data; 624 | loc = glGetUniformLocation(shader_data->p, name); 625 | if( loc == -1 ) 626 | return NULL; 627 | 628 | SDL_Uniform* uniform = (SDL_Uniform*) malloc( sizeof(SDL_Uniform) ); 629 | if( !uniform ){ 630 | SDL_OutOfMemory(); 631 | return NULL; 632 | } 633 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*) malloc( sizeof(SDL_GL_UniformData) ); 634 | if( !udata ){ 635 | free(uniform); 636 | SDL_OutOfMemory(); 637 | return NULL; 638 | } 639 | 640 | uniform->shader = shader; 641 | 642 | uniform->setUniform_iv = SDL_GL_setUniform_iv; 643 | uniform->setUniform_i = SDL_GL_setUniform_i; 644 | uniform->setUniform_i2 = SDL_GL_setUniform_i2; 645 | uniform->setUniform_i3 = SDL_GL_setUniform_i3; 646 | uniform->setUniform_i4 = SDL_GL_setUniform_i4; 647 | 648 | uniform->setUniform_fv = SDL_GL_setUniform_fv; 649 | uniform->setUniform_f = SDL_GL_setUniform_f; 650 | uniform->setUniform_f2 = SDL_GL_setUniform_f2; 651 | uniform->setUniform_f3 = SDL_GL_setUniform_f3; 652 | uniform->setUniform_f4 = SDL_GL_setUniform_f4; 653 | 654 | udata->p = shader_data->p; 655 | udata->loc = loc; 656 | 657 | uniform->driver_data = udata; 658 | return uniform; 659 | 660 | } 661 | int SDL_GL_destroyUniform( SDL_Shader* shader, SDL_Uniform* uniform ){ 662 | free(uniform->driver_data); 663 | free(uniform); 664 | return 0; 665 | } 666 | 667 | int SDL_GL_setUniform_matrix( SDL_Uniform* uniform, GLfloat* mat ) { 668 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 669 | glUniformMatrix4fv( udata->loc, 1, GL_FALSE, mat ); 670 | return glGetError(); 671 | } 672 | 673 | int SDL_GL_setUniform_fv( SDL_Uniform* uniform, float* vector, int num ) { 674 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 675 | glUniform1fv( udata->loc, num, vector); 676 | return glGetError(); 677 | } 678 | int SDL_GL_setUniform_f( SDL_Uniform* uniform, float a ) { 679 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 680 | glUniform1f(udata->loc, a ); 681 | return glGetError(); 682 | } 683 | int SDL_GL_setUniform_f2( SDL_Uniform* uniform, float a, float b ){ 684 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 685 | glUniform2f(udata->loc, a,b ); 686 | return glGetError(); 687 | } 688 | int SDL_GL_setUniform_f3( SDL_Uniform* uniform, float a, float b, float c ){ 689 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 690 | glUniform3f(udata->loc, a,b,c ); 691 | return glGetError(); 692 | } 693 | int SDL_GL_setUniform_f4( SDL_Uniform* uniform, float a, float b, float c, float d ){ 694 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 695 | glUniform4f(udata->loc, a,b,c,d ); 696 | return glGetError(); 697 | } 698 | 699 | 700 | int SDL_GL_setUniform_iv( SDL_Uniform* uniform, int* vector, int num ) { 701 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 702 | glUniform1iv( udata->loc, num, vector); 703 | return glGetError(); 704 | } 705 | int SDL_GL_setUniform_i( SDL_Uniform* uniform, int a ) { 706 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 707 | glUniform1i(udata->loc, a ); 708 | return glGetError(); 709 | } 710 | int SDL_GL_setUniform_i2( SDL_Uniform* uniform, int a, int b ){ 711 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 712 | glUniform2i(udata->loc, a,b ); 713 | return glGetError(); 714 | } 715 | int SDL_GL_setUniform_i3( SDL_Uniform* uniform, int a, int b, int c ){ 716 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 717 | glUniform3i(udata->loc, a,b,c ); 718 | return glGetError(); 719 | } 720 | int SDL_GL_setUniform_i4( SDL_Uniform* uniform, int a, int b, int c, int d ){ 721 | SDL_GL_UniformData* udata = (SDL_GL_UniformData*)uniform->driver_data; 722 | glUniform4i(udata->loc, a,b,c,d ); 723 | return glGetError(); 724 | } 725 | 726 | #else 727 | extern void __suppress_a_warning__(); 728 | #endif /* SDL_SHADER_OPENGL */ 729 | --------------------------------------------------------------------------------