Path: blob/21.2-virgl/src/gallium/drivers/softpipe/sp_state_derived.c
4570 views
/**************************************************************************1*2* Copyright 2003 VMware, Inc.3* 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 VMWARE AND/OR ITS SUPPLIERS 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 "util/u_inlines.h"28#include "util/u_math.h"29#include "util/u_memory.h"30#include "util/u_pstipple.h"31#include "pipe/p_shader_tokens.h"32#include "draw/draw_context.h"33#include "draw/draw_vertex.h"34#include "sp_context.h"35#include "sp_screen.h"36#include "sp_state.h"37#include "sp_texture.h"38#include "sp_tex_sample.h"39#include "sp_tex_tile_cache.h"404142/**43* Mark the current vertex layout as "invalid".44* We'll validate the vertex layout later, when we start to actually45* render a point or line or tri.46*/47static void48invalidate_vertex_layout(struct softpipe_context *softpipe)49{50softpipe->setup_info.valid = 0;51}525354/**55* The vertex info describes how to convert the post-transformed vertices56* (simple float[][4]) used by the 'draw' module into vertices for57* rasterization.58*59* This function validates the vertex layout.60*/61static void62softpipe_compute_vertex_info(struct softpipe_context *softpipe)63{64struct sp_setup_info *sinfo = &softpipe->setup_info;6566if (sinfo->valid == 0) {67const struct tgsi_shader_info *fsInfo = &softpipe->fs_variant->info;68struct vertex_info *vinfo = &softpipe->vertex_info;69uint i;70int vs_index;71/*72* This doesn't quite work right (wrt face injection, prim id,73* wide points) - hit a couple assertions, misrenderings plus74* memory corruption. Albeit could fix (the former two) by calling75* this "more often" (rasterizer changes etc.). (The latter would76* need to be included in draw_prepare_shader_outputs, but it looks77* like that would potentially allocate quite some unused additional78* vertex outputs.)79* draw_prepare_shader_outputs(softpipe->draw);80*/8182/*83* Those can't actually be 0 (because pos is always at 0).84* But use ints anyway to avoid confusion (in vs outputs, they85* can very well be at pos 0).86*/87softpipe->viewport_index_slot = -1;88softpipe->layer_slot = -1;89softpipe->psize_slot = -1;9091vinfo->num_attribs = 0;9293/*94* Put position always first (setup needs it there).95*/96vs_index = draw_find_shader_output(softpipe->draw,97TGSI_SEMANTIC_POSITION, 0);9899draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);100101/*102* Match FS inputs against VS outputs, emitting the necessary103* attributes.104*/105for (i = 0; i < fsInfo->num_inputs; i++) {106enum sp_interp_mode interp = SP_INTERP_LINEAR;107108switch (fsInfo->input_interpolate[i]) {109case TGSI_INTERPOLATE_CONSTANT:110interp = SP_INTERP_CONSTANT;111break;112case TGSI_INTERPOLATE_LINEAR:113interp = SP_INTERP_LINEAR;114break;115case TGSI_INTERPOLATE_PERSPECTIVE:116interp = SP_INTERP_PERSPECTIVE;117break;118case TGSI_INTERPOLATE_COLOR:119assert(fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR);120break;121default:122assert(0);123}124125switch (fsInfo->input_semantic_name[i]) {126case TGSI_SEMANTIC_POSITION:127interp = SP_INTERP_POS;128break;129130case TGSI_SEMANTIC_COLOR:131if (fsInfo->input_interpolate[i] == TGSI_INTERPOLATE_COLOR) {132if (softpipe->rasterizer->flatshade)133interp = SP_INTERP_CONSTANT;134else135interp = SP_INTERP_PERSPECTIVE;136}137break;138}139140/*141* Search for each input in current vs output:142*/143vs_index = draw_find_shader_output(softpipe->draw,144fsInfo->input_semantic_name[i],145fsInfo->input_semantic_index[i]);146147if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR &&148vs_index == -1) {149/*150* try and find a bcolor.151* Note that if there's both front and back color, draw will152* have copied back to front color already.153*/154vs_index = draw_find_shader_output(softpipe->draw,155TGSI_SEMANTIC_BCOLOR,156fsInfo->input_semantic_index[i]);157}158159sinfo->attrib[i].interp = interp;160/* extremely pointless index map */161sinfo->attrib[i].src_index = i + 1;162/*163* For vp index and layer, if the fs requires them but the vs doesn't164* provide them, draw (vbuf) will give us the required 0 (slot -1).165* (This means in this case we'll also use those slots in setup, which166* isn't necessary but they'll contain the correct (0) value.)167*/168if (fsInfo->input_semantic_name[i] ==169TGSI_SEMANTIC_VIEWPORT_INDEX) {170softpipe->viewport_index_slot = (int)vinfo->num_attribs;171draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);172} else if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_LAYER) {173softpipe->layer_slot = (int)vinfo->num_attribs;174draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);175/*176* Note that we'd actually want to skip position (as we won't use177* the attribute in the fs) but can't. The reason is that we don't178* actually have an input/output map for setup (even though it looks179* like we do...). Could adjust for this though even without a map.180*/181} else {182/*183* Note that we'd actually want to skip position (as we won't use184* the attribute in the fs) but can't. The reason is that we don't185* actually have an input/output map for setup (even though it looks186* like we do...). Could adjust for this though even without a map.187*/188draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);189}190}191192/* Figure out if we need pointsize as well.193*/194vs_index = draw_find_shader_output(softpipe->draw,195TGSI_SEMANTIC_PSIZE, 0);196197if (vs_index >= 0) {198softpipe->psize_slot = (int)vinfo->num_attribs;199draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);200}201202/* Figure out if we need viewport index (if it wasn't already in fs input) */203if (softpipe->viewport_index_slot < 0) {204vs_index = draw_find_shader_output(softpipe->draw,205TGSI_SEMANTIC_VIEWPORT_INDEX,2060);207if (vs_index >= 0) {208softpipe->viewport_index_slot =(int)vinfo->num_attribs;209draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);210}211}212213/* Figure out if we need layer (if it wasn't already in fs input) */214if (softpipe->layer_slot < 0) {215vs_index = draw_find_shader_output(softpipe->draw,216TGSI_SEMANTIC_LAYER,2170);218if (vs_index >= 0) {219softpipe->layer_slot = (int)vinfo->num_attribs;220draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);221}222}223224draw_compute_vertex_size(vinfo);225softpipe->setup_info.valid = 1;226}227return;228}229230231/**232* Called from vbuf module.233*234* This will trigger validation of the vertex layout (and also compute235* the required information for setup).236*/237struct vertex_info *238softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)239{240softpipe_compute_vertex_info(softpipe);241return &softpipe->vertex_info;242}243244245/**246* Recompute cliprect from scissor bounds, scissor enable and surface size.247*/248static void249compute_cliprect(struct softpipe_context *sp)250{251unsigned i;252/* SP_NEW_FRAMEBUFFER253*/254uint surfWidth = sp->framebuffer.width;255uint surfHeight = sp->framebuffer.height;256257for (i = 0; i < PIPE_MAX_VIEWPORTS; i++) {258/* SP_NEW_RASTERIZER259*/260if (sp->rasterizer->scissor) {261262/* SP_NEW_SCISSOR263*264* clip to scissor rect:265*/266sp->cliprect[i].minx = MAX2(sp->scissors[i].minx, 0);267sp->cliprect[i].miny = MAX2(sp->scissors[i].miny, 0);268sp->cliprect[i].maxx = MIN2(sp->scissors[i].maxx, surfWidth);269sp->cliprect[i].maxy = MIN2(sp->scissors[i].maxy, surfHeight);270}271else {272/* clip to surface bounds */273sp->cliprect[i].minx = 0;274sp->cliprect[i].miny = 0;275sp->cliprect[i].maxx = surfWidth;276sp->cliprect[i].maxy = surfHeight;277}278}279}280281282static void283set_shader_sampler(struct softpipe_context *softpipe,284enum pipe_shader_type shader,285int max_sampler)286{287int i;288for (i = 0; i <= max_sampler; i++) {289softpipe->tgsi.sampler[shader]->sp_sampler[i] =290(struct sp_sampler *)(softpipe->samplers[shader][i]);291}292}293294void295softpipe_update_compute_samplers(struct softpipe_context *softpipe)296{297set_shader_sampler(softpipe, PIPE_SHADER_COMPUTE, softpipe->cs->max_sampler);298}299300static void301update_tgsi_samplers( struct softpipe_context *softpipe )302{303unsigned i, sh;304305set_shader_sampler(softpipe, PIPE_SHADER_VERTEX,306softpipe->vs->max_sampler);307set_shader_sampler(softpipe, PIPE_SHADER_FRAGMENT,308softpipe->fs_variant->info.file_max[TGSI_FILE_SAMPLER]);309if (softpipe->gs) {310set_shader_sampler(softpipe, PIPE_SHADER_GEOMETRY,311softpipe->gs->max_sampler);312}313314/* XXX is this really necessary here??? */315for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {316for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {317struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[sh][i];318if (tc && tc->texture) {319struct softpipe_resource *spt = softpipe_resource(tc->texture);320if (spt->timestamp != tc->timestamp) {321sp_tex_tile_cache_validate_texture( tc );322/*323_debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);324*/325tc->timestamp = spt->timestamp;326}327}328}329}330}331332333static void334update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)335{336struct sp_fragment_shader_variant_key key;337338memset(&key, 0, sizeof(key));339340if (prim == PIPE_PRIM_TRIANGLES)341key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;342343if (softpipe->fs) {344softpipe->fs_variant = softpipe_find_fs_variant(softpipe,345softpipe->fs, &key);346347/* prepare the TGSI interpreter for FS execution */348softpipe->fs_variant->prepare(softpipe->fs_variant,349softpipe->fs_machine,350(struct tgsi_sampler *) softpipe->351tgsi.sampler[PIPE_SHADER_FRAGMENT],352(struct tgsi_image *)softpipe->tgsi.image[PIPE_SHADER_FRAGMENT],353(struct tgsi_buffer *)softpipe->tgsi.buffer[PIPE_SHADER_FRAGMENT]);354}355else {356softpipe->fs_variant = NULL;357}358359/* This would be the logical place to pass the fragment shader360* to the draw module. However, doing this here, during state361* validation, causes problems with the 'draw' module helpers for362* wide/AA/stippled lines.363* In principle, the draw's fragment shader should be per-variant364* but that doesn't work. So we use a single draw fragment shader365* per fragment shader, not per variant.366*/367#if 0368if (softpipe->fs_variant) {369draw_bind_fragment_shader(softpipe->draw,370softpipe->fs_variant->draw_shader);371}372else {373draw_bind_fragment_shader(softpipe->draw, NULL);374}375#endif376}377378379/**380* This should be called when the polygon stipple pattern changes.381* We create a new texture from the stipple pattern and create a new382* sampler view.383*/384static void385update_polygon_stipple_pattern(struct softpipe_context *softpipe)386{387struct pipe_resource *tex;388struct pipe_sampler_view *view;389390tex = util_pstipple_create_stipple_texture(&softpipe->pipe,391softpipe->poly_stipple.stipple);392pipe_resource_reference(&softpipe->pstipple.texture, tex);393pipe_resource_reference(&tex, NULL);394395view = util_pstipple_create_sampler_view(&softpipe->pipe,396softpipe->pstipple.texture);397pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);398pipe_sampler_view_reference(&view, NULL);399}400401402/**403* Should be called when polygon stipple is enabled/disabled or when404* the fragment shader changes.405* We add/update the fragment sampler and sampler views to sample from406* the polygon stipple texture. The texture unit that we use depends on407* the fragment shader (we need to use a unit not otherwise used by the408* shader).409*/410static void411update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)412{413if (prim == PIPE_PRIM_TRIANGLES &&414softpipe->fs_variant->key.polygon_stipple) {415const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;416417/* sampler state */418softpipe->samplers[PIPE_SHADER_FRAGMENT][unit] = softpipe->pstipple.sampler;419420/* sampler view state */421softpipe_set_sampler_views(&softpipe->pipe, PIPE_SHADER_FRAGMENT,422unit, 1, 0, &softpipe->pstipple.sampler_view);423424softpipe->dirty |= SP_NEW_SAMPLER;425}426}427428429/* Hopefully this will remain quite simple, otherwise need to pull in430* something like the gallium frontend mechanism.431*/432void433softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)434{435struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);436437/* Check for updated textures.438*/439if (softpipe->tex_timestamp != sp_screen->timestamp) {440softpipe->tex_timestamp = sp_screen->timestamp;441softpipe->dirty |= SP_NEW_TEXTURE;442}443444#if DO_PSTIPPLE_IN_HELPER_MODULE445if (softpipe->dirty & SP_NEW_STIPPLE)446/* before updating samplers! */447update_polygon_stipple_pattern(softpipe);448#endif449450if (softpipe->dirty & (SP_NEW_RASTERIZER |451SP_NEW_FS))452update_fragment_shader(softpipe, prim);453454#if DO_PSTIPPLE_IN_HELPER_MODULE455if (softpipe->dirty & (SP_NEW_RASTERIZER |456SP_NEW_STIPPLE |457SP_NEW_FS))458update_polygon_stipple_enable(softpipe, prim);459#endif460461/* TODO: this looks suboptimal */462if (softpipe->dirty & (SP_NEW_SAMPLER |463SP_NEW_TEXTURE |464SP_NEW_FS |465SP_NEW_VS))466update_tgsi_samplers( softpipe );467468if (softpipe->dirty & (SP_NEW_RASTERIZER |469SP_NEW_FS |470SP_NEW_VS))471invalidate_vertex_layout( softpipe );472473if (softpipe->dirty & (SP_NEW_SCISSOR |474SP_NEW_RASTERIZER |475SP_NEW_FRAMEBUFFER))476compute_cliprect(softpipe);477478if (softpipe->dirty & (SP_NEW_BLEND |479SP_NEW_DEPTH_STENCIL_ALPHA |480SP_NEW_FRAMEBUFFER |481SP_NEW_STIPPLE |482SP_NEW_FS))483sp_build_quad_pipeline(softpipe);484485softpipe->dirty = 0;486}487488489