Skip to content

Latest commit

 

History

History
2353 lines (2098 loc) · 93.7 KB

SDL_render_d3d11.cpp

File metadata and controls

2353 lines (2098 loc) · 93.7 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
/*
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
#ifdef __WINRT__
#include <windows.ui.core.h>
#include <windows.foundation.h>
Aug 28, 2013
Aug 28, 2013
29
30
31
32
33
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
#include <windows.ui.xaml.media.dxinterop.h>
#endif
34
35
36
37
#endif
extern "C" {
#include "../../core/windows/SDL_windows.h"
Oct 26, 2013
Oct 26, 2013
38
#include "SDL_hints.h"
39
40
41
42
43
44
45
46
47
48
49
50
51
//#include "SDL_loadso.h"
#include "SDL_system.h"
#include "SDL_syswm.h"
#include "../SDL_sysrender.h"
#include "SDL_log.h"
#include "../../video/SDL_sysvideo.h"
//#include "stdio.h"
}
#include <fstream>
#include <string>
#include <vector>
Dec 25, 2013
Dec 25, 2013
52
53
54
55
#include <D3D11_1.h>
#include <DirectXMath.h>
#include <wrl/client.h>
56
57
58
59
60
61
62
63
64
65
using namespace DirectX;
using namespace Microsoft::WRL;
using namespace std;
#ifdef __WINRT__
using namespace Windows::Graphics::Display;
using namespace Windows::UI::Core;
#endif
Nov 2, 2013
Nov 2, 2013
66
67
68
69
/* Texture sampling types */
static const D3D11_FILTER SDL_D3D11_NEAREST_PIXEL_FILTER = D3D11_FILTER_MIN_MAG_MIP_POINT;
static const D3D11_FILTER SDL_D3D11_LINEAR_FILTER = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
Dec 25, 2013
Dec 25, 2013
70
/* Vertex shader, common values */
Dec 25, 2013
Dec 25, 2013
71
struct VertexShaderConstants
Dec 25, 2013
Dec 25, 2013
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{
DirectX::XMFLOAT4X4 model;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
};
/* Per-vertex data */
struct VertexPositionColor
{
DirectX::XMFLOAT3 pos;
DirectX::XMFLOAT2 tex;
DirectX::XMFLOAT4 color;
};
/* Per-texture data */
typedef struct
{
Microsoft::WRL::ComPtr<ID3D11Texture2D> mainTexture;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> mainTextureResourceView;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mainTextureRenderTargetView;
SDL_PixelFormat * pixelFormat;
Microsoft::WRL::ComPtr<ID3D11Texture2D> stagingTexture;
DirectX::XMINT2 lockedTexturePosition;
D3D11_FILTER scaleMode;
} D3D11_TextureData;
/* Private renderer data */
typedef struct
{
Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext1> d3dContext;
Microsoft::WRL::ComPtr<IDXGISwapChain1> swapChain;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mainRenderTargetView;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> currentOffscreenRenderTargetView;
Microsoft::WRL::ComPtr<ID3D11InputLayout> inputLayout;
Microsoft::WRL::ComPtr<ID3D11Buffer> vertexBuffer;
Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> texturePixelShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> colorPixelShader;
Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeBlend;
Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeAdd;
Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeMod;
Microsoft::WRL::ComPtr<ID3D11SamplerState> nearestPixelSampler;
Microsoft::WRL::ComPtr<ID3D11SamplerState> linearSampler;
Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer;
D3D_FEATURE_LEVEL featureLevel;
// Vertex buffer constants:
Dec 25, 2013
Dec 25, 2013
120
VertexShaderConstants vertexShaderConstantsData;
Dec 25, 2013
Dec 25, 2013
121
122
123
124
125
126
127
128
129
130
Microsoft::WRL::ComPtr<ID3D11Buffer> vertexShaderConstants;
// Cached renderer properties.
DirectX::XMFLOAT2 windowSizeInDIPs;
DirectX::XMFLOAT2 renderTargetSize;
Windows::Graphics::Display::DisplayOrientations orientation;
// Transform used for display orientation.
DirectX::XMFLOAT4X4 orientationTransform3D;
} D3D11_RenderData;
Dec 25, 2013
Dec 25, 2013
132
Dec 26, 2013
Dec 26, 2013
133
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
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
304
305
306
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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Direct3D 11.x shaders
SDL's shaders are compiled into SDL itself, to simplify distribution.
All Direct3D 11.x shaders were compiled with the following:
fxc /E"main" /T "<TYPE>" /Fo"<OUTPUT FILE>" "<INPUT FILE>"
Variables:
- <TYPE>: the type of shader. A table of utilized shader types is
listed below.
- <OUTPUT FILE>: where to store compiled output
- <INPUT FILE>: where to read shader source code from
Shader types:
- ps_4_0_level_9_1: Pixel shader for Windows 8+, including Windows RT
- vs_4_0_level_9_1: Vertex shader for Windows 8+, including Windows RT
- ps_4_0_level_9_3: Pixel shader for Windows Phone 8
- vs_4_0_level_9_3: Vertex shader for Windows Phone 8
*/
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
#define D3D11_USE_SHADER_MODEL_4_0_level_9_3
#else
#define D3D11_USE_SHADER_MODEL_4_0_level_9_1
#endif
/* The texture-rendering pixel shader:
--- D3D11_PixelShader_Textures.hlsl ---
Texture2D theTexture : register(t0);
SamplerState theSampler : register(s0);
struct PixelShaderInput
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR0;
};
float4 main(PixelShaderInput input) : SV_TARGET
{
return theTexture.Sample(theSampler, input.tex) * input.color;
}
*/
#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
static const DWORD D3D11_PixelShader_Textures[] = {
0x43425844, 0x6299b59f, 0x155258f2, 0x873ab86a, 0xfcbb6dcd, 0x00000001,
0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
0x00280000, 0x00000000, 0xffff0200, 0x0200001f, 0x80000000, 0xb0030000,
0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
};
#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
static const DWORD D3D11_PixelShader_Textures[] = {
0x43425844, 0x5876569a, 0x01b6c87e, 0x8447454f, 0xc7f3ef10, 0x00000001,
0x00000330, 0x00000006, 0x00000038, 0x000000c0, 0x0000015c, 0x000001d8,
0x00000288, 0x000002fc, 0x396e6f41, 0x00000080, 0x00000080, 0xffff0200,
0x00000058, 0x00000028, 0x00280000, 0x00280000, 0x00280000, 0x00240001,
0x00280000, 0x00000000, 0xffff0201, 0x0200001f, 0x80000000, 0xb0030000,
0x0200001f, 0x80000000, 0xb00f0001, 0x0200001f, 0x90000000, 0xa00f0800,
0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000005, 0x800f0000,
0x80e40000, 0xb0e40001, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff,
0x52444853, 0x00000094, 0x00000040, 0x00000025, 0x0300005a, 0x00106000,
0x00000000, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062,
0x00101032, 0x00000001, 0x03001062, 0x001010f2, 0x00000002, 0x03000065,
0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x09000045, 0x001000f2,
0x00000000, 0x00101046, 0x00000001, 0x00107e46, 0x00000000, 0x00106000,
0x00000000, 0x07000038, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000,
0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074, 0x00000003,
0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x00000000, 0x00000000,
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452, 0x000000a8,
0x00000000, 0x00000000, 0x00000002, 0x0000001c, 0xffff0400, 0x00000100,
0x00000072, 0x0000005c, 0x00000003, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000001, 0x00000001, 0x00000067, 0x00000002, 0x00000005,
0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x53656874,
0x6c706d61, 0x74007265, 0x65546568, 0x72757478, 0x694d0065, 0x736f7263,
0x2074666f, 0x20295228, 0x4c534c48, 0x61685320, 0x20726564, 0x706d6f43,
0x72656c69, 0x332e3920, 0x32392e30, 0x312e3030, 0x34383336, 0xababab00,
0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
0x00000000, 0x00000003, 0x00000001, 0x00000303, 0x00000065, 0x00000000,
0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
};
#else
#error "An appropriate 'textures' pixel shader is not defined"
#endif
/* The color-only-rendering pixel shader:
--- D3D11_PixelShader_Colors.hlsl ---
struct PixelShaderInput
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR0;
};
float4 main(PixelShaderInput input) : SV_TARGET
{
return input.color;
}
*/
#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
static const DWORD D3D11_PixelShader_Colors[] = {
0x43425844, 0xd74c28fe, 0xa1eb8804, 0x269d512a, 0x7699723d, 0x00000001,
0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
0x00240000, 0xffff0200, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054
};
#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
static const DWORD D3D11_PixelShader_Colors[] = {
0x43425844, 0x93f6ccfc, 0x5f919270, 0x7a11aa4f, 0x9148e931, 0x00000001,
0x00000240, 0x00000006, 0x00000038, 0x00000084, 0x000000c4, 0x00000140,
0x00000198, 0x0000020c, 0x396e6f41, 0x00000044, 0x00000044, 0xffff0200,
0x00000020, 0x00000024, 0x00240000, 0x00240000, 0x00240000, 0x00240000,
0x00240000, 0xffff0201, 0x0200001f, 0x80000000, 0xb00f0001, 0x02000001,
0x800f0800, 0xb0e40001, 0x0000ffff, 0x52444853, 0x00000038, 0x00000040,
0x0000000e, 0x03001062, 0x001010f2, 0x00000002, 0x03000065, 0x001020f2,
0x00000000, 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000002,
0x0100003e, 0x54415453, 0x00000074, 0x00000002, 0x00000000, 0x00000000,
0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x46454452, 0x00000050, 0x00000000, 0x00000000,
0x00000000, 0x0000001c, 0xffff0400, 0x00000100, 0x0000001c, 0x7263694d,
0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072,
0x6c69706d, 0x39207265, 0x2e30332e, 0x30303239, 0x3336312e, 0xab003438,
0x4e475349, 0x0000006c, 0x00000003, 0x00000008, 0x00000050, 0x00000000,
0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000,
0x00000000, 0x00000003, 0x00000001, 0x00000003, 0x00000065, 0x00000000,
0x00000000, 0x00000003, 0x00000002, 0x00000f0f, 0x505f5653, 0x5449534f,
0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f, 0x4e47534f,
0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000,
0x00000003, 0x00000000, 0x0000000f, 0x545f5653, 0x45475241, 0xabab0054,
};
#else
#error "An appropriate 'colors' pixel shader is not defined."
#endif
/* The sole vertex shader:
--- D3D11_VertexShader.hlsl ---
#pragma pack_matrix( row_major )
cbuffer VertexShaderConstants : register(b0)
{
matrix model;
matrix view;
matrix projection;
};
struct VertexShaderInput
{
float3 pos : POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR0;
};
struct VertexShaderOutput
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR0;
};
VertexShaderOutput main(VertexShaderInput input)
{
VertexShaderOutput output;
float4 pos = float4(input.pos, 1.0f);
// Transform the vertex position into projected space.
pos = mul(pos, model);
pos = mul(pos, view);
pos = mul(pos, projection);
output.pos = pos;
// Pass through texture coordinates and color values without transformation
output.tex = input.tex;
output.color = input.color;
return output;
}
*/
#if defined(D3D11_USE_SHADER_MODEL_4_0_level_9_1)
static const DWORD D3D11_VertexShader[] = {
0x43425844, 0x3f31b022, 0x2ffad8b8, 0xd6c45cbd, 0xa7894c28, 0x00000001,
0x00000690, 0x00000006, 0x00000038, 0x000001b8, 0x00000418, 0x00000494,
0x000005ac, 0x0000061c, 0x396e6f41, 0x00000178, 0x00000178, 0xfffe0200,
0x00000144, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
0x00300001, 0x00000000, 0x0001000c, 0x00000000, 0x00000000, 0xfffe0200,
0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x03000005, 0x800f0001,
0x80550000, 0xa0e4000a, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40009,
0x80e40001, 0x04000004, 0x800f0001, 0x80aa0000, 0xa0e4000b, 0x80e40001,
0x04000004, 0x800f0000, 0x80ff0000, 0xa0e4000c, 0x80e40001, 0x04000004,
0xc0030000, 0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000,
0x80e40000, 0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001,
0x90e40002, 0x0000ffff, 0x52444853, 0x00000258, 0x00010040, 0x00000096,
0x04000059, 0x00208e46, 0x00000000, 0x0000000c, 0x0300005f, 0x00101072,
0x00000000, 0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2,
0x00000002, 0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065,
0x00102032, 0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068,
0x00000002, 0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000,
0x00208e46, 0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000,
0x00101006, 0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46,
0x00000000, 0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000,
0x00208e46, 0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000,
0x001000f2, 0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000,
0x00000003, 0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000,
0x00208e46, 0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001,
0x00100006, 0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46,
0x00000001, 0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000,
0x00208e46, 0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032,
0x001000f2, 0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000,
0x00000007, 0x00100e46, 0x00000001, 0x08000038, 0x001000f2, 0x00000001,
0x00100556, 0x00000000, 0x00208e46, 0x00000000, 0x00000009, 0x0a000032,
0x001000f2, 0x00000001, 0x00100006, 0x00000000, 0x00208e46, 0x00000000,
0x00000008, 0x00100e46, 0x00000001, 0x0a000032, 0x001000f2, 0x00000001,
0x00100aa6, 0x00000000, 0x00208e46, 0x00000000, 0x0000000a, 0x00100e46,
0x00000001, 0x0a000032, 0x001020f2, 0x00000000, 0x00100ff6, 0x00000000,
0x00208e46, 0x00000000, 0x0000000b, 0x00100e46, 0x00000001, 0x05000036,
0x00102032, 0x00000001, 0x00101046, 0x00000001, 0x05000036, 0x001020f2,
0x00000002, 0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074,
0x0000000f, 0x00000002, 0x00000000, 0x00000006, 0x00000004, 0x00000000,
0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452,
0x00000110, 0x00000001, 0x00000054, 0x00000001, 0x0000001c, 0xfffe0400,
0x00000100, 0x000000dc, 0x0000003c, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000001, 0x00000001, 0x74726556, 0x68537865,
0x72656461, 0x736e6f43, 0x746e6174, 0xabab0073, 0x0000003c, 0x00000003,
0x0000006c, 0x000000c0, 0x00000000, 0x00000000, 0x000000b4, 0x00000000,
0x00000040, 0x00000002, 0x000000bc, 0x00000000, 0x000000cc, 0x00000040,
0x00000040, 0x00000002, 0x000000bc, 0x00000000, 0x000000d1, 0x00000080,
0x00000040, 0x00000002, 0x000000bc, 0x00000000, 0x65646f6d, 0xabab006c,
0x00030002, 0x00040004, 0x00000000, 0x00000000, 0x77656976, 0x6f727000,
0x7463656a, 0x006e6f69, 0x7263694d, 0x666f736f, 0x52282074, 0x4c482029,
0x53204c53, 0x65646168, 0x6f432072, 0x6c69706d, 0x39207265, 0x2e30332e,
0x30303239, 0x3336312e, 0xab003438, 0x4e475349, 0x00000068, 0x00000003,
0x00000008, 0x00000050, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
0x00000707, 0x00000059, 0x00000000, 0x00000000, 0x00000003, 0x00000001,
0x00000303, 0x00000062, 0x00000000, 0x00000000, 0x00000003, 0x00000002,
0x00000f0f, 0x49534f50, 0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044,
0x00524f4c, 0x4e47534f, 0x0000006c, 0x00000003, 0x00000008, 0x00000050,
0x00000000, 0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c,
0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000c03, 0x00000065,
0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x0000000f, 0x505f5653,
0x5449534f, 0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f,
};
#elif defined(D3D11_USE_SHADER_MODEL_4_0_level_9_3)
static const DWORD D3D11_VertexShader[] = {
0x43425844, 0xacfd840a, 0x6a6ae1e1, 0xc3649c43, 0x8bfc0816, 0x00000001,
0x00000690, 0x00000006, 0x00000038, 0x000001b8, 0x00000418, 0x00000494,
0x000005ac, 0x0000061c, 0x396e6f41, 0x00000178, 0x00000178, 0xfffe0200,
0x00000144, 0x00000034, 0x00240001, 0x00300000, 0x00300000, 0x00240000,
0x00300001, 0x00000000, 0x0001000c, 0x00000000, 0x00000000, 0xfffe0201,
0x0200001f, 0x80000005, 0x900f0000, 0x0200001f, 0x80010005, 0x900f0001,
0x0200001f, 0x80020005, 0x900f0002, 0x03000005, 0x800f0000, 0x90550000,
0xa0e40002, 0x04000004, 0x800f0000, 0x90000000, 0xa0e40001, 0x80e40000,
0x04000004, 0x800f0000, 0x90aa0000, 0xa0e40003, 0x80e40000, 0x03000002,
0x800f0000, 0x80e40000, 0xa0e40004, 0x03000005, 0x800f0001, 0x80550000,
0xa0e40006, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40005, 0x80e40001,
0x04000004, 0x800f0001, 0x80aa0000, 0xa0e40007, 0x80e40001, 0x04000004,
0x800f0000, 0x80ff0000, 0xa0e40008, 0x80e40001, 0x03000005, 0x800f0001,
0x80550000, 0xa0e4000a, 0x04000004, 0x800f0001, 0x80000000, 0xa0e40009,
0x80e40001, 0x04000004, 0x800f0001, 0x80aa0000, 0xa0e4000b, 0x80e40001,
0x04000004, 0x800f0000, 0x80ff0000, 0xa0e4000c, 0x80e40001, 0x04000004,
0xc0030000, 0x80ff0000, 0xa0e40000, 0x80e40000, 0x02000001, 0xc00c0000,
0x80e40000, 0x02000001, 0xe0030000, 0x90e40001, 0x02000001, 0xe00f0001,
0x90e40002, 0x0000ffff, 0x52444853, 0x00000258, 0x00010040, 0x00000096,
0x04000059, 0x00208e46, 0x00000000, 0x0000000c, 0x0300005f, 0x00101072,
0x00000000, 0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x001010f2,
0x00000002, 0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065,
0x00102032, 0x00000001, 0x03000065, 0x001020f2, 0x00000002, 0x02000068,
0x00000002, 0x08000038, 0x001000f2, 0x00000000, 0x00101556, 0x00000000,
0x00208e46, 0x00000000, 0x00000001, 0x0a000032, 0x001000f2, 0x00000000,
0x00101006, 0x00000000, 0x00208e46, 0x00000000, 0x00000000, 0x00100e46,
0x00000000, 0x0a000032, 0x001000f2, 0x00000000, 0x00101aa6, 0x00000000,
0x00208e46, 0x00000000, 0x00000002, 0x00100e46, 0x00000000, 0x08000000,
0x001000f2, 0x00000000, 0x00100e46, 0x00000000, 0x00208e46, 0x00000000,
0x00000003, 0x08000038, 0x001000f2, 0x00000001, 0x00100556, 0x00000000,
0x00208e46, 0x00000000, 0x00000005, 0x0a000032, 0x001000f2, 0x00000001,
0x00100006, 0x00000000, 0x00208e46, 0x00000000, 0x00000004, 0x00100e46,
0x00000001, 0x0a000032, 0x001000f2, 0x00000001, 0x00100aa6, 0x00000000,
0x00208e46, 0x00000000, 0x00000006, 0x00100e46, 0x00000001, 0x0a000032,
0x001000f2, 0x00000000, 0x00100ff6, 0x00000000, 0x00208e46, 0x00000000,
0x00000007, 0x00100e46, 0x00000001, 0x08000038, 0x001000f2, 0x00000001,
0x00100556, 0x00000000, 0x00208e46, 0x00000000, 0x00000009, 0x0a000032,
0x001000f2, 0x00000001, 0x00100006, 0x00000000, 0x00208e46, 0x00000000,
0x00000008, 0x00100e46, 0x00000001, 0x0a000032, 0x001000f2, 0x00000001,
0x00100aa6, 0x00000000, 0x00208e46, 0x00000000, 0x0000000a, 0x00100e46,
0x00000001, 0x0a000032, 0x001020f2, 0x00000000, 0x00100ff6, 0x00000000,
0x00208e46, 0x00000000, 0x0000000b, 0x00100e46, 0x00000001, 0x05000036,
0x00102032, 0x00000001, 0x00101046, 0x00000001, 0x05000036, 0x001020f2,
0x00000002, 0x00101e46, 0x00000002, 0x0100003e, 0x54415453, 0x00000074,
0x0000000f, 0x00000002, 0x00000000, 0x00000006, 0x00000004, 0x00000000,
0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x46454452,
0x00000110, 0x00000001, 0x00000054, 0x00000001, 0x0000001c, 0xfffe0400,
0x00000100, 0x000000dc, 0x0000003c, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000001, 0x00000001, 0x74726556, 0x68537865,
0x72656461, 0x736e6f43, 0x746e6174, 0xabab0073, 0x0000003c, 0x00000003,
0x0000006c, 0x000000c0, 0x00000000, 0x00000000, 0x000000b4, 0x00000000,
0x00000040, 0x00000002, 0x000000bc, 0x00000000, 0x000000cc, 0x00000040,
0x00000040, 0x00000002, 0x000000bc, 0x00000000, 0x000000d1, 0x00000080,
0x00000040, 0x00000002, 0x000000bc, 0x00000000, 0x65646f6d, 0xabab006c,
0x00030002, 0x00040004, 0x00000000, 0x00000000, 0x77656976, 0x6f727000,
0x7463656a, 0x006e6f69, 0x7263694d, 0x666f736f, 0x52282074, 0x4c482029,
0x53204c53, 0x65646168, 0x6f432072, 0x6c69706d, 0x39207265, 0x2e30332e,
0x30303239, 0x3336312e, 0xab003438, 0x4e475349, 0x00000068, 0x00000003,
0x00000008, 0x00000050, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
0x00000707, 0x00000059, 0x00000000, 0x00000000, 0x00000003, 0x00000001,
0x00000303, 0x00000062, 0x00000000, 0x00000000, 0x00000003, 0x00000002,
0x00000f0f, 0x49534f50, 0x4e4f4954, 0x58455400, 0x524f4f43, 0x4f430044,
0x00524f4c, 0x4e47534f, 0x0000006c, 0x00000003, 0x00000008, 0x00000050,
0x00000000, 0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c,
0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000c03, 0x00000065,
0x00000000, 0x00000000, 0x00000003, 0x00000002, 0x0000000f, 0x505f5653,
0x5449534f, 0x004e4f49, 0x43584554, 0x44524f4f, 0x4c4f4300, 0xab00524f
};
#else
#error "An appropriate vertex shader is not defined."
#endif
Dec 25, 2013
Dec 25, 2013
522
/* Direct3D 11.1 renderer implementation */
523
524
525
526
527
528
529
530
531
532
533
534
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
static void D3D11_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
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 *srcPixels,
int srcPitch);
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);
static int D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
static int D3D11_UpdateViewport(SDL_Renderer * renderer);
Aug 14, 2013
Aug 14, 2013
535
static int D3D11_UpdateClipRect(SDL_Renderer * renderer);
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
static int D3D11_RenderClear(SDL_Renderer * renderer);
static int D3D11_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_FPoint * points, int count);
static int D3D11_RenderDrawLines(SDL_Renderer * renderer,
const SDL_FPoint * points, int count);
static int D3D11_RenderFillRects(SDL_Renderer * renderer,
const SDL_FRect * rects, int count);
static int D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect);
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);
static int D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
static void D3D11_RenderPresent(SDL_Renderer * renderer);
static void D3D11_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
/* Direct3D 11.1 Internal Functions */
HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
HRESULT D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer);
HRESULT D3D11_HandleDeviceLost(SDL_Renderer * renderer);
extern "C" SDL_RenderDriver D3D11_RenderDriver = {
D3D11_CreateRenderer,
{
"direct3d 11.1",
(
SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC |
SDL_RENDERER_TARGETTEXTURE
), // flags. see SDL_RendererFlags
2, // num_texture_formats
{ // texture_formats
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_ARGB8888
},
0, // max_texture_width: will be filled in later
0 // max_texture_height: will be filled in later
}
};
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;
}
}
//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;
}
data->featureLevel = (D3D_FEATURE_LEVEL) 0;
data->windowSizeInDIPs = XMFLOAT2(0, 0);
data->renderTargetSize = XMFLOAT2(0, 0);
renderer->WindowEvent = D3D11_WindowEvent;
renderer->CreateTexture = D3D11_CreateTexture;
renderer->UpdateTexture = D3D11_UpdateTexture;
renderer->LockTexture = D3D11_LockTexture;
renderer->UnlockTexture = D3D11_UnlockTexture;
renderer->SetRenderTarget = D3D11_SetRenderTarget;
renderer->UpdateViewport = D3D11_UpdateViewport;
Aug 14, 2013
Aug 14, 2013
643
renderer->UpdateClipRect = D3D11_UpdateClipRect;
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
renderer->RenderClear = D3D11_RenderClear;
renderer->RenderDrawPoints = D3D11_RenderDrawPoints;
renderer->RenderDrawLines = D3D11_RenderDrawLines;
renderer->RenderFillRects = D3D11_RenderFillRects;
renderer->RenderCopy = D3D11_RenderCopy;
renderer->RenderCopyEx = D3D11_RenderCopyEx;
renderer->RenderReadPixels = D3D11_RenderReadPixels;
renderer->RenderPresent = D3D11_RenderPresent;
renderer->DestroyTexture = D3D11_DestroyTexture;
renderer->DestroyRenderer = D3D11_DestroyRenderer;
renderer->info = D3D11_RenderDriver.info;
renderer->driverdata = data;
// 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;
/* Initialize Direct3D resources */
if (FAILED(D3D11_CreateDeviceResources(renderer))) {
D3D11_DestroyRenderer(renderer);
return NULL;
}
if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
D3D11_DestroyRenderer(renderer);
return NULL;
}
// TODO, WinRT: fill in renderer->info.texture_formats where appropriate
return renderer;
}
static void
D3D11_DestroyRenderer(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
if (data) {
delete data;
data = NULL;
}
}
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)) {
Dec 25, 2013
Dec 25, 2013
710
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBlendState", result);
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
return result;
}
return S_OK;
}
// Create resources that depend on the device.
HRESULT
D3D11_CreateDeviceResources(SDL_Renderer * renderer)
{
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;
Oct 26, 2013
Oct 26, 2013
727
// Make sure Direct3D's debugging feature gets used, if the app requests it.
Dec 26, 2013
Dec 26, 2013
728
729
730
const char *hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
if (hint) {
if (*hint == '1') {
Oct 26, 2013
Oct 26, 2013
731
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
Dec 26, 2013
Dec 26, 2013
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
762
763
764
765
766
// 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)) {
Dec 25, 2013
Dec 25, 2013
767
WIN_SetErrorFromHRESULT(__FUNCTION__ ", D3D11CreateDevice", result);
768
769
770
771
772
773
return result;
}
// Get the Direct3D 11.1 API device and context interfaces.
result = device.As(&(data->d3dDevice));
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
774
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device to ID3D11Device1", result);
775
776
777
778
779
return result;
}
result = context.As(&data->d3dContext);
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
780
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11DeviceContext to ID3D11DeviceContext1", result);
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
return result;
}
//
// 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;
}
//
// Load in SDL's one and only vertex shader:
//
result = data->d3dDevice->CreateVertexShader(
Dec 26, 2013
Dec 26, 2013
814
815
D3D11_VertexShader,
sizeof(D3D11_VertexShader),
816
817
818
819
nullptr,
&data->vertexShader
);
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
820
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateVertexShader", result);
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
return result;
}
//
// Create an input layout for SDL's vertex shader:
//
const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
result = data->d3dDevice->CreateInputLayout(
vertexDesc,
ARRAYSIZE(vertexDesc),
Dec 26, 2013
Dec 26, 2013
837
838
D3D11_VertexShader,
sizeof(D3D11_VertexShader),
839
840
841
&data->inputLayout
);
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
842
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateInputLayout", result);
843
844
845
846
847
848
return result;
}
//
// Load in SDL's pixel shaders
//
Dec 26, 2013
Dec 26, 2013
849
850
851
852
853
854
855
result = data->d3dDevice->CreatePixelShader(
D3D11_PixelShader_Textures,
sizeof(D3D11_PixelShader_Textures),
nullptr,
&data->texturePixelShader
);
856
if (FAILED(result)) {
Dec 26, 2013
Dec 26, 2013
857
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['textures' shader]", result);
858
859
860
return result;
}
Dec 26, 2013
Dec 26, 2013
861
862
863
864
865
866
result = data->d3dDevice->CreatePixelShader(
D3D11_PixelShader_Colors,
sizeof(D3D11_PixelShader_Colors),
nullptr,
&data->colorPixelShader
);
867
if (FAILED(result)) {
Dec 26, 2013
Dec 26, 2013
868
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreatePixelShader ['color' shader]", result);
869
870
871
872
873
874
return result;
}
//
// Setup space to hold vertex shader constants:
//
Dec 25, 2013
Dec 25, 2013
875
CD3D11_BUFFER_DESC constantBufferDesc(sizeof(VertexShaderConstants), D3D11_BIND_CONSTANT_BUFFER);
876
877
878
879
880
881
result = data->d3dDevice->CreateBuffer(
&constantBufferDesc,
nullptr,
&data->vertexShaderConstants
);
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
882
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateBuffer [vertex shader constants]", result);
883
884
885
886
887
888
889
890
891
892
return result;
}
//
// Make sure that the vertex buffer, if already created, gets freed.
// It will be recreated later.
//
data->vertexBuffer = nullptr;
//
Nov 2, 2013
Nov 2, 2013
893
// Create samplers to use when drawing textures:
894
895
//
D3D11_SAMPLER_DESC samplerDesc;
Nov 2, 2013
Nov 2, 2013
896
samplerDesc.Filter = SDL_D3D11_NEAREST_PIXEL_FILTER;
897
898
899
900
901
902
903
904
905
906
907
908
909
910
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,
Nov 2, 2013
Nov 2, 2013
911
912
913
&data->nearestPixelSampler
);
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
914
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [nearest-pixel filter]", result);
Nov 2, 2013
Nov 2, 2013
915
916
917
918
919
920
921
return result;
}
samplerDesc.Filter = SDL_D3D11_LINEAR_FILTER;
result = data->d3dDevice->CreateSamplerState(
&samplerDesc,
&data->linearSampler
922
923
);
if (FAILED(result)) {
Dec 25, 2013
Dec 25, 2013
924
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateSamplerState [linear filter]", result);
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
return result;
}
//
// 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)) {
Dec 25, 2013
Dec 25, 2013
945
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ID3D11Device1::CreateRasterizerState", result);
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
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
return result;
}
//
// 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;
}
//
// All done!
//
return S_OK;
}
#ifdef __WINRT__
static ABI::Windows::UI::Core::ICoreWindow *
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
{
SDL_Window * sdlWindow = renderer->window;
if ( ! renderer->window ) {
return nullptr;
}