src/shader.c

changeset 239
3b78ad115ccd
parent 225
c42c7d1a3c34
equal deleted inserted replaced
238:e22abcd22e47 239:3b78ad115ccd
235 glUniformMatrix4fv(shader->projection, 1, GL_FALSE, camera->projection); 235 glUniformMatrix4fv(shader->projection, 1, GL_FALSE, camera->projection);
236 glUniformMatrix4fv(shader->view, 1, GL_FALSE, camera->view); 236 glUniformMatrix4fv(shader->view, 1, GL_FALSE, camera->view);
237 return asc_error_catch_gl("Activating shader"); 237 return asc_error_catch_gl("Activating shader");
238 } 238 }
239 239
240 const AscShaderProgram *asc_shader_register(unsigned int id, asc_shader_create_func create_func, int create_flags) { 240 const AscShaderProgram *asc_shader_lookup(unsigned int id, asc_shader_create_func create_func, int create_flags) {
241 AscGLContext *glctx = asc_active_glctx; 241 AscShaderProgram *prog = NULL;
242 #ifndef NDEBUG 242 CxIterator iter = cxListIterator(asc_active_glctx->shaders);
243 { 243 cx_foreach(AscShaderProgram *, p, iter) {
244 const AscShaderProgram *prog = asc_shader_lookup(id); 244 if (p->id == id) {
245 if (prog != NULL) { 245 prog = p;
246 asc_error("Shader program %u already exists. This is a bug!", id); 246 break;
247 // still return it, so that the caller does not die immediately 247 }
248 return prog; 248 }
249 }
250 }
251 #endif
252 AscShaderProgram *prog = create_func(create_flags);
253 if (prog == NULL) { 249 if (prog == NULL) {
254 // create an empty program to prevent future loading attempts 250 AscGLContext *glctx = asc_active_glctx;
255 prog = cxZallocDefault(sizeof(AscShaderProgram)); 251 prog = create_func(create_flags);
256 } 252 if (prog == NULL) {
257 prog->id = id; 253 // create an empty program to prevent future loading attempts
258 cxListAdd(glctx->shaders, prog); 254 prog = cxZallocDefault(sizeof(AscShaderProgram));
259 return prog; 255 }
260 } 256 prog->id = id;
261 257 cxListAdd(glctx->shaders, prog);
262 const AscShaderProgram *asc_shader_lookup(unsigned int id) {
263 CxIterator iter = cxListIterator(asc_active_glctx->shaders);
264 cx_foreach(const AscShaderProgram *, prog, iter) {
265 if (prog->id == id) return prog;
266 }
267 return NULL;
268 }
269
270 const AscShaderProgram *asc_shader_lookup_or_create(unsigned int id, asc_shader_create_func create_func, int create_flags) {
271 const AscShaderProgram *prog = asc_shader_lookup(id);
272 if (prog == NULL) {
273 return asc_shader_register(id, create_func, create_flags);
274 } 258 }
275 return prog; 259 return prog;
276 } 260 }
277 261
278 void asc_shader_clear_registry(void) { 262 void asc_shader_clear_registry(void) {

mercurial