Path: blob/21.2-virgl/src/gallium/auxiliary/postprocess/pp_run.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 "postprocess.h"28#include "postprocess/pp_filters.h"29#include "postprocess/pp_private.h"3031#include "frontend/api.h"32#include "util/u_inlines.h"33#include "util/u_sampler.h"3435#include "tgsi/tgsi_parse.h"363738void39pp_blit(struct pipe_context *pipe,40struct pipe_resource *src_tex,41int srcX0, int srcY0,42int srcX1, int srcY1,43int srcZ0,44struct pipe_surface *dst,45int dstX0, int dstY0,46int dstX1, int dstY1)47{48struct pipe_blit_info blit;4950memset(&blit, 0, sizeof(blit));5152blit.src.resource = src_tex;53blit.src.level = 0;54blit.src.format = src_tex->format;55blit.src.box.x = srcX0;56blit.src.box.y = srcY0;57blit.src.box.z = srcZ0;58blit.src.box.width = srcX1 - srcX0;59blit.src.box.height = srcY1 - srcY0;60blit.src.box.depth = 1;6162blit.dst.resource = dst->texture;63blit.dst.level = dst->u.tex.level;64blit.dst.format = dst->format;65blit.dst.box.x = dstX0;66blit.dst.box.y = dstY0;67blit.dst.box.z = 0;68blit.dst.box.width = dstX1 - dstX0;69blit.dst.box.height = dstY1 - dstY0;70blit.dst.box.depth = 1;7172blit.mask = PIPE_MASK_RGBA;7374pipe->blit(pipe, &blit);75}7677/**78* Main run function of the PP queue. Called on swapbuffers/flush.79*80* Runs all requested filters in order and handles shuffling the temp81* buffers in between.82*/83void84pp_run(struct pp_queue_t *ppq, struct pipe_resource *in,85struct pipe_resource *out, struct pipe_resource *indepth)86{87struct pipe_resource *refin = NULL, *refout = NULL;88unsigned int i;89struct cso_context *cso = ppq->p->cso;9091if (ppq->n_filters == 0)92return;9394assert(ppq->pp_queue);95assert(ppq->tmp[0]);9697if (in->width0 != ppq->p->framebuffer.width ||98in->height0 != ppq->p->framebuffer.height) {99pp_debug("Resizing the temp pp buffers\n");100pp_free_fbos(ppq);101pp_init_fbos(ppq, in->width0, in->height0);102}103104if (in == out && ppq->n_filters == 1) {105/* Make a copy of in to tmp[0] in this case. */106unsigned int w = ppq->p->framebuffer.width;107unsigned int h = ppq->p->framebuffer.height;108109110pp_blit(ppq->p->pipe, in, 0, 0,111w, h, 0, ppq->tmps[0],1120, 0, w, h);113114in = ppq->tmp[0];115}116117/* save state (restored below) */118cso_save_state(cso, (CSO_BIT_BLEND |119CSO_BIT_DEPTH_STENCIL_ALPHA |120CSO_BIT_FRAGMENT_SHADER |121CSO_BIT_FRAMEBUFFER |122CSO_BIT_TESSCTRL_SHADER |123CSO_BIT_TESSEVAL_SHADER |124CSO_BIT_GEOMETRY_SHADER |125CSO_BIT_RASTERIZER |126CSO_BIT_SAMPLE_MASK |127CSO_BIT_MIN_SAMPLES |128CSO_BIT_FRAGMENT_SAMPLERS |129CSO_BIT_STENCIL_REF |130CSO_BIT_STREAM_OUTPUTS |131CSO_BIT_VERTEX_ELEMENTS |132CSO_BIT_VERTEX_SHADER |133CSO_BIT_VIEWPORT |134CSO_BIT_PAUSE_QUERIES |135CSO_BIT_RENDER_CONDITION));136137/* set default state */138cso_set_sample_mask(cso, ~0);139cso_set_min_samples(cso, 1);140cso_set_stream_outputs(cso, 0, NULL, NULL);141cso_set_tessctrl_shader_handle(cso, NULL);142cso_set_tesseval_shader_handle(cso, NULL);143cso_set_geometry_shader_handle(cso, NULL);144cso_set_render_condition(cso, NULL, FALSE, 0);145146// Kept only for this frame.147pipe_resource_reference(&ppq->depth, indepth);148pipe_resource_reference(&refin, in);149pipe_resource_reference(&refout, out);150151switch (ppq->n_filters) {152case 0:153/* Failsafe, but never reached. */154break;155case 1: /* No temp buf */156ppq->pp_queue[0] (ppq, in, out, 0);157break;158case 2: /* One temp buf */159160ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);161ppq->pp_queue[1] (ppq, ppq->tmp[0], out, 1);162163break;164default: /* Two temp bufs */165assert(ppq->tmp[1]);166ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);167168for (i = 1; i < (ppq->n_filters - 1); i++) {169if (i % 2 == 0)170ppq->pp_queue[i] (ppq, ppq->tmp[1], ppq->tmp[0], i);171172else173ppq->pp_queue[i] (ppq, ppq->tmp[0], ppq->tmp[1], i);174}175176if (i % 2 == 0)177ppq->pp_queue[i] (ppq, ppq->tmp[1], out, i);178179else180ppq->pp_queue[i] (ppq, ppq->tmp[0], out, i);181182break;183}184185/* restore state we changed */186cso_restore_state(cso);187188/* Unbind resources that we have bound. */189struct pipe_context *pipe = ppq->p->pipe;190pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, NULL);191pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, false, NULL);192pipe->set_vertex_buffers(pipe, 0, 0, 1, false, NULL);193pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 0, 3, NULL);194195/* restore states not restored by cso */196if (ppq->p->st) {197ppq->p->st->invalidate_state(ppq->p->st,198ST_INVALIDATE_FS_SAMPLER_VIEWS |199ST_INVALIDATE_FS_CONSTBUF0 |200ST_INVALIDATE_VS_CONSTBUF0 |201ST_INVALIDATE_VERTEX_BUFFERS);202}203204pipe_resource_reference(&ppq->depth, NULL);205pipe_resource_reference(&refin, NULL);206pipe_resource_reference(&refout, NULL);207}208209210/* Utility functions for the filters. You're not forced to use these if */211/* your filter is more complicated. */212213/** Setup this resource as the filter input. */214void215pp_filter_setup_in(struct pp_program *p, struct pipe_resource *in)216{217struct pipe_sampler_view v_tmp;218u_sampler_view_default_template(&v_tmp, in, in->format);219p->view = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);220}221222/** Setup this resource as the filter output. */223void224pp_filter_setup_out(struct pp_program *p, struct pipe_resource *out)225{226p->surf.format = out->format;227228p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, out, &p->surf);229}230231/** Clean up the input and output set with the above. */232void233pp_filter_end_pass(struct pp_program *p)234{235pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);236pipe_sampler_view_reference(&p->view, NULL);237}238239/**240* Convert the TGSI assembly to a runnable shader.241*242* We need not care about geometry shaders. All we have is screen quads.243*/244void *245pp_tgsi_to_state(struct pipe_context *pipe, const char *text, bool isvs,246const char *name)247{248struct pipe_shader_state state;249struct tgsi_token *tokens = NULL;250void *ret_state = NULL;251252/*253* Allocate temporary token storage. State creation will duplicate254* tokens so we must free them on exit.255*/256tokens = tgsi_alloc_tokens(PP_MAX_TOKENS);257258if (!tokens) {259pp_debug("Failed to allocate temporary token storage.\n");260return NULL;261}262263if (tgsi_text_translate(text, tokens, PP_MAX_TOKENS) == FALSE) {264_debug_printf("pp: Failed to translate a shader for %s\n", name);265return NULL;266}267268pipe_shader_state_from_tgsi(&state, tokens);269270if (isvs) {271ret_state = pipe->create_vs_state(pipe, &state);272FREE(tokens);273} else {274ret_state = pipe->create_fs_state(pipe, &state);275FREE(tokens);276}277278return ret_state;279}280281/** Setup misc state for the filter. */282void283pp_filter_misc_state(struct pp_program *p)284{285cso_set_blend(p->cso, &p->blend);286cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);287cso_set_rasterizer(p->cso, &p->rasterizer);288cso_set_viewport(p->cso, &p->viewport);289290cso_set_vertex_elements(p->cso, &p->velem);291}292293/** Draw with the filter to the set output. */294void295pp_filter_draw(struct pp_program *p)296{297util_draw_vertex_buffer(p->pipe, p->cso, p->vbuf, 0, 0,298PIPE_PRIM_QUADS, 4, 2);299}300301/** Set the framebuffer as active. */302void303pp_filter_set_fb(struct pp_program *p)304{305cso_set_framebuffer(p->cso, &p->framebuffer);306}307308/** Set the framebuffer as active and clear it. */309void310pp_filter_set_clear_fb(struct pp_program *p)311{312cso_set_framebuffer(p->cso, &p->framebuffer);313p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR0, NULL, &p->clear_color, 0, 0);314}315316317