st/mesa: fix glDrawPixels with a texture
[mesa.git] / src / mesa / state_tracker / st_cb_drawpixels_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_drawpixels.h"
30 #include "tgsi/tgsi_transform.h"
31 #include "tgsi/tgsi_scan.h"
32
33 struct tgsi_drawpix_transform {
34 struct tgsi_transform_context base;
35 struct tgsi_shader_info info;
36 bool use_texcoord;
37 bool scale_and_bias;
38 bool pixel_maps;
39 bool first_instruction_emitted;
40 unsigned scale_const;
41 unsigned bias_const;
42 unsigned color_temp;
43 unsigned drawpix_sampler;
44 unsigned pixelmap_sampler;
45 unsigned texcoord_const;
46 };
47
48 static inline struct tgsi_drawpix_transform *
49 tgsi_drawpix_transform(struct tgsi_transform_context *tctx)
50 {
51 return (struct tgsi_drawpix_transform *)tctx;
52 }
53
54 static void
55 set_src(struct tgsi_full_instruction *inst, unsigned i, unsigned file, unsigned index,
56 unsigned x, unsigned y, unsigned z, unsigned w)
57 {
58 inst->Src[i].Register.File = file;
59 inst->Src[i].Register.Index = index;
60 inst->Src[i].Register.SwizzleX = x;
61 inst->Src[i].Register.SwizzleY = y;
62 inst->Src[i].Register.SwizzleZ = z;
63 inst->Src[i].Register.SwizzleW = w;
64 }
65
66 #define SET_SRC(inst, i, file, index, x, y, z, w) \
67 set_src(inst, i, file, index, TGSI_SWIZZLE_##x, TGSI_SWIZZLE_##y, \
68 TGSI_SWIZZLE_##z, TGSI_SWIZZLE_##w)
69
70 static void
71 transform_instr(struct tgsi_transform_context *tctx,
72 struct tgsi_full_instruction *current_inst)
73 {
74 struct tgsi_drawpix_transform *ctx = tgsi_drawpix_transform(tctx);
75 struct tgsi_full_declaration decl;
76 struct tgsi_full_instruction inst;
77 unsigned i, sem_texcoord = ctx->use_texcoord ? TGSI_SEMANTIC_TEXCOORD :
78 TGSI_SEMANTIC_GENERIC;
79 int texcoord_index = -1;
80
81 if (ctx->first_instruction_emitted)
82 goto transform_inst;
83
84 ctx->first_instruction_emitted = true;
85
86 /* Add scale and bias constants. */
87 if (ctx->scale_and_bias) {
88 if (ctx->info.const_file_max[0] < (int)ctx->scale_const) {
89 decl = tgsi_default_full_declaration();
90 decl.Declaration.File = TGSI_FILE_CONSTANT;
91 decl.Range.First = decl.Range.Last = ctx->scale_const;
92 tctx->emit_declaration(tctx, &decl);
93 }
94
95 if (ctx->info.const_file_max[0] < (int)ctx->bias_const) {
96 decl = tgsi_default_full_declaration();
97 decl.Declaration.File = TGSI_FILE_CONSTANT;
98 decl.Range.First = decl.Range.Last = ctx->bias_const;
99 tctx->emit_declaration(tctx, &decl);
100 }
101 }
102
103 if (ctx->info.const_file_max[0] < (int)ctx->texcoord_const) {
104 decl = tgsi_default_full_declaration();
105 decl.Declaration.File = TGSI_FILE_CONSTANT;
106 decl.Range.First = decl.Range.Last = ctx->texcoord_const;
107 tctx->emit_declaration(tctx, &decl);
108 }
109
110 /* Add a new temp. */
111 ctx->color_temp = ctx->info.file_max[TGSI_FILE_TEMPORARY] + 1;
112 decl = tgsi_default_full_declaration();
113 decl.Declaration.File = TGSI_FILE_TEMPORARY;
114 decl.Range.First = decl.Range.Last = ctx->color_temp;
115 tctx->emit_declaration(tctx, &decl);
116
117 /* Add TEXCOORD[texcoord_slot] if it's missing. */
118 for (i = 0; i < ctx->info.num_inputs; i++) {
119 if (ctx->info.input_semantic_name[i] == sem_texcoord &&
120 ctx->info.input_semantic_index[i] == 0) {
121 texcoord_index = i;
122 break;
123 }
124 }
125
126 if (texcoord_index == -1) {
127 decl = tgsi_default_full_declaration();
128 decl.Declaration.File = TGSI_FILE_INPUT;
129 decl.Declaration.Semantic = 1;
130 decl.Semantic.Name = sem_texcoord;
131 decl.Declaration.Interpolate = 1;
132 decl.Interp.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
133 decl.Range.First = decl.Range.Last = ctx->info.num_inputs;
134 texcoord_index = ctx->info.num_inputs;
135 tctx->emit_declaration(tctx, &decl);
136 }
137
138 /* Declare the drawpix sampler if it's missing. */
139 if (!(ctx->info.samplers_declared & (1 << ctx->drawpix_sampler))) {
140 decl = tgsi_default_full_declaration();
141 decl.Declaration.File = TGSI_FILE_SAMPLER;
142 decl.Range.First = decl.Range.Last = ctx->drawpix_sampler;
143 tctx->emit_declaration(tctx, &decl);
144 }
145
146 /* Declare the pixel map sampler if it's missing. */
147 if (ctx->pixel_maps &&
148 !(ctx->info.samplers_declared & (1 << ctx->pixelmap_sampler))) {
149 decl = tgsi_default_full_declaration();
150 decl.Declaration.File = TGSI_FILE_SAMPLER;
151 decl.Range.First = decl.Range.Last = ctx->pixelmap_sampler;
152 tctx->emit_declaration(tctx, &decl);
153 }
154
155 /* Get initial pixel color from the texture.
156 * TEX temp, fragment.texcoord[0], texture[0], 2D;
157 */
158 inst = tgsi_default_full_instruction();
159 inst.Instruction.Opcode = TGSI_OPCODE_TEX;
160 inst.Instruction.Texture = 1;
161 inst.Texture.Texture = TGSI_TEXTURE_2D;
162
163 inst.Instruction.NumDstRegs = 1;
164 inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
165 inst.Dst[0].Register.Index = ctx->color_temp;
166 inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW;
167
168 inst.Instruction.NumSrcRegs = 2;
169 SET_SRC(&inst, 0, TGSI_FILE_INPUT, texcoord_index, X, Y, Z, W);
170 inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
171 inst.Src[1].Register.Index = ctx->drawpix_sampler;
172
173 tctx->emit_instruction(tctx, &inst);
174
175 /* Apply the scale and bias. */
176 if (ctx->scale_and_bias) {
177 /* MAD temp, temp, scale, bias; */
178 inst = tgsi_default_full_instruction();
179 inst.Instruction.Opcode = TGSI_OPCODE_MAD;
180
181 inst.Instruction.NumDstRegs = 1;
182 inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
183 inst.Dst[0].Register.Index = ctx->color_temp;
184 inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZW;
185
186 inst.Instruction.NumSrcRegs = 3;
187 SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->color_temp, X, Y, Z, W);
188 SET_SRC(&inst, 1, TGSI_FILE_CONSTANT, ctx->scale_const, X, Y, Z, W);
189 SET_SRC(&inst, 2, TGSI_FILE_CONSTANT, ctx->bias_const, X, Y, Z, W);
190
191 tctx->emit_instruction(tctx, &inst);
192 }
193
194 if (ctx->pixel_maps) {
195 /* do four pixel map look-ups with two TEX instructions: */
196
197 /* TEX temp.xy, temp.xyyy, texture[1], 2D; */
198 inst = tgsi_default_full_instruction();
199 inst.Instruction.Opcode = TGSI_OPCODE_TEX;
200 inst.Instruction.Texture = 1;
201 inst.Texture.Texture = TGSI_TEXTURE_2D;
202
203 inst.Instruction.NumDstRegs = 1;
204 inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
205 inst.Dst[0].Register.Index = ctx->color_temp;
206 inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XY;
207
208 inst.Instruction.NumSrcRegs = 2;
209 SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->color_temp, X, Y, Y, Y);
210 inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
211 inst.Src[1].Register.Index = ctx->pixelmap_sampler;
212
213 tctx->emit_instruction(tctx, &inst);
214
215 /* TEX temp.zw, temp.zwww, texture[1], 2D; */
216 inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_ZW;
217 SET_SRC(&inst, 0, TGSI_FILE_TEMPORARY, ctx->color_temp, Z, W, W, W);
218 tctx->emit_instruction(tctx, &inst);
219 }
220
221 /* Now, "color_temp" should be used in place of IN:COLOR0,
222 * and CONST[texcoord_slot] should be used in place of IN:TEXCOORD0.
223 */
224
225 transform_inst:
226
227 for (i = 0; i < current_inst->Instruction.NumSrcRegs; i++) {
228 struct tgsi_full_src_register *src = &current_inst->Src[i];
229 unsigned reg = src->Register.Index;
230
231 if (src->Register.File != TGSI_FILE_INPUT || src->Register.Indirect)
232 continue;
233
234 if (ctx->info.input_semantic_name[reg] == TGSI_SEMANTIC_COLOR &&
235 ctx->info.input_semantic_index[reg] == 0) {
236 src->Register.File = TGSI_FILE_TEMPORARY;
237 src->Register.Index = ctx->color_temp;
238 } else if (ctx->info.input_semantic_name[reg] == sem_texcoord &&
239 ctx->info.input_semantic_index[reg] == 0) {
240 src->Register.File = TGSI_FILE_CONSTANT;
241 src->Register.Index = ctx->texcoord_const;
242 }
243 }
244
245 tctx->emit_instruction(tctx, current_inst);
246 }
247
248 const struct tgsi_token *
249 st_get_drawpix_shader(const struct tgsi_token *tokens, bool use_texcoord,
250 bool scale_and_bias, unsigned scale_const,
251 unsigned bias_const, bool pixel_maps,
252 unsigned drawpix_sampler, unsigned pixelmap_sampler,
253 unsigned texcoord_const)
254 {
255 struct tgsi_drawpix_transform ctx;
256 struct tgsi_token *newtoks;
257 int newlen;
258
259 memset(&ctx, 0, sizeof(ctx));
260 ctx.base.transform_instruction = transform_instr;
261 ctx.use_texcoord = use_texcoord;
262 ctx.scale_and_bias = scale_and_bias;
263 ctx.scale_const = scale_const;
264 ctx.bias_const = bias_const;
265 ctx.pixel_maps = pixel_maps;
266 ctx.drawpix_sampler = drawpix_sampler;
267 ctx.pixelmap_sampler = pixelmap_sampler;
268 ctx.texcoord_const = texcoord_const;
269 tgsi_scan_shader(tokens, &ctx.info);
270
271 newlen = tgsi_num_tokens(tokens) + 30;
272 newtoks = tgsi_alloc_tokens(newlen);
273 if (!newtoks)
274 return NULL;
275
276 tgsi_transform_shader(tokens, newtoks, newlen, &ctx.base);
277 return newtoks;
278 }