1 | #include <array>
|
---|
2 | #include <cstring>
|
---|
3 |
|
---|
4 | #include <d3dcompiler.h>
|
---|
5 | #include <d3d11.h>
|
---|
6 |
|
---|
7 | #include <windows.h>
|
---|
8 | #include <windowsx.h>
|
---|
9 |
|
---|
10 | #include "../test_utils.h"
|
---|
11 |
|
---|
12 | using namespace dxvk;
|
---|
13 |
|
---|
14 | const std::string g_vsCode =
|
---|
15 | "float4 main(float4 v_pos : VS_POSITION) : SV_POSITION {\n"
|
---|
16 | " return v_pos;\n"
|
---|
17 | "}\n";
|
---|
18 |
|
---|
19 | Com<ID3D11Device> g_d3d11Device;
|
---|
20 | Com<ID3D11DeviceContext> g_d3d11Context;
|
---|
21 |
|
---|
22 | Com<ID3D11VertexShader> g_vertShader;
|
---|
23 | Com<ID3D11InputLayout> g_inputLayout;
|
---|
24 |
|
---|
25 | Com<ID3D11Buffer> g_vertexBuffer;
|
---|
26 |
|
---|
27 | Com<ID3D11Texture2D> g_depthRender;
|
---|
28 | Com<ID3D11Texture2D> g_depthRead;
|
---|
29 | Com<ID3D11DepthStencilView> g_depthView;
|
---|
30 | Com<ID3D11DepthStencilState>g_depthState;
|
---|
31 |
|
---|
32 | struct Vertex {
|
---|
33 | float x, y, z, w;
|
---|
34 | };
|
---|
35 |
|
---|
36 | int WINAPI WinMain(HINSTANCE hInstance,
|
---|
37 | HINSTANCE hPrevInstance,
|
---|
38 | LPSTR lpCmdLine,
|
---|
39 | int nCmdShow) {
|
---|
40 | if (FAILED(D3D11CreateDevice(
|
---|
41 | nullptr, D3D_DRIVER_TYPE_HARDWARE,
|
---|
42 | nullptr, 0, nullptr, 0, D3D11_SDK_VERSION,
|
---|
43 | &g_d3d11Device, nullptr, &g_d3d11Context))) {
|
---|
44 | std::cerr << "Failed to create D3D11 device" << std::endl;
|
---|
45 | return 1;
|
---|
46 | }
|
---|
47 |
|
---|
48 | Com<ID3DBlob> vsBlob;
|
---|
49 | Com<ID3DBlob> gsBlob;
|
---|
50 |
|
---|
51 | if (FAILED(D3DCompile(g_vsCode.data(), g_vsCode.size(),
|
---|
52 | "Vertex shader", nullptr, nullptr, "main", "vs_4_0",
|
---|
53 | 0, 0, &vsBlob, nullptr))) {
|
---|
54 | std::cerr << "Failed to compile vertex shader" << std::endl;
|
---|
55 | return 1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (FAILED(g_d3d11Device->CreateVertexShader(
|
---|
59 | vsBlob->GetBufferPointer(),
|
---|
60 | vsBlob->GetBufferSize(),
|
---|
61 | nullptr, &g_vertShader))) {
|
---|
62 | std::cerr << "Failed to create vertex shader" << std::endl;
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | std::array<D3D11_INPUT_ELEMENT_DESC, 1> iaElements = {{
|
---|
67 | { "VS_POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
---|
68 | }};
|
---|
69 |
|
---|
70 | if (FAILED(g_d3d11Device->CreateInputLayout(
|
---|
71 | iaElements.data(),
|
---|
72 | iaElements.size(),
|
---|
73 | vsBlob->GetBufferPointer(),
|
---|
74 | vsBlob->GetBufferSize(),
|
---|
75 | &g_inputLayout))) {
|
---|
76 | std::cerr << "Failed to create input layout" << std::endl;
|
---|
77 | return 1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | std::array<Vertex, 4> vertexData = {{
|
---|
81 | { -1.0f, -1.0f, 0.00f, 1.0f },
|
---|
82 | { -1.0f, 1.0f, 0.66f, 1.0f },
|
---|
83 | { 1.0f, -1.0f, 0.33f, 1.0f },
|
---|
84 | { 1.0f, 1.0f, 1.00f, 1.0f },
|
---|
85 | }};
|
---|
86 |
|
---|
87 | D3D11_BUFFER_DESC vertexDesc;
|
---|
88 | vertexDesc.ByteWidth = vertexData.size() * sizeof(Vertex);
|
---|
89 | vertexDesc.Usage = D3D11_USAGE_IMMUTABLE;
|
---|
90 | vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
---|
91 | vertexDesc.CPUAccessFlags = 0;
|
---|
92 | vertexDesc.MiscFlags = 0;
|
---|
93 | vertexDesc.StructureByteStride = 0;
|
---|
94 |
|
---|
95 | D3D11_SUBRESOURCE_DATA vertexInfo;
|
---|
96 | vertexInfo.pSysMem = vertexData.data();
|
---|
97 | vertexInfo.SysMemPitch = vertexDesc.ByteWidth;
|
---|
98 | vertexInfo.SysMemSlicePitch = vertexDesc.ByteWidth;
|
---|
99 |
|
---|
100 | if (FAILED(g_d3d11Device->CreateBuffer(&vertexDesc, &vertexInfo, &g_vertexBuffer))) {
|
---|
101 | std::cerr << "Failed to create vertex buffer" << std::endl;
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | D3D11_TEXTURE2D_DESC depthDesc;
|
---|
106 | depthDesc.Width = 16;
|
---|
107 | depthDesc.Height = 16;
|
---|
108 | depthDesc.MipLevels = 1;
|
---|
109 | depthDesc.ArraySize = 1;
|
---|
110 | depthDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
---|
111 | // depthDesc.Format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
|
---|
112 | depthDesc.SampleDesc = { 1, 0 };
|
---|
113 | depthDesc.Usage = D3D11_USAGE_DEFAULT;
|
---|
114 | depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
---|
115 | depthDesc.CPUAccessFlags = 0;
|
---|
116 | depthDesc.MiscFlags = 0;
|
---|
117 |
|
---|
118 | if (FAILED(g_d3d11Device->CreateTexture2D(&depthDesc, nullptr, &g_depthRender))) {
|
---|
119 | std::cerr << "Failed to create render buffer" << std::endl;
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 |
|
---|
123 | depthDesc.Usage = D3D11_USAGE_STAGING;
|
---|
124 | depthDesc.BindFlags = 0;
|
---|
125 | depthDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
---|
126 |
|
---|
127 | if (FAILED(g_d3d11Device->CreateTexture2D(&depthDesc, nullptr, &g_depthRead))) {
|
---|
128 | std::cerr << "Failed to create readback buffer" << std::endl;
|
---|
129 | return 1;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (FAILED(g_d3d11Device->CreateDepthStencilView(g_depthRender.ptr(), nullptr, &g_depthView))) {
|
---|
133 | std::cerr << "Failed to create depth-stencil view" << std::endl;
|
---|
134 | return 1;
|
---|
135 | }
|
---|
136 |
|
---|
137 | D3D11_DEPTH_STENCIL_DESC dsDesc;
|
---|
138 | dsDesc.DepthEnable = TRUE;
|
---|
139 | dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
|
---|
140 | dsDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
|
---|
141 | dsDesc.StencilEnable = FALSE;
|
---|
142 | dsDesc.StencilReadMask = 0;
|
---|
143 | dsDesc.StencilWriteMask = 0;
|
---|
144 | dsDesc.FrontFace = { };
|
---|
145 | dsDesc.BackFace = { };
|
---|
146 |
|
---|
147 | if (FAILED(g_d3d11Device->CreateDepthStencilState(&dsDesc, &g_depthState))) {
|
---|
148 | std::cerr << "Failed to create depth-stencil state" << std::endl;
|
---|
149 | return 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | FLOAT omBlendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
---|
153 |
|
---|
154 | D3D11_VIEWPORT omViewport;
|
---|
155 | omViewport.TopLeftX = 0.0f;
|
---|
156 | omViewport.TopLeftY = 0.0f;
|
---|
157 | omViewport.Width = 16.0f;
|
---|
158 | omViewport.Height = 16.0f;
|
---|
159 | omViewport.MinDepth = 0.0f;
|
---|
160 | omViewport.MaxDepth = 1.0f;
|
---|
161 |
|
---|
162 | UINT vbOffset = 0;
|
---|
163 | UINT vbStride = sizeof(Vertex);
|
---|
164 |
|
---|
165 | g_d3d11Context->RSSetState(nullptr);
|
---|
166 | g_d3d11Context->RSSetViewports(1, &omViewport);
|
---|
167 |
|
---|
168 | g_d3d11Context->OMSetRenderTargets(0, nullptr, g_depthView.ptr());
|
---|
169 | g_d3d11Context->OMSetBlendState(nullptr, omBlendFactor, 0xFFFFFFFF);
|
---|
170 | g_d3d11Context->OMSetDepthStencilState(g_depthState.ptr(), 0);
|
---|
171 |
|
---|
172 | g_d3d11Context->ClearDepthStencilView(g_depthView.ptr(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 0.5f, 0x80);
|
---|
173 |
|
---|
174 | g_d3d11Context->IASetInputLayout(g_inputLayout.ptr());
|
---|
175 | g_d3d11Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
---|
176 | g_d3d11Context->IASetVertexBuffers(0, 1, &g_vertexBuffer, &vbStride, &vbOffset);
|
---|
177 |
|
---|
178 | g_d3d11Context->VSSetShader(g_vertShader.ptr(), nullptr, 0);
|
---|
179 | g_d3d11Context->Draw(4, 0);
|
---|
180 |
|
---|
181 | g_d3d11Context->CopyResource(g_depthRead.ptr(), g_depthRender.ptr());
|
---|
182 |
|
---|
183 | D3D11_MAPPED_SUBRESOURCE mapped;
|
---|
184 |
|
---|
185 | if (FAILED(g_d3d11Context->Map(g_depthRead.ptr(), 0, D3D11_MAP_READ, 0, &mapped))) {
|
---|
186 | std::cerr << "Failed to map image" << std::endl;
|
---|
187 | return 1;
|
---|
188 | }
|
---|
189 |
|
---|
190 | for (uint32_t y = 0; y < 16; y++) {
|
---|
191 | auto data = reinterpret_cast<const uint32_t*>(mapped.pData)
|
---|
192 | + (y * mapped.RowPitch / 4);
|
---|
193 |
|
---|
194 | for (uint32_t x = 0; x < 16; x++)
|
---|
195 | std::cout << std::hex << std::setfill('0') << std::setw(8) << data[x] << " ";
|
---|
196 |
|
---|
197 | std::cout << std::endl;
|
---|
198 | }
|
---|
199 |
|
---|
200 | g_d3d11Context->Unmap(g_depthRead.ptr(), 0);
|
---|
201 | g_d3d11Context->ClearState();
|
---|
202 | return 0;
|
---|
203 | }
|
---|