1 /**************************************************************************
3 * Copyright (C) 2015 Advanced Micro Devices, Inc.
4 * Copyright 2007 VMware, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #include "st_cb_drawpixels.h"
30 #include "tgsi/tgsi_transform.h"
31 #include "tgsi/tgsi_scan.h"
33 struct tgsi_drawpix_transform
{
34 struct tgsi_transform_context base
;
35 struct tgsi_shader_info info
;
39 bool first_instruction_emitted
;
43 unsigned drawpix_sampler
;
44 unsigned pixelmap_sampler
;
45 unsigned texcoord_const
;
48 static inline struct tgsi_drawpix_transform
*
49 tgsi_drawpix_transform(struct tgsi_transform_context
*tctx
)
51 return (struct tgsi_drawpix_transform
*)tctx
;
55 set_src(struct tgsi_full_instruction
*inst
, unsigned i
, unsigned file
, unsigned index
,
56 unsigned x
, unsigned y
, unsigned z
, unsigned w
)
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
;
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)
71 transform_instr(struct tgsi_transform_context
*tctx
,
72 struct tgsi_full_instruction
*current_inst
)
74 struct tgsi_drawpix_transform
*ctx
= tgsi_drawpix_transform(tctx
);
75 unsigned i
, sem_texcoord
= ctx
->use_texcoord
? TGSI_SEMANTIC_TEXCOORD
:
76 TGSI_SEMANTIC_GENERIC
;
77 int texcoord_index
= -1;
79 if (ctx
->first_instruction_emitted
)
82 ctx
->first_instruction_emitted
= true;
84 /* Add scale and bias constants. */
85 if (ctx
->scale_and_bias
) {
86 if (ctx
->info
.const_file_max
[0] < (int)ctx
->scale_const
) {
87 tgsi_transform_const_decl(tctx
, ctx
->scale_const
, ctx
->scale_const
);
90 if (ctx
->info
.const_file_max
[0] < (int)ctx
->bias_const
) {
91 tgsi_transform_const_decl(tctx
, ctx
->bias_const
, ctx
->bias_const
);
95 if (ctx
->info
.const_file_max
[0] < (int)ctx
->texcoord_const
) {
96 tgsi_transform_const_decl(tctx
, ctx
->texcoord_const
, ctx
->texcoord_const
);
100 ctx
->color_temp
= ctx
->info
.file_max
[TGSI_FILE_TEMPORARY
] + 1;
101 tgsi_transform_temp_decl(tctx
, ctx
->color_temp
);
103 /* Add TEXCOORD[texcoord_slot] if it's missing. */
104 for (i
= 0; i
< ctx
->info
.num_inputs
; i
++) {
105 if (ctx
->info
.input_semantic_name
[i
] == sem_texcoord
&&
106 ctx
->info
.input_semantic_index
[i
] == 0) {
112 if (texcoord_index
== -1) {
113 texcoord_index
= ctx
->info
.num_inputs
;
114 tgsi_transform_input_decl(tctx
, texcoord_index
, sem_texcoord
, 0,
115 TGSI_INTERPOLATE_PERSPECTIVE
);
118 /* Declare the drawpix sampler if it's missing. */
119 if (!(ctx
->info
.samplers_declared
& (1 << ctx
->drawpix_sampler
))) {
120 tgsi_transform_sampler_decl(tctx
, ctx
->drawpix_sampler
);
123 /* Declare the pixel map sampler if it's missing. */
124 if (ctx
->pixel_maps
&&
125 !(ctx
->info
.samplers_declared
& (1 << ctx
->pixelmap_sampler
))) {
126 tgsi_transform_sampler_decl(tctx
, ctx
->pixelmap_sampler
);
129 /* Get initial pixel color from the texture.
130 * TEX temp, fragment.texcoord[0], texture[0], 2D;
132 tgsi_transform_tex_2d_inst(tctx
, TGSI_FILE_TEMPORARY
, ctx
->color_temp
,
133 TGSI_FILE_INPUT
, texcoord_index
,
134 ctx
->drawpix_sampler
);
136 /* Apply the scale and bias. */
137 if (ctx
->scale_and_bias
) {
138 /* MAD temp, temp, scale, bias; */
139 tgsi_transform_op3_inst(tctx
, TGSI_OPCODE_MAD
,
140 TGSI_FILE_TEMPORARY
, ctx
->color_temp
,
142 TGSI_FILE_TEMPORARY
, ctx
->color_temp
,
143 TGSI_FILE_CONSTANT
, ctx
->scale_const
,
144 TGSI_FILE_CONSTANT
, ctx
->bias_const
);
147 if (ctx
->pixel_maps
) {
148 /* do four pixel map look-ups with two TEX instructions: */
149 struct tgsi_full_instruction inst
;
151 /* TEX temp.xy, temp.xyyy, texture[1], 2D; */
152 inst
= tgsi_default_full_instruction();
153 inst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
154 inst
.Instruction
.Texture
= 1;
155 inst
.Texture
.Texture
= TGSI_TEXTURE_2D
;
157 inst
.Instruction
.NumDstRegs
= 1;
158 inst
.Dst
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
159 inst
.Dst
[0].Register
.Index
= ctx
->color_temp
;
160 inst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_XY
;
162 inst
.Instruction
.NumSrcRegs
= 2;
163 SET_SRC(&inst
, 0, TGSI_FILE_TEMPORARY
, ctx
->color_temp
, X
, Y
, Y
, Y
);
164 inst
.Src
[1].Register
.File
= TGSI_FILE_SAMPLER
;
165 inst
.Src
[1].Register
.Index
= ctx
->pixelmap_sampler
;
167 tctx
->emit_instruction(tctx
, &inst
);
169 /* TEX temp.zw, temp.zwww, texture[1], 2D; */
170 inst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_ZW
;
171 SET_SRC(&inst
, 0, TGSI_FILE_TEMPORARY
, ctx
->color_temp
, Z
, W
, W
, W
);
172 tctx
->emit_instruction(tctx
, &inst
);
175 /* Now, "color_temp" should be used in place of IN:COLOR0,
176 * and CONST[texcoord_slot] should be used in place of IN:TEXCOORD0.
181 for (i
= 0; i
< current_inst
->Instruction
.NumSrcRegs
; i
++) {
182 struct tgsi_full_src_register
*src
= ¤t_inst
->Src
[i
];
183 unsigned reg
= src
->Register
.Index
;
185 if (src
->Register
.File
!= TGSI_FILE_INPUT
|| src
->Register
.Indirect
)
188 if (ctx
->info
.input_semantic_name
[reg
] == TGSI_SEMANTIC_COLOR
&&
189 ctx
->info
.input_semantic_index
[reg
] == 0) {
190 src
->Register
.File
= TGSI_FILE_TEMPORARY
;
191 src
->Register
.Index
= ctx
->color_temp
;
192 } else if (ctx
->info
.input_semantic_name
[reg
] == sem_texcoord
&&
193 ctx
->info
.input_semantic_index
[reg
] == 0) {
194 src
->Register
.File
= TGSI_FILE_CONSTANT
;
195 src
->Register
.Index
= ctx
->texcoord_const
;
199 tctx
->emit_instruction(tctx
, current_inst
);
202 const struct tgsi_token
*
203 st_get_drawpix_shader(const struct tgsi_token
*tokens
, bool use_texcoord
,
204 bool scale_and_bias
, unsigned scale_const
,
205 unsigned bias_const
, bool pixel_maps
,
206 unsigned drawpix_sampler
, unsigned pixelmap_sampler
,
207 unsigned texcoord_const
)
209 struct tgsi_drawpix_transform ctx
;
210 struct tgsi_token
*newtoks
;
213 memset(&ctx
, 0, sizeof(ctx
));
214 ctx
.base
.transform_instruction
= transform_instr
;
215 ctx
.use_texcoord
= use_texcoord
;
216 ctx
.scale_and_bias
= scale_and_bias
;
217 ctx
.scale_const
= scale_const
;
218 ctx
.bias_const
= bias_const
;
219 ctx
.pixel_maps
= pixel_maps
;
220 ctx
.drawpix_sampler
= drawpix_sampler
;
221 ctx
.pixelmap_sampler
= pixelmap_sampler
;
222 ctx
.texcoord_const
= texcoord_const
;
223 tgsi_scan_shader(tokens
, &ctx
.info
);
225 newlen
= tgsi_num_tokens(tokens
) + 30;
226 newtoks
= tgsi_alloc_tokens(newlen
);
230 tgsi_transform_shader(tokens
, newtoks
, newlen
, &ctx
.base
);