src/render/direct3d11/SDL_render_d3d11.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 02 Feb 2013 19:32:44 -0500
changeset 8400 1315402d9028
child 8401 29ecd2eca4af
permissions -rw-r--r--
WinRT: started refactoring Direct3D 11.1 code into a new SDL_Renderer backend
dludwig@8400
     1
/*
dludwig@8400
     2
  Simple DirectMedia Layer
dludwig@8400
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8400
     4
dludwig@8400
     5
  This software is provided 'as-is', without any express or implied
dludwig@8400
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8400
     7
  arising from the use of this software.
dludwig@8400
     8
dludwig@8400
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8400
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8400
    11
  freely, subject to the following restrictions:
dludwig@8400
    12
dludwig@8400
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8400
    14
     claim that you wrote the original software. If you use this software
dludwig@8400
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8400
    16
     appreciated but is not required.
dludwig@8400
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8400
    18
     misrepresented as being the original software.
dludwig@8400
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8400
    20
*/
dludwig@8400
    21
dludwig@8400
    22
#include "SDL_config.h"
dludwig@8400
    23
dludwig@8400
    24
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8400
    25
dludwig@8400
    26
extern "C" {
dludwig@8400
    27
#include "../../core/windows/SDL_windows.h"
dludwig@8400
    28
//#include "SDL_hints.h"
dludwig@8400
    29
//#include "SDL_loadso.h"
dludwig@8400
    30
#include "SDL_syswm.h"
dludwig@8400
    31
#include "../SDL_sysrender.h"
dludwig@8400
    32
//#include "stdio.h"
dludwig@8400
    33
}
dludwig@8400
    34
dludwig@8400
    35
#include "SDL_render_d3d11_cpp.h"
dludwig@8400
    36
dludwig@8400
    37
/* Direct3D renderer implementation */
dludwig@8400
    38
dludwig@8400
    39
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
dludwig@8400
    40
//static void D3D11_WindowEvent(SDL_Renderer * renderer,
dludwig@8400
    41
//                            const SDL_WindowEvent *event);
dludwig@8400
    42
//static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    43
//static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    44
//                             const SDL_Rect * rect, const void *pixels,
dludwig@8400
    45
//                             int pitch);
dludwig@8400
    46
//static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    47
//                           const SDL_Rect * rect, void **pixels, int *pitch);
dludwig@8400
    48
//static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    49
//static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
dludwig@8400
    50
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
dludwig@8400
    51
//static int D3D11_RenderClear(SDL_Renderer * renderer);
dludwig@8400
    52
//static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
dludwig@8400
    53
//                                const SDL_FPoint * points, int count);
dludwig@8400
    54
//static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
dludwig@8400
    55
//                               const SDL_FPoint * points, int count);
dludwig@8400
    56
//static int D3D11_RenderFillRects(SDL_Renderer * renderer,
dludwig@8400
    57
//                               const SDL_FRect * rects, int count);
dludwig@8400
    58
//static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    59
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect);
dludwig@8400
    60
//static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
dludwig@8400
    61
//                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
dludwig@8400
    62
//                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
dludwig@8400
    63
//static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
dludwig@8400
    64
//                                Uint32 format, void * pixels, int pitch);
dludwig@8400
    65
//static void D3D11_RenderPresent(SDL_Renderer * renderer);
dludwig@8400
    66
//static void D3D11_DestroyTexture(SDL_Renderer * renderer,
dludwig@8400
    67
//                               SDL_Texture * texture);
dludwig@8400
    68
//static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
dludwig@8400
    69
dludwig@8400
    70
dludwig@8400
    71
extern "C" {
dludwig@8400
    72
    SDL_RenderDriver D3D11_RenderDriver = {
dludwig@8400
    73
    D3D11_CreateRenderer,
dludwig@8400
    74
    {
dludwig@8400
    75
     "direct3d",
dludwig@8400
    76
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
dludwig@8400
    77
     1,
dludwig@8400
    78
     {SDL_PIXELFORMAT_ARGB8888},
dludwig@8400
    79
     0,
dludwig@8400
    80
     0}
dludwig@8400
    81
    };
dludwig@8400
    82
}
dludwig@8400
    83
dludwig@8400
    84
//typedef struct
dludwig@8400
    85
//{
dludwig@8400
    86
//    float x, y, z;
dludwig@8400
    87
//    DWORD color;
dludwig@8400
    88
//    float u, v;
dludwig@8400
    89
//} Vertex;
dludwig@8400
    90
dludwig@8400
    91
SDL_Renderer *
dludwig@8400
    92
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
dludwig@8400
    93
{
dludwig@8400
    94
    SDL_Renderer *renderer;
dludwig@8400
    95
    D3D11_RenderData *data;
dludwig@8400
    96
//    SDL_SysWMinfo windowinfo;
dludwig@8400
    97
 //   HRESULT result;
dludwig@8400
    98
 //   D3DPRESENT_PARAMETERS pparams;
dludwig@8400
    99
 //   IDirect3DSwapChain9 *chain;
dludwig@8400
   100
 //   D3DCAPS9 caps;
dludwig@8400
   101
 //   Uint32 window_flags;
dludwig@8400
   102
 //   int w, h;
dludwig@8400
   103
 //   SDL_DisplayMode fullscreen_mode;
dludwig@8400
   104
 //   D3DMATRIX matrix;
dludwig@8400
   105
 //   int d3dxVersion;
dludwig@8400
   106
	//char d3dxDLLFile[50];
dludwig@8400
   107
dludwig@8400
   108
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
dludwig@8400
   109
    if (!renderer) {
dludwig@8400
   110
        SDL_OutOfMemory();
dludwig@8400
   111
        return NULL;
dludwig@8400
   112
    }
dludwig@8400
   113
    SDL_zerop(renderer);
dludwig@8400
   114
dludwig@8400
   115
    data = new D3D11_RenderData;    // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
dludwig@8400
   116
    if (!data) {
dludwig@8400
   117
        delete data;
dludwig@8400
   118
        SDL_OutOfMemory();
dludwig@8400
   119
        return NULL;
dludwig@8400
   120
    }
dludwig@8400
   121
dludwig@8400
   122
    // TODO: Create Direct3D Object(s)
dludwig@8400
   123
dludwig@8400
   124
    //renderer->WindowEvent = D3D11_WindowEvent;
dludwig@8400
   125
    //renderer->CreateTexture = D3D11_CreateTexture;
dludwig@8400
   126
    //renderer->UpdateTexture = D3D11_UpdateTexture;
dludwig@8400
   127
    //renderer->LockTexture = D3D11_LockTexture;
dludwig@8400
   128
    //renderer->UnlockTexture = D3D11_UnlockTexture;
dludwig@8400
   129
    //renderer->SetRenderTarget = D3D11_SetRenderTarget;
dludwig@8400
   130
    renderer->UpdateViewport = D3D11_UpdateViewport;
dludwig@8400
   131
    //renderer->RenderClear = D3D11_RenderClear;
dludwig@8400
   132
    //renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
dludwig@8400
   133
    //renderer->RenderDrawLines = D3D11_RenderDrawLines;
dludwig@8400
   134
    //renderer->RenderFillRects = D3D11_RenderFillRects;
dludwig@8400
   135
    //renderer->RenderCopy = D3D11_RenderCopy;
dludwig@8400
   136
    //renderer->RenderCopyEx = D3D11_RenderCopyEx;
dludwig@8400
   137
    //renderer->RenderReadPixels = D3D11_RenderReadPixels;
dludwig@8400
   138
    //renderer->RenderPresent = D3D11_RenderPresent;
dludwig@8400
   139
    //renderer->DestroyTexture = D3D11_DestroyTexture;
dludwig@8400
   140
    //renderer->DestroyRenderer = D3D11_DestroyRenderer;
dludwig@8400
   141
    renderer->info = D3D11_RenderDriver.info;
dludwig@8400
   142
    renderer->driverdata = data;
dludwig@8400
   143
dludwig@8400
   144
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
dludwig@8400
   145
dludwig@8400
   146
    //SDL_VERSION(&windowinfo.version);
dludwig@8400
   147
    //SDL_GetWindowWMInfo(window, &windowinfo);
dludwig@8400
   148
dludwig@8400
   149
    //window_flags = SDL_GetWindowFlags(window);
dludwig@8400
   150
    //SDL_GetWindowSize(window, &w, &h);
dludwig@8400
   151
    //SDL_GetWindowDisplayMode(window, &fullscreen_mode);
dludwig@8400
   152
dludwig@8400
   153
    //SDL_zero(pparams);
dludwig@8400
   154
    //pparams.hDeviceWindow = windowinfo.info.win.window;
dludwig@8400
   155
    //pparams.BackBufferWidth = w;
dludwig@8400
   156
    //pparams.BackBufferHeight = h;
dludwig@8400
   157
    //if (window_flags & SDL_WINDOW_FULLSCREEN) {
dludwig@8400
   158
    //    pparams.BackBufferFormat =
dludwig@8400
   159
    //        PixelFormatToD3DFMT(fullscreen_mode.format);
dludwig@8400
   160
    //} else {
dludwig@8400
   161
    //    pparams.BackBufferFormat = D3DFMT_UNKNOWN;
dludwig@8400
   162
    //}
dludwig@8400
   163
    //pparams.BackBufferCount = 1;
dludwig@8400
   164
    //pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
dludwig@8400
   165
dludwig@8400
   166
    //if (window_flags & SDL_WINDOW_FULLSCREEN) {
dludwig@8400
   167
    //    pparams.Windowed = FALSE;
dludwig@8400
   168
    //    pparams.FullScreen_RefreshRateInHz =
dludwig@8400
   169
    //        fullscreen_mode.refresh_rate;
dludwig@8400
   170
    //} else {
dludwig@8400
   171
    //    pparams.Windowed = TRUE;
dludwig@8400
   172
    //    pparams.FullScreen_RefreshRateInHz = 0;
dludwig@8400
   173
    //}
dludwig@8400
   174
    //if (flags & SDL_RENDERER_PRESENTVSYNC) {
dludwig@8400
   175
    //    pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
dludwig@8400
   176
    //} else {
dludwig@8400
   177
    //    pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
dludwig@8400
   178
    //}
dludwig@8400
   179
dludwig@8400
   180
    ///* FIXME: Which adapter? */
dludwig@8400
   181
    //data->adapter = D3DADAPTER_DEFAULT;
dludwig@8400
   182
    //IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
dludwig@8400
   183
dludwig@8400
   184
    //result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
dludwig@8400
   185
    //                                 D3DDEVTYPE_HAL,
dludwig@8400
   186
    //                                 pparams.hDeviceWindow,
dludwig@8400
   187
    //                                 D3DCREATE_FPU_PRESERVE | ((caps.
dludwig@8400
   188
    //                                  DevCaps &
dludwig@8400
   189
    //                                  D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
dludwig@8400
   190
    //                                 D3DCREATE_HARDWARE_VERTEXPROCESSING :
dludwig@8400
   191
    //                                 D3DCREATE_SOFTWARE_VERTEXPROCESSING),
dludwig@8400
   192
    //                                 &pparams, &data->device);
dludwig@8400
   193
    //if (FAILED(result)) {
dludwig@8400
   194
    //    D3D11_DestroyRenderer(renderer);
dludwig@8400
   195
    //    D3D11_SetError("CreateDevice()", result);
dludwig@8400
   196
    //    return NULL;
dludwig@8400
   197
    //}
dludwig@8400
   198
    //data->beginScene = SDL_TRUE;
dludwig@8400
   199
    //data->scaleMode = D3DTEXF_FORCE_DWORD;
dludwig@8400
   200
dludwig@8400
   201
    ///* Get presentation parameters to fill info */
dludwig@8400
   202
    //result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
dludwig@8400
   203
    //if (FAILED(result)) {
dludwig@8400
   204
    //    D3D11_DestroyRenderer(renderer);
dludwig@8400
   205
    //    D3D11_SetError("GetSwapChain()", result);
dludwig@8400
   206
    //    return NULL;
dludwig@8400
   207
    //}
dludwig@8400
   208
    //result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
dludwig@8400
   209
    //if (FAILED(result)) {
dludwig@8400
   210
    //    IDirect3DSwapChain9_Release(chain);
dludwig@8400
   211
    //    D3D11_DestroyRenderer(renderer);
dludwig@8400
   212
    //    D3D11_SetError("GetPresentParameters()", result);
dludwig@8400
   213
    //    return NULL;
dludwig@8400
   214
    //}
dludwig@8400
   215
    //IDirect3DSwapChain9_Release(chain);
dludwig@8400
   216
    //if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
dludwig@8400
   217
    //    renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
dludwig@8400
   218
    //}
dludwig@8400
   219
    //data->pparams = pparams;
dludwig@8400
   220
dludwig@8400
   221
    //IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
dludwig@8400
   222
    //renderer->info.max_texture_width = caps.MaxTextureWidth;
dludwig@8400
   223
    //renderer->info.max_texture_height = caps.MaxTextureHeight;
dludwig@8400
   224
    //if (caps.NumSimultaneousRTs >= 2) {
dludwig@8400
   225
    //    renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
dludwig@8400
   226
    //}
dludwig@8400
   227
dludwig@8400
   228
    ///* Set up parameters for rendering */
dludwig@8400
   229
    //IDirect3DDevice9_SetVertexShader(data->device, NULL);
dludwig@8400
   230
    //IDirect3DDevice9_SetFVF(data->device,
dludwig@8400
   231
    //                        D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
dludwig@8400
   232
    //IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
dludwig@8400
   233
    //IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
dludwig@8400
   234
    //                                D3DCULL_NONE);
dludwig@8400
   235
    //IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
dludwig@8400
   236
    ///* Enable color modulation by diffuse color */
dludwig@8400
   237
    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
dludwig@8400
   238
    //                                      D3DTOP_MODULATE);
dludwig@8400
   239
    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
dludwig@8400
   240
    //                                      D3DTA_TEXTURE);
dludwig@8400
   241
    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
dludwig@8400
   242
    //                                      D3DTA_DIFFUSE);
dludwig@8400
   243
    ///* Enable alpha modulation by diffuse alpha */
dludwig@8400
   244
    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
dludwig@8400
   245
    //                                      D3DTOP_MODULATE);
dludwig@8400
   246
    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
dludwig@8400
   247
    //                                      D3DTA_TEXTURE);
dludwig@8400
   248
    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
dludwig@8400
   249
    //                                      D3DTA_DIFFUSE);
dludwig@8400
   250
    ///* Disable second texture stage, since we're done */
dludwig@8400
   251
    //IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
dludwig@8400
   252
    //                                      D3DTOP_DISABLE);
dludwig@8400
   253
    //IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
dludwig@8400
   254
    //                                      D3DTOP_DISABLE);
dludwig@8400
   255
dludwig@8400
   256
    ///* Store the default render target */
dludwig@8400
   257
    //IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
dludwig@8400
   258
    //data->currentRenderTarget = NULL;
dludwig@8400
   259
dludwig@8400
   260
    ///* Set an identity world and view matrix */
dludwig@8400
   261
    //matrix.m[0][0] = 1.0f;
dludwig@8400
   262
    //matrix.m[0][1] = 0.0f;
dludwig@8400
   263
    //matrix.m[0][2] = 0.0f;
dludwig@8400
   264
    //matrix.m[0][3] = 0.0f;
dludwig@8400
   265
    //matrix.m[1][0] = 0.0f;
dludwig@8400
   266
    //matrix.m[1][1] = 1.0f;
dludwig@8400
   267
    //matrix.m[1][2] = 0.0f;
dludwig@8400
   268
    //matrix.m[1][3] = 0.0f;
dludwig@8400
   269
    //matrix.m[2][0] = 0.0f;
dludwig@8400
   270
    //matrix.m[2][1] = 0.0f;
dludwig@8400
   271
    //matrix.m[2][2] = 1.0f;
dludwig@8400
   272
    //matrix.m[2][3] = 0.0f;
dludwig@8400
   273
    //matrix.m[3][0] = 0.0f;
dludwig@8400
   274
    //matrix.m[3][1] = 0.0f;
dludwig@8400
   275
    //matrix.m[3][2] = 0.0f;
dludwig@8400
   276
    //matrix.m[3][3] = 1.0f;
dludwig@8400
   277
    //IDirect3DDevice9_SetTransform(data->device, D3DTS_WORLD, &matrix);
dludwig@8400
   278
    //IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, &matrix);
dludwig@8400
   279
dludwig@8400
   280
    return renderer;
dludwig@8400
   281
}
dludwig@8400
   282
dludwig@8400
   283
static int
dludwig@8400
   284
D3D11_UpdateViewport(SDL_Renderer * renderer)
dludwig@8400
   285
{
dludwig@8400
   286
    return 0;
dludwig@8400
   287
}
dludwig@8400
   288
dludwig@8400
   289
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
dludwig@8400
   290
dludwig@8400
   291
/* vi: set ts=4 sw=4 expandtab: */