Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Implementation of render targets, by Mason Wheeler and Gabriel Jacobo

Thanks guys!
  • Loading branch information
slouken committed Jan 19, 2012
1 parent 972b4ce commit 3743e40e4071b53012c44e7ac41aabeded0f6d2b
@@ -88,7 +88,8 @@ typedef struct SDL_RendererInfo
typedef enum
{
SDL_TEXTUREACCESS_STATIC, /**< Changes rarely, not lockable */
SDL_TEXTUREACCESS_STREAMING /**< Changes frequently, lockable */
SDL_TEXTUREACCESS_STREAMING, /**< Changes frequently, lockable */
SDL_TEXTUREACCESS_TARGET /**< Texture can be used as a render target */
} SDL_TextureAccess;

/**
@@ -561,6 +562,31 @@ extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
const SDL_Rect * srcrect,
const SDL_Rect * dstrect);


/**
* \fn SDL_bool SDL_RenderTargetSupported(SDL_Renderer *renderer)
*
* \brief Determines whether a window supports the use of render targets
*
* \param renderer The renderer that will be checked
*
* \return SDL_TRUE if supported, SDL_FALSE if not.
*/
extern DECLSPEC SDL_bool SDLCALL SDL_RenderTargetSupported(SDL_Renderer *renderer);

/**
* \fn int SDL_SetTargetTexture(SDL_Renderer *renderer, SDL_Texture *texture)
*
* \brief Set a texture as the current rendering target.
*
* \param renderer The renderer that will be checked
*
* \param texture The targeted texture, or NULL for the default render target
*
* \return 0 on success, or -1 if there is no rendering context current, or the driver doesn't support the requested operation.
*/
extern DECLSPEC int SDLCALL SDL_SetTargetTexture(SDL_Renderer *renderer, SDL_Texture *texture);

/**
* \brief Read pixels from the current rendering target.
*
@@ -1014,9 +1014,9 @@ int
SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
SDL_Rect full_rect;

CHECK_RENDERER_MAGIC(renderer, -1);

/* If 'rect' == NULL, then outline the whole surface */
if (!rect) {
full_rect.x = 0;
@@ -1150,6 +1150,35 @@ SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
format, pixels, pitch);
}

SDL_bool
SDL_RenderTargetSupported(SDL_Renderer *renderer)
{
if ((!renderer) || (!renderer->SetTargetTexture)) {
return SDL_FALSE;
}
return SDL_TRUE;
}

int
SDL_SetTargetTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{

if(!renderer) {
return -1;
}
if (!renderer->SetTargetTexture) {
SDL_Unsupported();
return -1;
}
// Warning: texture==NULL is a valid parameter
if( texture ) {
CHECK_TEXTURE_MAGIC(texture, -1);
if(renderer != texture->renderer) return -1;
}

return renderer->SetTargetTexture(renderer, texture);
}

void
SDL_RenderPresent(SDL_Renderer * renderer)
{
@@ -87,6 +87,7 @@ struct SDL_Renderer
int count);
int (*RenderCopy) (SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
int (*SetTargetTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
void (*RenderPresent) (SDL_Renderer * renderer);
@@ -111,6 +111,7 @@ static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
static int D3D_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void D3D_RenderPresent(SDL_Renderer * renderer);
static void D3D_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
@@ -138,6 +139,12 @@ typedef struct
SDL_bool updateSize;
SDL_bool beginScene;
D3DTEXTUREFILTERTYPE scaleMode;
IDirect3DSurface9 *defaultRenderTarget;
IDirect3DSurface9 *currentRenderTarget;
SDL_bool renderTargetActive;
SDL_Rect viewport_copy;

Uint32 NumSimultaneousRTs;
} D3D_RenderData;

typedef struct
@@ -392,6 +399,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->RenderFillRects = D3D_RenderFillRects;
renderer->RenderCopy = D3D_RenderCopy;
renderer->RenderReadPixels = D3D_RenderReadPixels;
renderer->SetTargetTexture = D3D_SetTargetTexture;
renderer->RenderPresent = D3D_RenderPresent;
renderer->DestroyTexture = D3D_DestroyTexture;
renderer->DestroyRenderer = D3D_DestroyRenderer;
@@ -478,6 +486,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
renderer->info.max_texture_width = caps.MaxTextureWidth;
renderer->info.max_texture_height = caps.MaxTextureHeight;
data->NumSimultaneousRTs = caps.NumSimultaneousRTs;

/* Set up parameters for rendering */
IDirect3DDevice9_SetVertexShader(data->device, NULL);
@@ -507,6 +516,11 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
D3DTOP_DISABLE);

/* Store the default render target */
IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
data->currentRenderTarget = NULL;
data->renderTargetActive = SDL_FALSE;

/* Set an identity world and view matrix */
matrix.m[0][0] = 1.0f;
matrix.m[0][1] = 0.0f;
@@ -554,6 +568,80 @@ GetScaleQuality(void)
}
}

static int
D3D_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
D3D_TextureData *texturedata;
HRESULT result;

if (!renderer) return -1;
D3D_ActivateRenderer(renderer);

if (data->NumSimultaneousRTs < 2) {
SDL_Unsupported();
return -1;
}

// Release the previous render target if it wasn't the default one
if (data->currentRenderTarget != NULL) {
IDirect3DSurface9_Release(data->currentRenderTarget);
data->currentRenderTarget = NULL;
}

/* Prepare an identity world and view matrix */
D3DMATRIX matrix;
matrix.m[0][0] = 1.0f;
matrix.m[0][1] = 0.0f;
matrix.m[0][2] = 0.0f;
matrix.m[0][3] = 0.0f;
matrix.m[1][0] = 0.0f;
matrix.m[1][1] = 1.0f;
matrix.m[1][2] = 0.0f;
matrix.m[1][3] = 0.0f;
matrix.m[2][0] = 0.0f;
matrix.m[2][1] = 0.0f;
matrix.m[2][2] = 1.0f;
matrix.m[2][3] = 0.0f;
matrix.m[3][0] = 0.0f;
matrix.m[3][1] = 0.0f;
matrix.m[3][2] = 0.0f;
matrix.m[3][3] = 1.0f;

if (texture == NULL) {
if (data->renderTargetActive) {
data->renderTargetActive = SDL_FALSE;
IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget );
renderer->viewport = data->viewport_copy;
D3D_UpdateViewport(renderer);
}
return 0;
}
if (renderer != texture->renderer) return -1;

if ( !data->renderTargetActive ) {
data->viewport_copy = renderer->viewport;
}

texturedata = (D3D_TextureData *) texture->driverdata;
result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget );
if(FAILED(result)) {
return -1;
}
result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget );
if(FAILED(result)) {
return -1;
}

data->renderTargetActive = SDL_TRUE;
renderer->viewport.x = 0;
renderer->viewport.y = 0;
renderer->viewport.w = texture->w;
renderer->viewport.h = texture->h;
D3D_UpdateViewport(renderer);
return 0;
}

static int
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
@@ -580,6 +668,11 @@ D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
usage = D3DUSAGE_DYNAMIC;
} else
#endif
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
pool = D3DPOOL_DEFAULT; // D3DPOOL_MANAGED does not work with usage=D3DUSAGE_RENDERTARGET
usage = D3DUSAGE_RENDERTARGET;
}
else
{
pool = D3DPOOL_MANAGED;
usage = 0;
@@ -1187,6 +1280,13 @@ D3D_DestroyRenderer(SDL_Renderer * renderer)
D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;

if (data) {
// Release the render target
IDirect3DSurface9_Release(data->defaultRenderTarget);
if (data->currentRenderTarget != NULL) {
IDirect3DSurface9_Release(data->currentRenderTarget);
data->currentRenderTarget = NULL;
}

if (data->device) {
IDirect3DDevice9_Release(data->device);
}

0 comments on commit 3743e40

Please sign in to comment.