-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrenderer.cpp
More file actions
304 lines (252 loc) · 12.8 KB
/
renderer.cpp
File metadata and controls
304 lines (252 loc) · 12.8 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
120
121
122
123
124
125
126
127
128
129
130
131
132
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
#include "renderer.h"
#include "distortion.h"
#include <d3dcompiler.h>
#include <debugapi.h>
#include <string>
using Microsoft::WRL::ComPtr;
const int IMAGE_WIDTH = 1016;
const int IMAGE_HEIGHT = 1016;
// Texture is 1024 by 1024, but the real image is actually 1016x1016
const int TEXTURE_WIDTH = 1024;
const int TEXTURE_HEIGHT = 1024;
const size_t BC4_DATA_SIZE = (1024 * 1024) / 2; // BC4 is 4 bits per pixel
DisplayMode g_displayMode = DisplayMode::SideBySide;
ComPtr<ID3D11Device> g_pd3dDevice;
ComPtr<ID3D11DeviceContext> g_pImmediateContext;
ComPtr<IDXGISwapChain> g_pSwapChain;
ComPtr<ID3D11RenderTargetView> g_pRenderTargetView;
ComPtr<ID3D11Texture2D> g_pBC4Texture1;
ComPtr<ID3D11ShaderResourceView> g_pBC4TextureSRV1;
ComPtr<ID3D11Texture2D> g_pBC4Texture2;
ComPtr<ID3D11ShaderResourceView> g_pBC4TextureSRV2;
ComPtr<ID3D11SamplerState> g_pSamplerState;
ComPtr<ID3D11Buffer> g_pUndistortVertexBuffer[2];
ComPtr<ID3D11Buffer> g_pUndistortIndexBuffer[2];
ComPtr<ID3D11VertexShader> g_pUndistortVertexShader;
ComPtr<ID3D11PixelShader> g_pUndistortPixelShader;
ComPtr<ID3D11InputLayout> g_pUndistortInputLayout;
UINT g_uUndistortIndexCount[2] = { 0, 0 };
void Resize(UINT width, UINT height)
{
if (g_pSwapChain)
{
g_pImmediateContext->OMSetRenderTargets(0, 0, 0);
g_pRenderTargetView.Reset();
g_pSwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
ComPtr<ID3D11Texture2D> pBuffer;
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &pBuffer);
g_pd3dDevice->CreateRenderTargetView( pBuffer.Get(), NULL, &g_pRenderTargetView);
g_pImmediateContext->OMSetRenderTargets( 1, g_pRenderTargetView.GetAddressOf(), NULL );
}
}
void UpdateMeshes(SharedMemoryData& sharedMemoryData, float zoomFactor, bool useDistortion) {
for (int i = 0; i < 2; ++i) {
std::vector<UndistortVertex> vertices;
std::vector<DWORD> indices;
if (useDistortion)
{
CameraParameters cam_params;
CameraIntrinsics cam_intrinsics;
if (!get_distortion_config(sharedMemoryData, i, cam_params, cam_intrinsics)) {
continue;
}
create_undistortion_mesh(TEXTURE_WIDTH, TEXTURE_HEIGHT, zoomFactor, cam_intrinsics, cam_params, vertices, indices);
}
else
{
create_default_mesh(IMAGE_WIDTH, IMAGE_HEIGHT, TEXTURE_WIDTH, TEXTURE_HEIGHT, vertices, indices);
}
g_uUndistortIndexCount[i] = static_cast<UINT>(indices.size());
g_pUndistortVertexBuffer[i].Reset();
g_pUndistortIndexBuffer[i].Reset();
D3D11_BUFFER_DESC bd = {};
bd.Usage = D3D11_USAGE_DEFAULT;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.ByteWidth = sizeof(UndistortVertex) * (UINT)vertices.size();
D3D11_SUBRESOURCE_DATA InitData = {};
InitData.pSysMem = vertices.data();
g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pUndistortVertexBuffer[i]);
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.ByteWidth = sizeof(DWORD) * (UINT)indices.size();
InitData.pSysMem = indices.data();
g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pUndistortIndexBuffer[i]);
}
}
HRESULT InitDevice(HWND hWnd, SharedMemoryData& sharedMemoryData, float zoomFactor) {
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferCount = 1;
sd.BufferDesc.Width = IMAGE_WIDTH * 2; // Double width for side-by-side
sd.BufferDesc.Height = IMAGE_HEIGHT;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
HRESULT hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, NULL, &g_pImmediateContext);
if (FAILED(hr)) return hr;
ComPtr<ID3D11Texture2D> pBackBuffer;
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)pBackBuffer.GetAddressOf());
if (FAILED(hr)) return hr;
hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer.Get(), NULL, &g_pRenderTargetView);
if (FAILED(hr)) return hr;
g_pImmediateContext->OMSetRenderTargets(1, g_pRenderTargetView.GetAddressOf(), NULL);
const char* undistort_shader_code = R"(
Texture2D txDiffuse : register(t0);
SamplerState samLinear : register(s0);
struct VS_INPUT {
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};
struct PS_INPUT {
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input) {
PS_INPUT output;
output.Pos = input.Pos;
output.Tex = input.Tex;
return output;
}
float4 PS(PS_INPUT input) : SV_Target {
if (input.Tex.x < 0.0 || input.Tex.x > 1.0 || input.Tex.y < 0.0 || input.Tex.y > 1.0) {
return float4(0.0, 0.0, 0.0, 1.0);
}
float val = pow(txDiffuse.Sample(samLinear, input.Tex).r, 0.75f) * 4.0f - 0.5f;
return float4(val, val, val, 1.0f);
}
)";
ComPtr<ID3DBlob> pVSBlob, pPSBlob, pErrorBlob;
hr = D3DCompile(undistort_shader_code, strlen(undistort_shader_code), NULL, NULL, NULL, "VS", "vs_5_0", 0, 0, &pVSBlob, &pErrorBlob);
if (FAILED(hr)) { if(pErrorBlob) MessageBoxA(NULL, (char*)pErrorBlob->GetBufferPointer(), "Shader Error", MB_OK); return hr; }
hr = D3DCompile(undistort_shader_code, strlen(undistort_shader_code), NULL, NULL, NULL, "PS", "ps_5_0", 0, 0, &pPSBlob, &pErrorBlob);
if (FAILED(hr)) { if(pErrorBlob) MessageBoxA(NULL, (char*)pErrorBlob->GetBufferPointer(), "Shader Error", MB_OK); return hr; }
g_pd3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pUndistortVertexShader);
g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pUndistortPixelShader);
D3D11_INPUT_ELEMENT_DESC undistort_layout[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
hr = g_pd3dDevice->CreateInputLayout(undistort_layout, ARRAYSIZE(undistort_layout), pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &g_pUndistortInputLayout);
if (FAILED(hr)) return hr;
UpdateMeshes(sharedMemoryData, zoomFactor);
D3D11_TEXTURE2D_DESC texDesc = {};
texDesc.Width = TEXTURE_WIDTH;
texDesc.Height = TEXTURE_WIDTH;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_BC4_UNORM;
texDesc.SampleDesc.Count = 1;
texDesc.Usage = D3D11_USAGE_DYNAMIC;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = g_pd3dDevice->CreateTexture2D(&texDesc, NULL, &g_pBC4Texture1);
if (FAILED(hr)) return hr;
hr = g_pd3dDevice->CreateShaderResourceView(g_pBC4Texture1.Get(), NULL, &g_pBC4TextureSRV1);
if (FAILED(hr)) return hr;
hr = g_pd3dDevice->CreateTexture2D(&texDesc, NULL, &g_pBC4Texture2);
if (FAILED(hr)) return hr;
hr = g_pd3dDevice->CreateShaderResourceView(g_pBC4Texture2.Get(), NULL, &g_pBC4TextureSRV2);
if (FAILED(hr)) return hr;
D3D11_SAMPLER_DESC sampDesc = {};
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &g_pSamplerState);
if (FAILED(hr)) return hr;
return hr;
}
void Render(SharedMemoryData& sharedMemoryData) {
D3D11_MAPPED_SUBRESOURCE mappedResourceL;
D3D11_MAPPED_SUBRESOURCE mappedResourceR;
if (SUCCEEDED(g_pImmediateContext->Map(g_pBC4Texture1.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResourceL))
&& SUCCEEDED(g_pImmediateContext->Map(g_pBC4Texture2.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResourceR))) {
copy_latest_image_buffer(sharedMemoryData, mappedResourceL.pData, mappedResourceR.pData, BC4_DATA_SIZE);
g_pImmediateContext->Unmap(g_pBC4Texture1.Get(), 0);
g_pImmediateContext->Unmap(g_pBC4Texture2.Get(), 0);
}
float clear_color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView.Get(), clear_color);
ComPtr<ID3D11Texture2D> pBackBuffer;
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &pBackBuffer);
D3D11_TEXTURE2D_DESC backBufferDesc;
pBackBuffer->GetDesc(&backBufferDesc);
float window_width = static_cast<float>(backBufferDesc.Width);
float window_height = static_cast<float>(backBufferDesc.Height);
D3D11_VIEWPORT vp;
float image_aspect_ratio = 1.0f; // Cameras are square
float viewport_width, viewport_height;
if (g_displayMode == DisplayMode::SideBySide) {
viewport_width = window_width / 2.0f;
viewport_height = viewport_width / image_aspect_ratio;
} else {
viewport_width = window_width;
viewport_height = viewport_width / image_aspect_ratio;
}
vp.Width = viewport_width;
vp.Height = viewport_height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftY = (window_height - viewport_height) / 2.0f;
UINT stride = sizeof(UndistortVertex);
UINT offset = 0;
g_pImmediateContext->IASetInputLayout(g_pUndistortInputLayout.Get());
g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
g_pImmediateContext->VSSetShader(g_pUndistortVertexShader.Get(), NULL, 0);
g_pImmediateContext->PSSetShader(g_pUndistortPixelShader.Get(), NULL, 0);
g_pImmediateContext->PSSetSamplers(0, 1, g_pSamplerState.GetAddressOf());
if (g_displayMode == DisplayMode::Camera1) {
vp.TopLeftX = 0;
g_pImmediateContext->RSSetViewports(1, &vp);
g_pImmediateContext->PSSetShaderResources(0, 1, g_pBC4TextureSRV1.GetAddressOf());
g_pImmediateContext->IASetVertexBuffers(0, 1, g_pUndistortVertexBuffer[0].GetAddressOf(), &stride, &offset);
g_pImmediateContext->IASetIndexBuffer(g_pUndistortIndexBuffer[0].Get(), DXGI_FORMAT_R32_UINT, 0);
g_pImmediateContext->DrawIndexed(g_uUndistortIndexCount[0], 0, 0);
} else if (g_displayMode == DisplayMode::Camera2) {
vp.TopLeftX = 0;
g_pImmediateContext->RSSetViewports(1, &vp);
g_pImmediateContext->PSSetShaderResources(0, 1, g_pBC4TextureSRV2.GetAddressOf());
g_pImmediateContext->IASetVertexBuffers(0, 1, g_pUndistortVertexBuffer[1].GetAddressOf(), &stride, &offset);
g_pImmediateContext->IASetIndexBuffer(g_pUndistortIndexBuffer[1].Get(), DXGI_FORMAT_R32_UINT, 0);
g_pImmediateContext->DrawIndexed(g_uUndistortIndexCount[1], 0, 0);
} else { // SideBySide
// Draw left camera
vp.TopLeftX = 0;
g_pImmediateContext->RSSetViewports(1, &vp);
g_pImmediateContext->PSSetShaderResources(0, 1, g_pBC4TextureSRV1.GetAddressOf());
g_pImmediateContext->IASetVertexBuffers(0, 1, g_pUndistortVertexBuffer[0].GetAddressOf(), &stride, &offset);
g_pImmediateContext->IASetIndexBuffer(g_pUndistortIndexBuffer[0].Get(), DXGI_FORMAT_R32_UINT, 0);
g_pImmediateContext->DrawIndexed(g_uUndistortIndexCount[0], 0, 0);
// Draw right camera
vp.TopLeftX = window_width / 2.0f;
g_pImmediateContext->RSSetViewports(1, &vp);
g_pImmediateContext->PSSetShaderResources(0, 1, g_pBC4TextureSRV2.GetAddressOf());
g_pImmediateContext->IASetVertexBuffers(0, 1, g_pUndistortVertexBuffer[1].GetAddressOf(), &stride, &offset);
g_pImmediateContext->IASetIndexBuffer(g_pUndistortIndexBuffer[1].Get(), DXGI_FORMAT_R32_UINT, 0);
g_pImmediateContext->DrawIndexed(g_uUndistortIndexCount[1], 0, 0);
}
g_pSwapChain->Present(1, 0);
}
void CleanupDevice() {
if (g_pImmediateContext) g_pImmediateContext->ClearState();
g_pUndistortInputLayout.Reset();
g_pUndistortPixelShader.Reset();
g_pUndistortVertexShader.Reset();
g_pUndistortIndexBuffer[0].Reset(); g_pUndistortIndexBuffer[1].Reset();
g_pUndistortVertexBuffer[0].Reset(); g_pUndistortVertexBuffer[1].Reset();
g_pSamplerState.Reset();
g_pBC4TextureSRV1.Reset();
g_pBC4Texture1.Reset();
g_pBC4TextureSRV2.Reset();
g_pBC4Texture2.Reset();
g_pRenderTargetView.Reset();
g_pSwapChain.Reset();
g_pImmediateContext.Reset();
g_pd3dDevice.Reset();
}