Path: blob/21.2-virgl/src/gallium/auxiliary/postprocess/pp_init.c
4561 views
/**************************************************************************1*2* Copyright 2011 Lauri Kasanen3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "pipe/p_compiler.h"2829#include "postprocess/filters.h"30#include "postprocess/pp_private.h"3132#include "pipe/p_screen.h"33#include "util/u_inlines.h"34#include "util/u_math.h"35#include "util/u_debug.h"36#include "util/u_memory.h"37#include "cso_cache/cso_context.h"3839/** Initialize the post-processing queue. */40struct pp_queue_t *41pp_init(struct pipe_context *pipe, const unsigned int *enabled,42struct cso_context *cso, struct st_context_iface *st)43{44unsigned int num_filters = 0;45unsigned int curpos = 0, i, tmp_req = 0;46struct pp_queue_t *ppq;4748pp_debug("Initializing the post-processing queue.\n");4950/* How many filters were requested? */51for (i = 0; i < PP_FILTERS; i++) {52if (enabled[i])53num_filters++;54}55if (num_filters == 0)56return NULL;5758ppq = CALLOC(1, sizeof(struct pp_queue_t));5960if (!ppq) {61pp_debug("Unable to allocate memory for ppq.\n");62goto error;63}6465ppq->pp_queue = CALLOC(num_filters, sizeof(pp_func));66if (ppq->pp_queue == NULL) {67pp_debug("Unable to allocate memory for pp_queue.\n");68goto error;69}7071ppq->shaders = CALLOC(num_filters, sizeof(void *));72ppq->filters = CALLOC(num_filters, sizeof(unsigned int));7374if ((ppq->shaders == NULL) ||75(ppq->filters == NULL)) {76pp_debug("Unable to allocate memory for shaders and filter arrays.\n");77goto error;78}7980ppq->p = pp_init_prog(ppq, pipe, cso, st);81if (ppq->p == NULL) {82pp_debug("pp_init_prog returned NULL.\n");83goto error;84}8586/* Add the enabled filters to the queue, in order */87curpos = 0;88for (i = 0; i < PP_FILTERS; i++) {89if (enabled[i]) {90ppq->pp_queue[curpos] = pp_filters[i].main;91tmp_req = MAX2(tmp_req, pp_filters[i].inner_tmps);92ppq->filters[curpos] = i;9394if (pp_filters[i].shaders) {95ppq->shaders[curpos] =96CALLOC(pp_filters[i].shaders + 1, sizeof(void *));97if (!ppq->shaders[curpos]) {98pp_debug("Unable to allocate memory for shader list.\n");99goto error;100}101}102103/* Call the initialization function for the filter. */104if (!pp_filters[i].init(ppq, curpos, enabled[i])) {105pp_debug("Initialization for filter %u failed.\n", i);106goto error;107}108109curpos++;110}111}112113ppq->n_filters = curpos;114ppq->n_tmp = (curpos > 2 ? 2 : 1);115ppq->n_inner_tmp = tmp_req;116117ppq->fbos_init = false;118119for (i = 0; i < curpos; i++)120ppq->shaders[i][0] = ppq->p->passvs;121122pp_debug("Queue successfully allocated. %u filter(s).\n", curpos);123124return ppq;125126error:127128if (ppq) {129/* Assign curpos, since we only need to destroy initialized filters. */130ppq->n_filters = curpos;131132/* Call the common free function which must handle partial initialization. */133pp_free(ppq);134}135136return NULL;137}138139/** Free any allocated FBOs (temp buffers). Called after resizing for example. */140void141pp_free_fbos(struct pp_queue_t *ppq)142{143144unsigned int i;145146if (!ppq->fbos_init)147return;148149for (i = 0; i < ppq->n_tmp; i++) {150pipe_surface_reference(&ppq->tmps[i], NULL);151pipe_resource_reference(&ppq->tmp[i], NULL);152}153for (i = 0; i < ppq->n_inner_tmp; i++) {154pipe_surface_reference(&ppq->inner_tmps[i], NULL);155pipe_resource_reference(&ppq->inner_tmp[i], NULL);156}157pipe_surface_reference(&ppq->stencils, NULL);158pipe_resource_reference(&ppq->stencil, NULL);159160ppq->fbos_init = false;161}162163/**164* Free the pp queue. Called on context termination and failure in165* pp_init.166*/167void168pp_free(struct pp_queue_t *ppq)169{170unsigned int i, j;171172if (!ppq)173return;174175pp_free_fbos(ppq);176177if (ppq->p) {178if (ppq->p->pipe && ppq->filters && ppq->shaders) {179for (i = 0; i < ppq->n_filters; i++) {180unsigned int filter = ppq->filters[i];181182if (ppq->shaders[i] == NULL) {183continue;184}185186/*187* Common shader destruction code for all postprocessing188* filters.189*/190for (j = 0; j < pp_filters[filter].shaders; j++) {191if (ppq->shaders[i][j] == NULL) {192/* We reached the end of initialized shaders. */193break;194}195196if (ppq->shaders[i][j] == ppq->p->passvs) {197continue;198}199200assert(ppq);201assert(ppq->p);202assert(ppq->p->pipe);203204if (j >= pp_filters[filter].verts) {205assert(ppq->p->pipe->delete_fs_state);206ppq->p->pipe->delete_fs_state(ppq->p->pipe,207ppq->shaders[i][j]);208ppq->shaders[i][j] = NULL;209} else {210assert(ppq->p->pipe->delete_vs_state);211ppq->p->pipe->delete_vs_state(ppq->p->pipe,212ppq->shaders[i][j]);213ppq->shaders[i][j] = NULL;214}215}216217/* Finally call each filter type's free functionality. */218pp_filters[filter].free(ppq, i);219}220}221222FREE(ppq->p);223}224225/*226* Handle partial initialization for common resource destruction227* in the create path.228*/229FREE(ppq->filters);230FREE(ppq->shaders);231FREE(ppq->pp_queue);232233FREE(ppq);234235pp_debug("Queue taken down.\n");236}237238/** Internal debug function. Should be available to final users. */239void240pp_debug(const char *fmt, ...)241{242va_list ap;243244if (!debug_get_bool_option("PP_DEBUG", FALSE))245return;246247va_start(ap, fmt);248_debug_vprintf(fmt, ap);249va_end(ap);250}251252/** Allocate the temp FBOs. Called on makecurrent and resize. */253void254pp_init_fbos(struct pp_queue_t *ppq, unsigned int w,255unsigned int h)256{257258struct pp_program *p = ppq->p; /* The lazy will inherit the earth */259260unsigned int i;261struct pipe_resource tmp_res;262263if (ppq->fbos_init)264return;265266pp_debug("Initializing FBOs, size %ux%u\n", w, h);267pp_debug("Requesting %u temps and %u inner temps\n", ppq->n_tmp,268ppq->n_inner_tmp);269270memset(&tmp_res, 0, sizeof(tmp_res));271tmp_res.target = PIPE_TEXTURE_2D;272tmp_res.format = p->surf.format = PIPE_FORMAT_B8G8R8A8_UNORM;273tmp_res.width0 = w;274tmp_res.height0 = h;275tmp_res.depth0 = 1;276tmp_res.array_size = 1;277tmp_res.last_level = 0;278tmp_res.bind = PIPE_BIND_RENDER_TARGET;279280if (!p->screen->is_format_supported(p->screen, tmp_res.format,281tmp_res.target, 1, 1, tmp_res.bind))282pp_debug("Temp buffers' format fail\n");283284for (i = 0; i < ppq->n_tmp; i++) {285ppq->tmp[i] = p->screen->resource_create(p->screen, &tmp_res);286ppq->tmps[i] = p->pipe->create_surface(p->pipe, ppq->tmp[i], &p->surf);287288if (!ppq->tmp[i] || !ppq->tmps[i])289goto error;290}291292for (i = 0; i < ppq->n_inner_tmp; i++) {293ppq->inner_tmp[i] = p->screen->resource_create(p->screen, &tmp_res);294ppq->inner_tmps[i] = p->pipe->create_surface(p->pipe,295ppq->inner_tmp[i],296&p->surf);297298if (!ppq->inner_tmp[i] || !ppq->inner_tmps[i])299goto error;300}301302tmp_res.bind = PIPE_BIND_DEPTH_STENCIL;303304tmp_res.format = p->surf.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;305306if (!p->screen->is_format_supported(p->screen, tmp_res.format,307tmp_res.target, 1, 1, tmp_res.bind)) {308309tmp_res.format = p->surf.format = PIPE_FORMAT_Z24_UNORM_S8_UINT;310311if (!p->screen->is_format_supported(p->screen, tmp_res.format,312tmp_res.target, 1, 1, tmp_res.bind))313pp_debug("Temp Sbuffer format fail\n");314}315316ppq->stencil = p->screen->resource_create(p->screen, &tmp_res);317ppq->stencils = p->pipe->create_surface(p->pipe, ppq->stencil, &p->surf);318if (!ppq->stencil || !ppq->stencils)319goto error;320321p->framebuffer.width = w;322p->framebuffer.height = h;323324p->viewport.scale[0] = p->viewport.translate[0] = (float) w / 2.0f;325p->viewport.scale[1] = p->viewport.translate[1] = (float) h / 2.0f;326p->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;327p->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;328p->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;329p->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;330331ppq->fbos_init = true;332333return;334335error:336pp_debug("Failed to allocate temp buffers!\n");337}338339340