gallium: rename st_manager to pipe_frontend_screen (think dri_screen)
[platform/upstream/mesa.git] / src / gallium / frontends / wgl / stw_st.c
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2010 LunarG Inc.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
23  *
24  * Authors:
25  *    Chia-I Wu <olv@lunarg.com>
26  */
27
28 #include "util/u_memory.h"
29 #include "util/u_inlines.h"
30 #include "util/u_atomic.h"
31 #include "pipe/p_state.h"
32
33 #include "stw_st.h"
34 #include "stw_device.h"
35 #include "stw_framebuffer.h"
36 #include "stw_pixelformat.h"
37 #include "stw_winsys.h"
38
39 #ifdef GALLIUM_ZINK
40 #include <vulkan/vulkan.h>
41 #include "kopper_interface.h"
42 #endif
43
44 struct stw_st_framebuffer {
45    struct st_framebuffer_iface base;
46
47    struct stw_framebuffer *fb;
48    struct st_visual stvis;
49
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;
56 };
57
58 static uint32_t stwfb_ID = 0;
59
60 /**
61  * Is the given mutex held by the calling thread?
62  */
63 bool
64 stw_own_mutex(const CRITICAL_SECTION *cs)
65 {
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);
72    if (0)
73       _debug_printf("%p %p\n", cs->OwningThread, dummy.OwningThread);
74    bool ret = cs->OwningThread == dummy.OwningThread;
75    LeaveCriticalSection(&dummy);
76    DeleteCriticalSection(&dummy);
77    return ret;
78 }
79
80 static void
81 stw_pipe_blit(struct pipe_context *pipe,
82               struct pipe_resource *dst,
83               struct pipe_resource *src)
84 {
85    struct pipe_blit_info blit;
86
87    if (!dst || !src)
88       return;
89
90    /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
91     *  Fragment Operations):
92     *
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).
105     *
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.
109     */
110
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;
124
125    pipe->blit(pipe, &blit);
126 }
127
128 #ifdef GALLIUM_ZINK
129
130 static_assert(sizeof(struct kopper_vk_surface_create_storage) >= sizeof(VkWin32SurfaceCreateInfoKHR), "");
131
132 static void
133 stw_st_fill_private_loader_data(struct stw_st_framebuffer *stwfb, struct kopper_loader_info *out)
134 {
135    VkWin32SurfaceCreateInfoKHR *win32 = (VkWin32SurfaceCreateInfoKHR *)&out->bos;
136    win32->sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
137    win32->pNext = NULL;
138    win32->flags = 0;
139    win32->hinstance = GetModuleHandle(NULL);
140    win32->hwnd = stwfb->fb->hWnd;
141    out->has_alpha = true;
142 }
143 #endif
144 /**
145  * Remove outdated textures and create the requested ones.
146  */
147 static void
148 stw_st_framebuffer_validate_locked(struct st_context_iface *stctx,
149                                    struct st_framebuffer_iface *stfb,
150                                    unsigned width, unsigned height,
151                                    unsigned mask)
152 {
153    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
154    struct pipe_resource templ;
155    unsigned i;
156
157    memset(&templ, 0, sizeof(templ));
158    templ.target = PIPE_TEXTURE_2D;
159    templ.width0 = width;
160    templ.height0 = height;
161    templ.depth0 = 1;
162    templ.array_size = 1;
163    templ.last_level = 0;
164
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;
174       else
175          mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
176    }
177
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);
183       }
184       pipe_resource_reference(&stwfb->back_texture, NULL);
185
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);
190       }
191    }
192
193    for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
194       enum pipe_format format;
195       unsigned bind;
196
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])
201             mask |= (1 << i);
202          continue;
203       }
204
205       switch (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;
212
213 #ifdef GALLIUM_ZINK
214          if (stw_dev->zink) {
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.
218              */
219             if (i == ST_ATTACHMENT_BACK_LEFT && stwfb->textures[ST_ATTACHMENT_FRONT_LEFT])
220                bind &= ~PIPE_BIND_DISPLAY_TARGET;
221          }
222 #endif
223
224          break;
225       case ST_ATTACHMENT_DEPTH_STENCIL:
226          format = stwfb->stvis.depth_stencil_format;
227          bind = PIPE_BIND_DEPTH_STENCIL;
228          break;
229       default:
230          format = PIPE_FORMAT_NONE;
231          break;
232       }
233
234       if (format != PIPE_FORMAT_NONE) {
235          templ.format = format;
236
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;
241
242             stwfb->msaa_textures[i] =
243                stw_dev->screen->resource_create(stw_dev->screen, &templ);
244          }
245
246          templ.bind = bind;
247          templ.nr_samples = templ.nr_storage_samples = 1;
248
249 #ifdef GALLIUM_ZINK
250          if (stw_dev->zink &&
251              i < ST_ATTACHMENT_DEPTH_STENCIL &&
252              stw_dev->screen->resource_create_drawable) {
253
254             struct kopper_loader_info loader_info;
255             void *data;
256
257             if (bind & PIPE_BIND_DISPLAY_TARGET) {
258                stw_st_fill_private_loader_data(stwfb, &loader_info);
259                data = &loader_info;
260             } else
261                data = stwfb->textures[ST_ATTACHMENT_FRONT_LEFT];
262
263             assert(data);
264             stwfb->textures[i] =
265                stw_dev->screen->resource_create_drawable(stw_dev->screen,
266                                                              &templ,
267                                                              data);
268          } else {
269 #endif
270             if (stwfb->fb->winsys_framebuffer)
271                stwfb->textures[i] = stwfb->fb->winsys_framebuffer->get_resource(
272                   stwfb->fb->winsys_framebuffer, i);
273             else
274                stwfb->textures[i] =
275                   stw_dev->screen->resource_create(stw_dev->screen, &templ);
276 #ifdef GALLIUM_ZINK
277          }
278 #endif
279       }
280    }
281
282    /* When a fake front buffer is needed for drawing, we use the back buffer
283     * as it reduces the number of blits needed:
284     *
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.
288     *
289     * A new texture is created to store the back buffer content.
290     */
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;
296
297          stwfb->back_texture =
298             stw_dev->screen->resource_create(stw_dev->screen, &templ);
299
300          /* TODO Only blit if there is something currently drawn on the back buffer */
301          stw_pipe_blit(stctx->pipe,
302                        stwfb->back_texture,
303                        stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);
304       }
305
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]);
310    }
311
312    stwfb->texture_width = width;
313    stwfb->texture_height = height;
314    stwfb->texture_mask = mask;
315 }
316
317 static bool
318 stw_st_framebuffer_validate(struct st_context_iface *stctx,
319                             struct st_framebuffer_iface *stfb,
320                             const enum st_attachment_type *statts,
321                             unsigned count,
322                             struct pipe_resource **out)
323 {
324    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
325    unsigned statt_mask, i;
326
327    statt_mask = 0x0;
328    for (i = 0; i < count; i++)
329       statt_mask |= 1 << statts[i];
330
331    stw_framebuffer_lock(stwfb->fb);
332
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;
337    }
338
339    struct pipe_resource **textures =
340       stwfb->stvis.samples > 1 ? stwfb->msaa_textures
341                                : stwfb->textures;
342
343    for (i = 0; i < count; i++) {
344       struct pipe_resource *texture = NULL;
345
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;
351       } else {
352          texture = textures[statts[i]];
353       }
354       pipe_resource_reference(&out[i], texture);
355    }
356
357    stw_framebuffer_unlock(stwfb->fb);
358
359    return true;
360 }
361
362 struct notify_before_flush_cb_args {
363    struct st_context_iface *stctx;
364    struct stw_st_framebuffer *stwfb;
365    unsigned flags;
366 };
367
368 static void
369 notify_before_flush_cb(void* _args)
370 {
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;
374
375    if (args->stwfb->stvis.samples > 1) {
376       /* Resolve the MSAA back buffer. */
377       stw_pipe_blit(pipe,
378                     args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
379                     args->stwfb->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
380
381       /* FRONT_LEFT is resolved in flush_frontbuffer. */
382    } else if (args->stwfb->back_texture) {
383       /* Fake front texture is dirty ?? */
384       stw_pipe_blit(pipe,
385                     args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
386                     args->stwfb->back_texture);
387    }
388
389    if (args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT])
390       pipe->flush_resource(pipe, args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);
391 }
392
393 void
394 stw_st_flush(struct st_context_iface *stctx,
395              struct st_framebuffer_iface *stfb,
396              unsigned flags)
397 {
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;
402
403    args.stctx = stctx;
404    args.stwfb = stwfb;
405    args.flags = flags;
406
407    if (flags & ST_FLUSH_END_OF_FRAME && !stwfb->fb->winsys_framebuffer)
408       flags |= ST_FLUSH_WAIT;
409
410    if (flags & ST_FLUSH_WAIT)
411       pfence = &fence;
412    stctx->flush(stctx, flags, pfence, notify_before_flush_cb, &args);
413
414    /* TODO: remove this if the framebuffer state doesn't change. */
415    stctx->invalidate_state(stctx, ST_INVALIDATE_FB_STATE);
416 }
417
418 /**
419  * Present an attachment of the framebuffer.
420  */
421 static bool
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)
426 {
427    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
428    struct pipe_resource *resource;
429
430    assert(stw_own_mutex(&stwfb->fb->mutex));
431
432    resource = stwfb->textures[statt];
433    if (resource) {
434       stw_framebuffer_present_locked(hdc, stwfb->fb, resource);
435    }
436    else {
437       stw_framebuffer_unlock(stwfb->fb);
438    }
439
440    assert(!stw_own_mutex(&stwfb->fb->mutex));
441
442    return true;
443 }
444
445 static bool
446 stw_st_framebuffer_flush_front(struct st_context_iface *stctx,
447                                struct st_framebuffer_iface *stfb,
448                                enum st_attachment_type statt)
449 {
450    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
451    struct pipe_context *pipe = stctx->pipe;
452    bool ret;
453    HDC hDC;
454    bool need_swap_textures = false;
455
456    if (statt != ST_ATTACHMENT_FRONT_LEFT)
457       return false;
458
459    stw_framebuffer_lock(stwfb->fb);
460
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;
467       }
468
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;
475    }
476
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;
481    }
482
483    if (stwfb->textures[statt])
484       pipe->flush_resource(pipe, stwfb->textures[statt]);
485
486    pipe->flush(pipe, NULL, 0);
487
488    /* We must not cache HDCs anywhere, as they can be invalidated by the
489     * application, or screen resolution changes. */
490
491    hDC = GetDC(stwfb->fb->hWnd);
492
493    ret = stw_st_framebuffer_present_locked(hDC, stctx, &stwfb->base, statt);
494
495    ReleaseDC(stwfb->fb->hWnd, hDC);
496
497    return ret;
498 }
499
500 /**
501  * Create a framebuffer interface.
502  */
503 struct st_framebuffer_iface *
504 stw_st_create_framebuffer(struct stw_framebuffer *fb, struct pipe_frontend_screen *fscreen)
505 {
506    struct stw_st_framebuffer *stwfb;
507
508    stwfb = CALLOC_STRUCT(stw_st_framebuffer);
509    if (!stwfb)
510       return NULL;
511
512    stwfb->fb = fb;
513    stwfb->stvis = fb->pfi->stvis;
514    stwfb->base.ID = p_atomic_inc_return(&stwfb_ID);
515    stwfb->base.fscreen = fscreen;
516
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;
521
522    return &stwfb->base;
523 }
524
525 /**
526  * Destroy a framebuffer interface.
527  */
528 void
529 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
530 {
531    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
532    int i;
533
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);
537    }
538    pipe_resource_reference(&stwfb->back_texture, NULL);
539
540    /* Notify the st manager that the framebuffer interface is no
541     * longer valid.
542     */
543    st_api_destroy_drawable(&stwfb->base);
544
545    FREE(stwfb);
546 }
547
548 /**
549  * Swap the buffers of the given framebuffer.
550  */
551 bool
552 stw_st_swap_framebuffer_locked(HDC hdc, struct st_context_iface *stctx,
553                                struct st_framebuffer_iface *stfb)
554 {
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;
558    unsigned mask;
559
560    /* swap the textures */
561    ptex = stwfb->textures[front];
562    stwfb->textures[front] = stwfb->textures[back];
563    stwfb->textures[back] = ptex;
564
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;
569
570
571    /* Fake front texture is now dirty */
572    if (stwfb->needs_fake_front)
573       p_atomic_inc(&stwfb->base.stamp);
574
575    /* convert to mask */
576    front = 1 << front;
577    back = 1 << back;
578
579    /* swap the bits in mask */
580    mask = stwfb->texture_mask & ~(front | back);
581    if (stwfb->texture_mask & front)
582       mask |= back;
583    if (stwfb->texture_mask & back)
584       mask |= front;
585    stwfb->texture_mask = mask;
586
587    front = ST_ATTACHMENT_FRONT_LEFT;
588    return stw_st_framebuffer_present_locked(hdc, stctx, &stwfb->base, front);
589 }
590
591
592 /**
593  * Return the pipe_resource that correspond to given buffer.
594  */
595 struct pipe_resource *
596 stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb,
597                              enum st_attachment_type att)
598 {
599    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
600    return stwfb->textures[att];
601 }