Path: blob/21.2-virgl/src/gallium/drivers/r300/r300_vs.c
4570 views
/*1* Copyright 2009 Corbin Simpson <[email protected]>2* Copyright 2009 Marek Olšák <[email protected]>3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE. */2223#include "r300_vs.h"2425#include "r300_context.h"26#include "r300_screen.h"27#include "r300_tgsi_to_rc.h"28#include "r300_reg.h"2930#include "tgsi/tgsi_dump.h"31#include "tgsi/tgsi_parse.h"32#include "tgsi/tgsi_ureg.h"3334#include "compiler/radeon_compiler.h"3536/* Convert info about VS output semantics into r300_shader_semantics. */37static void r300_shader_read_vs_outputs(38struct r300_context *r300,39struct tgsi_shader_info* info,40struct r300_shader_semantics* vs_outputs)41{42int i;43unsigned index;4445r300_shader_semantics_reset(vs_outputs);4647for (i = 0; i < info->num_outputs; i++) {48index = info->output_semantic_index[i];4950switch (info->output_semantic_name[i]) {51case TGSI_SEMANTIC_POSITION:52assert(index == 0);53vs_outputs->pos = i;54break;5556case TGSI_SEMANTIC_PSIZE:57assert(index == 0);58vs_outputs->psize = i;59break;6061case TGSI_SEMANTIC_COLOR:62assert(index < ATTR_COLOR_COUNT);63vs_outputs->color[index] = i;64break;6566case TGSI_SEMANTIC_BCOLOR:67assert(index < ATTR_COLOR_COUNT);68vs_outputs->bcolor[index] = i;69break;7071case TGSI_SEMANTIC_GENERIC:72assert(index < ATTR_GENERIC_COUNT);73vs_outputs->generic[index] = i;74vs_outputs->num_generic++;75break;7677case TGSI_SEMANTIC_FOG:78assert(index == 0);79vs_outputs->fog = i;80break;8182case TGSI_SEMANTIC_EDGEFLAG:83assert(index == 0);84fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");85break;8687case TGSI_SEMANTIC_CLIPVERTEX:88assert(index == 0);89/* Draw does clip vertex for us. */90if (r300->screen->caps.has_tcl) {91fprintf(stderr, "r300 VP: cannot handle clip vertex output.\n");92}93break;9495default:96fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",97info->output_semantic_name[i]);98}99}100101/* WPOS is a straight copy of POSITION and it's always emitted. */102vs_outputs->wpos = i;103}104105static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)106{107struct r300_vertex_shader * vs = c->UserData;108struct r300_shader_semantics* outputs = &vs->outputs;109struct tgsi_shader_info* info = &vs->info;110int i, reg = 0;111boolean any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||112outputs->bcolor[1] != ATTR_UNUSED;113114/* Fill in the input mapping */115for (i = 0; i < info->num_inputs; i++)116c->code->inputs[i] = i;117118/* Position. */119if (outputs->pos != ATTR_UNUSED) {120c->code->outputs[outputs->pos] = reg++;121} else {122assert(0);123}124125/* Point size. */126if (outputs->psize != ATTR_UNUSED) {127c->code->outputs[outputs->psize] = reg++;128}129130/* If we're writing back facing colors we need to send131* four colors to make front/back face colors selection work.132* If the vertex program doesn't write all 4 colors, lets133* pretend it does by skipping output index reg so the colors134* get written into appropriate output vectors.135*/136137/* Colors. */138for (i = 0; i < ATTR_COLOR_COUNT; i++) {139if (outputs->color[i] != ATTR_UNUSED) {140c->code->outputs[outputs->color[i]] = reg++;141} else if (any_bcolor_used ||142outputs->color[1] != ATTR_UNUSED) {143reg++;144}145}146147/* Back-face colors. */148for (i = 0; i < ATTR_COLOR_COUNT; i++) {149if (outputs->bcolor[i] != ATTR_UNUSED) {150c->code->outputs[outputs->bcolor[i]] = reg++;151} else if (any_bcolor_used) {152reg++;153}154}155156/* Texture coordinates. */157for (i = 0; i < ATTR_GENERIC_COUNT; i++) {158if (outputs->generic[i] != ATTR_UNUSED) {159c->code->outputs[outputs->generic[i]] = reg++;160}161}162163/* Fog coordinates. */164if (outputs->fog != ATTR_UNUSED) {165c->code->outputs[outputs->fog] = reg++;166}167168/* WPOS. */169c->code->outputs[outputs->wpos] = reg++;170}171172void r300_init_vs_outputs(struct r300_context *r300,173struct r300_vertex_shader *vs)174{175tgsi_scan_shader(vs->state.tokens, &vs->info);176r300_shader_read_vs_outputs(r300, &vs->info, &vs->outputs);177}178179static void r300_dummy_vertex_shader(180struct r300_context* r300,181struct r300_vertex_shader* shader)182{183struct ureg_program *ureg;184struct ureg_dst dst;185struct ureg_src imm;186187/* Make a simple vertex shader which outputs (0, 0, 0, 1),188* effectively rendering nothing. */189ureg = ureg_create(PIPE_SHADER_VERTEX);190dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);191imm = ureg_imm4f(ureg, 0, 0, 0, 1);192193ureg_MOV(ureg, dst, imm);194ureg_END(ureg);195196shader->state.tokens = tgsi_dup_tokens(ureg_finalize(ureg));197ureg_destroy(ureg);198199shader->dummy = TRUE;200r300_init_vs_outputs(r300, shader);201r300_translate_vertex_shader(r300, shader);202}203204void r300_translate_vertex_shader(struct r300_context *r300,205struct r300_vertex_shader *vs)206{207struct r300_vertex_program_compiler compiler;208struct tgsi_to_rc ttr;209unsigned i;210211/* Setup the compiler */212memset(&compiler, 0, sizeof(compiler));213rc_init(&compiler.Base, NULL);214215DBG_ON(r300, DBG_VP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;216DBG_ON(r300, DBG_P_STAT) ? compiler.Base.Debug |= RC_DBG_STATS : 0;217compiler.code = &vs->code;218compiler.UserData = vs;219compiler.Base.is_r500 = r300->screen->caps.is_r500;220compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);221compiler.Base.has_half_swizzles = FALSE;222compiler.Base.has_presub = FALSE;223compiler.Base.has_omod = FALSE;224compiler.Base.max_temp_regs = 32;225compiler.Base.max_constants = 256;226compiler.Base.max_alu_insts = r300->screen->caps.is_r500 ? 1024 : 256;227228if (compiler.Base.Debug & RC_DBG_LOG) {229DBG(r300, DBG_VP, "r300: Initial vertex program\n");230tgsi_dump(vs->state.tokens, 0);231}232233/* Translate TGSI to our internal representation */234ttr.compiler = &compiler.Base;235ttr.info = &vs->info;236ttr.use_half_swizzles = FALSE;237238r300_tgsi_to_rc(&ttr, vs->state.tokens);239240if (ttr.error) {241fprintf(stderr, "r300 VP: Cannot translate a shader. "242"Using a dummy shader instead.\n");243r300_dummy_vertex_shader(r300, vs);244return;245}246247if (compiler.Base.Program.Constants.Count > 200) {248compiler.Base.remove_unused_constants = TRUE;249}250251compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs + 1));252compiler.SetHwInputOutput = &set_vertex_inputs_outputs;253254/* Insert the WPOS output. */255rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);256257/* Invoke the compiler */258r3xx_compile_vertex_program(&compiler);259if (compiler.Base.Error) {260fprintf(stderr, "r300 VP: Compiler error:\n%sUsing a dummy shader"261" instead.\n", compiler.Base.ErrorMsg);262263if (vs->dummy) {264fprintf(stderr, "r300 VP: Cannot compile the dummy shader! "265"Giving up...\n");266abort();267}268269rc_destroy(&compiler.Base);270r300_dummy_vertex_shader(r300, vs);271return;272}273274/* Initialize numbers of constants for each type. */275vs->externals_count = 0;276for (i = 0;277i < vs->code.constants.Count &&278vs->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {279vs->externals_count = i+1;280}281for (; i < vs->code.constants.Count; i++) {282assert(vs->code.constants.Constants[i].Type == RC_CONSTANT_IMMEDIATE);283}284vs->immediates_count = vs->code.constants.Count - vs->externals_count;285286/* And, finally... */287rc_destroy(&compiler.Base);288}289290291