1 | #include <SDL2/SDL.h>
|
---|
2 | #include <SDL2/SDL_vulkan.h>
|
---|
3 |
|
---|
4 | #include <cstring>
|
---|
5 | #include <string>
|
---|
6 | #include <sstream>
|
---|
7 |
|
---|
8 | #include <d3d11_4.h>
|
---|
9 | #include <dxgi1_6.h>
|
---|
10 |
|
---|
11 | #include <wsi/native_wsi.h>
|
---|
12 |
|
---|
13 | #include "../test_utils.h"
|
---|
14 |
|
---|
15 | using namespace dxvk;
|
---|
16 |
|
---|
17 | struct Vertex {
|
---|
18 | float x, y;
|
---|
19 | };
|
---|
20 |
|
---|
21 | struct VsConstants {
|
---|
22 | float x, y;
|
---|
23 | float w, h;
|
---|
24 | };
|
---|
25 |
|
---|
26 | struct VsConstantsPad {
|
---|
27 | VsConstants data;
|
---|
28 | uint32_t pad[60];
|
---|
29 | };
|
---|
30 |
|
---|
31 | struct PsConstants {
|
---|
32 | float r, g, b, a;
|
---|
33 | };
|
---|
34 |
|
---|
35 | struct DrawOptions {
|
---|
36 | bool mapDiscardOnce;
|
---|
37 | bool sortByTexture;
|
---|
38 | bool drawIndexed;
|
---|
39 | };
|
---|
40 |
|
---|
41 | /*
|
---|
42 | cbuffer vs_cb : register(b0) {
|
---|
43 | float2 v_offset;
|
---|
44 | float2 v_scale;
|
---|
45 | };
|
---|
46 | float4 main(float4 v_pos : IN_POSITION) : SV_POSITION {
|
---|
47 | float2 coord = 2.0f * (v_pos * v_scale + v_offset) - 1.0f;
|
---|
48 | return float4(coord, 0.0f, 1.0f);
|
---|
49 | }
|
---|
50 | */
|
---|
51 |
|
---|
52 | const std::array<uint8_t, 876> g_vertexShaderCode = {{
|
---|
53 | 0x44, 0x58, 0x42, 0x43, 0x71, 0x79, 0x39, 0xb7, 0xac, 0x61, 0x62, 0xf3, 0x71, 0x08, 0x5c, 0x2c, // DXBCqy9..ab.q.\,
|
---|
54 | 0xe6, 0x27, 0x6b, 0xac, 0x01, 0x00, 0x00, 0x00, 0x6c, 0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // .'k.....l.......
|
---|
55 | 0x34, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0xb4, 0x01, 0x00, 0x00, 0xe8, 0x01, 0x00, 0x00, // 4...............
|
---|
56 | 0xd0, 0x02, 0x00, 0x00, 0x52, 0x44, 0x45, 0x46, 0x44, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // ....RDEFD.......
|
---|
57 | 0x6c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x01, 0x05, 0xfe, 0xff, // l.......<.......
|
---|
58 | 0x00, 0x85, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x13, 0x13, 0x44, 0x25, 0x3c, 0x00, 0x00, 0x00, // ..........D%<...
|
---|
59 | 0x18, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, // ....(...(...$...
|
---|
60 | 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........d.......
|
---|
61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
62 | 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
63 | 0x76, 0x73, 0x5f, 0x63, 0x62, 0x00, 0xab, 0xab, 0x64, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // vs_cb...d.......
|
---|
64 | 0x84, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
65 | 0xd4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // ................
|
---|
66 | 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
67 | 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // ................
|
---|
68 | 0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
69 | 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
70 | 0x76, 0x5f, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x00, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x32, 0x00, // v_offset.float2.
|
---|
71 | 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
72 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
73 | 0xdd, 0x00, 0x00, 0x00, 0x76, 0x5f, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x00, 0x4d, 0x69, 0x63, 0x72, // ....v_scale.Micr
|
---|
74 | 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, 0x52, 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, // osoft (R) HLSL S
|
---|
75 | 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x31, // hader Compiler 1
|
---|
76 | 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x30, 0x30, 0x31, 0x31, 0x2e, 0x31, 0x36, 0x33, 0x38, 0x34, 0x00, // 0.0.10011.16384.
|
---|
77 | 0x49, 0x53, 0x47, 0x4e, 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // ISGN,...........
|
---|
78 | 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // ...............
|
---|
79 | 0x00, 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x49, 0x4e, 0x5f, 0x50, 0x4f, 0x53, 0x49, 0x54, // ........IN_POSIT
|
---|
80 | 0x49, 0x4f, 0x4e, 0x00, 0x4f, 0x53, 0x47, 0x4e, 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // ION.OSGN,.......
|
---|
81 | 0x08, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // .... ...........
|
---|
82 | 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x50, // ............SV_P
|
---|
83 | 0x4f, 0x53, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x00, 0x53, 0x48, 0x45, 0x58, 0xe0, 0x00, 0x00, 0x00, // OSITION.SHEX....
|
---|
84 | 0x51, 0x00, 0x01, 0x00, 0x38, 0x00, 0x00, 0x00, 0x6a, 0x08, 0x00, 0x01, 0x59, 0x00, 0x00, 0x07, // Q...8...j...Y...
|
---|
85 | 0x46, 0x8e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // F.0.............
|
---|
86 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x00, 0x00, 0x03, 0x32, 0x10, 0x10, 0x00, // ........_...2...
|
---|
87 | 0x00, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x04, 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // ....g.... ......
|
---|
88 | 0x01, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x0d, // ....h.......2...
|
---|
89 | 0x32, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // 2.......F.......
|
---|
90 | 0xe6, 0x8a, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ..0.............
|
---|
91 | 0x46, 0x80, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // F.0.............
|
---|
92 | 0x32, 0x00, 0x00, 0x0f, 0x32, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x10, 0x00, // 2...2 ......F...
|
---|
93 | 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, // .....@.....@...@
|
---|
94 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xbf, // .........@......
|
---|
95 | 0x00, 0x00, 0x80, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x08, // ............6...
|
---|
96 | 0xc2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // . .......@......
|
---|
97 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x3e, 0x00, 0x00, 0x01, // ...........?>...
|
---|
98 | 0x53, 0x54, 0x41, 0x54, 0x94, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // STAT............
|
---|
99 | 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
100 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
101 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
102 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
103 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
104 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
105 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
106 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
107 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ............
|
---|
108 | }};
|
---|
109 |
|
---|
110 | /*
|
---|
111 | Texture2D<float4> tex0 : register(t0);
|
---|
112 | cbuffer ps_cb : register(b0) {
|
---|
113 | float4 color;
|
---|
114 | };
|
---|
115 | float4 main() : SV_TARGET {
|
---|
116 | return color * tex0.Load(int3(0, 0, 0));
|
---|
117 | };
|
---|
118 | */
|
---|
119 |
|
---|
120 | const std::array<uint8_t, 784> g_pixelShaderCode = {{
|
---|
121 | 0x44, 0x58, 0x42, 0x43, 0x24, 0xb1, 0x22, 0x41, 0x4b, 0x93, 0x1d, 0x4c, 0x42, 0xb2, 0x74, 0x67, // DXBC$."AK..LB.tg
|
---|
122 | 0xc4, 0x86, 0x07, 0x32, 0x01, 0x00, 0x00, 0x00, 0x10, 0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // ...2............
|
---|
123 | 0x34, 0x00, 0x00, 0x00, 0x7c, 0x01, 0x00, 0x00, 0x8c, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, // 4...|...........
|
---|
124 | 0x74, 0x02, 0x00, 0x00, 0x52, 0x44, 0x45, 0x46, 0x40, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // t...RDEF@.......
|
---|
125 | 0x98, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x01, 0x05, 0xff, 0xff, // ........<.......
|
---|
126 | 0x00, 0x85, 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x13, 0x13, 0x44, 0x25, 0x3c, 0x00, 0x00, 0x00, // ..........D%<...
|
---|
127 | 0x18, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, // ....(...(...$...
|
---|
128 | 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // ................
|
---|
129 | 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
130 | 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
131 | 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
132 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // ................
|
---|
133 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x30, 0x00, 0x70, 0x73, 0x5f, // ........tex0.ps_
|
---|
134 | 0x63, 0x62, 0x00, 0xab, 0x91, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00, // cb..............
|
---|
135 | 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00, 0x00, 0x00, // ................
|
---|
136 | 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xe8, 0x00, 0x00, 0x00, // ................
|
---|
137 | 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, // ................
|
---|
138 | 0x00, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x34, // ....color.float4
|
---|
139 | 0x00, 0xab, 0xab, 0xab, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
140 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
141 | 0x00, 0x00, 0x00, 0x00, 0xde, 0x00, 0x00, 0x00, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, // ........Microsof
|
---|
142 | 0x74, 0x20, 0x28, 0x52, 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, 0x61, 0x64, 0x65, // t (R) HLSL Shade
|
---|
143 | 0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x31, 0x30, 0x2e, 0x30, 0x2e, // r Compiler 10.0.
|
---|
144 | 0x31, 0x30, 0x30, 0x31, 0x31, 0x2e, 0x31, 0x36, 0x33, 0x38, 0x34, 0x00, 0x49, 0x53, 0x47, 0x4e, // 10011.16384.ISGN
|
---|
145 | 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4f, 0x53, 0x47, 0x4e, // ............OSGN
|
---|
146 | 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, // ,........... ...
|
---|
147 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
148 | 0x0f, 0x00, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x00, 0xab, 0xab, // ....SV_TARGET...
|
---|
149 | 0x53, 0x48, 0x45, 0x58, 0xac, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, // SHEX....Q...+...
|
---|
150 | 0x6a, 0x08, 0x00, 0x01, 0x59, 0x00, 0x00, 0x07, 0x46, 0x8e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, // j...Y...F.0.....
|
---|
151 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
152 | 0x58, 0x18, 0x00, 0x07, 0x46, 0x7e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // X...F~0.........
|
---|
153 | 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03, // ....UU......e...
|
---|
154 | 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, // . ......h.......
|
---|
155 | 0x2d, 0x00, 0x00, 0x0b, 0xf2, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, // -............@..
|
---|
156 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
157 | 0x46, 0x7e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x09, // F~ .........8...
|
---|
158 | 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x0e, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, // . ......F.......
|
---|
159 | 0x46, 0x8e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // F.0.............
|
---|
160 | 0x3e, 0x00, 0x00, 0x01, 0x53, 0x54, 0x41, 0x54, 0x94, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // >...STAT........
|
---|
161 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // ................
|
---|
162 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
163 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
164 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
165 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
166 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
167 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
168 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................
|
---|
169 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ................
|
---|
170 | }};
|
---|
171 |
|
---|
172 | class TriangleApp {
|
---|
173 |
|
---|
174 | public:
|
---|
175 |
|
---|
176 | TriangleApp(SDL_Window* window)
|
---|
177 | : m_window(window) {
|
---|
178 | Com<ID3D11Device> device;
|
---|
179 |
|
---|
180 | D3D_FEATURE_LEVEL fl = D3D_FEATURE_LEVEL_11_1;
|
---|
181 |
|
---|
182 | HRESULT status = D3D11CreateDevice(
|
---|
183 | nullptr, D3D_DRIVER_TYPE_HARDWARE,
|
---|
184 | nullptr, 0, &fl, 1, D3D11_SDK_VERSION,
|
---|
185 | &device, nullptr, nullptr);
|
---|
186 |
|
---|
187 | if (FAILED(status)) {
|
---|
188 | std::cerr << "Failed to create D3D11 device" << std::endl;
|
---|
189 | return;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (FAILED(device->QueryInterface(IID_PPV_ARGS(&m_device)))) {
|
---|
193 | std::cerr << "Failed to query ID3D11DeviceContext1" << std::endl;
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | Com<IDXGIDevice> dxgiDevice;
|
---|
198 |
|
---|
199 | if (FAILED(m_device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)))) {
|
---|
200 | std::cerr << "Failed to query DXGI device" << std::endl;
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (FAILED(dxgiDevice->GetAdapter(&m_adapter))) {
|
---|
205 | std::cerr << "Failed to query DXGI adapter" << std::endl;
|
---|
206 | return;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (FAILED(m_adapter->GetParent(IID_PPV_ARGS(&m_factory)))) {
|
---|
210 | std::cerr << "Failed to query DXGI factory" << std::endl;
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | m_device->GetImmediateContext1(&m_context);
|
---|
215 |
|
---|
216 | DXGI_SWAP_CHAIN_DESC1 swapDesc;
|
---|
217 | swapDesc.Width = m_windowSizeW;
|
---|
218 | swapDesc.Height = m_windowSizeH;
|
---|
219 | swapDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
---|
220 | swapDesc.Stereo = FALSE;
|
---|
221 | swapDesc.SampleDesc = { 1, 0 };
|
---|
222 | swapDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
---|
223 | swapDesc.BufferCount = 3;
|
---|
224 | swapDesc.Scaling = DXGI_SCALING_STRETCH;
|
---|
225 | swapDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
---|
226 | swapDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
|
---|
227 | swapDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
|
---|
228 |
|
---|
229 | DXGI_SWAP_CHAIN_FULLSCREEN_DESC fsDesc;
|
---|
230 | fsDesc.RefreshRate = { 0, 0 };
|
---|
231 | fsDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
|
---|
232 | fsDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
|
---|
233 | fsDesc.Windowed = TRUE;
|
---|
234 |
|
---|
235 | Com<IDXGISwapChain1> swapChain;
|
---|
236 | if (FAILED(m_factory->CreateSwapChainForHwnd(m_device.ptr(), wsi::toHwnd(m_window), &swapDesc, &fsDesc, nullptr, &swapChain))) {
|
---|
237 | std::cerr << "Failed to create DXGI swap chain" << std::endl;
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (FAILED(swapChain->QueryInterface(IID_PPV_ARGS(&m_swapChain)))) {
|
---|
242 | std::cerr << "Failed to query DXGI swap chain interface" << std::endl;
|
---|
243 | return;
|
---|
244 | }
|
---|
245 |
|
---|
246 | m_factory->MakeWindowAssociation(m_window, 0);
|
---|
247 |
|
---|
248 | if (FAILED(m_device->CreateVertexShader(
|
---|
249 | g_vertexShaderCode.data(),
|
---|
250 | g_vertexShaderCode.size(),
|
---|
251 | nullptr, &m_vs))) {
|
---|
252 | std::cerr << "Failed to create vertex shader" << std::endl;
|
---|
253 | return;
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (FAILED(m_device->CreatePixelShader(
|
---|
257 | g_pixelShaderCode.data(),
|
---|
258 | g_pixelShaderCode.size(),
|
---|
259 | nullptr, &m_ps))) {
|
---|
260 | std::cerr << "Failed to create pixel shader" << std::endl;
|
---|
261 | return;
|
---|
262 | }
|
---|
263 |
|
---|
264 | std::array<D3D11_INPUT_ELEMENT_DESC, 1> vertexFormatDesc = {{
|
---|
265 | { "IN_POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
---|
266 | }};
|
---|
267 |
|
---|
268 | if (FAILED(m_device->CreateInputLayout(
|
---|
269 | vertexFormatDesc.data(),
|
---|
270 | vertexFormatDesc.size(),
|
---|
271 | g_vertexShaderCode.data(),
|
---|
272 | g_vertexShaderCode.size(),
|
---|
273 | &m_vertexFormat))) {
|
---|
274 | std::cerr << "Failed to create input layout" << std::endl;
|
---|
275 | return;
|
---|
276 | }
|
---|
277 |
|
---|
278 | std::array<Vertex, 6> vertexData = {{
|
---|
279 | Vertex { -0.3f, 0.1f },
|
---|
280 | Vertex { 0.5f, 0.9f },
|
---|
281 | Vertex { 1.3f, 0.1f },
|
---|
282 | Vertex { -0.3f, 0.9f },
|
---|
283 | Vertex { 1.3f, 0.9f },
|
---|
284 | Vertex { 0.5f, 0.1f },
|
---|
285 | }};
|
---|
286 |
|
---|
287 | D3D11_BUFFER_DESC vboDesc;
|
---|
288 | vboDesc.ByteWidth = sizeof(vertexData);
|
---|
289 | vboDesc.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
290 | vboDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
291 | vboDesc.CPUAccessFlags = 0;
|
---|
292 | vboDesc.MiscFlags = 0;
|
---|
293 | vboDesc.StructureByteStride = 0;
|
---|
294 |
|
---|
295 | D3D11_SUBRESOURCE_DATA vboData;
|
---|
296 | vboData.pSysMem = vertexData.data();
|
---|
297 | vboData.SysMemPitch = vboDesc.ByteWidth;
|
---|
298 | vboData.SysMemSlicePitch = vboDesc.ByteWidth;
|
---|
299 |
|
---|
300 | if (FAILED(m_device->CreateBuffer(&vboDesc, &vboData, &m_vbo))) {
|
---|
301 | std::cerr << "Failed to create index buffer" << std::endl;
|
---|
302 | return;
|
---|
303 | }
|
---|
304 |
|
---|
305 | std::array<uint32_t, 6> indexData = {{ 0, 1, 2, 3, 4, 5 }};
|
---|
306 |
|
---|
307 | D3D11_BUFFER_DESC iboDesc;
|
---|
308 | iboDesc.ByteWidth = sizeof(indexData);
|
---|
309 | iboDesc.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
310 | iboDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
---|
311 | iboDesc.CPUAccessFlags = 0;
|
---|
312 | iboDesc.MiscFlags = 0;
|
---|
313 | iboDesc.StructureByteStride = 0;
|
---|
314 |
|
---|
315 | D3D11_SUBRESOURCE_DATA iboData;
|
---|
316 | iboData.pSysMem = indexData.data();
|
---|
317 | iboData.SysMemPitch = iboDesc.ByteWidth;
|
---|
318 | iboData.SysMemSlicePitch = iboDesc.ByteWidth;
|
---|
319 |
|
---|
320 | if (FAILED(m_device->CreateBuffer(&iboDesc, &iboData, &m_ibo))) {
|
---|
321 | std::cerr << "Failed to create index buffer" << std::endl;
|
---|
322 | return;
|
---|
323 | }
|
---|
324 |
|
---|
325 | D3D11_BUFFER_DESC cbDesc;
|
---|
326 | cbDesc.ByteWidth = sizeof(PsConstants);
|
---|
327 | cbDesc.Usage = D3D11_USAGE_DYNAMIC;
|
---|
328 | cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
---|
329 | cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
---|
330 | cbDesc.MiscFlags = 0;
|
---|
331 | cbDesc.StructureByteStride = 0;
|
---|
332 |
|
---|
333 | if (FAILED(m_device->CreateBuffer(&cbDesc, nullptr, &m_cbPs))) {
|
---|
334 | std::cerr << "Failed to create constant buffer" << std::endl;
|
---|
335 | return;
|
---|
336 | }
|
---|
337 |
|
---|
338 | cbDesc.ByteWidth = sizeof(VsConstantsPad) * 128 * 8;
|
---|
339 |
|
---|
340 | if (FAILED(m_device->CreateBuffer(&cbDesc, nullptr, &m_cbVs))) {
|
---|
341 | std::cerr << "Failed to create constant buffer" << std::endl;
|
---|
342 | return;
|
---|
343 | }
|
---|
344 |
|
---|
345 | std::array<uint32_t, 2> colors = { 0xFFFFFFFF, 0xFFC0C0C0 };
|
---|
346 |
|
---|
347 | D3D11_SUBRESOURCE_DATA texData;
|
---|
348 | texData.pSysMem = &colors[0];
|
---|
349 | texData.SysMemPitch = sizeof(colors[0]);
|
---|
350 | texData.SysMemSlicePitch = sizeof(colors[0]);
|
---|
351 |
|
---|
352 | D3D11_TEXTURE2D_DESC texDesc;
|
---|
353 | texDesc.Width = 1;
|
---|
354 | texDesc.Height = 1;
|
---|
355 | texDesc.MipLevels = 1;
|
---|
356 | texDesc.ArraySize = 1;
|
---|
357 | texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
---|
358 | texDesc.SampleDesc = { 1, 0 };
|
---|
359 | texDesc.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
360 | texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
---|
361 | texDesc.CPUAccessFlags = 0;
|
---|
362 | texDesc.MiscFlags = 0;
|
---|
363 |
|
---|
364 | if (FAILED(m_device->CreateTexture2D(&texDesc, &texData, &m_tex0))) {
|
---|
365 | std::cerr << "Failed to create texture" << std::endl;
|
---|
366 | return;
|
---|
367 | }
|
---|
368 |
|
---|
369 | texData.pSysMem = &colors[1];
|
---|
370 |
|
---|
371 | if (FAILED(m_device->CreateTexture2D(&texDesc, &texData, &m_tex1))) {
|
---|
372 | std::cerr << "Failed to create texture" << std::endl;
|
---|
373 | return;
|
---|
374 | }
|
---|
375 |
|
---|
376 | if (FAILED(m_device->CreateShaderResourceView(m_tex0.ptr(), nullptr, &m_srv0))
|
---|
377 | || FAILED(m_device->CreateShaderResourceView(m_tex1.ptr(), nullptr, &m_srv1))) {
|
---|
378 | std::cerr << "Failed to create SRV" << std::endl;
|
---|
379 | return;
|
---|
380 | }
|
---|
381 |
|
---|
382 | Com<ID3D11Texture2D> backBuffer;
|
---|
383 | if (FAILED(m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)))) {
|
---|
384 | std::cerr << "Failed to get swap chain back buffer" << std::endl;
|
---|
385 | return;
|
---|
386 | }
|
---|
387 |
|
---|
388 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
|
---|
389 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
---|
390 | rtvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
|
---|
391 | rtvDesc.Texture2D = { 0u };
|
---|
392 |
|
---|
393 | if (FAILED(m_device->CreateRenderTargetView(backBuffer.ptr(), &rtvDesc, &m_rtv))) {
|
---|
394 | std::cerr << "Failed to create render target view" << std::endl;
|
---|
395 | return;
|
---|
396 | }
|
---|
397 |
|
---|
398 | m_initialized = true;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | ~TriangleApp() {
|
---|
403 | m_context->ClearState();
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | bool run() {
|
---|
408 | if (!m_initialized)
|
---|
409 | return false;
|
---|
410 |
|
---|
411 | if (m_occluded && (m_occluded = isOccluded()))
|
---|
412 | return true;
|
---|
413 |
|
---|
414 | if (!beginFrame())
|
---|
415 | return true;
|
---|
416 |
|
---|
417 | std::array<PsConstants, 2> colors = {{
|
---|
418 | PsConstants { 0.25f, 0.25f, 0.25f, 1.0f },
|
---|
419 | PsConstants { 0.40f, 0.40f, 0.40f, 1.0f },
|
---|
420 | }};
|
---|
421 |
|
---|
422 | for (uint32_t i = 0; i < 8; i++) {
|
---|
423 | DrawOptions options;
|
---|
424 | options.sortByTexture = i & 1;
|
---|
425 | options.drawIndexed = i & 2;
|
---|
426 | options.mapDiscardOnce = i & 4;
|
---|
427 | drawLines(colors[i & 1], options, i);
|
---|
428 | }
|
---|
429 |
|
---|
430 | if (!endFrame())
|
---|
431 | return false;
|
---|
432 |
|
---|
433 | return true;
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | void drawLines(const PsConstants& psData, const DrawOptions& options, uint32_t baseY) {
|
---|
438 | D3D11_MAPPED_SUBRESOURCE sr;
|
---|
439 |
|
---|
440 | // Update color for the row
|
---|
441 | m_context->PSSetConstantBuffers(0, 1, &m_cbPs);
|
---|
442 | m_context->Map(m_cbPs.ptr(), 0, D3D11_MAP_WRITE_DISCARD, 0, &sr);
|
---|
443 | std::memcpy(sr.pData, &psData, sizeof(psData));
|
---|
444 | m_context->Unmap(m_cbPs.ptr(), 0);
|
---|
445 |
|
---|
446 | baseY *= 8;
|
---|
447 |
|
---|
448 | if (options.mapDiscardOnce) {
|
---|
449 | uint32_t drawIndex = 0;
|
---|
450 |
|
---|
451 | // Discard and map the entire vertex constant buffer
|
---|
452 | // once, then bind sub-ranges while emitting draw calls
|
---|
453 | m_context->Map(m_cbVs.ptr(), 0, D3D11_MAP_WRITE_DISCARD, 0, &sr);
|
---|
454 | auto vsData = reinterpret_cast<VsConstantsPad*>(sr.pData);
|
---|
455 |
|
---|
456 | for (uint32_t y = 0; y < 8; y++) {
|
---|
457 | for (uint32_t x = 0; x < 128; x++)
|
---|
458 | vsData[drawIndex++].data = getVsConstants(x, baseY + y);
|
---|
459 | }
|
---|
460 |
|
---|
461 | m_context->Unmap(m_cbVs.ptr(), 0);
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (options.drawIndexed)
|
---|
465 | m_context->IASetIndexBuffer(m_ibo.ptr(), DXGI_FORMAT_R32_UINT, 0);
|
---|
466 |
|
---|
467 | uint32_t vsStride = sizeof(Vertex);
|
---|
468 | uint32_t vsOffset = 0;
|
---|
469 | m_context->IASetVertexBuffers(0, 1, &m_vbo, &vsStride, &vsOffset);
|
---|
470 |
|
---|
471 | uint32_t maxZ = options.sortByTexture ? 2 : 1;
|
---|
472 |
|
---|
473 | for (uint32_t z = 0; z < maxZ; z++) {
|
---|
474 | uint32_t drawIndex = z;
|
---|
475 |
|
---|
476 | if (options.sortByTexture) {
|
---|
477 | ID3D11ShaderResourceView* view = z ? m_srv1.ptr() : m_srv0.ptr();
|
---|
478 | m_context->PSSetShaderResources(0, 1, &view);
|
---|
479 | }
|
---|
480 |
|
---|
481 | for (uint32_t y = 0; y < 8; y++) {
|
---|
482 | for (uint32_t x = z; x < 128; x += maxZ) {
|
---|
483 | uint32_t triIndex = (x ^ y) & 1;
|
---|
484 |
|
---|
485 | if (!options.mapDiscardOnce) {
|
---|
486 | D3D11_MAP mapMode = drawIndex ? D3D11_MAP_WRITE_NO_OVERWRITE : D3D11_MAP_WRITE_DISCARD;
|
---|
487 | m_context->Map(m_cbVs.ptr(), 0, mapMode, 0, &sr);
|
---|
488 | auto vsData = reinterpret_cast<VsConstantsPad*>(sr.pData);
|
---|
489 | vsData[drawIndex].data = getVsConstants(x, baseY + y);
|
---|
490 | m_context->Unmap(m_cbVs.ptr(), 0);
|
---|
491 | }
|
---|
492 |
|
---|
493 | uint32_t constantOffset = 16 * drawIndex;
|
---|
494 | uint32_t constantCount = 16;
|
---|
495 | m_context->VSSetConstantBuffers1(0, 1, &m_cbVs, &constantOffset, &constantCount);
|
---|
496 |
|
---|
497 | if (!options.sortByTexture) {
|
---|
498 | ID3D11ShaderResourceView* view = triIndex ? m_srv1.ptr() : m_srv0.ptr();
|
---|
499 | m_context->PSSetShaderResources(0, 1, &view);
|
---|
500 | }
|
---|
501 |
|
---|
502 | // Submit draw call
|
---|
503 | uint32_t baseIndex = 3 * triIndex;
|
---|
504 |
|
---|
505 | if (options.drawIndexed)
|
---|
506 | m_context->DrawIndexed(3, baseIndex, 0);
|
---|
507 | else
|
---|
508 | m_context->Draw(3, baseIndex);
|
---|
509 |
|
---|
510 | drawIndex += maxZ;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | static VsConstants getVsConstants(uint32_t x, uint32_t y) {
|
---|
518 | VsConstants result;
|
---|
519 | result.x = float(x) / 128.0f;
|
---|
520 | result.y = float(y) / 64.0f;
|
---|
521 | result.w = 1.0f / 128.0f;
|
---|
522 | result.h = 1.0f / 64.0f;
|
---|
523 | return result;
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | bool beginFrame() {
|
---|
528 | int32_t w, h;
|
---|
529 | SDL_GetWindowSize(m_window, &w, &h);
|
---|
530 |
|
---|
531 | uint32_t newWindowSizeW = uint32_t(w);
|
---|
532 | uint32_t newWindowSizeH = uint32_t(h);
|
---|
533 |
|
---|
534 | if (m_windowSizeW != newWindowSizeW || m_windowSizeH != newWindowSizeH) {
|
---|
535 | m_rtv = nullptr;
|
---|
536 | m_context->ClearState();
|
---|
537 |
|
---|
538 | DXGI_SWAP_CHAIN_DESC1 desc;
|
---|
539 | m_swapChain->GetDesc1(&desc);
|
---|
540 |
|
---|
541 | if (FAILED(m_swapChain->ResizeBuffers(desc.BufferCount,
|
---|
542 | newWindowSizeW, newWindowSizeH, desc.Format, desc.Flags))) {
|
---|
543 | std::cerr << "Failed to resize back buffers" << std::endl;
|
---|
544 | return false;
|
---|
545 | }
|
---|
546 |
|
---|
547 | Com<ID3D11Texture2D> backBuffer;
|
---|
548 | if (FAILED(m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)))) {
|
---|
549 | std::cerr << "Failed to get swap chain back buffer" << std::endl;
|
---|
550 | return false;
|
---|
551 | }
|
---|
552 |
|
---|
553 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
|
---|
554 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
---|
555 | rtvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
|
---|
556 | rtvDesc.Texture2D = { 0u };
|
---|
557 |
|
---|
558 | if (FAILED(m_device->CreateRenderTargetView(backBuffer.ptr(), &rtvDesc, &m_rtv))) {
|
---|
559 | std::cerr << "Failed to create render target view" << std::endl;
|
---|
560 | return false;
|
---|
561 | }
|
---|
562 |
|
---|
563 | m_windowSizeW = newWindowSizeW;
|
---|
564 | m_windowSizeH = newWindowSizeH;
|
---|
565 | }
|
---|
566 |
|
---|
567 | // Set up render state
|
---|
568 | FLOAT color[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
|
---|
569 | m_context->OMSetRenderTargets(1, &m_rtv, nullptr);
|
---|
570 | m_context->ClearRenderTargetView(m_rtv.ptr(), color);
|
---|
571 |
|
---|
572 | m_context->VSSetShader(m_vs.ptr(), nullptr, 0);
|
---|
573 | m_context->PSSetShader(m_ps.ptr(), nullptr, 0);
|
---|
574 |
|
---|
575 | m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
---|
576 | m_context->IASetInputLayout(m_vertexFormat.ptr());
|
---|
577 |
|
---|
578 | D3D11_VIEWPORT viewport;
|
---|
579 | viewport.TopLeftX = 0.0f;
|
---|
580 | viewport.TopLeftY = 0.0f;
|
---|
581 | viewport.Width = float(m_windowSizeW);
|
---|
582 | viewport.Height = float(m_windowSizeH);
|
---|
583 | viewport.MinDepth = 0.0f;
|
---|
584 | viewport.MaxDepth = 1.0f;
|
---|
585 | m_context->RSSetViewports(1, &viewport);
|
---|
586 | return true;
|
---|
587 | }
|
---|
588 |
|
---|
589 |
|
---|
590 | bool endFrame() {
|
---|
591 | HRESULT hr = m_swapChain->Present(0, DXGI_PRESENT_TEST);
|
---|
592 |
|
---|
593 | if (hr == S_OK)
|
---|
594 | hr = m_swapChain->Present(0, 0);
|
---|
595 |
|
---|
596 | m_occluded = hr == DXGI_STATUS_OCCLUDED;
|
---|
597 | return true;
|
---|
598 | }
|
---|
599 |
|
---|
600 | bool isOccluded() {
|
---|
601 | return m_swapChain->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED;
|
---|
602 | }
|
---|
603 |
|
---|
604 | private:
|
---|
605 |
|
---|
606 | SDL_Window* m_window;
|
---|
607 | uint32_t m_windowSizeW = 1024;
|
---|
608 | uint32_t m_windowSizeH = 600;
|
---|
609 | bool m_initialized = false;
|
---|
610 | bool m_occluded = false;
|
---|
611 |
|
---|
612 | Com<IDXGIFactory3> m_factory;
|
---|
613 | Com<IDXGIAdapter> m_adapter;
|
---|
614 | Com<ID3D11Device1> m_device;
|
---|
615 | Com<ID3D11DeviceContext1> m_context;
|
---|
616 | Com<IDXGISwapChain2> m_swapChain;
|
---|
617 |
|
---|
618 | Com<ID3D11RenderTargetView> m_rtv;
|
---|
619 | Com<ID3D11Buffer> m_ibo;
|
---|
620 | Com<ID3D11Buffer> m_vbo;
|
---|
621 | Com<ID3D11InputLayout> m_vertexFormat;
|
---|
622 |
|
---|
623 | Com<ID3D11Texture2D> m_tex0;
|
---|
624 | Com<ID3D11Texture2D> m_tex1;
|
---|
625 | Com<ID3D11ShaderResourceView> m_srv0;
|
---|
626 | Com<ID3D11ShaderResourceView> m_srv1;
|
---|
627 |
|
---|
628 | Com<ID3D11Buffer> m_cbPs;
|
---|
629 | Com<ID3D11Buffer> m_cbVs;
|
---|
630 |
|
---|
631 | Com<ID3D11VertexShader> m_vs;
|
---|
632 | Com<ID3D11PixelShader> m_ps;
|
---|
633 |
|
---|
634 | };
|
---|
635 |
|
---|
636 | int main(int argc, char** argv) {
|
---|
637 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
---|
638 | std::cerr << "Failed to init SDL" << std::endl;
|
---|
639 | return 1;
|
---|
640 | }
|
---|
641 |
|
---|
642 | SDL_Window* window = SDL_CreateWindow(
|
---|
643 | "DXVK Native Triangle! - D3D11",
|
---|
644 | SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
645 | 1024, 600,
|
---|
646 | SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
|
---|
647 | if (!window) {
|
---|
648 | std::cerr << "Failed to create SDL window" << std::endl;
|
---|
649 | return 1;
|
---|
650 | }
|
---|
651 |
|
---|
652 | TriangleApp app(window);
|
---|
653 |
|
---|
654 | bool running = true;
|
---|
655 | while (running) {
|
---|
656 | SDL_Event event;
|
---|
657 | while (SDL_PollEvent(&event)) {
|
---|
658 | switch (event.type) {
|
---|
659 | case SDL_QUIT:
|
---|
660 | running = false;
|
---|
661 | break;
|
---|
662 | default:
|
---|
663 | break;
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (!app.run())
|
---|
668 | break;
|
---|
669 | }
|
---|
670 |
|
---|
671 | return 0;
|
---|
672 | }
|
---|
673 |
|
---|