this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Added simple-shader demo, which shows how to set up several types of shaders.

+387 -1
+4 -1
demos/CMakeLists.txt
··· 14 14 target_link_libraries (3d-demo ${DEMO_LIBS}) 15 15 16 16 add_executable(tutorial-space-demo tutorial-space/main.c) 17 - target_link_libraries (tutorial-space-demo ${DEMO_LIBS}) 17 + target_link_libraries (tutorial-space-demo ${DEMO_LIBS}) 18 + 19 + add_executable(simple-shader-demo simple-shader/main.c) 20 + target_link_libraries (simple-shader-demo ${DEMO_LIBS})
+22
demos/data/shaders/alpha_mask.frag
··· 1 + 2 + in vec4 color; 3 + in vec2 texCoord; 4 + out vec4 fragColor; 5 + 6 + uniform sampler2D tex; 7 + uniform sampler2D mask_tex; 8 + 9 + uniform float offset_x; 10 + uniform float offset_y; 11 + uniform float resolution_x; 12 + uniform float resolution_y; 13 + uniform float mask_resolution_x; 14 + uniform float mask_resolution_y; 15 + 16 + void main(void) 17 + { 18 + vec4 col = texture2D(tex, texCoord); 19 + vec2 mask_coords = vec2((texCoord.x*resolution_x + offset_x)/mask_resolution_x, (texCoord.y*resolution_y + offset_y)/mask_resolution_y); 20 + vec4 mask = texture2D(mask_tex, mask_coords); 21 + fragColor = vec4(col.rgb, col.a*mask.a); 22 + }
+11
demos/data/shaders/color.frag
··· 1 + varying vec4 color; 2 + varying vec2 texCoord; 3 + 4 + uniform sampler2D tex; 5 + uniform vec4 myColor; 6 + 7 + void main(void) 8 + { 9 + vec4 c = myColor * color; 10 + gl_FragColor = texture2D(tex, texCoord) * c; 11 + }
+11
demos/data/shaders/common.frag
··· 1 + 2 + in vec4 color; 3 + in vec2 texCoord; 4 + out vec4 fragColor; 5 + 6 + uniform sampler2D tex; 7 + 8 + void main(void) 9 + { 10 + fragColor = texture2D(tex, texCoord) * color; 11 + }
+14
demos/data/shaders/common.vert
··· 1 + attribute vec3 gpu_Vertex; 2 + attribute vec2 gpu_TexCoord; 3 + attribute vec4 gpu_Color; 4 + uniform mat4 gpu_ModelViewProjectionMatrix; 5 + 6 + varying vec4 color; 7 + varying vec2 texCoord; 8 + 9 + void main(void) 10 + { 11 + color = gpu_Color; 12 + texCoord = vec2(gpu_TexCoord); 13 + gl_Position = gpu_ModelViewProjectionMatrix * vec4(gpu_Vertex, 1.0); 14 + }
+51
demos/data/shaders/marching_ants.frag
··· 1 + 2 + in vec4 color; 3 + in vec2 texCoord; 4 + out vec4 fragColor; 5 + 6 + uniform sampler2D tex; 7 + 8 + uniform float resolution_x; 9 + uniform float resolution_y; 10 + 11 + uniform float screen_w; 12 + uniform float screen_h; 13 + 14 + uniform float time; 15 + uniform float zoom; 16 + 17 + void main(void) 18 + { 19 + float x_offset = 1.0/(screen_w*zoom) + 1.0/(resolution_x*zoom); 20 + float y_offset = 1.0/(screen_h*zoom) + 1.0/(resolution_y*zoom); 21 + 22 + vec4 center = texture2D(tex, texCoord); 23 + vec4 left = texture2D(tex, vec2(texCoord.s - x_offset, texCoord.t)); 24 + vec4 right = texture2D(tex, vec2(texCoord.s + x_offset, texCoord.t)); 25 + vec4 up = texture2D(tex, vec2(texCoord.s, texCoord.t - y_offset)); 26 + vec4 down = texture2D(tex, vec2(texCoord.s, texCoord.t + y_offset)); 27 + 28 + bool is_image_edge = (texCoord.s - x_offset < 0 || texCoord.s + x_offset > 1 || texCoord.t - y_offset < 0 || texCoord.t + y_offset > 1); 29 + 30 + if(left.a != right.a || left.a != up.a || left.a != down.a || (center.a > 0.5 && is_image_edge)) 31 + { 32 + if(center.a > 0.5) 33 + { 34 + float speed = 0.6; 35 + float ant_size = 10; 36 + float black_to_white_ratio = 0.5; 37 + float ant_position = (gl_FragCoord.x + gl_FragCoord.y); 38 + float t = step(black_to_white_ratio, mod(time*speed + ant_position/ant_size, 1.0)); 39 + 40 + fragColor = vec4(t, t, t, 1); 41 + } 42 + else 43 + fragColor = vec4(1, 1, 1, 1); 44 + } 45 + else if(center.a < 0.5) 46 + { 47 + fragColor = vec4(0, 0, 0, 0.1); 48 + } 49 + else 50 + discard; 51 + }
demos/data/test2.png

This is a binary file and will not be displayed.

demos/data/test3.png

This is a binary file and will not be displayed.

+272
demos/simple-shader/main.c
··· 1 + #include "SDL.h" 2 + #include "SDL_gpu.h" 3 + #include <stdlib.h> 4 + #include <string.h> 5 + #include <math.h> 6 + 7 + 8 + // Loads a shader and prepends version/compatibility info before compiling it. 9 + // Normally, you can just use GPU_LoadShader() for shader source files or GPU_CompileShader() for strings. 10 + // However, some hardware (certain ATI/AMD cards) does not let you put non-#version preprocessing at the top of the file. 11 + // Therefore, I need to prepend the version info here so I can support both GLSL and GLSLES with one shader file. 12 + Uint32 load_shader(GPU_ShaderEnum shader_type, const char* filename) 13 + { 14 + SDL_RWops* rwops; 15 + Uint32 shader; 16 + char* source; 17 + int header_size, file_size; 18 + const char* header = ""; 19 + GPU_Renderer* renderer = GPU_GetCurrentRenderer(); 20 + 21 + // Open file 22 + rwops = SDL_RWFromFile(filename, "rb"); 23 + if(rwops == NULL) 24 + { 25 + GPU_PushErrorCode("load_shader", GPU_ERROR_FILE_NOT_FOUND, "Shader file \"%s\" not found", filename); 26 + return 0; 27 + } 28 + 29 + // Get file size 30 + file_size = SDL_RWseek(rwops, 0, SEEK_END); 31 + SDL_RWseek(rwops, 0, SEEK_SET); 32 + 33 + // Get size from header 34 + if(renderer->shader_language == GPU_LANGUAGE_GLSL) 35 + { 36 + if(renderer->max_shader_version >= 120) 37 + header = "#version 120\n"; 38 + else 39 + header = "#version 110\n"; // Maybe this is good enough? 40 + } 41 + else if(renderer->shader_language == GPU_LANGUAGE_GLSLES) 42 + header = "#version 100\nprecision mediump int;\nprecision mediump float;\n"; 43 + 44 + header_size = strlen(header); 45 + 46 + // Allocate source buffer 47 + source = (char*)malloc(sizeof(char)*(header_size + file_size + 1)); 48 + 49 + // Prepend header 50 + strcpy(source, header); 51 + 52 + // Read in source code 53 + SDL_RWread(rwops, source + strlen(source), 1, file_size); 54 + source[header_size + file_size] = '\0'; 55 + 56 + // Compile the shader 57 + shader = GPU_CompileShader(shader_type, source); 58 + 59 + // Clean up 60 + free(source); 61 + SDL_RWclose(rwops); 62 + 63 + return shader; 64 + } 65 + 66 + GPU_ShaderBlock load_shader_program(Uint32* p, const char* vertex_shader_file, const char* fragment_shader_file) 67 + { 68 + Uint32 v, f; 69 + v = load_shader(GPU_VERTEX_SHADER, vertex_shader_file); 70 + 71 + if(!v) 72 + GPU_LogError("Failed to load vertex shader (%s): %s\n", vertex_shader_file, GPU_GetShaderMessage()); 73 + 74 + f = load_shader(GPU_FRAGMENT_SHADER, fragment_shader_file); 75 + 76 + if(!f) 77 + GPU_LogError("Failed to load fragment shader (%s): %s\n", fragment_shader_file, GPU_GetShaderMessage()); 78 + 79 + *p = GPU_LinkShaders(v, f); 80 + 81 + if(!*p) 82 + { 83 + GPU_ShaderBlock b = {-1, -1, -1, -1}; 84 + GPU_LogError("Failed to link shader program (%s + %s): %s\n", vertex_shader_file, fragment_shader_file, GPU_GetShaderMessage()); 85 + return b; 86 + } 87 + 88 + { 89 + GPU_ShaderBlock block = GPU_LoadShaderBlock(*p, "gpu_Vertex", "gpu_TexCoord", "gpu_Color", "gpu_ModelViewProjectionMatrix"); 90 + GPU_ActivateShaderProgram(*p, &block); 91 + 92 + return block; 93 + } 94 + } 95 + 96 + void free_shader(Uint32 p) 97 + { 98 + GPU_FreeShaderProgram(p); 99 + } 100 + 101 + 102 + 103 + // Not going to change dynamically in this demo, so can be done once. 104 + 105 + void prepare_mask_shader(Uint32 shader, GPU_Image* image, GPU_Image* mask_image) 106 + { 107 + if(image != NULL) 108 + { 109 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_x"), image->w); 110 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_y"), image->h); 111 + } 112 + if(mask_image != NULL) 113 + { 114 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "mask_resolution_x"), mask_image->w); 115 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "mask_resolution_y"), mask_image->h); 116 + } 117 + 118 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "offset_x"), 0.0f); 119 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "offset_y"), 0.0f); 120 + 121 + GPU_SetShaderImage(mask_image, GPU_GetUniformLocation(shader, "mask_tex"), 1); 122 + } 123 + 124 + void prepare_marching_ants_shader(Uint32 shader, GPU_Target* screen, GPU_Image* image) 125 + { 126 + if(image != NULL) 127 + { 128 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_x"), image->w); 129 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_y"), image->h); 130 + } 131 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "screen_w"), screen->w); 132 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "screen_h"), screen->h); 133 + GPU_SetUniformf(GPU_GetUniformLocation(shader, "zoom"), 1.0f); 134 + } 135 + 136 + 137 + 138 + // Will change every frame 139 + 140 + void update_color_shader(float r, float g, float b, float a, int color_loc) 141 + { 142 + float fcolor[4] = {r, g, b, a}; 143 + GPU_SetUniformfv(color_loc, 4, 1, fcolor); 144 + } 145 + 146 + void update_vertex_color_shader(float t, float vertex_colors[24]) 147 + { 148 + for(int i = 0; i < 6; ++i) 149 + { 150 + vertex_colors[i] = 0.5f + 0.5f*(1 + sin(t + (i)))/2; 151 + vertex_colors[i+1] = 0.5f + 0.5f*(1 + sin(t + (i+1)))/2; 152 + vertex_colors[i+2] = 0.5f + 0.5f*(1 + sin(t + (i+2)))/2; 153 + vertex_colors[i+3] = 1.0f; 154 + } 155 + } 156 + 157 + void update_marching_ants_shader(float t, int time_loc) 158 + { 159 + GPU_SetUniformf(time_loc, t); 160 + } 161 + 162 + 163 + 164 + 165 + void main_loop(GPU_Target* screen) 166 + { 167 + GPU_Image* image = GPU_LoadImage("data/test3.png"); 168 + 169 + Uint32 color_shader; 170 + GPU_ShaderBlock color_block = load_shader_program(&color_shader, "data/shaders/common.vert", "data/shaders/color.frag"); 171 + int color_loc = GPU_GetUniformLocation(color_shader, "myColor"); 172 + 173 + // Disabled for now because it seems to be a bit buggy 174 + // Having problems with this... It seems to take 1 color for each vertex, even shared ones (indices, so not 16 floats but 24). 175 + // It also seems not to be working properly anyhow. Some vertices are still dark. 176 + /*Uint32 vertex_color_shader; 177 + GPU_ShaderBlock vertex_color_block = load_shader_program(&vertex_color_shader, "data/shaders/common.vert", "data/shaders/common.frag"); 178 + vertex_color_block.color_loc = -1; // Disable so it doesn't conflict with custom attribute values 179 + int vertex_color_loc = GPU_GetAttributeLocation(vertex_color_shader, "gpu_Color"); 180 + GPU_AttributeFormat vertex_color_attribute_format = GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, 4*sizeof(float), 0); 181 + float vertex_colors[24]; 182 + GPU_Attribute vertex_color_attribute = GPU_MakeAttribute(vertex_color_loc, vertex_colors, vertex_color_attribute_format);*/ 183 + 184 + Uint32 mask_shader; 185 + GPU_ShaderBlock mask_block = load_shader_program(&mask_shader, "data/shaders/common.vert", "data/shaders/alpha_mask.frag"); 186 + GPU_Image* mask_image = GPU_LoadImage("data/test2.png"); 187 + prepare_mask_shader(mask_shader, image, mask_image); 188 + 189 + Uint32 marching_ants_shader; 190 + GPU_ShaderBlock marching_ants_block = load_shader_program(&marching_ants_shader, "data/shaders/common.vert", "data/shaders/marching_ants.frag"); 191 + prepare_marching_ants_shader(marching_ants_shader, screen, image); 192 + int marching_ants_time_loc = GPU_GetUniformLocation(marching_ants_shader, "time"); 193 + 194 + // TODO: Add a real vertex shader 195 + 196 + 197 + float t; 198 + 199 + Uint8 done; 200 + SDL_Event event; 201 + 202 + done = 0; 203 + while(!done) 204 + { 205 + while(SDL_PollEvent(&event)) 206 + { 207 + if(event.type == SDL_QUIT) 208 + done = 1; 209 + else if(event.type == SDL_KEYDOWN) 210 + { 211 + if(event.key.keysym.sym == SDLK_ESCAPE) 212 + done = 1; 213 + } 214 + } 215 + 216 + t = SDL_GetTicks()/1000.0f; 217 + 218 + GPU_Clear(screen); 219 + 220 + // Color mod 221 + GPU_ActivateShaderProgram(color_shader, &color_block); 222 + update_color_shader((1+sin(t))/2, (1+sin(t+1))/2, (1+sin(t+2))/2, 1.0f, color_loc); 223 + GPU_Blit(image, NULL, screen, screen->w/4, screen->h/4); 224 + 225 + // Per-vertex colors 226 + // Disabled for now because it seems to be a bit buggy 227 + /*GPU_ActivateShaderProgram(vertex_color_shader, &vertex_color_block); 228 + update_vertex_color_shader(t, vertex_colors); 229 + GPU_SetAttributeSource(6, vertex_color_attribute); 230 + GPU_Blit(image, NULL, screen, 3*screen->w/4, screen->h/4); 231 + GPU_SetAttributeSource(0, vertex_color_attribute);*/ 232 + 233 + // Alpha mask 234 + GPU_ActivateShaderProgram(mask_shader, &mask_block); 235 + GPU_Blit(image, NULL, screen, screen->w/4, 3*screen->h/4); 236 + 237 + // Marching ants 238 + GPU_ActivateShaderProgram(0, NULL); 239 + GPU_Blit(image, NULL, screen, 3*screen->w/4, 3*screen->h/4); 240 + GPU_ActivateShaderProgram(marching_ants_shader, &marching_ants_block); 241 + update_marching_ants_shader(t, marching_ants_time_loc); 242 + GPU_Blit(image, NULL, screen, 3*screen->w/4, 3*screen->h/4); 243 + 244 + GPU_ActivateShaderProgram(0, NULL); 245 + 246 + GPU_Flip(screen); 247 + } 248 + 249 + GPU_FreeImage(mask_image); 250 + GPU_FreeImage(image); 251 + 252 + free_shader(color_shader); 253 + free_shader(mask_shader); 254 + free_shader(marching_ants_shader); 255 + } 256 + 257 + int main(int argc, char* argv[]) 258 + { 259 + GPU_Target* screen; 260 + 261 + screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); 262 + if(screen == NULL) 263 + return -1; 264 + 265 + main_loop(screen); 266 + 267 + GPU_Quit(); 268 + 269 + return 0; 270 + } 271 + 272 +
+2
src/renderer_GL_common.inl
··· 6472 6472 return; 6473 6473 if(source.location < 0 || source.location >= 16) 6474 6474 return; 6475 + 6476 + FlushBlitBuffer(renderer); 6475 6477 cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data; 6476 6478 a = &cdata->shader_attributes[source.location]; 6477 6479 if(source.format.is_per_sprite)