2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 LunarG Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "util/u_memory.h"
29 #include "util/u_inlines.h"
30 #include "util/u_atomic.h"
31 #include "pipe/p_state.h"
34 #include "stw_device.h"
35 #include "stw_framebuffer.h"
36 #include "stw_pixelformat.h"
37 #include "stw_winsys.h"
40 #include <vulkan/vulkan.h>
41 #include "kopper_interface.h"
44 struct stw_st_framebuffer {
45 struct st_framebuffer_iface base;
47 struct stw_framebuffer *fb;
48 struct st_visual stvis;
50 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
51 struct pipe_resource *msaa_textures[ST_ATTACHMENT_COUNT];
52 struct pipe_resource *back_texture;
53 bool needs_fake_front;
54 unsigned texture_width, texture_height;
55 unsigned texture_mask;
58 static uint32_t stwfb_ID = 0;
61 * Is the given mutex held by the calling thread?
64 stw_own_mutex(const CRITICAL_SECTION *cs)
66 // We can't compare OwningThread with our thread handle/id (see
67 // http://stackoverflow.com/a/12675635 ) but we can compare with the
68 // OwningThread member of a critical section we know we own.
69 CRITICAL_SECTION dummy;
70 InitializeCriticalSection(&dummy);
71 EnterCriticalSection(&dummy);
73 _debug_printf("%p %p\n", cs->OwningThread, dummy.OwningThread);
74 bool ret = cs->OwningThread == dummy.OwningThread;
75 LeaveCriticalSection(&dummy);
76 DeleteCriticalSection(&dummy);
81 stw_pipe_blit(struct pipe_context *pipe,
82 struct pipe_resource *dst,
83 struct pipe_resource *src)
85 struct pipe_blit_info blit;
90 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
91 * Fragment Operations):
93 * If a framebuffer object is not bound, after all operations have
94 * been completed on the multisample buffer, the sample values for
95 * each color in the multisample buffer are combined to produce a
96 * single color value, and that value is written into the
97 * corresponding color buffers selected by DrawBuffer or
98 * DrawBuffers. An implementation may defer the writing of the color
99 * buffers until a later time, but the state of the framebuffer must
100 * behave as if the color buffers were updated as each fragment was
101 * processed. The method of combination is not specified. If the
102 * framebuffer contains sRGB values, then it is recommended that the
103 * an average of sample values is computed in a linearized space, as
104 * for blending (see section 4.1.7).
106 * In other words, to do a resolve operation in a linear space, we have
107 * to set sRGB formats if the original resources were sRGB, so don't use
108 * util_format_linear.
111 memset(&blit, 0, sizeof(blit));
112 blit.dst.resource = dst;
113 blit.dst.box.width = dst->width0;
114 blit.dst.box.height = dst->height0;
115 blit.dst.box.depth = 1;
116 blit.dst.format = dst->format;
117 blit.src.resource = src;
118 blit.src.box.width = src->width0;
119 blit.src.box.height = src->height0;
120 blit.src.box.depth = 1;
121 blit.src.format = src->format;
122 blit.mask = PIPE_MASK_RGBA;
123 blit.filter = PIPE_TEX_FILTER_NEAREST;
125 pipe->blit(pipe, &blit);
130 static_assert(sizeof(struct kopper_vk_surface_create_storage) >= sizeof(VkWin32SurfaceCreateInfoKHR), "");
133 stw_st_fill_private_loader_data(struct stw_st_framebuffer *stwfb, struct kopper_loader_info *out)
135 VkWin32SurfaceCreateInfoKHR *win32 = (VkWin32SurfaceCreateInfoKHR *)&out->bos;
136 win32->sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
139 win32->hinstance = GetModuleHandle(NULL);
140 win32->hwnd = stwfb->fb->hWnd;
141 out->has_alpha = true;
145 * Remove outdated textures and create the requested ones.
148 stw_st_framebuffer_validate_locked(struct st_context_iface *stctx,
149 struct st_framebuffer_iface *stfb,
150 unsigned width, unsigned height,
153 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
154 struct pipe_resource templ;
157 memset(&templ, 0, sizeof(templ));
158 templ.target = PIPE_TEXTURE_2D;
159 templ.width0 = width;
160 templ.height0 = height;
162 templ.array_size = 1;
163 templ.last_level = 0;
165 /* As of now, the only stw_winsys_framebuffer implementation is
166 * d3d12_wgl_framebuffer and it doesn't support front buffer
167 * drawing. A fake front texture is needed to handle that scenario.
168 * For MSAA, we just need to make sure that the back buffer also
169 * exists, so we can blt to it during flush_frontbuffer. */
170 if (mask & ST_ATTACHMENT_FRONT_LEFT_MASK &&
171 stwfb->fb->winsys_framebuffer) {
172 if (stwfb->stvis.samples <= 1)
173 stwfb->needs_fake_front = true;
175 mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
178 /* remove outdated textures */
179 if (stwfb->texture_width != width || stwfb->texture_height != height) {
180 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
181 pipe_resource_reference(&stwfb->msaa_textures[i], NULL);
182 pipe_resource_reference(&stwfb->textures[i], NULL);
184 pipe_resource_reference(&stwfb->back_texture, NULL);
186 if (stwfb->fb->winsys_framebuffer) {
187 templ.nr_samples = templ.nr_storage_samples = 1;
188 templ.format = stwfb->stvis.color_format;
189 stwfb->fb->winsys_framebuffer->resize(stwfb->fb->winsys_framebuffer, stctx->pipe, &templ);
193 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
194 enum pipe_format format;
197 /* the texture already exists or not requested */
198 if (stwfb->textures[i] || !(mask & (1 << i))) {
199 /* remember the texture */
200 if (stwfb->textures[i])
206 case ST_ATTACHMENT_FRONT_LEFT:
207 case ST_ATTACHMENT_BACK_LEFT:
208 format = stwfb->stvis.color_format;
209 bind = PIPE_BIND_DISPLAY_TARGET |
210 PIPE_BIND_SAMPLER_VIEW |
211 PIPE_BIND_RENDER_TARGET;
215 /* Covers the case where we have already created a drawable that
216 * then got swapped and now we have to make a new back buffer.
217 * For Zink, we just alias the front buffer in that case.
219 if (i == ST_ATTACHMENT_BACK_LEFT && stwfb->textures[ST_ATTACHMENT_FRONT_LEFT])
220 bind &= ~PIPE_BIND_DISPLAY_TARGET;
225 case ST_ATTACHMENT_DEPTH_STENCIL:
226 format = stwfb->stvis.depth_stencil_format;
227 bind = PIPE_BIND_DEPTH_STENCIL;
230 format = PIPE_FORMAT_NONE;
234 if (format != PIPE_FORMAT_NONE) {
235 templ.format = format;
237 if (bind != PIPE_BIND_DEPTH_STENCIL && stwfb->stvis.samples > 1) {
238 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
239 templ.nr_samples = templ.nr_storage_samples =
240 stwfb->stvis.samples;
242 stwfb->msaa_textures[i] =
243 stw_dev->screen->resource_create(stw_dev->screen, &templ);
247 templ.nr_samples = templ.nr_storage_samples = 1;
251 i < ST_ATTACHMENT_DEPTH_STENCIL &&
252 stw_dev->screen->resource_create_drawable) {
254 struct kopper_loader_info loader_info;
257 if (bind & PIPE_BIND_DISPLAY_TARGET) {
258 stw_st_fill_private_loader_data(stwfb, &loader_info);
261 data = stwfb->textures[ST_ATTACHMENT_FRONT_LEFT];
265 stw_dev->screen->resource_create_drawable(stw_dev->screen,
270 if (stwfb->fb->winsys_framebuffer)
271 stwfb->textures[i] = stwfb->fb->winsys_framebuffer->get_resource(
272 stwfb->fb->winsys_framebuffer, i);
275 stw_dev->screen->resource_create(stw_dev->screen, &templ);
282 /* When a fake front buffer is needed for drawing, we use the back buffer
283 * as it reduces the number of blits needed:
285 * - When flushing the front buffer, we only need to swap the real buffers
286 * - When swapping buffers, we can safely overwrite the fake front buffer
287 * as it is now invalid anyway.
289 * A new texture is created to store the back buffer content.
291 if (stwfb->needs_fake_front) {
292 if (!stwfb->back_texture) {
293 templ.format = stwfb->stvis.color_format;
294 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
295 templ.nr_samples = templ.nr_storage_samples = 1;
297 stwfb->back_texture =
298 stw_dev->screen->resource_create(stw_dev->screen, &templ);
300 /* TODO Only blit if there is something currently drawn on the back buffer */
301 stw_pipe_blit(stctx->pipe,
303 stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);
306 /* Copying front texture content to fake front texture (back texture) */
307 stw_pipe_blit(stctx->pipe,
308 stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
309 stwfb->textures[ST_ATTACHMENT_FRONT_LEFT]);
312 stwfb->texture_width = width;
313 stwfb->texture_height = height;
314 stwfb->texture_mask = mask;
318 stw_st_framebuffer_validate(struct st_context_iface *stctx,
319 struct st_framebuffer_iface *stfb,
320 const enum st_attachment_type *statts,
322 struct pipe_resource **out)
324 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
325 unsigned statt_mask, i;
328 for (i = 0; i < count; i++)
329 statt_mask |= 1 << statts[i];
331 stw_framebuffer_lock(stwfb->fb);
333 if (stwfb->fb->must_resize || stwfb->needs_fake_front || (statt_mask & ~stwfb->texture_mask)) {
334 stw_st_framebuffer_validate_locked(stctx, &stwfb->base,
335 stwfb->fb->width, stwfb->fb->height, statt_mask);
336 stwfb->fb->must_resize = FALSE;
339 struct pipe_resource **textures =
340 stwfb->stvis.samples > 1 ? stwfb->msaa_textures
343 for (i = 0; i < count; i++) {
344 struct pipe_resource *texture = NULL;
346 if (stwfb->needs_fake_front) {
347 if (statts[i] == ST_ATTACHMENT_FRONT_LEFT)
348 texture = textures[ST_ATTACHMENT_BACK_LEFT]; /* Fake front buffer */
349 else if (statts[i] == ST_ATTACHMENT_BACK_LEFT)
350 texture = stwfb->back_texture;
352 texture = textures[statts[i]];
354 pipe_resource_reference(&out[i], texture);
357 stw_framebuffer_unlock(stwfb->fb);
362 struct notify_before_flush_cb_args {
363 struct st_context_iface *stctx;
364 struct stw_st_framebuffer *stwfb;
369 notify_before_flush_cb(void* _args)
371 struct notify_before_flush_cb_args *args = (struct notify_before_flush_cb_args *) _args;
372 struct st_context_iface *st = args->stctx;
373 struct pipe_context *pipe = st->pipe;
375 if (args->stwfb->stvis.samples > 1) {
376 /* Resolve the MSAA back buffer. */
378 args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
379 args->stwfb->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
381 /* FRONT_LEFT is resolved in flush_frontbuffer. */
382 } else if (args->stwfb->back_texture) {
383 /* Fake front texture is dirty ?? */
385 args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
386 args->stwfb->back_texture);
389 if (args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT])
390 pipe->flush_resource(pipe, args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);
394 stw_st_flush(struct st_context_iface *stctx,
395 struct st_framebuffer_iface *stfb,
398 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
399 struct notify_before_flush_cb_args args;
400 struct pipe_fence_handle **pfence = NULL;
401 struct pipe_fence_handle *fence = NULL;
407 if (flags & ST_FLUSH_END_OF_FRAME && !stwfb->fb->winsys_framebuffer)
408 flags |= ST_FLUSH_WAIT;
410 if (flags & ST_FLUSH_WAIT)
412 stctx->flush(stctx, flags, pfence, notify_before_flush_cb, &args);
414 /* TODO: remove this if the framebuffer state doesn't change. */
415 stctx->invalidate_state(stctx, ST_INVALIDATE_FB_STATE);
419 * Present an attachment of the framebuffer.
422 stw_st_framebuffer_present_locked(HDC hdc,
423 struct st_context_iface *stctx,
424 struct st_framebuffer_iface *stfb,
425 enum st_attachment_type statt)
427 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
428 struct pipe_resource *resource;
430 assert(stw_own_mutex(&stwfb->fb->mutex));
432 resource = stwfb->textures[statt];
434 stw_framebuffer_present_locked(hdc, stwfb->fb, resource);
437 stw_framebuffer_unlock(stwfb->fb);
440 assert(!stw_own_mutex(&stwfb->fb->mutex));
446 stw_st_framebuffer_flush_front(struct st_context_iface *stctx,
447 struct st_framebuffer_iface *stfb,
448 enum st_attachment_type statt)
450 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
451 struct pipe_context *pipe = stctx->pipe;
454 bool need_swap_textures = false;
456 if (statt != ST_ATTACHMENT_FRONT_LEFT)
459 stw_framebuffer_lock(stwfb->fb);
461 /* Resolve the front buffer. */
462 if (stwfb->stvis.samples > 1) {
463 enum st_attachment_type blit_target = statt;
464 if (stwfb->fb->winsys_framebuffer) {
465 blit_target = ST_ATTACHMENT_BACK_LEFT;
466 need_swap_textures = true;
469 stw_pipe_blit(pipe, stwfb->textures[blit_target],
470 stwfb->msaa_textures[statt]);
471 } else if (stwfb->needs_fake_front) {
472 /* fake front texture is now invalid */
473 p_atomic_inc(&stwfb->base.stamp);
474 need_swap_textures = true;
477 if (need_swap_textures) {
478 struct pipe_resource *ptex = stwfb->textures[ST_ATTACHMENT_FRONT_LEFT];
479 stwfb->textures[ST_ATTACHMENT_FRONT_LEFT] = stwfb->textures[ST_ATTACHMENT_BACK_LEFT];
480 stwfb->textures[ST_ATTACHMENT_BACK_LEFT] = ptex;
483 if (stwfb->textures[statt])
484 pipe->flush_resource(pipe, stwfb->textures[statt]);
486 pipe->flush(pipe, NULL, 0);
488 /* We must not cache HDCs anywhere, as they can be invalidated by the
489 * application, or screen resolution changes. */
491 hDC = GetDC(stwfb->fb->hWnd);
493 ret = stw_st_framebuffer_present_locked(hDC, stctx, &stwfb->base, statt);
495 ReleaseDC(stwfb->fb->hWnd, hDC);
501 * Create a framebuffer interface.
503 struct st_framebuffer_iface *
504 stw_st_create_framebuffer(struct stw_framebuffer *fb, struct pipe_frontend_screen *fscreen)
506 struct stw_st_framebuffer *stwfb;
508 stwfb = CALLOC_STRUCT(stw_st_framebuffer);
513 stwfb->stvis = fb->pfi->stvis;
514 stwfb->base.ID = p_atomic_inc_return(&stwfb_ID);
515 stwfb->base.fscreen = fscreen;
517 stwfb->base.visual = &stwfb->stvis;
518 p_atomic_set(&stwfb->base.stamp, 1);
519 stwfb->base.flush_front = stw_st_framebuffer_flush_front;
520 stwfb->base.validate = stw_st_framebuffer_validate;
526 * Destroy a framebuffer interface.
529 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
531 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
534 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
535 pipe_resource_reference(&stwfb->msaa_textures[i], NULL);
536 pipe_resource_reference(&stwfb->textures[i], NULL);
538 pipe_resource_reference(&stwfb->back_texture, NULL);
540 /* Notify the st manager that the framebuffer interface is no
543 st_api_destroy_drawable(&stwfb->base);
549 * Swap the buffers of the given framebuffer.
552 stw_st_swap_framebuffer_locked(HDC hdc, struct st_context_iface *stctx,
553 struct st_framebuffer_iface *stfb)
555 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
556 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT;
557 struct pipe_resource *ptex;
560 /* swap the textures */
561 ptex = stwfb->textures[front];
562 stwfb->textures[front] = stwfb->textures[back];
563 stwfb->textures[back] = ptex;
565 /* swap msaa_textures */
566 ptex = stwfb->msaa_textures[front];
567 stwfb->msaa_textures[front] = stwfb->msaa_textures[back];
568 stwfb->msaa_textures[back] = ptex;
571 /* Fake front texture is now dirty */
572 if (stwfb->needs_fake_front)
573 p_atomic_inc(&stwfb->base.stamp);
575 /* convert to mask */
579 /* swap the bits in mask */
580 mask = stwfb->texture_mask & ~(front | back);
581 if (stwfb->texture_mask & front)
583 if (stwfb->texture_mask & back)
585 stwfb->texture_mask = mask;
587 front = ST_ATTACHMENT_FRONT_LEFT;
588 return stw_st_framebuffer_present_locked(hdc, stctx, &stwfb->base, front);
593 * Return the pipe_resource that correspond to given buffer.
595 struct pipe_resource *
596 stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb,
597 enum st_attachment_type att)
599 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
600 return stwfb->textures[att];