Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1719 lines (1506 loc) · 59.5 KB

SDL_render_d3d11.cpp

File metadata and controls

1719 lines (1506 loc) · 59.5 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Simple DirectMedia Layer
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
extern "C" {
#include "../../core/windows/SDL_windows.h"
//#include "SDL_hints.h"
//#include "SDL_loadso.h"
Feb 9, 2013
Feb 9, 2013
30
#include "SDL_system.h"
31
32
#include "SDL_syswm.h"
#include "../SDL_sysrender.h"
Feb 18, 2013
Feb 18, 2013
33
34
#include "SDL_log.h"
#include "../../video/SDL_sysvideo.h"
35
36
37
//#include "stdio.h"
}
Feb 9, 2013
Feb 9, 2013
38
39
40
41
#include <fstream>
#include <string>
#include <vector>
42
43
#include "SDL_render_d3d11_cpp.h"
Feb 9, 2013
Feb 9, 2013
44
45
46
47
using namespace DirectX;
using namespace Microsoft::WRL;
using namespace std;
Feb 9, 2013
Feb 9, 2013
48
49
50
51
52
#ifdef __WINRT__
using namespace Windows::Graphics::Display;
using namespace Windows::UI::Core;
#endif
Feb 9, 2013
Feb 9, 2013
53
/* Direct3D 11.1 renderer implementation */
54
55
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
Feb 9, 2013
Feb 9, 2013
56
57
static void D3D11_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
Feb 9, 2013
Feb 9, 2013
58
59
60
61
static int D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
Mar 31, 2013
Mar 31, 2013
62
63
64
static int D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void D3D11_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
65
66
//static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
Feb 9, 2013
Feb 9, 2013
67
static int D3D11_RenderClear(SDL_Renderer * renderer);
Mar 25, 2013
Mar 25, 2013
68
69
static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_FPoint * points, int count);
Mar 25, 2013
Mar 25, 2013
70
71
static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
const SDL_FPoint * points, int count);
Feb 16, 2013
Feb 16, 2013
72
73
static int D3D11_RenderFillRects(SDL_Renderer * renderer,
const SDL_FRect * rects, int count);
Feb 9, 2013
Feb 9, 2013
74
75
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect);
Apr 2, 2013
Apr 2, 2013
76
77
78
static int D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
Apr 2, 2013
Apr 2, 2013
79
80
static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
Feb 3, 2013
Feb 3, 2013
81
static void D3D11_RenderPresent(SDL_Renderer * renderer);
Feb 9, 2013
Feb 9, 2013
82
83
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
Feb 9, 2013
Feb 9, 2013
84
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
Feb 9, 2013
Feb 9, 2013
86
/* Direct3D 11.1 Internal Functions */
Feb 9, 2013
Feb 9, 2013
87
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
Feb 9, 2013
Feb 9, 2013
88
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
Feb 9, 2013
Feb 9, 2013
89
90
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
Feb 24, 2013
Feb 24, 2013
92
extern "C" SDL_RenderDriver D3D11_RenderDriver = {
93
94
D3D11_CreateRenderer,
{
Feb 24, 2013
Feb 24, 2013
95
"direct3d 11.1",
Feb 24, 2013
Feb 24, 2013
96
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), // flags. see SDL_RendererFlags
Feb 24, 2013
Feb 24, 2013
97
98
99
100
101
2, // num_texture_formats
{ // texture_formats
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_ARGB8888
},
Feb 24, 2013
Feb 24, 2013
102
103
0, // max_texture_width: will be filled in later
0 // max_texture_height: will be filled in later
Feb 24, 2013
Feb 24, 2013
104
105
106
}
};
Apr 2, 2013
Apr 2, 2013
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
static Uint32
DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat) {
switch (dxgiFormat) {
case DXGI_FORMAT_B8G8R8A8_UNORM:
return SDL_PIXELFORMAT_ARGB8888;
case DXGI_FORMAT_B8G8R8X8_UNORM:
return SDL_PIXELFORMAT_RGB888;
default:
return SDL_PIXELFORMAT_UNKNOWN;
}
}
static DXGI_FORMAT
SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
{
switch (sdlFormat) {
case SDL_PIXELFORMAT_ARGB8888:
return DXGI_FORMAT_B8G8R8A8_UNORM;
case SDL_PIXELFORMAT_RGB888:
return DXGI_FORMAT_B8G8R8X8_UNORM;
default:
return DXGI_FORMAT_UNKNOWN;
}
}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//typedef struct
//{
// float x, y, z;
// DWORD color;
// float u, v;
//} Vertex;
SDL_Renderer *
D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
D3D11_RenderData *data;
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
SDL_zerop(renderer);
data = new D3D11_RenderData; // Use the C++ 'new' operator to make sure the struct's members initialize using C++ rules
if (!data) {
SDL_OutOfMemory();
return NULL;
}
Feb 9, 2013
Feb 9, 2013
159
data->featureLevel = (D3D_FEATURE_LEVEL) 0;
Feb 9, 2013
Feb 9, 2013
160
161
data->windowSizeInDIPs = XMFLOAT2(0, 0);
data->renderTargetSize = XMFLOAT2(0, 0);
Feb 9, 2013
Feb 9, 2013
163
renderer->WindowEvent = D3D11_WindowEvent;
Feb 9, 2013
Feb 9, 2013
164
165
renderer->CreateTexture = D3D11_CreateTexture;
renderer->UpdateTexture = D3D11_UpdateTexture;
Mar 31, 2013
Mar 31, 2013
166
167
renderer->LockTexture = D3D11_LockTexture;
renderer->UnlockTexture = D3D11_UnlockTexture;
168
169
//renderer->SetRenderTarget = D3D11_SetRenderTarget;
renderer->UpdateViewport = D3D11_UpdateViewport;
Feb 9, 2013
Feb 9, 2013
170
renderer->RenderClear = D3D11_RenderClear;
Mar 25, 2013
Mar 25, 2013
171
renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
Mar 25, 2013
Mar 25, 2013
172
renderer->RenderDrawLines = D3D11_RenderDrawLines;
Feb 16, 2013
Feb 16, 2013
173
renderer->RenderFillRects = D3D11_RenderFillRects;
Feb 9, 2013
Feb 9, 2013
174
renderer->RenderCopy = D3D11_RenderCopy;
Apr 2, 2013
Apr 2, 2013
175
renderer->RenderCopyEx = D3D11_RenderCopyEx;
Apr 2, 2013
Apr 2, 2013
176
renderer->RenderReadPixels = D3D11_RenderReadPixels;
Feb 3, 2013
Feb 3, 2013
177
renderer->RenderPresent = D3D11_RenderPresent;
Feb 9, 2013
Feb 9, 2013
178
renderer->DestroyTexture = D3D11_DestroyTexture;
Feb 9, 2013
Feb 9, 2013
179
renderer->DestroyRenderer = D3D11_DestroyRenderer;
180
181
renderer->info = D3D11_RenderDriver.info;
renderer->info.flags = SDL_RENDERER_ACCELERATED;
Feb 9, 2013
Feb 9, 2013
182
renderer->driverdata = data;
Feb 9, 2013
Feb 9, 2013
184
185
186
// HACK: make sure the SDL_Renderer references the SDL_Window data now, in
// order to give init functions access to the underlying window handle:
renderer->window = window;
Feb 9, 2013
Feb 9, 2013
188
189
190
191
192
193
194
195
196
/* Initialize Direct3D resources */
if (FAILED(D3D11_CreateDeviceResources(renderer))) {
D3D11_DestroyRenderer(renderer);
return NULL;
}
if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
D3D11_DestroyRenderer(renderer);
return NULL;
}
Feb 9, 2013
Feb 9, 2013
198
199
// TODO, WinRT: fill in renderer->info.texture_formats where appropriate
200
201
202
return renderer;
}
Feb 9, 2013
Feb 9, 2013
203
204
205
206
207
208
209
210
211
212
static void
D3D11_DestroyRenderer(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
if (data) {
delete data;
data = NULL;
}
}
Feb 9, 2013
Feb 9, 2013
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
static bool
D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
{
ifstream in(fileName, ios::in | ios::binary);
if (!in) {
return false;
}
in.seekg(0, ios::end);
out.resize((size_t) in.tellg());
in.seekg(0, ios::beg);
in.read(&out[0], out.size());
return in.good();
}
static bool
D3D11_ReadShaderContents(const wstring & shaderName, vector<char> & out)
{
wstring fileName;
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
Apr 2, 2013
Apr 2, 2013
234
fileName = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_INSTALLED_LOCATION);
Feb 9, 2013
Feb 9, 2013
235
236
fileName += L"\\SDL_VS2012_WinRT\\";
#elif WINAPI_FAMILY == WINAPI_PHONE_APP
Apr 2, 2013
Apr 2, 2013
237
fileName = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_INSTALLED_LOCATION);
Feb 9, 2013
Feb 9, 2013
238
239
240
241
242
243
244
fileName += L"\\";
#endif
// WinRT, TODO: test Direct3D 11.1 shader loading on Win32
fileName += shaderName;
return D3D11_ReadFileContents(fileName, out);
}
Feb 16, 2013
Feb 16, 2013
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
static HRESULT
D3D11_LoadPixelShader(SDL_Renderer * renderer,
const wstring & shaderName,
ID3D11PixelShader ** shaderOutput)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK;
vector<char> fileData;
if (!D3D11_ReadShaderContents(shaderName, fileData)) {
SDL_SetError("Unable to open SDL's pixel shader file.");
return E_FAIL;
}
result = data->d3dDevice->CreatePixelShader(
&fileData[0],
fileData.size(),
nullptr,
shaderOutput
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
return S_OK;
}
Feb 16, 2013
Feb 16, 2013
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
static HRESULT
D3D11_CreateBlendMode(SDL_Renderer * renderer,
BOOL enableBlending,
D3D11_BLEND srcBlend,
D3D11_BLEND destBlend,
ID3D11BlendState ** blendStateOutput)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK;
D3D11_BLEND_DESC blendDesc;
memset(&blendDesc, 0, sizeof(blendDesc));
blendDesc.AlphaToCoverageEnable = FALSE;
blendDesc.IndependentBlendEnable = FALSE;
blendDesc.RenderTarget[0].BlendEnable = enableBlending;
blendDesc.RenderTarget[0].SrcBlend = srcBlend;
blendDesc.RenderTarget[0].DestBlend = destBlend;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
result = data->d3dDevice->CreateBlendState(&blendDesc, blendStateOutput);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
return S_OK;
}
Feb 9, 2013
Feb 9, 2013
304
// Create resources that depend on the device.
Feb 9, 2013
Feb 9, 2013
305
HRESULT
Feb 9, 2013
Feb 9, 2013
306
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
Feb 9, 2013
Feb 9, 2013
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
// This flag adds support for surfaces with a different color channel ordering
// than the API default. It is required for compatibility with Direct2D.
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(_DEBUG)
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
// This array defines the set of DirectX hardware feature levels this app will support.
// Note the ordering should be preserved.
// Don't forget to declare your application's minimum required feature level in its
// description. All applications are assumed to support 9.1 unless otherwise stated.
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
// Create the Direct3D 11 API device object and a corresponding context.
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
HRESULT result = S_OK;
result = D3D11CreateDevice(
nullptr, // Specify nullptr to use the default adapter.
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
creationFlags, // Set set debug and Direct2D compatibility flags.
featureLevels, // List of feature levels this app can support.
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
&device, // Returns the Direct3D device created.
&data->featureLevel, // Returns feature level of device created.
&context // Returns the device immediate context.
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
// Get the Direct3D 11.1 API device and context interfaces.
Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice1;
result = device.As(&(data->d3dDevice));
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
result = context.As(&data->d3dContext);
if (FAILED(result)) {
return result;
}
Feb 24, 2013
Feb 24, 2013
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Make note of the maximum texture size
// Max texture sizes are documented on MSDN, at:
// http://msdn.microsoft.com/en-us/library/windows/apps/ff476876.aspx
//
switch (data->d3dDevice->GetFeatureLevel()) {
case D3D_FEATURE_LEVEL_11_1:
case D3D_FEATURE_LEVEL_11_0:
renderer->info.max_texture_width = renderer->info.max_texture_height = 16384;
break;
case D3D_FEATURE_LEVEL_10_1:
case D3D_FEATURE_LEVEL_10_0:
renderer->info.max_texture_width = renderer->info.max_texture_height = 8192;
break;
case D3D_FEATURE_LEVEL_9_3:
renderer->info.max_texture_width = renderer->info.max_texture_height = 4096;
break;
case D3D_FEATURE_LEVEL_9_2:
case D3D_FEATURE_LEVEL_9_1:
renderer->info.max_texture_width = renderer->info.max_texture_height = 2048;
break;
}
Feb 9, 2013
Feb 9, 2013
394
395
396
//
// Load in SDL's one and only vertex shader:
//
Feb 16, 2013
Feb 16, 2013
397
vector<char> fileData;
Feb 24, 2013
Feb 24, 2013
398
if (!D3D11_ReadShaderContents(L"SDL_D3D11_VertexShader_Default.cso", fileData)) {
Feb 9, 2013
Feb 9, 2013
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
SDL_SetError("Unable to open SDL's vertex shader file.");
return E_FAIL;
}
result = data->d3dDevice->CreateVertexShader(
&fileData[0],
fileData.size(),
nullptr,
&data->vertexShader
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
//
// Create an input layout for SDL's vertex shader:
//
const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
Feb 16, 2013
Feb 16, 2013
419
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
Feb 9, 2013
Feb 9, 2013
420
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
Feb 16, 2013
Feb 16, 2013
421
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
Feb 9, 2013
Feb 9, 2013
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
};
result = data->d3dDevice->CreateInputLayout(
vertexDesc,
ARRAYSIZE(vertexDesc),
&fileData[0],
fileData.size(),
&data->inputLayout
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
//
Feb 16, 2013
Feb 16, 2013
437
// Load in SDL's pixel shaders
Feb 9, 2013
Feb 9, 2013
438
//
Apr 8, 2013
Apr 8, 2013
439
result = D3D11_LoadPixelShader(renderer, L"SDL_D3D11_PixelShader_TextureColored.cso", &data->texturePixelShader);
Feb 16, 2013
Feb 16, 2013
440
441
442
if (FAILED(result)) {
// D3D11_LoadPixelShader will have aleady set the SDL error
return result;
Feb 9, 2013
Feb 9, 2013
443
444
}
Feb 16, 2013
Feb 16, 2013
445
result = D3D11_LoadPixelShader(renderer, L"SDL_D3D11_PixelShader_FixedColor.cso", &data->colorPixelShader);
Feb 9, 2013
Feb 9, 2013
446
if (FAILED(result)) {
Feb 16, 2013
Feb 16, 2013
447
// D3D11_LoadPixelShader will have aleady set the SDL error
Feb 9, 2013
Feb 9, 2013
448
449
450
return result;
}
Feb 10, 2013
Feb 10, 2013
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//
// Setup space to hold vertex shader constants:
//
CD3D11_BUFFER_DESC constantBufferDesc(sizeof(SDL_VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
result = data->d3dDevice->CreateBuffer(
&constantBufferDesc,
nullptr,
&data->vertexShaderConstants
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
Feb 9, 2013
Feb 9, 2013
465
//
Feb 13, 2013
Feb 13, 2013
466
467
// Make sure that the vertex buffer, if already created, gets freed.
// It will be recreated later.
Feb 9, 2013
Feb 9, 2013
468
//
Feb 13, 2013
Feb 13, 2013
469
data->vertexBuffer = nullptr;
Feb 9, 2013
Feb 9, 2013
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
// Create a sampler to use when drawing textures:
//
D3D11_SAMPLER_DESC samplerDesc;
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0.0f;
samplerDesc.BorderColor[1] = 0.0f;
samplerDesc.BorderColor[2] = 0.0f;
samplerDesc.BorderColor[3] = 0.0f;
samplerDesc.MinLOD = 0.0f;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
result = data->d3dDevice->CreateSamplerState(
&samplerDesc,
&data->mainSampler
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
Feb 13, 2013
Feb 13, 2013
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//
// Setup the Direct3D rasterizer
//
D3D11_RASTERIZER_DESC rasterDesc;
memset(&rasterDesc, 0, sizeof(rasterDesc));
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_NONE;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;
result = data->d3dDevice->CreateRasterizerState(&rasterDesc, &data->mainRasterizer);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
Feb 16, 2013
Feb 16, 2013
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//
// Create blending states:
//
result = D3D11_CreateBlendMode(
renderer,
TRUE,
D3D11_BLEND_SRC_ALPHA,
D3D11_BLEND_INV_SRC_ALPHA,
&data->blendModeBlend);
if (FAILED(result)) {
// D3D11_CreateBlendMode will set the SDL error, if it fails
return result;
}
result = D3D11_CreateBlendMode(
renderer,
TRUE,
D3D11_BLEND_SRC_ALPHA,
D3D11_BLEND_ONE,
&data->blendModeAdd);
if (FAILED(result)) {
// D3D11_CreateBlendMode will set the SDL error, if it fails
return result;
}
result = D3D11_CreateBlendMode(
renderer,
TRUE,
D3D11_BLEND_ZERO,
D3D11_BLEND_SRC_COLOR,
&data->blendModeMod);
if (FAILED(result)) {
// D3D11_CreateBlendMode will set the SDL error, if it fails
return result;
}
Feb 9, 2013
Feb 9, 2013
554
555
556
557
558
559
//
// All done!
//
return S_OK;
}
Feb 9, 2013
Feb 9, 2013
560
561
#ifdef __WINRT__
Feb 9, 2013
Feb 9, 2013
562
static CoreWindow ^
Feb 9, 2013
Feb 9, 2013
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
{
SDL_Window * sdlWindow = renderer->window;
if ( ! renderer->window ) {
return nullptr;
}
SDL_SysWMinfo sdlWindowInfo;
SDL_VERSION(&sdlWindowInfo.version);
if ( ! SDL_GetWindowWMInfo(sdlWindow, &sdlWindowInfo) ) {
return nullptr;
}
if (sdlWindowInfo.subsystem != SDL_SYSWM_WINDOWSRT) {
return nullptr;
}
CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
if ( ! coreWindowPointer ) {
return nullptr;
}
return *coreWindowPointer;
}
Feb 9, 2013
Feb 9, 2013
588
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
Feb 9, 2013
Feb 9, 2013
589
590
591
592
593
594
595
596
static float
D3D11_ConvertDipsToPixels(float dips)
{
static const float dipsPerInch = 96.0f;
return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
}
#endif
Feb 9, 2013
Feb 9, 2013
597
// Initialize all resources that change when the window's size changes.
Feb 9, 2013
Feb 9, 2013
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// WinRT, TODO: get D3D11_CreateWindowSizeDependentResources working on Win32
HRESULT
D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK;
Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
// Store the window bounds so the next time we get a SizeChanged event we can
// avoid rebuilding everything if the size is identical.
data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
// Calculate the necessary swap chain and render target size in pixels.
float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
float windowHeight = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.y);
// The width and height of the swap chain must be based on the window's
// landscape-oriented width and height. If the window is in a portrait
// orientation, the dimensions must be reversed.
data->orientation = DisplayProperties::CurrentOrientation;
Feb 18, 2013
Feb 18, 2013
619
const bool swapDimensions =
Feb 9, 2013
Feb 9, 2013
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
data->orientation == DisplayOrientations::Portrait ||
data->orientation == DisplayOrientations::PortraitFlipped;
data->renderTargetSize.x = swapDimensions ? windowHeight : windowWidth;
data->renderTargetSize.y = swapDimensions ? windowWidth : windowHeight;
if(data->swapChain != nullptr)
{
// If the swap chain already exists, resize it.
result = data->swapChain->ResizeBuffers(
2, // Double-buffered swap chain.
static_cast<UINT>(data->renderTargetSize.x),
static_cast<UINT>(data->renderTargetSize.y),
DXGI_FORMAT_B8G8R8A8_UNORM,
0
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
}
else
{
// Otherwise, create a new one using the same adapter as the existing Direct3D device.
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
swapChainDesc.Height = static_cast<UINT>(data->renderTargetSize.y);
swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
swapChainDesc.Stereo = false;
swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
#else
swapChainDesc.Scaling = DXGI_SCALING_NONE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
#endif
swapChainDesc.Flags = 0;
ComPtr<IDXGIDevice1> dxgiDevice;
result = data->d3dDevice.As(&dxgiDevice);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
ComPtr<IDXGIAdapter> dxgiAdapter;
result = dxgiDevice->GetAdapter(&dxgiAdapter);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
ComPtr<IDXGIFactory2> dxgiFactory;
result = dxgiAdapter->GetParent(
__uuidof(IDXGIFactory2),
&dxgiFactory
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
result = dxgiFactory->CreateSwapChainForCoreWindow(
data->d3dDevice.Get(),
reinterpret_cast<IUnknown*>(coreWindow),
&swapChainDesc,
nullptr, // Allow on all displays.
&data->swapChain
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
// Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
// ensures that the application will only render after each VSync, minimizing power consumption.
result = dxgiDevice->SetMaximumFrameLatency(1);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
}
// Set the proper orientation for the swap chain, and generate the
// 3D matrix transformation for rendering to the rotated swap chain.
DXGI_MODE_ROTATION rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
switch (data->orientation)
{
case DisplayOrientations::Landscape:
rotation = DXGI_MODE_ROTATION_IDENTITY;
break;
case DisplayOrientations::Portrait:
rotation = DXGI_MODE_ROTATION_ROTATE270;
break;
case DisplayOrientations::LandscapeFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE180;
break;
case DisplayOrientations::PortraitFlipped:
rotation = DXGI_MODE_ROTATION_ROTATE90;
break;
default:
throw ref new Platform::FailureException();
}
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
// TODO, WinRT: Windows Phone does not have the IDXGISwapChain1::SetRotation method. Check if an alternative is available, or needed.
result = data->swapChain->SetRotation(rotation);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
#endif
// Create a render target view of the swap chain back buffer.
ComPtr<ID3D11Texture2D> backBuffer;
result = data->swapChain->GetBuffer(
0,
__uuidof(ID3D11Texture2D),
&backBuffer
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
result = data->d3dDevice->CreateRenderTargetView(
backBuffer.Get(),
nullptr,
&data->renderTargetView
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
Feb 17, 2013
Feb 17, 2013
762
763
764
765
if (D3D11_UpdateViewport(renderer) != 0) {
// D3D11_UpdateViewport will set the SDL error if it fails.
return E_FAIL;
}
Feb 9, 2013
Feb 9, 2013
766
767
768
769
return S_OK;
}
Feb 9, 2013
Feb 9, 2013
770
// This method is called when the window's size changes.
Feb 9, 2013
Feb 9, 2013
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
HRESULT
D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK;
Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
if (coreWindow->Bounds.Width != data->windowSizeInDIPs.x ||
coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
data->orientation != DisplayProperties::CurrentOrientation)
{
ID3D11RenderTargetView* nullViews[] = {nullptr};
data->d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
data->renderTargetView = nullptr;
data->d3dContext->Flush();
result = D3D11_CreateWindowSizeDependentResources(renderer);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
}
return S_OK;
}
HRESULT
D3D11_HandleDeviceLost(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK;
Feb 9, 2013
Feb 9, 2013
802
// Reset these member variables to ensure that D3D11_UpdateForWindowSizeChange recreates all resources.
Feb 9, 2013
Feb 9, 2013
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
data->windowSizeInDIPs.x = 0;
data->windowSizeInDIPs.y = 0;
data->swapChain = nullptr;
result = D3D11_CreateDeviceResources(renderer);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
result = D3D11_UpdateForWindowSizeChange(renderer);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
return S_OK;
}
Feb 9, 2013
Feb 9, 2013
822
823
824
825
826
827
828
829
830
831
static void
D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
//D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
if (event->event == SDL_WINDOWEVENT_RESIZED) {
D3D11_UpdateForWindowSizeChange(renderer);
}
}
Feb 9, 2013
Feb 9, 2013
832
833
834
835
836
837
static int
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
D3D11_TextureData *textureData;
HRESULT result;
Apr 2, 2013
Apr 2, 2013
838
839
840
841
842
DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
if (textureFormat == SDL_PIXELFORMAT_UNKNOWN) {
SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
__FUNCTION__, texture->format);
return -1;
Feb 24, 2013
Feb 24, 2013
843
}
Feb 9, 2013
Feb 9, 2013
844
845
846
847
848
849
850
textureData = new D3D11_TextureData;
if (!textureData) {
SDL_OutOfMemory();
return -1;
}
textureData->pixelFormat = SDL_AllocFormat(texture->format);
Mar 31, 2013
Mar 31, 2013
851
textureData->lockedTexturePosition = XMINT2(0, 0);
Feb 9, 2013
Feb 9, 2013
852
853
854
855
856
857
858
859
texture->driverdata = textureData;
D3D11_TEXTURE2D_DESC textureDesc = {0};
textureDesc.Width = texture->w;
textureDesc.Height = texture->h;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
Feb 24, 2013
Feb 24, 2013
860
textureDesc.Format = textureFormat;
Feb 9, 2013
Feb 9, 2013
861
862
863
864
865
866
867
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DYNAMIC;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
textureDesc.MiscFlags = 0;
Mar 31, 2013
Mar 31, 2013
868
869
#if 0
// Fill the texture with a non-black color, for debugging purposes:
Feb 9, 2013
Feb 9, 2013
870
const int numPixels = textureDesc.Width * textureDesc.Height;
Mar 31, 2013
Mar 31, 2013
871
const int pixelSizeInBytes = textureData->pixelFormat->BytesPerPixel;
Feb 9, 2013
Feb 9, 2013
872
std::vector<uint8> initialTexturePixels(numPixels * pixelSizeInBytes, 0x00);
Mar 31, 2013
Mar 31, 2013
873
874
875
876
877
878
for (int i = 0; i < (numPixels * pixelSizeInBytes); i += pixelSizeInBytes) {
initialTexturePixels[i+0] = 0xff;
initialTexturePixels[i+1] = 0xff;
initialTexturePixels[i+2] = 0x00;
initialTexturePixels[i+3] = 0xff;
}
Feb 9, 2013
Feb 9, 2013
879
880
881
882
D3D11_SUBRESOURCE_DATA initialTextureData = {0};
initialTextureData.pSysMem = (void *)&(initialTexturePixels[0]);
initialTextureData.SysMemPitch = textureDesc.Width * pixelSizeInBytes;
initialTextureData.SysMemSlicePitch = numPixels * pixelSizeInBytes;
Mar 31, 2013
Mar 31, 2013
883
884
#endif
Feb 9, 2013
Feb 9, 2013
885
886
result = rendererData->d3dDevice->CreateTexture2D(
&textureDesc,
Mar 31, 2013
Mar 31, 2013
887
NULL, // &initialTextureData,
Feb 9, 2013
Feb 9, 2013
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
&textureData->mainTexture
);
if (FAILED(result)) {
D3D11_DestroyTexture(renderer, texture);
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return -1;
}
D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
resourceViewDesc.Format = textureDesc.Format;
resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
resourceViewDesc.Texture2D.MostDetailedMip = 0;
resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
result = rendererData->d3dDevice->CreateShaderResourceView(
textureData->mainTexture.Get(),
&resourceViewDesc,
&textureData->mainTextureResourceView
);
if (FAILED(result)) {
D3D11_DestroyTexture(renderer, texture);
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return -1;
}
return 0;
}
static void
D3D11_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture)
{
D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
if (textureData) {
if (textureData->pixelFormat) {
SDL_FreeFormat(textureData->pixelFormat);
textureData->pixelFormat = NULL;
}
delete textureData;
texture->driverdata = NULL;
}
}
static int
D3D11_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch)
{
D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
HRESULT result = S_OK;
D3D11_MAPPED_SUBRESOURCE textureMemory = {0};
result = rendererData->d3dContext->Map(
textureData->mainTexture.Get(),
0,
D3D11_MAP_WRITE_DISCARD,
0,
&textureMemory
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return -1;
}
// Copy pixel data to the locked texture's memory:
for (int y = 0; y < rect->h; ++y) {
memcpy(
((Uint8 *)textureMemory.pData) + (textureMemory.RowPitch * y),
((Uint8 *)pixels) + (pitch * y),
pitch
);
}
// Clean up a bit, then commit the texture's memory back to Direct3D:
rendererData->d3dContext->Unmap(
textureData->mainTexture.Get(),
0);
return 0;
}
Mar 31, 2013
Mar 31, 2013
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static int
D3D11_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
HRESULT result = S_OK;
if (textureData->stagingTexture) {
SDL_SetError("texture is already locked");
return -1;
}
// Create a 'staging' texture, which will be used to write to a portion
// of the main texture. This is necessary, as Direct3D 11.1 does not
// have the ability to write a CPU-bound pixel buffer to a rectangular
// subrect of a texture. Direct3D 11.1 can, however, write a pixel
// buffer to an entire texture, hence the use of a staging texture.
D3D11_TEXTURE2D_DESC stagingTextureDesc;
textureData->mainTexture->GetDesc(&stagingTextureDesc);
stagingTextureDesc.Width = rect->w;
stagingTextureDesc.Height = rect->h;
stagingTextureDesc.BindFlags = 0;
stagingTextureDesc.MiscFlags = 0;
stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
result = rendererData->d3dDevice->CreateTexture2D(
&stagingTextureDesc,
NULL,
&textureData->stagingTexture);