st/mesa: implement glBitmap shader transformation using tgsi_transform_shader
[mesa.git] / src / mesa / state_tracker / st_cb_bitmap_shader.c
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 "tgsi/tgsi_dump.h"
33 #include "util/u_debug.h"
34
35 struct tgsi_bitmap_transform {
36 struct tgsi_transform_context base;
37 struct tgsi_shader_info info;
38 unsigned sampler_index;
39 bool use_texcoord;
40 bool swizzle_xxxx;
41 bool first_instruction_emitted;
42 };
43
44 static inline struct tgsi_bitmap_transform *
45 tgsi_bitmap_transform(struct tgsi_transform_context *tctx)
46 {
47 return (struct tgsi_bitmap_transform *)tctx;
48 }
49
50 static void
51 transform_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_declaration decl;
56 struct tgsi_full_instruction inst;
57 unsigned i, semantic;
58 int texcoord_index = -1;
59
60 if (ctx->first_instruction_emitted) {
61 tctx->emit_instruction(tctx, current_inst);
62 return;
63 }
64
65 ctx->first_instruction_emitted = true;
66
67 /* Add TEMP[0] if it's missing. */
68 if (ctx->info.file_max[TGSI_FILE_TEMPORARY] == -1) {
69 decl = tgsi_default_full_declaration();
70 decl.Declaration.File = TGSI_FILE_TEMPORARY;
71 tctx->emit_declaration(tctx, &decl);
72 }
73
74 /* Add TEXCOORD[0] if it's missing. */
75 semantic = ctx->use_texcoord ? TGSI_SEMANTIC_TEXCOORD :
76 TGSI_SEMANTIC_GENERIC;
77 for (i = 0; i < ctx->info.num_inputs; i++) {
78 if (ctx->info.input_semantic_name[i] == semantic &&
79 ctx->info.input_semantic_index[i] == 0) {
80 texcoord_index = i;
81 break;
82 }
83 }
84
85 if (texcoord_index == -1) {
86 decl = tgsi_default_full_declaration();
87 decl.Declaration.File = TGSI_FILE_INPUT;
88 decl.Declaration.Semantic = 1;
89 decl.Semantic.Name = semantic;
90 decl.Declaration.Interpolate = 1;
91 decl.Interp.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
92 decl.Range.First = decl.Range.Last = ctx->info.num_inputs;
93 texcoord_index = ctx->info.num_inputs;
94 tctx->emit_declaration(tctx, &decl);
95 }
96
97 /* Declare the sampler. */
98 decl = tgsi_default_full_declaration();
99 decl.Declaration.File = TGSI_FILE_SAMPLER;
100 decl.Range.First = decl.Range.Last = ctx->sampler_index;
101 tctx->emit_declaration(tctx, &decl);
102
103 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
104 inst = tgsi_default_full_instruction();
105 inst.Instruction.Opcode = TGSI_OPCODE_TEX;
106 inst.Instruction.Texture = 1;
107 inst.Texture.Texture = TGSI_TEXTURE_2D;
108
109 inst.Instruction.NumDstRegs = 1;
110 inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
111 inst.Dst[0].Register.Index = 0;
112 inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW;
113
114 inst.Instruction.NumSrcRegs = 2;
115 inst.Src[0].Register.File = TGSI_FILE_INPUT;
116 inst.Src[0].Register.Index = texcoord_index;
117 inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X;
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 inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
122 inst.Src[1].Register.Index = ctx->sampler_index;
123
124 tctx->emit_instruction(tctx, &inst);
125
126 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
127 inst = tgsi_default_full_instruction();
128 inst.Instruction.Opcode = TGSI_OPCODE_KILL_IF;
129 inst.Instruction.NumDstRegs = 0;
130 inst.Instruction.NumSrcRegs = 1;
131
132 inst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
133 inst.Src[0].Register.Index = 0;
134 inst.Src[0].Register.Negate = 1;
135 inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X;
136 if (ctx->swizzle_xxxx) {
137 inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X;
138 inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X;
139 inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_X;
140 } else {
141 inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y;
142 inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Z;
143 inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_W;
144 }
145 tctx->emit_instruction(tctx, &inst);
146
147 /* And emit the instruction we got. */
148 tctx->emit_instruction(tctx, current_inst);
149 }
150
151 const struct tgsi_token *
152 st_get_bitmap_shader(const struct tgsi_token *tokens,
153 unsigned sampler_index,
154 bool use_texcoord, bool swizzle_xxxx)
155 {
156 struct tgsi_bitmap_transform ctx;
157 struct tgsi_token *newtoks;
158 int newlen;
159
160 memset(&ctx, 0, sizeof(ctx));
161 ctx.base.transform_instruction = transform_instr;
162 ctx.sampler_index = sampler_index;
163 ctx.use_texcoord = use_texcoord;
164 ctx.swizzle_xxxx = swizzle_xxxx;
165 tgsi_scan_shader(tokens, &ctx.info);
166
167 newlen = tgsi_num_tokens(tokens) + 20;
168 newtoks = tgsi_alloc_tokens(newlen);
169 if (!newtoks)
170 return NULL;
171
172 tgsi_transform_shader(tokens, newtoks, newlen, &ctx.base);
173 return newtoks;
174 }