Fixed bug 2625 - Direct3D9 with SDL_TEXTUREACCESS_TARGET textures causes an application crash
authorSam Lantinga <slouken@libsdl.org>
Thu, 04 Jun 2015 00:56:11 -0700
changeset 9702115bc7548a8d
parent 9701 1c0e96906974
child 9703 215d1dcd8e90
Fixed bug 2625 - Direct3D9 with SDL_TEXTUREACCESS_TARGET textures causes an application crash

Roberto

I have debugged the code checking the function calls when Direct3D is the renderer, remember that with software and OpenGL renderers, this issue is not happening.

- Create the texture:
SDL_Texture *pTex = SDL_CreateTexture(pRenderer, iFormat, SDL_TEXTUREACCESS_TARGET, pSurf->w, pSurf->h);

- Update the texture:
SDL_UpdateTexture(pTex, NULL, pSurf->pixels, pSurf->pitch);
SDL_render.c, SDL_UpdateTexture(): return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
SDL_render_d3d.c, D3D_UpdateTexture(): if (D3D_UpdateTextureRep(data->device, &texturedata->texture, texture->format, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
SDL_render_d3d.c, D3D_UpdateTextureRep(): if (D3D_CreateStagingTexture(device, texture) < 0) {
SDL_render_d3d.c, D3D_CreateStagingTexture(): result = IDirect3DDevice9_CreateTexture(..., D3DPOOL_SYSTEMMEM, ...) --> FAIL! with INVALIDCALL code

After checking a bit the Microsoft documentation, I found this:

D3DUSAGE_RENDERTARGET can only be used with D3DPOOL_DEFAULT. (https://msdn.microsoft.com/en-us/library/windows/desktop/bb172625%28v=vs.85%29.aspx)

The call that fails, is using D3DUSAGE_RENDERTARGET with D3DPOOL_SYSTEMMEM which is unsupported, hence the INVALIDCALL return code.
src/render/direct3d/SDL_render_d3d.c
     1.1 --- a/src/render/direct3d/SDL_render_d3d.c	Thu Jun 04 02:12:06 2015 -0400
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Thu Jun 04 00:56:11 2015 -0700
     1.3 @@ -843,7 +843,7 @@
     1.4      HRESULT result;
     1.5  
     1.6      if (texture->staging == NULL) {
     1.7 -        result = IDirect3DDevice9_CreateTexture(device, texture->w, texture->h, 1, texture->usage,
     1.8 +        result = IDirect3DDevice9_CreateTexture(device, texture->w, texture->h, 1, 0,
     1.9              PixelFormatToD3DFMT(texture->format),
    1.10              D3DPOOL_SYSTEMMEM, &texture->staging, NULL);
    1.11          if (FAILED(result)) {