Path: blob/21.2-virgl/src/gallium/auxiliary/tgsi/tgsi_emulate.c
4565 views
/*1* Copyright (C) 2015 Advanced Micro Devices, 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 (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* 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 NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23*/2425#include "tgsi/tgsi_transform.h"26#include "tgsi/tgsi_scan.h"27#include "tgsi/tgsi_dump.h"28#include "util/u_debug.h"2930#include "tgsi_emulate.h"3132struct tgsi_emulation_context {33struct tgsi_transform_context base;34struct tgsi_shader_info info;35unsigned flags;36bool first_instruction_emitted;37};3839static inline struct tgsi_emulation_context *40tgsi_emulation_context(struct tgsi_transform_context *tctx)41{42return (struct tgsi_emulation_context *)tctx;43}4445static void46transform_decl(struct tgsi_transform_context *tctx,47struct tgsi_full_declaration *decl)48{49struct tgsi_emulation_context *ctx = tgsi_emulation_context(tctx);5051if (ctx->flags & TGSI_EMU_FORCE_PERSAMPLE_INTERP &&52decl->Declaration.File == TGSI_FILE_INPUT) {53assert(decl->Declaration.Interpolate);54decl->Interp.Location = TGSI_INTERPOLATE_LOC_SAMPLE;55}5657tctx->emit_declaration(tctx, decl);58}5960static void61passthrough_edgeflag(struct tgsi_transform_context *tctx)62{63struct tgsi_emulation_context *ctx = tgsi_emulation_context(tctx);64struct tgsi_full_declaration decl;65struct tgsi_full_instruction new_inst;6667/* Input */68decl = tgsi_default_full_declaration();69decl.Declaration.File = TGSI_FILE_INPUT;70decl.Range.First = decl.Range.Last = ctx->info.num_inputs;71tctx->emit_declaration(tctx, &decl);7273/* Output */74decl = tgsi_default_full_declaration();75decl.Declaration.File = TGSI_FILE_OUTPUT;76decl.Declaration.Semantic = true;77decl.Range.First = decl.Range.Last = ctx->info.num_outputs;78decl.Semantic.Name = TGSI_SEMANTIC_EDGEFLAG;79decl.Semantic.Index = 0;80tctx->emit_declaration(tctx, &decl);8182/* MOV */83new_inst = tgsi_default_full_instruction();84new_inst.Instruction.Opcode = TGSI_OPCODE_MOV;8586new_inst.Instruction.NumDstRegs = 1;87new_inst.Dst[0].Register.File = TGSI_FILE_OUTPUT;88new_inst.Dst[0].Register.Index = ctx->info.num_outputs;89new_inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW;9091new_inst.Instruction.NumSrcRegs = 1;92new_inst.Src[0].Register.File = TGSI_FILE_INPUT;93new_inst.Src[0].Register.Index = ctx->info.num_inputs;94new_inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X;95new_inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X;96new_inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X;97new_inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_X;9899tctx->emit_instruction(tctx, &new_inst);100}101102static void103transform_instr(struct tgsi_transform_context *tctx,104struct tgsi_full_instruction *inst)105{106struct tgsi_emulation_context *ctx = tgsi_emulation_context(tctx);107108/* Pass through edgeflags. */109if (!ctx->first_instruction_emitted) {110ctx->first_instruction_emitted = true;111112if (ctx->flags & TGSI_EMU_PASSTHROUGH_EDGEFLAG)113passthrough_edgeflag(tctx);114}115116/* Clamp color outputs. */117if (ctx->flags & TGSI_EMU_CLAMP_COLOR_OUTPUTS) {118int i;119for (i = 0; i < inst->Instruction.NumDstRegs; i++) {120unsigned semantic;121122if (inst->Dst[i].Register.File != TGSI_FILE_OUTPUT ||123inst->Dst[i].Register.Indirect)124continue;125126semantic =127ctx->info.output_semantic_name[inst->Dst[i].Register.Index];128129if (semantic == TGSI_SEMANTIC_COLOR ||130semantic == TGSI_SEMANTIC_BCOLOR)131inst->Instruction.Saturate = true;132}133}134135tctx->emit_instruction(tctx, inst);136}137138const struct tgsi_token *139tgsi_emulate(const struct tgsi_token *tokens, unsigned flags)140{141struct tgsi_emulation_context ctx;142struct tgsi_token *newtoks;143int newlen;144145if (!(flags & (TGSI_EMU_CLAMP_COLOR_OUTPUTS |146TGSI_EMU_PASSTHROUGH_EDGEFLAG |147TGSI_EMU_FORCE_PERSAMPLE_INTERP)))148return NULL;149150memset(&ctx, 0, sizeof(ctx));151ctx.flags = flags;152tgsi_scan_shader(tokens, &ctx.info);153154if (flags & TGSI_EMU_FORCE_PERSAMPLE_INTERP)155ctx.base.transform_declaration = transform_decl;156157if (flags & (TGSI_EMU_CLAMP_COLOR_OUTPUTS |158TGSI_EMU_PASSTHROUGH_EDGEFLAG))159ctx.base.transform_instruction = transform_instr;160161newlen = tgsi_num_tokens(tokens) + 20;162newtoks = tgsi_alloc_tokens(newlen);163if (!newtoks)164return NULL;165166tgsi_transform_shader(tokens, newtoks, newlen, &ctx.base);167return newtoks;168}169170171