Path: blob/21.2-virgl/src/gallium/auxiliary/tgsi/tgsi_two_side.c
4565 views
/*1* Copyright 2013 VMware, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the6* "Software"), to deal in the Software without restriction, including7* without limitation the rights to use, copy, modify, merge, publish,8* distribute, sub license, and/or sell copies of the Software, and to9* permit persons to whom the Software is furnished to do so, subject to10* the following conditions:11*12* The above copyright notice and this permission notice (including the13* next paragraph) shall be included in all copies or substantial portions14* of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS17* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.19* IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR20* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,21* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE22* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*/242526/**27* This utility transforms fragment shaders to facilitate two-sided lighting.28*29* Basically, if the FS has any color inputs (TGSI_SEMANTIC_COLOR) we'll:30* 1. create corresponding back-color inputs (TGSI_SEMANTIC_BCOLOR)31* 2. use the FACE register to choose between front/back colors and put the32* selected color in new temp regs.33* 3. replace reads of the original color inputs with the new temp regs.34*35* Then, the driver just needs to link the VS front/back output colors to36* the FS front/back input colors.37*/3839#include "util/u_debug.h"40#include "util/u_math.h"41#include "tgsi_info.h"42#include "tgsi_two_side.h"43#include "tgsi_transform.h"444546#define INVALID_INDEX 9999474849struct two_side_transform_context50{51struct tgsi_transform_context base;52uint num_temps;53uint num_inputs;54uint face_input; /**< index of the FACE input */55uint front_color_input[2]; /**< INPUT regs */56uint front_color_interp[2];/**< TGSI_INTERPOLATE_x */57uint back_color_input[2]; /**< INPUT regs */58uint new_colors[2]; /**< TEMP regs */59};606162static inline struct two_side_transform_context *63two_side_transform_context(struct tgsi_transform_context *ctx)64{65return (struct two_side_transform_context *) ctx;66}676869static void70xform_decl(struct tgsi_transform_context *ctx,71struct tgsi_full_declaration *decl)72{73struct two_side_transform_context *ts = two_side_transform_context(ctx);74unsigned range_end = decl->Range.Last + 1;7576if (decl->Declaration.File == TGSI_FILE_INPUT) {77if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR) {78/* found a front color */79assert(decl->Semantic.Index < 2);80ts->front_color_input[decl->Semantic.Index] = decl->Range.First;81ts->front_color_interp[decl->Semantic.Index] = decl->Interp.Interpolate;82}83else if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {84ts->face_input = decl->Range.First;85}86ts->num_inputs = MAX2(ts->num_inputs, range_end);87}88else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {89ts->num_temps = MAX2(ts->num_temps, range_end);90}9192ctx->emit_declaration(ctx, decl);93}949596static void97emit_prolog(struct tgsi_transform_context *ctx)98{99struct two_side_transform_context *ts = two_side_transform_context(ctx);100struct tgsi_full_declaration decl;101struct tgsi_full_instruction inst;102uint num_colors = 0;103uint i;104105/* Declare 0, 1 or 2 new BCOLOR inputs */106for (i = 0; i < 2; i++) {107if (ts->front_color_input[i] != INVALID_INDEX) {108decl = tgsi_default_full_declaration();109decl.Declaration.File = TGSI_FILE_INPUT;110decl.Declaration.Interpolate = 1;111decl.Declaration.Semantic = 1;112decl.Semantic.Name = TGSI_SEMANTIC_BCOLOR;113decl.Semantic.Index = i;114decl.Range.First = decl.Range.Last = ts->num_inputs++;115decl.Interp.Interpolate = ts->front_color_interp[i];116ctx->emit_declaration(ctx, &decl);117ts->back_color_input[i] = decl.Range.First;118num_colors++;119}120}121122if (num_colors > 0) {123/* Declare 1 or 2 temp registers */124decl = tgsi_default_full_declaration();125decl.Declaration.File = TGSI_FILE_TEMPORARY;126decl.Range.First = ts->num_temps;127decl.Range.Last = ts->num_temps + num_colors - 1;128ctx->emit_declaration(ctx, &decl);129ts->new_colors[0] = ts->num_temps;130ts->new_colors[1] = ts->num_temps + 1;131132if (ts->face_input == INVALID_INDEX) {133/* declare FACE INPUT register */134decl = tgsi_default_full_declaration();135decl.Declaration.File = TGSI_FILE_INPUT;136decl.Declaration.Semantic = 1;137decl.Semantic.Name = TGSI_SEMANTIC_FACE;138decl.Semantic.Index = 0;139decl.Range.First = decl.Range.Last = ts->num_inputs++;140ctx->emit_declaration(ctx, &decl);141ts->face_input = decl.Range.First;142}143144/* CMP temp[c0], face, bcolor[c0], fcolor[c0]145* temp[c0] = face < 0.0 ? bcolor[c0] : fcolor[c0]146*/147for (i = 0; i < 2; i++) {148if (ts->front_color_input[i] != INVALID_INDEX) {149inst = tgsi_default_full_instruction();150inst.Instruction.Opcode = TGSI_OPCODE_CMP;151inst.Instruction.NumDstRegs = 1;152inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;153inst.Dst[0].Register.Index = ts->new_colors[i];154inst.Instruction.NumSrcRegs = 3;155inst.Src[0].Register.File = TGSI_FILE_INPUT;156inst.Src[0].Register.Index = ts->face_input;157inst.Src[1].Register.File = TGSI_FILE_INPUT;158inst.Src[1].Register.Index = ts->back_color_input[i];159inst.Src[2].Register.File = TGSI_FILE_INPUT;160inst.Src[2].Register.Index = ts->front_color_input[i];161162ctx->emit_instruction(ctx, &inst);163}164}165}166}167168169static void170xform_inst(struct tgsi_transform_context *ctx,171struct tgsi_full_instruction *inst)172{173struct two_side_transform_context *ts = two_side_transform_context(ctx);174const struct tgsi_opcode_info *info =175tgsi_get_opcode_info(inst->Instruction.Opcode);176uint i, j;177178/* Look for src regs which reference the input color and replace179* them with the temp color.180*/181for (i = 0; i < info->num_src; i++) {182if (inst->Src[i].Register.File == TGSI_FILE_INPUT) {183for (j = 0; j < 2; j++) {184if (inst->Src[i].Register.Index == (int)ts->front_color_input[j]) {185/* replace color input with temp reg */186inst->Src[i].Register.File = TGSI_FILE_TEMPORARY;187inst->Src[i].Register.Index = ts->new_colors[j];188break;189}190}191}192}193194ctx->emit_instruction(ctx, inst);195}196197198struct tgsi_token *199tgsi_add_two_side(const struct tgsi_token *tokens_in)200{201struct two_side_transform_context transform;202const uint num_new_tokens = 100; /* should be enough */203const uint new_len = tgsi_num_tokens(tokens_in) + num_new_tokens;204struct tgsi_token *new_tokens;205206/* setup transformation context */207memset(&transform, 0, sizeof(transform));208transform.base.transform_declaration = xform_decl;209transform.base.transform_instruction = xform_inst;210transform.base.prolog = emit_prolog;211transform.face_input = INVALID_INDEX;212transform.front_color_input[0] = INVALID_INDEX;213transform.front_color_input[1] = INVALID_INDEX;214transform.front_color_interp[0] = TGSI_INTERPOLATE_COLOR;215transform.front_color_interp[1] = TGSI_INTERPOLATE_COLOR;216transform.back_color_input[0] = INVALID_INDEX;217transform.back_color_input[1] = INVALID_INDEX;218219/* allocate new tokens buffer */220new_tokens = tgsi_alloc_tokens(new_len);221if (!new_tokens)222return NULL;223224/* transform the shader */225tgsi_transform_shader(tokens_in, new_tokens, new_len, &transform.base);226227return new_tokens;228}229230231