src/render/opengl/SDL_render_gl.c
changeset 6246 c70ec935a4bb
parent 6237 240f1bced46b
child 6247 b6212690f78d
     1.1 --- a/src/render/opengl/SDL_render_gl.c	Sat Jan 21 22:14:38 2012 -0500
     1.2 +++ b/src/render/opengl/SDL_render_gl.c	Sat Jan 21 22:22:30 2012 -0500
     1.3 @@ -54,6 +54,7 @@
     1.4  static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
     1.5                            const SDL_Rect * rect, void **pixels, int *pitch);
     1.6  static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     1.7 +static int GL_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     1.8  static int GL_UpdateViewport(SDL_Renderer * renderer);
     1.9  static int GL_RenderClear(SDL_Renderer * renderer);
    1.10  static int GL_RenderDrawPoints(SDL_Renderer * renderer,
    1.11 @@ -66,7 +67,6 @@
    1.12                           const SDL_Rect * srcrect, const SDL_Rect * dstrect);
    1.13  static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.14                                 Uint32 pixel_format, void * pixels, int pitch);
    1.15 -static int GL_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.16  static void GL_RenderPresent(SDL_Renderer * renderer);
    1.17  static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.18  static void GL_DestroyRenderer(SDL_Renderer * renderer);
    1.19 @@ -104,8 +104,6 @@
    1.20      
    1.21      SDL_bool GL_EXT_framebuffer_object_supported;
    1.22      GL_FBOList *framebuffers;
    1.23 -    SDL_Texture *renderTarget;
    1.24 -    SDL_Rect viewport_copy;
    1.25  
    1.26      /* OpenGL functions */
    1.27  #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
    1.28 @@ -309,19 +307,19 @@
    1.29      renderer->UpdateTexture = GL_UpdateTexture;
    1.30      renderer->LockTexture = GL_LockTexture;
    1.31      renderer->UnlockTexture = GL_UnlockTexture;
    1.32 +    renderer->SetTargetTexture = GL_SetTargetTexture;
    1.33      renderer->UpdateViewport = GL_UpdateViewport;
    1.34      renderer->RenderClear = GL_RenderClear;
    1.35      renderer->RenderDrawPoints = GL_RenderDrawPoints;
    1.36      renderer->RenderDrawLines = GL_RenderDrawLines;
    1.37      renderer->RenderFillRects = GL_RenderFillRects;
    1.38      renderer->RenderCopy = GL_RenderCopy;
    1.39 -    renderer->SetTargetTexture = GL_SetTargetTexture;
    1.40      renderer->RenderReadPixels = GL_RenderReadPixels;
    1.41      renderer->RenderPresent = GL_RenderPresent;
    1.42      renderer->DestroyTexture = GL_DestroyTexture;
    1.43      renderer->DestroyRenderer = GL_DestroyRenderer;
    1.44      renderer->info = GL_RenderDriver.info;
    1.45 -    renderer->info.flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
    1.46 +    renderer->info.flags = SDL_RENDERER_ACCELERATED;
    1.47      renderer->driverdata = data;
    1.48      renderer->window = window;
    1.49  
    1.50 @@ -399,11 +397,11 @@
    1.51              SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
    1.52          data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
    1.53              SDL_GL_GetProcAddress("glBindFramebufferEXT");
    1.54 -       data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
    1.55 +        data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
    1.56              SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
    1.57 +        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
    1.58      }
    1.59      data->framebuffers = NULL;
    1.60 -    data->renderTarget = NULL;
    1.61  
    1.62      /* Set up parameters for rendering */
    1.63      GL_ResetState(renderer);
    1.64 @@ -466,74 +464,6 @@
    1.65  }
    1.66  
    1.67  static int
    1.68 -GL_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    1.69 -{
    1.70 -    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;    
    1.71 -    
    1.72 -    GL_TextureData *texturedata;
    1.73 -    GLenum status;
    1.74 -
    1.75 -    if (!renderer) return -1;
    1.76 -    GL_ActivateRenderer(renderer);
    1.77 -    
    1.78 -    if (! data->GL_EXT_framebuffer_object_supported) {
    1.79 -        SDL_Unsupported();
    1.80 -        return -1;
    1.81 -    }
    1.82 -    
    1.83 -    if (texture == NULL) {
    1.84 -        if (data->renderTarget != NULL) {
    1.85 -            data->renderTarget = NULL;
    1.86 -            renderer->viewport = data->viewport_copy;
    1.87 -            data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    1.88 -            data->glMatrixMode(GL_PROJECTION);
    1.89 -            data->glLoadIdentity();
    1.90 -            data->glMatrixMode(GL_MODELVIEW);
    1.91 -            data->glLoadIdentity();
    1.92 -            data->glViewport(renderer->viewport.x, renderer->viewport.y, renderer->viewport.w, renderer->viewport.h);
    1.93 -            data->glOrtho(0.0, (GLdouble) renderer->viewport.w, (GLdouble) renderer->viewport.h, 0.0, 0.0, 1.0);
    1.94 -        }
    1.95 -        return 0;
    1.96 -    }
    1.97 -    if (renderer != texture->renderer) return -1;
    1.98 -    if (data->renderTarget==NULL) {
    1.99 -        // Keep a copy of the default viewport to restore when texture==NULL
   1.100 -        data->viewport_copy = renderer->viewport;
   1.101 -    }
   1.102 -    
   1.103 -    
   1.104 -    texturedata = (GL_TextureData *) texture->driverdata;
   1.105 -    if (!texturedata) {
   1.106 -        if (texture->native && texture->native->driverdata) {
   1.107 -            texture = texture->native;
   1.108 -            texturedata = texture->driverdata;
   1.109 -        }
   1.110 -        else return -1;
   1.111 -    }
   1.112 -    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
   1.113 -    /* TODO: check if texture pixel format allows this operation */
   1.114 -    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
   1.115 -    /* Check FBO status */
   1.116 -    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   1.117 -    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
   1.118 -        return -1;
   1.119 -    }
   1.120 -
   1.121 -    data->renderTarget = texture;
   1.122 -    renderer->viewport.x = 0;
   1.123 -    renderer->viewport.y = 0;
   1.124 -    renderer->viewport.w = texture->w;
   1.125 -    renderer->viewport.h = texture->h;
   1.126 -    data->glMatrixMode(GL_PROJECTION);
   1.127 -    data->glLoadIdentity();
   1.128 -    data->glOrtho(0.0, (GLdouble) texture->w, 0.0, (GLdouble) texture->h, 0.0, 1.0);
   1.129 -    data->glMatrixMode(GL_MODELVIEW);
   1.130 -    data->glLoadIdentity();
   1.131 -    data->glViewport(0, 0, texture->w, texture->h);    
   1.132 -    return 0;
   1.133 -}
   1.134 -
   1.135 -static int
   1.136  GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.137  {
   1.138      GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   1.139 @@ -777,6 +707,33 @@
   1.140  }
   1.141  
   1.142  static int
   1.143 +GL_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.144 +{
   1.145 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;    
   1.146 +    GL_TextureData *texturedata;
   1.147 +    GLenum status;
   1.148 +
   1.149 +    GL_ActivateRenderer(renderer);
   1.150 +    
   1.151 +    if (texture == NULL) {
   1.152 +        data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
   1.153 +        return 0;
   1.154 +    }
   1.155 +
   1.156 +    texturedata = (GL_TextureData *) texture->driverdata;
   1.157 +    data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
   1.158 +    /* TODO: check if texture pixel format allows this operation */
   1.159 +    data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
   1.160 +    /* Check FBO status */
   1.161 +    status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
   1.162 +    if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
   1.163 +        SDL_SetError("glFramebufferTexture2DEXT() failed");
   1.164 +        return -1;
   1.165 +    }
   1.166 +    return 0;
   1.167 +}
   1.168 +
   1.169 +static int
   1.170  GL_UpdateViewport(SDL_Renderer * renderer)
   1.171  {
   1.172      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   1.173 @@ -791,10 +748,19 @@
   1.174  
   1.175      data->glMatrixMode(GL_PROJECTION);
   1.176      data->glLoadIdentity();
   1.177 -    data->glOrtho((GLdouble) 0,
   1.178 -                  (GLdouble) renderer->viewport.w,
   1.179 -                  (GLdouble) renderer->viewport.h,
   1.180 -                  (GLdouble) 0, 0.0, 1.0);
   1.181 +    if (renderer->target) {
   1.182 +        data->glOrtho((GLdouble) 0,
   1.183 +                      (GLdouble) renderer->viewport.w,
   1.184 +                      (GLdouble) 0,
   1.185 +                      (GLdouble) renderer->viewport.h,
   1.186 +                       0.0, 1.0);
   1.187 +    } else {
   1.188 +        data->glOrtho((GLdouble) 0,
   1.189 +                      (GLdouble) renderer->viewport.w,
   1.190 +                      (GLdouble) renderer->viewport.h,
   1.191 +                      (GLdouble) 0,
   1.192 +                       0.0, 1.0);
   1.193 +    }
   1.194      return 0;
   1.195  }
   1.196