Debian Codegraph @

src/gallium/winsys/d3d12/wgl/d3d12_wgl_framebuffer.cpp

raw
1 /*
2 * Copyright © Microsoft Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "d3d12_wgl_public.h"
25
26 #include <new>
27
28 #include <windows.h>
29 #include <dxgi1_4.h>
30 #include <directx/d3d12.h>
31 #include <wrl.h>
32 #include <dxguids/dxguids.h>
33
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "frontend/api.h"
37 #include "frontend/winsys_handle.h"
38
39 #include "stw_device.h"
40 #include "stw_pixelformat.h"
41 #include "stw_winsys.h"
42
43 #include "d3d12/d3d12_format.h"
44 #include "d3d12/d3d12_resource.h"
45 #include "d3d12/d3d12_screen.h"
46
47 #include <dcomp.h>
48
49 using Microsoft::WRL::ComPtr;
50 constexpr uint32_t num_buffers = 2;
51
52 struct d3d12_wgl_framebuffer {
53 struct stw_winsys_framebuffer base;
54
55 struct d3d12_screen *screen;
56 enum pipe_format pformat;
57 HWND window;
58 ComPtr<IDCompositionDevice> dcomp;
59 ComPtr<IDCompositionTarget> target;
60 ComPtr<IDCompositionVisual> visual;
61 ComPtr<IDXGISwapChain3> swapchain;
62 bool need_fillrect = true;
63 HANDLE waitable_object;
64 int latency = 2;
65 struct pipe_resource *buffers[num_buffers];
66 bool single_buffered;
67 struct pipe_resource *offscreen_buffer;
68 };
69
70 static struct d3d12_wgl_framebuffer *
71 d3d12_wgl_framebuffer(struct stw_winsys_framebuffer *fb)
72 {
73 return (struct d3d12_wgl_framebuffer *)fb;
74 }
75
76 static void
77 d3d12_wgl_framebuffer_drain_queue(struct d3d12_wgl_framebuffer *framebuffer)
78 {
79 /* Fully drain */
80 mtx_lock(&framebuffer->screen->submit_mutex);
81 UINT64 value = ++framebuffer->screen->fence_value;
82 framebuffer->screen->cmdqueue->Signal(framebuffer->screen->fence, value);
83 mtx_unlock(&framebuffer->screen->submit_mutex);
84 framebuffer->screen->fence->SetEventOnCompletion(value, nullptr);
85 }
86
87 static void
88 d3d12_wgl_framebuffer_destroy(struct stw_winsys_framebuffer *fb,
89 pipe_context *ctx)
90 {
91 struct d3d12_wgl_framebuffer *framebuffer = d3d12_wgl_framebuffer(fb);
92 struct pipe_fence_handle *fence = NULL;
93
94 if (ctx) {
95 /* Ensure all resources are flushed */
96 ctx->flush(ctx, &fence, PIPE_FLUSH_HINT_FINISH);
97 if (fence) {
98 ctx->screen->fence_finish(ctx->screen, ctx, fence, OS_TIMEOUT_INFINITE);
99 ctx->screen->fence_reference(ctx->screen, &fence, NULL);
100 }
101 }
102
103 d3d12_wgl_framebuffer_drain_queue(framebuffer);
104
105 for (uint32_t i = 0; i < num_buffers; ++i) {
106 if (framebuffer->buffers[i]) {
107 d3d12_resource_release(d3d12_resource(framebuffer->buffers[i]));
108 pipe_resource_reference(&framebuffer->buffers[i], NULL);
109 }
110 }
111
112 if (framebuffer->offscreen_buffer) {
113 pipe_resource_reference(&framebuffer->offscreen_buffer, NULL);
114 }
115
116 if (framebuffer->waitable_object)
117 CloseHandle(framebuffer->waitable_object);
118
119 delete framebuffer;
120 }
121
122 static IDCompositionDevice *
123 dcomp_get_device()
124 {
125 HMODULE dcomp_mod = LoadLibraryA("DComp.DLL");
126 if (!dcomp_mod) {
127 return NULL;
128 }
129
130 typedef HRESULT (STDAPICALLTYPE *PFN_DCOMP_CREATE_DEVICE)(IDXGIDevice *, REFIID, void **);
131 PFN_DCOMP_CREATE_DEVICE DCompositionCreateDevice;
132
133 DCompositionCreateDevice = (PFN_DCOMP_CREATE_DEVICE)GetProcAddress(dcomp_mod, "DCompositionCreateDevice");
134 if (!DCompositionCreateDevice) {
135 return NULL;
136 }
137
138 IDCompositionDevice *device;
139 HRESULT hr = DCompositionCreateDevice(NULL, IID_PPV_ARGS(&device));
140 if (FAILED(hr)) {
141 return NULL;
142 }
143
144 return device;
145 }
146
147 static void
148 d3d12_wgl_framebuffer_resize(stw_winsys_framebuffer *fb,
149 pipe_context *ctx,
150 pipe_resource *templ)
151 {
152 struct d3d12_wgl_framebuffer *framebuffer = d3d12_wgl_framebuffer(fb);
153 struct d3d12_dxgi_screen *screen = d3d12_dxgi_screen(framebuffer->screen);
154
155 DXGI_SWAP_CHAIN_DESC1 desc = {};
156 desc.BufferCount = num_buffers;
157 desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT;
158 desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING | DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
159 desc.Format = d3d12_get_format(templ->format);
160 if (desc.Format == DXGI_FORMAT_B8G8R8X8_UNORM)
161 desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
162 desc.Width = templ->width0;
163 desc.Height = templ->height0;
164 desc.SampleDesc.Count = 1;
165 desc.AlphaMode = util_format_has_alpha(templ->format) ?
166 DXGI_ALPHA_MODE_PREMULTIPLIED : DXGI_ALPHA_MODE_IGNORE;
167 desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
168 desc.Scaling = DXGI_SCALING_STRETCH;
169
170 framebuffer->pformat = templ->format;
171
172 if (desc.AlphaMode != DXGI_ALPHA_MODE_IGNORE) {
173 if (!framebuffer->dcomp) {
174 framebuffer->dcomp.Attach(dcomp_get_device());
175 if (!framebuffer->dcomp) {
176 debug_printf("D3D12: failed to create dcomp device\n");
177 return;
178 }
179 }
180
181 if (!framebuffer->target) {
182 if (FAILED(framebuffer->dcomp->CreateTargetForHwnd(framebuffer->window, TRUE, &framebuffer->target))) {
183 debug_printf("D3D12: failed to create dcomp target\n");
184 return;
185 }
186 }
187 }
188
189 if (!framebuffer->swapchain) {
190 ComPtr<IDXGISwapChain1> swapchain1;
191 if (desc.AlphaMode != DXGI_ALPHA_MODE_IGNORE) {
192 if (FAILED(screen->factory->CreateSwapChainForComposition(
193 screen->base.cmdqueue,
194 &desc,
195 nullptr,
196 &swapchain1))) {
197 debug_printf("D3D12: failed to create swapchain\n");
198 return;
199 }
200 } else {
201 if (FAILED(screen->factory->CreateSwapChainForHwnd(
202 screen->base.cmdqueue,
203 framebuffer->window,
204 &desc,
205 nullptr,
206 nullptr,
207 &swapchain1))) {
208 debug_printf("D3D12: failed to create swapchain\n");
209 return;
210 }
211 }
212
213 swapchain1.As(&framebuffer->swapchain);
214
215 framebuffer->waitable_object = framebuffer->swapchain->GetFrameLatencyWaitableObject();
216 WaitForSingleObject(framebuffer->waitable_object, INFINITE);
217
218 framebuffer->swapchain->SetMaximumFrameLatency(framebuffer->latency);
219 }
220 else {
221 struct pipe_fence_handle *fence = NULL;
222
223 /* Ensure all resources are flushed */
224 ctx->flush(ctx, &fence, PIPE_FLUSH_HINT_FINISH);
225 if (fence) {
226 ctx->screen->fence_finish(ctx->screen, ctx, fence, OS_TIMEOUT_INFINITE);
227 ctx->screen->fence_reference(ctx->screen, &fence, NULL);
228 }
229
230 d3d12_wgl_framebuffer_drain_queue(framebuffer);
231
232 for (uint32_t i = 0; i < num_buffers; ++i) {
233 if (framebuffer->buffers[i]) {
234 d3d12_resource_release(d3d12_resource(framebuffer->buffers[i]));
235 pipe_resource_reference(&framebuffer->buffers[i], NULL);
236 }
237 }
238 if (FAILED(framebuffer->swapchain->ResizeBuffers(num_buffers, desc.Width, desc.Height, desc.Format, desc.Flags))) {
239 debug_printf("D3D12: failed to resize swapchain\n");
240 }
241 }
242
243 if (!framebuffer->visual && desc.AlphaMode != DXGI_ALPHA_MODE_IGNORE) {
244 if (FAILED(framebuffer->dcomp->CreateVisual(&framebuffer->visual)) ||
245 FAILED(framebuffer->target->SetRoot(framebuffer->visual.Get())) ||
246 FAILED(framebuffer->visual->SetContent(framebuffer->swapchain.Get())) ||
247 FAILED(framebuffer->dcomp->Commit())) {
248 debug_printf("D3D12: failed to configure visual\n");
249 return;
250 }
251 }
252
253 for (uint32_t i = 0; i < num_buffers; ++i) {
254 ID3D12Resource *res;
255 framebuffer->swapchain->GetBuffer(i, IID_PPV_ARGS(&res));
256 if (!res)
257 continue;
258
259 struct winsys_handle handle;
260 memset(&handle, 0, sizeof(handle));
261 handle.type = WINSYS_HANDLE_TYPE_D3D12_RES;
262 handle.format = framebuffer->pformat;
263 handle.com_obj = res;
264
265 D3D12_RESOURCE_DESC res_desc = GetDesc(res);
266
267 struct pipe_resource templ;
268 memset(&templ, 0, sizeof(templ));
269 templ.target = PIPE_TEXTURE_2D;
270 templ.format = framebuffer->pformat;
271 templ.width0 = res_desc.Width;
272 templ.height0 = res_desc.Height;
273 templ.depth0 = 1;
274 templ.array_size = res_desc.DepthOrArraySize;
275 templ.nr_samples = res_desc.SampleDesc.Count;
276 templ.last_level = res_desc.MipLevels - 1;
277 templ.bind = PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_RENDER_TARGET;
278 templ.usage = PIPE_USAGE_DEFAULT;
279 templ.flags = 0;
280
281 pipe_resource_reference(&framebuffer->buffers[i],
282 screen->base.base.resource_from_handle(&screen->base.base, &templ, &handle,
283 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
284
285 #ifndef NDEBUG
286 struct d3d12_bo *bo = d3d12_resource(framebuffer->buffers[i])->bo;
287 bo->is_front_buffer = i != 0;
288 #endif
289 }
290
291 if (framebuffer->single_buffered) {
292 if (framebuffer->offscreen_buffer) {
293 pipe_resource_reference(&framebuffer->offscreen_buffer, NULL);
294 }
295 struct pipe_resource local_templ = *templ;
296 local_templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
297 framebuffer->offscreen_buffer = screen->base.base.resource_create(&screen->base.base, &local_templ);
298 }
299
300 framebuffer->need_fillrect = util_format_has_alpha(templ->format);
301 }
302
303 static bool
304 d3d12_wgl_framebuffer_present(stw_winsys_framebuffer *fb, int interval)
305 {
306 auto framebuffer = d3d12_wgl_framebuffer(fb);
307 if (!framebuffer->swapchain) {
308 debug_printf("D3D12: Cannot present; no swapchain");
309 return false;
310 }
311
312 if (framebuffer->need_fillrect && IsWindowVisible(framebuffer->window)) {
313 HDC hdc = GetWindowDC(framebuffer->window);
314 RECT rect;
315 GetWindowRect(framebuffer->window, &rect);
316 rect.right -= rect.left;
317 rect.bottom -= rect.top;
318 rect.left = 0;
319 rect.top = 0;
320 FillRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
321 ReleaseDC(framebuffer->window, hdc);
322 framebuffer->need_fillrect = false;
323 }
324
325 HRESULT hr;
326 if (interval < 1)
327 hr = framebuffer->swapchain->Present(0, DXGI_PRESENT_ALLOW_TEARING);
328 else
329 hr = framebuffer->swapchain->Present(interval, 0);
330 assert(SUCCEEDED(hr));
331
332 #ifndef NDEBUG
333 uint32_t back_buffer_idx = framebuffer->swapchain->GetCurrentBackBufferIndex();
334 for (uint32_t i = 0; i < num_buffers; ++i) {
335 struct d3d12_bo *bo = d3d12_resource(framebuffer->buffers[i])->bo;
336 bo->is_front_buffer = i != back_buffer_idx;
337 }
338 #endif
339
340 if (SUCCEEDED(hr))
341 (void)WaitForSingleObject(framebuffer->waitable_object, 2000);
342 return SUCCEEDED(hr);
343 }
344
345 static struct pipe_resource *
346 d3d12_wgl_framebuffer_get_resource(struct stw_winsys_framebuffer *pframebuffer,
347 st_attachment_type statt)
348 {
349 auto framebuffer = d3d12_wgl_framebuffer(pframebuffer);
350
351 if (!framebuffer->swapchain)
352 return nullptr;
353
354 if (framebuffer->single_buffered) {
355 assert(statt == ST_ATTACHMENT_FRONT_LEFT);
356 assert(framebuffer->offscreen_buffer);
357 pipe_reference(NULL, &framebuffer->offscreen_buffer->reference);
358 return framebuffer->offscreen_buffer;
359 }
360
361 UINT index = framebuffer->swapchain->GetCurrentBackBufferIndex();
362 if (statt == ST_ATTACHMENT_FRONT_LEFT)
363 index = !index;
364
365 assert(framebuffer->buffers[index]);
366 pipe_reference(NULL, &framebuffer->buffers[index]->reference);
367 return framebuffer->buffers[index];
368 }
369
370 static void
371 d3d12_wgl_framebuffer_flush_frontbuffer(struct stw_winsys_framebuffer *pframebuffer,
372 struct pipe_context *pipe)
373 {
374 auto framebuffer = d3d12_wgl_framebuffer(pframebuffer);
375 struct pipe_blit_info blit;
376
377 memset(&blit, 0, sizeof(blit));
378 uint32_t index = framebuffer->swapchain->GetCurrentBackBufferIndex();
379 blit.dst.resource = framebuffer->buffers[index];
380 blit.dst.box.width = blit.dst.resource->width0;
381 blit.dst.box.height = blit.dst.resource->height0;
382 blit.dst.box.depth = 1;
383 blit.dst.format = blit.dst.resource->format;
384 blit.src.resource = framebuffer->offscreen_buffer;
385 blit.src.box.width = blit.src.resource->width0;
386 blit.src.box.height = blit.src.resource->height0;
387 blit.src.box.depth = 1;
388 blit.src.format = blit.src.resource->format;
389 blit.mask = PIPE_MASK_RGBA;
390 blit.filter = PIPE_TEX_FILTER_NEAREST;
391
392 pipe->blit(pipe, &blit);
393 pipe->flush_resource(pipe, blit.dst.resource);
394 pipe->flush(pipe, NULL, 0);
395 }
396
397 static void
398 d3d12_wgl_framebuffer_set_latency(struct stw_winsys_framebuffer *pframebuffer,
399 int latency)
400 {
401 if (latency < 1)
402 return;
403
404 auto framebuffer = d3d12_wgl_framebuffer(pframebuffer);
405 int delta = latency - framebuffer->latency;
406 while (delta < 0 && framebuffer->waitable_object) {
407 WaitForSingleObject(framebuffer->waitable_object, INFINITE);
408 ++delta;
409 }
410 framebuffer->latency = latency;
411 if (framebuffer->swapchain)
412 framebuffer->swapchain->SetMaximumFrameLatency(latency);
413 }
414
415 struct stw_winsys_framebuffer *
416 d3d12_wgl_create_framebuffer(struct pipe_screen *screen,
417 HWND hWnd,
418 int iPixelFormat)
419 {
420 const struct stw_pixelformat_info *pfi =
421 stw_pixelformat_get_info(iPixelFormat);
422 if ((pfi->pfd.dwFlags & PFD_SUPPORT_GDI))
423 return NULL;
424
425 if (pfi->stvis.color_format != PIPE_FORMAT_B8G8R8A8_UNORM &&
426 pfi->stvis.color_format != PIPE_FORMAT_R8G8B8A8_UNORM &&
427 pfi->stvis.color_format != PIPE_FORMAT_R10G10B10A2_UNORM &&
428 pfi->stvis.color_format != PIPE_FORMAT_R16G16B16A16_FLOAT &&
429 pfi->stvis.color_format != PIPE_FORMAT_B8G8R8X8_UNORM &&
430 pfi->stvis.color_format != PIPE_FORMAT_R8G8B8X8_UNORM)
431 return NULL;
432
433 struct d3d12_wgl_framebuffer *fb = CALLOC_STRUCT(d3d12_wgl_framebuffer);
434 if (!fb)
435 return NULL;
436
437 new (fb) struct d3d12_wgl_framebuffer();
438
439 fb->window = hWnd;
440 fb->screen = d3d12_screen(screen);
441 fb->single_buffered = (pfi->pfd.dwFlags & PFD_DOUBLEBUFFER) == 0;
442 fb->base.destroy = d3d12_wgl_framebuffer_destroy;
443 fb->base.resize = d3d12_wgl_framebuffer_resize;
444 fb->base.present = d3d12_wgl_framebuffer_present;
445 fb->base.get_resource = d3d12_wgl_framebuffer_get_resource;
446 fb->base.flush_frontbuffer = d3d12_wgl_framebuffer_flush_frontbuffer;
447 fb->base.set_latency = d3d12_wgl_framebuffer_set_latency;
448
449 return &fb->base;
450 }