1/************************************************************************** 2 * 3 * Copyright (C) 2015 Advanced Micro Devices, Inc. 4 * Copyright 2007 VMware, Inc. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29#include "st_cb_bitmap.h" 30#include "tgsi/tgsi_transform.h" 31#include "tgsi/tgsi_scan.h" 32#include "util/u_debug.h" 33 34struct tgsi_bitmap_transform { 35 struct tgsi_transform_context base; 36 struct tgsi_shader_info info; 37 unsigned sampler_index; 38 unsigned tex_target; 39 bool use_texcoord; 40 bool swizzle_xxxx; 41 bool first_instruction_emitted; 42}; 43 44static inline struct tgsi_bitmap_transform * 45tgsi_bitmap_transform(struct tgsi_transform_context *tctx) 46{ 47 return (struct tgsi_bitmap_transform *)tctx; 48} 49 50static void 51transform_instr(struct tgsi_transform_context *tctx, 52 struct tgsi_full_instruction *current_inst) 53{ 54 struct tgsi_bitmap_transform *ctx = tgsi_bitmap_transform(tctx); 55 struct tgsi_full_instruction inst; 56 unsigned tgsi_tex_target = ctx->tex_target == PIPE_TEXTURE_2D 57 ? TGSI_TEXTURE_2D : TGSI_TEXTURE_RECT; 58 unsigned i, semantic; 59 int texcoord_index = -1; 60 61 if (ctx->first_instruction_emitted) { 62 tctx->emit_instruction(tctx, current_inst); 63 return; 64 } 65 66 ctx->first_instruction_emitted = true; 67 68 /* Add TEMP[0] if it's missing. */ 69 if (ctx->info.file_max[TGSI_FILE_TEMPORARY] == -1) { 70 tgsi_transform_temp_decl(tctx, 0); 71 } 72 73 /* Add TEXCOORD[0] if it's missing. */ 74 semantic = ctx->use_texcoord ? TGSI_SEMANTIC_TEXCOORD : 75 TGSI_SEMANTIC_GENERIC; 76 for (i = 0; i < ctx->info.num_inputs; i++) { 77 if (ctx->info.input_semantic_name[i] == semantic && 78 ctx->info.input_semantic_index[i] == 0) { 79 texcoord_index = i; 80 break; 81 } 82 } 83 84 if (texcoord_index == -1) { 85 texcoord_index = ctx->info.num_inputs; 86 tgsi_transform_input_decl(tctx, texcoord_index, 87 semantic, 0, TGSI_INTERPOLATE_PERSPECTIVE); 88 } 89 90 /* Declare the sampler. */ 91 tgsi_transform_sampler_decl(tctx, ctx->sampler_index); 92 93 /* Declare the sampler view. */ 94 tgsi_transform_sampler_view_decl(tctx, ctx->sampler_index, 95 tgsi_tex_target, TGSI_RETURN_TYPE_FLOAT); 96 97 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */ 98 tgsi_transform_tex_inst(tctx, 99 TGSI_FILE_TEMPORARY, 0, 100 TGSI_FILE_INPUT, texcoord_index, 101 tgsi_tex_target, ctx->sampler_index); 102 103 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */ 104 inst = tgsi_default_full_instruction(); 105 inst.Instruction.Opcode = TGSI_OPCODE_KILL_IF; 106 inst.Instruction.NumDstRegs = 0; 107 inst.Instruction.NumSrcRegs = 1; 108 109 inst.Src[0].Register.File = TGSI_FILE_TEMPORARY; 110 inst.Src[0].Register.Index = 0; 111 inst.Src[0].Register.Negate = 1; 112 inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X; 113 if (ctx->swizzle_xxxx) { 114 inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X; 115 inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X; 116 inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_X; 117 } else { 118 inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y; 119 inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Z; 120 inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_W; 121 } 122 tctx->emit_instruction(tctx, &inst); 123 124 /* And emit the instruction we got. */ 125 tctx->emit_instruction(tctx, current_inst); 126} 127 128const struct tgsi_token * 129st_get_bitmap_shader(const struct tgsi_token *tokens, 130 unsigned tex_target, unsigned sampler_index, 131 bool use_texcoord, bool swizzle_xxxx) 132{ 133 struct tgsi_bitmap_transform ctx; 134 struct tgsi_token *newtoks; 135 int newlen; 136 137 assert(tex_target == PIPE_TEXTURE_2D || 138 tex_target == PIPE_TEXTURE_RECT); 139 140 memset(&ctx, 0, sizeof(ctx)); 141 ctx.base.transform_instruction = transform_instr; 142 ctx.tex_target = tex_target; 143 ctx.sampler_index = sampler_index; 144 ctx.use_texcoord = use_texcoord; 145 ctx.swizzle_xxxx = swizzle_xxxx; 146 tgsi_scan_shader(tokens, &ctx.info); 147 148 newlen = tgsi_num_tokens(tokens) + 20; 149 newtoks = tgsi_alloc_tokens(newlen); 150 if (!newtoks) 151 return NULL; 152 153 tgsi_transform_shader(tokens, newtoks, newlen, &ctx.base); 154 return newtoks; 155} 156