src/video/SDL_renderer_gl.c
changeset 2847 7d020441fa81
parent 2846 3d7833d5a4be
child 2848 8a3aa505ecba
equal deleted inserted replaced
2846:3d7833d5a4be 2847:7d020441fa81
   476 
   476 
   477 
   477 
   478 #define DEBUG_PROGRAM_COMPILE 1
   478 #define DEBUG_PROGRAM_COMPILE 1
   479 
   479 
   480 static GLuint
   480 static GLuint
   481 compile_shader(GL_RenderData *data, GLenum shader_type, const char *source)
   481 compile_shader(GL_RenderData *data, GLenum shader_type, const char *_code)
   482 {
   482 {
       
   483     const int have_texture_rects = data->GL_ARB_texture_rectangle_supported;
       
   484     const char *replacement = have_texture_rects ? "RECT" : "2D";
       
   485     const size_t replacementlen = strlen(replacement);
       
   486     const char *token = "%TEXTURETARGET%";
       
   487     const size_t tokenlen = strlen(token);
       
   488     char *code = NULL;
       
   489     char *ptr = NULL;
       
   490     GLuint program = 0;
       
   491 
       
   492     /*
       
   493      * The TEX instruction needs a different target depending on what we use.
       
   494      *  To handle this, we use "%TEXTURETARGET%" and replace the string before
       
   495      *  compiling the shader.
       
   496      */
       
   497     code = SDL_strdup(_code);
       
   498     if (code == NULL)
       
   499         return 0;
       
   500 
       
   501     for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr+1, token)) {
       
   502         memcpy(ptr, replacement, replacementlen);
       
   503         memmove(ptr+replacementlen, ptr+tokenlen, strlen(ptr+tokenlen)+1);
       
   504     }
       
   505 
   483 #if DEBUG_PROGRAM_COMPILE
   506 #if DEBUG_PROGRAM_COMPILE
   484     printf("compiling shader:\n%s\n\n", source);
   507     printf("compiling shader:\n%s\n\n", code);
   485 #endif
   508 #endif
   486 
       
   487     GLuint program = 0;
       
   488 
   509 
   489     data->glGetError();  /* flush any existing error state. */
   510     data->glGetError();  /* flush any existing error state. */
   490     data->glGenProgramsARB(1, &program);
   511     data->glGenProgramsARB(1, &program);
   491     data->glBindProgramARB(shader_type, program);
   512     data->glBindProgramARB(shader_type, program);
   492     data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
   513     data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
   493                              SDL_strlen(source), source);
   514                              SDL_strlen(code), code);
       
   515 
       
   516     SDL_free(code);
   494 
   517 
   495     if (data->glGetError() == GL_INVALID_OPERATION)
   518     if (data->glGetError() == GL_INVALID_OPERATION)
   496     {
   519     {
   497 #if DEBUG_PROGRAM_COMPILE
   520 #if DEBUG_PROGRAM_COMPILE
   498         GLint pos = 0;
   521         GLint pos = 0;
   532     // We need 32 bits to store the data, but each pixel is 16 bits in itself.
   555     // We need 32 bits to store the data, but each pixel is 16 bits in itself.
   533     //  halve the coordinates to grab the correct 32 bits for the fragment.
   556     //  halve the coordinates to grab the correct 32 bits for the fragment.
   534     "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
   557     "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
   535 
   558 
   536     // Sample the YUV texture. Cb, Y1, Cr, Y2, are stored x,y,z,w
   559     // Sample the YUV texture. Cb, Y1, Cr, Y2, are stored x,y,z,w
   537     // !!! FIXME: "RECT" needs to be "2D" if we're not using texture_rectangle extension.  :/
   560     "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
   538     "TEX uyvy, work, texture[0], RECT;\n"
       
   539 
   561 
   540     // Do subtractions (128/255, 16/255, 128/255, 16/255)
   562     // Do subtractions (128/255, 16/255, 128/255, 16/255)
   541     "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
   563     "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
   542 
   564 
   543     // Choose the luminance component by texcoord.
   565     // Choose the luminance component by texcoord.