Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_pipe_vs.c
4570 views
/**********************************************************1* Copyright 2008-2009 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/2425#include "draw/draw_context.h"26#include "util/u_inlines.h"27#include "util/u_math.h"28#include "util/u_memory.h"29#include "util/u_bitmask.h"30#include "tgsi/tgsi_parse.h"31#include "tgsi/tgsi_text.h"3233#include "svga_context.h"34#include "svga_hw_reg.h"35#include "svga_cmd.h"36#include "svga_debug.h"37#include "svga_shader.h"38#include "svga_streamout.h"394041/**42* Substitute a debug shader.43*/44static const struct tgsi_token *45substitute_vs(unsigned shader_id, const struct tgsi_token *old_tokens)46{47#if 048if (shader_id == 12) {49static struct tgsi_token tokens[300];5051const char *text =52"VERT\n"53"DCL IN[0]\n"54"DCL IN[1]\n"55"DCL IN[2]\n"56"DCL OUT[0], POSITION\n"57"DCL TEMP[0..4]\n"58"IMM FLT32 { 1.0000, 1.0000, 1.0000, 1.0000 }\n"59"IMM FLT32 { 0.45, 1.0000, 1.0000, 1.0000 }\n"60"IMM FLT32 { 1.297863, 0.039245, 0.035993, 0.035976}\n"61"IMM FLT32 { -0.019398, 1.696131, -0.202151, -0.202050 }\n"62"IMM FLT32 { 0.051711, -0.348713, -0.979204, -0.978714 }\n"63"IMM FLT32 { 0.000000, 0.000003, 139.491577, 141.421356 }\n"64"DCL CONST[0..7]\n"65"DCL CONST[9..16]\n"66" MOV TEMP[2], IMM[0]\n"6768" MOV TEMP[2].xyz, IN[2]\n"69" MOV TEMP[2].xyz, IN[0]\n"70" MOV TEMP[2].xyz, IN[1]\n"7172" MUL TEMP[1], IMM[3], TEMP[2].yyyy\n"73" MAD TEMP[3], IMM[2], TEMP[2].xxxx, TEMP[1]\n"74" MAD TEMP[1], IMM[4], TEMP[2].zzzz, TEMP[3]\n"75" MAD TEMP[4], IMM[5], TEMP[2].wwww, TEMP[1]\n"7677" MOV OUT[0], TEMP[4]\n"78" END\n";7980if (!tgsi_text_translate(text,81tokens,82ARRAY_SIZE(tokens)))83{84assert(0);85return NULL;86}8788return tokens;89}90#endif9192return old_tokens;93}949596static void *97svga_create_vs_state(struct pipe_context *pipe,98const struct pipe_shader_state *templ)99{100struct svga_context *svga = svga_context(pipe);101struct svga_vertex_shader *vs = CALLOC_STRUCT(svga_vertex_shader);102103if (!vs)104return NULL;105106SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);107108/* substitute a debug shader?109*/110vs->base.tokens = tgsi_dup_tokens(substitute_vs(svga->debug.shader_id,111templ->tokens));112113/* Collect basic info that we'll need later:114*/115tgsi_scan_shader(vs->base.tokens, &vs->base.info);116117{118/* Need to do construct a new template in case we substituted a119* debug shader.120*/121struct pipe_shader_state tmp2 = *templ;122tmp2.tokens = vs->base.tokens;123vs->draw_shader = draw_create_vertex_shader(svga->swtnl.draw, &tmp2);124}125126vs->base.id = svga->debug.shader_id++;127128vs->generic_outputs = svga_get_generic_outputs_mask(&vs->base.info);129130/* check for any stream output declarations */131if (templ->stream_output.num_outputs) {132vs->base.stream_output = svga_create_stream_output(svga, &vs->base,133&templ->stream_output);134}135136SVGA_STATS_TIME_POP(svga_sws(svga));137return vs;138}139140141static void142svga_bind_vs_state(struct pipe_context *pipe, void *shader)143{144struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;145struct svga_context *svga = svga_context(pipe);146147if (vs == svga->curr.vs)148return;149150/* If the currently bound vertex shader has a generated geometry shader,151* then unbind the geometry shader before binding a new vertex shader.152* We need to unbind the geometry shader here because there is no153* pipe_shader associated with the generated geometry shader.154*/155if (svga->curr.vs != NULL && svga->curr.vs->gs != NULL)156svga->pipe.bind_gs_state(&svga->pipe, NULL);157158svga->curr.vs = vs;159svga->dirty |= SVGA_NEW_VS;160}161162163static void164svga_delete_vs_state(struct pipe_context *pipe, void *shader)165{166struct svga_context *svga = svga_context(pipe);167struct svga_vertex_shader *vs = (struct svga_vertex_shader *)shader;168struct svga_vertex_shader *next_vs;169struct svga_shader_variant *variant, *tmp;170171svga_hwtnl_flush_retry(svga);172173assert(vs->base.parent == NULL);174175while (vs) {176next_vs = (struct svga_vertex_shader *)vs->base.next;177178/* Check if there is a generated geometry shader to go with this179* vertex shader. If there is, then delete the geometry shader as well.180*/181if (vs->gs != NULL) {182svga->pipe.delete_gs_state(&svga->pipe, vs->gs);183}184185if (vs->base.stream_output != NULL)186svga_delete_stream_output(svga, vs->base.stream_output);187188draw_delete_vertex_shader(svga->swtnl.draw, vs->draw_shader);189190for (variant = vs->base.variants; variant; variant = tmp) {191tmp = variant->next;192193/* Check if deleting currently bound shader */194if (variant == svga->state.hw_draw.vs) {195SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, NULL));196svga->state.hw_draw.vs = NULL;197}198199svga_destroy_shader_variant(svga, variant);200}201202FREE((void *)vs->base.tokens);203FREE(vs);204vs = next_vs;205}206}207208209void210svga_init_vs_functions(struct svga_context *svga)211{212svga->pipe.create_vs_state = svga_create_vs_state;213svga->pipe.bind_vs_state = svga_bind_vs_state;214svga->pipe.delete_vs_state = svga_delete_vs_state;215}216217218219