Path: blob/21.2-virgl/src/gallium/frontends/wgl/stw_st.c
4561 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 2010 LunarG Inc.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*23* Authors:24* Chia-I Wu <[email protected]>25*/2627#include "util/u_memory.h"28#include "util/u_inlines.h"29#include "util/u_atomic.h"30#include "state_tracker/st_gl_api.h" /* for st_gl_api_create */31#include "pipe/p_state.h"3233#include "stw_st.h"34#include "stw_device.h"35#include "stw_framebuffer.h"36#include "stw_pixelformat.h"37#include "stw_winsys.h"3839struct stw_st_framebuffer {40struct st_framebuffer_iface base;4142struct stw_framebuffer *fb;43struct st_visual stvis;4445struct pipe_resource *textures[ST_ATTACHMENT_COUNT];46struct pipe_resource *msaa_textures[ST_ATTACHMENT_COUNT];47struct pipe_resource *back_texture;48bool needs_fake_front;49unsigned texture_width, texture_height;50unsigned texture_mask;51};5253static uint32_t stwfb_ID = 0;5455/**56* Is the given mutex held by the calling thread?57*/58bool59stw_own_mutex(const CRITICAL_SECTION *cs)60{61// We can't compare OwningThread with our thread handle/id (see62// http://stackoverflow.com/a/12675635 ) but we can compare with the63// OwningThread member of a critical section we know we own.64CRITICAL_SECTION dummy;65InitializeCriticalSection(&dummy);66EnterCriticalSection(&dummy);67if (0)68_debug_printf("%p %p\n", cs->OwningThread, dummy.OwningThread);69bool ret = cs->OwningThread == dummy.OwningThread;70LeaveCriticalSection(&dummy);71DeleteCriticalSection(&dummy);72return ret;73}7475static void76stw_pipe_blit(struct pipe_context *pipe,77struct pipe_resource *dst,78struct pipe_resource *src)79{80struct pipe_blit_info blit;8182if (!dst || !src)83return;8485/* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample86* Fragment Operations):87*88* If a framebuffer object is not bound, after all operations have89* been completed on the multisample buffer, the sample values for90* each color in the multisample buffer are combined to produce a91* single color value, and that value is written into the92* corresponding color buffers selected by DrawBuffer or93* DrawBuffers. An implementation may defer the writing of the color94* buffers until a later time, but the state of the framebuffer must95* behave as if the color buffers were updated as each fragment was96* processed. The method of combination is not specified. If the97* framebuffer contains sRGB values, then it is recommended that the98* an average of sample values is computed in a linearized space, as99* for blending (see section 4.1.7).100*101* In other words, to do a resolve operation in a linear space, we have102* to set sRGB formats if the original resources were sRGB, so don't use103* util_format_linear.104*/105106memset(&blit, 0, sizeof(blit));107blit.dst.resource = dst;108blit.dst.box.width = dst->width0;109blit.dst.box.height = dst->height0;110blit.dst.box.depth = 1;111blit.dst.format = dst->format;112blit.src.resource = src;113blit.src.box.width = src->width0;114blit.src.box.height = src->height0;115blit.src.box.depth = 1;116blit.src.format = src->format;117blit.mask = PIPE_MASK_RGBA;118blit.filter = PIPE_TEX_FILTER_NEAREST;119120pipe->blit(pipe, &blit);121}122123/**124* Remove outdated textures and create the requested ones.125*/126static void127stw_st_framebuffer_validate_locked(struct st_context_iface *stctx,128struct st_framebuffer_iface *stfb,129unsigned width, unsigned height,130unsigned mask)131{132struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);133struct pipe_resource templ;134unsigned i;135136memset(&templ, 0, sizeof(templ));137templ.target = PIPE_TEXTURE_2D;138templ.width0 = width;139templ.height0 = height;140templ.depth0 = 1;141templ.array_size = 1;142templ.last_level = 0;143144/* As of now, the only stw_winsys_framebuffer implementation is145* d3d12_wgl_framebuffer and it doesn't support front buffer146* drawing. A fake front texture is needed to handle that scenario */147if (mask & ST_ATTACHMENT_FRONT_LEFT_MASK &&148stwfb->fb->winsys_framebuffer &&149stwfb->stvis.samples <= 1) {150stwfb->needs_fake_front = true;151}152153/* remove outdated textures */154if (stwfb->texture_width != width || stwfb->texture_height != height) {155for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {156pipe_resource_reference(&stwfb->msaa_textures[i], NULL);157pipe_resource_reference(&stwfb->textures[i], NULL);158}159pipe_resource_reference(&stwfb->back_texture, NULL);160161if (stwfb->fb->winsys_framebuffer) {162templ.nr_samples = templ.nr_storage_samples = 1;163templ.format = stwfb->stvis.color_format;164stwfb->fb->winsys_framebuffer->resize(stwfb->fb->winsys_framebuffer, stctx->pipe, &templ);165}166}167168for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {169enum pipe_format format;170unsigned bind;171172/* the texture already exists or not requested */173if (stwfb->textures[i] || !(mask & (1 << i))) {174/* remember the texture */175if (stwfb->textures[i])176mask |= (1 << i);177continue;178}179180switch (i) {181case ST_ATTACHMENT_FRONT_LEFT:182case ST_ATTACHMENT_BACK_LEFT:183format = stwfb->stvis.color_format;184bind = PIPE_BIND_DISPLAY_TARGET |185PIPE_BIND_SAMPLER_VIEW |186PIPE_BIND_RENDER_TARGET;187break;188case ST_ATTACHMENT_DEPTH_STENCIL:189format = stwfb->stvis.depth_stencil_format;190bind = PIPE_BIND_DEPTH_STENCIL;191break;192default:193format = PIPE_FORMAT_NONE;194break;195}196197if (format != PIPE_FORMAT_NONE) {198templ.format = format;199200if (bind != PIPE_BIND_DEPTH_STENCIL && stwfb->stvis.samples > 1) {201templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;202templ.nr_samples = templ.nr_storage_samples =203stwfb->stvis.samples;204205stwfb->msaa_textures[i] =206stw_dev->screen->resource_create(stw_dev->screen, &templ);207}208209templ.bind = bind;210templ.nr_samples = templ.nr_storage_samples = 1;211if (stwfb->fb->winsys_framebuffer)212stwfb->textures[i] = stwfb->fb->winsys_framebuffer->get_resource(213stwfb->fb->winsys_framebuffer, i);214else215stwfb->textures[i] =216stw_dev->screen->resource_create(stw_dev->screen, &templ);217}218}219220/* When a fake front buffer is needed for drawing, we use the back buffer221* as it reduces the number of blits needed:222*223* - When flushing the front buffer, we only need to swap the real buffers224* - When swapping buffers, we can safely overwrite the fake front buffer225* as it is now invalid anyway.226*227* A new texture is created to store the back buffer content.228*/229if (stwfb->needs_fake_front) {230if (!stwfb->back_texture) {231templ.format = stwfb->stvis.color_format;232templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;233templ.nr_samples = templ.nr_storage_samples = 1;234235stwfb->back_texture =236stw_dev->screen->resource_create(stw_dev->screen, &templ);237238/* TODO Only blit if there is something currently drawn on the back buffer */239stw_pipe_blit(stctx->pipe,240stwfb->back_texture,241stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);242}243244/* Copying front texture content to fake front texture (back texture) */245stw_pipe_blit(stctx->pipe,246stwfb->textures[ST_ATTACHMENT_BACK_LEFT],247stwfb->textures[ST_ATTACHMENT_FRONT_LEFT]);248}249250stwfb->texture_width = width;251stwfb->texture_height = height;252stwfb->texture_mask = mask;253}254255static bool256stw_st_framebuffer_validate(struct st_context_iface *stctx,257struct st_framebuffer_iface *stfb,258const enum st_attachment_type *statts,259unsigned count,260struct pipe_resource **out)261{262struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);263unsigned statt_mask, i;264265statt_mask = 0x0;266for (i = 0; i < count; i++)267statt_mask |= 1 << statts[i];268269stw_framebuffer_lock(stwfb->fb);270271if (stwfb->fb->must_resize || stwfb->needs_fake_front || (statt_mask & ~stwfb->texture_mask)) {272stw_st_framebuffer_validate_locked(stctx, &stwfb->base,273stwfb->fb->width, stwfb->fb->height, statt_mask);274stwfb->fb->must_resize = FALSE;275}276277struct pipe_resource **textures =278stwfb->stvis.samples > 1 ? stwfb->msaa_textures279: stwfb->textures;280281for (i = 0; i < count; i++) {282struct pipe_resource *texture = NULL;283284if (stwfb->needs_fake_front) {285if (statts[i] == ST_ATTACHMENT_FRONT_LEFT)286texture = textures[ST_ATTACHMENT_BACK_LEFT]; /* Fake front buffer */287else if (statts[i] == ST_ATTACHMENT_BACK_LEFT)288texture = stwfb->back_texture;289} else {290texture = textures[statts[i]];291}292pipe_resource_reference(&out[i], texture);293}294295stw_framebuffer_unlock(stwfb->fb);296297return true;298}299300struct notify_before_flush_cb_args {301struct st_context_iface *stctx;302struct stw_st_framebuffer *stwfb;303unsigned flags;304};305306static void307notify_before_flush_cb(void* _args)308{309struct notify_before_flush_cb_args *args = (struct notify_before_flush_cb_args *) _args;310struct st_context_iface *st = args->stctx;311struct pipe_context *pipe = st->pipe;312313if (args->stwfb->stvis.samples > 1) {314/* Resolve the MSAA back buffer. */315stw_pipe_blit(pipe,316args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],317args->stwfb->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);318319/* FRONT_LEFT is resolved in flush_frontbuffer. */320} else if (args->stwfb->back_texture) {321/* Fake front texture is dirty ?? */322stw_pipe_blit(pipe,323args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],324args->stwfb->back_texture);325}326327if (args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT])328pipe->flush_resource(pipe, args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);329}330331void332stw_st_flush(struct st_context_iface *stctx,333struct st_framebuffer_iface *stfb,334unsigned flags)335{336struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);337struct notify_before_flush_cb_args args;338struct pipe_fence_handle **pfence = NULL;339struct pipe_fence_handle *fence = NULL;340341args.stctx = stctx;342args.stwfb = stwfb;343args.flags = flags;344345if (flags & ST_FLUSH_END_OF_FRAME && !stwfb->fb->winsys_framebuffer)346flags |= ST_FLUSH_WAIT;347348if (flags & ST_FLUSH_WAIT)349pfence = &fence;350stctx->flush(stctx, flags, pfence, notify_before_flush_cb, &args);351}352353/**354* Present an attachment of the framebuffer.355*/356static bool357stw_st_framebuffer_present_locked(HDC hdc,358struct st_context_iface *stctx,359struct st_framebuffer_iface *stfb,360enum st_attachment_type statt)361{362struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);363struct pipe_resource *resource;364365assert(stw_own_mutex(&stwfb->fb->mutex));366367resource = stwfb->textures[statt];368if (resource) {369stw_framebuffer_present_locked(hdc, stwfb->fb, resource);370}371else {372stw_framebuffer_unlock(stwfb->fb);373}374375assert(!stw_own_mutex(&stwfb->fb->mutex));376377return true;378}379380static bool381stw_st_framebuffer_flush_front(struct st_context_iface *stctx,382struct st_framebuffer_iface *stfb,383enum st_attachment_type statt)384{385struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);386struct pipe_context *pipe = stctx->pipe;387bool ret;388HDC hDC;389390if (statt != ST_ATTACHMENT_FRONT_LEFT)391return false;392393stw_framebuffer_lock(stwfb->fb);394395/* Resolve the front buffer. */396if (stwfb->stvis.samples > 1) {397stw_pipe_blit(pipe, stwfb->textures[statt], stwfb->msaa_textures[statt]);398} else if (stwfb->needs_fake_front) {399struct pipe_resource *ptex;400401/* swap the textures */402ptex = stwfb->textures[ST_ATTACHMENT_FRONT_LEFT];403stwfb->textures[ST_ATTACHMENT_FRONT_LEFT] = stwfb->textures[ST_ATTACHMENT_BACK_LEFT];404stwfb->textures[ST_ATTACHMENT_BACK_LEFT] = ptex;405406/* fake front texture is now invalid */407p_atomic_inc(&stwfb->base.stamp);408}409410if (stwfb->textures[statt])411pipe->flush_resource(pipe, stwfb->textures[statt]);412413pipe->flush(pipe, NULL, 0);414415/* We must not cache HDCs anywhere, as they can be invalidated by the416* application, or screen resolution changes. */417418hDC = GetDC(stwfb->fb->hWnd);419420ret = stw_st_framebuffer_present_locked(hDC, stctx, &stwfb->base, statt);421422ReleaseDC(stwfb->fb->hWnd, hDC);423424return ret;425}426427/**428* Create a framebuffer interface.429*/430struct st_framebuffer_iface *431stw_st_create_framebuffer(struct stw_framebuffer *fb)432{433struct stw_st_framebuffer *stwfb;434435stwfb = CALLOC_STRUCT(stw_st_framebuffer);436if (!stwfb)437return NULL;438439stwfb->fb = fb;440stwfb->stvis = fb->pfi->stvis;441stwfb->base.ID = p_atomic_inc_return(&stwfb_ID);442stwfb->base.state_manager = stw_dev->smapi;443444stwfb->base.visual = &stwfb->stvis;445p_atomic_set(&stwfb->base.stamp, 1);446stwfb->base.flush_front = stw_st_framebuffer_flush_front;447stwfb->base.validate = stw_st_framebuffer_validate;448449return &stwfb->base;450}451452/**453* Destroy a framebuffer interface.454*/455void456stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)457{458struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);459int i;460461for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {462pipe_resource_reference(&stwfb->msaa_textures[i], NULL);463pipe_resource_reference(&stwfb->textures[i], NULL);464}465pipe_resource_reference(&stwfb->back_texture, NULL);466467/* Notify the st manager that the framebuffer interface is no468* longer valid.469*/470stw_dev->stapi->destroy_drawable(stw_dev->stapi, &stwfb->base);471472FREE(stwfb);473}474475/**476* Swap the buffers of the given framebuffer.477*/478bool479stw_st_swap_framebuffer_locked(HDC hdc, struct st_context_iface *stctx,480struct st_framebuffer_iface *stfb)481{482struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);483unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT;484struct pipe_resource *ptex;485unsigned mask;486487/* swap the textures */488ptex = stwfb->textures[front];489stwfb->textures[front] = stwfb->textures[back];490stwfb->textures[back] = ptex;491492/* swap msaa_textures */493ptex = stwfb->msaa_textures[front];494stwfb->msaa_textures[front] = stwfb->msaa_textures[back];495stwfb->msaa_textures[back] = ptex;496497498/* Fake front texture is now dirty */499if (stwfb->needs_fake_front)500p_atomic_inc(&stwfb->base.stamp);501502/* convert to mask */503front = 1 << front;504back = 1 << back;505506/* swap the bits in mask */507mask = stwfb->texture_mask & ~(front | back);508if (stwfb->texture_mask & front)509mask |= back;510if (stwfb->texture_mask & back)511mask |= front;512stwfb->texture_mask = mask;513514front = ST_ATTACHMENT_FRONT_LEFT;515return stw_st_framebuffer_present_locked(hdc, stctx, &stwfb->base, front);516}517518519/**520* Return the pipe_resource that correspond to given buffer.521*/522struct pipe_resource *523stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb,524enum st_attachment_type att)525{526struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);527return stwfb->textures[att];528}529530531/**532* Create an st_api of the gallium frontend.533*/534struct st_api *535stw_st_create_api(void)536{537return st_gl_api_create();538}539540541