| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "util/u_debug.h" |
| 35 |
#include "util/u_math.h" |
| 36 |
#include "tgsi_info.h" |
| 37 |
#include "tgsi_dynamic_indexing.h" |
| 38 |
#include "tgsi_transform.h" |
| 39 |
#include "tgsi_dump.h" |
| 40 |
#include "pipe/p_state.h" |
| 41 |
|
| 42 |
|
| 43 |
struct dIndexing_transform_context |
| 44 |
{ |
| 45 |
struct tgsi_transform_context base; |
| 46 |
unsigned orig_num_tmp; |
| 47 |
unsigned orig_num_imm; |
| 48 |
unsigned num_const_bufs; |
| 49 |
unsigned num_samplers; |
| 50 |
unsigned num_iterations; |
| 51 |
unsigned const_buf_range[PIPE_MAX_CONSTANT_BUFFERS]; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
static inline struct dIndexing_transform_context * |
| 56 |
dIndexing_transform_context(struct tgsi_transform_context *ctx) |
| 57 |
{ |
| 58 |
return (struct dIndexing_transform_context *) ctx; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static void |
| 66 |
dIndexing_decl(struct tgsi_transform_context *ctx, |
| 67 |
struct tgsi_full_declaration *decl) |
| 68 |
{ |
| 69 |
struct dIndexing_transform_context *dc = dIndexing_transform_context(ctx); |
| 70 |
|
| 71 |
if (decl->Declaration.File == TGSI_FILE_TEMPORARY) { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
dc->orig_num_tmp = decl->Range.Last; |
| 77 |
decl->Range.Last = decl->Range.Last + 3; |
| 78 |
} |
| 79 |
else if (decl->Declaration.File == TGSI_FILE_CONSTANT) { |
| 80 |
|
| 81 |
dc->const_buf_range[decl->Dim.Index2D] = decl->Range.Last; |
| 82 |
} |
| 83 |
ctx->emit_declaration(ctx, decl); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static void |
| 91 |
dIndexing_prolog(struct tgsi_transform_context *ctx) |
| 92 |
{ |
| 93 |
tgsi_transform_immediate_int_decl(ctx, 0, 1, 2, 3); |
| 94 |
tgsi_transform_immediate_int_decl(ctx, 4, 5, 6, 7); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static void |
| 105 |
remove_dynamic_indexes(struct tgsi_transform_context *ctx, |
| 106 |
struct tgsi_full_instruction *orig_inst, |
| 107 |
const struct tgsi_full_src_register *reg) |
| 108 |
{ |
| 109 |
struct dIndexing_transform_context *dc = dIndexing_transform_context(ctx); |
| 110 |
int i, j; |
| 111 |
int tmp_loopIdx = dc->orig_num_tmp + 1; |
| 112 |
int tmp_cond = dc->orig_num_tmp + 2; |
| 113 |
int tmp_arrayIdx = dc->orig_num_tmp + 3; |
| 114 |
int imm_index = dc->orig_num_imm; |
| 115 |
struct tgsi_full_instruction inst; |
| 116 |
unsigned INVALID_INDEX = 99999; |
| 117 |
enum tgsi_file_type file = TGSI_FILE_NULL; |
| 118 |
unsigned index = INVALID_INDEX; |
| 119 |
unsigned imm_swz_index = INVALID_INDEX; |
| 120 |
|
| 121 |
|
| 122 |
inst = tgsi_default_full_instruction(); |
| 123 |
inst.Instruction.Opcode = TGSI_OPCODE_UADD; |
| 124 |
inst.Instruction.NumDstRegs = 1; |
| 125 |
tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY, |
| 126 |
tmp_arrayIdx, TGSI_WRITEMASK_X); |
| 127 |
inst.Instruction.NumSrcRegs = 2; |
| 128 |
if (reg->Register.File == TGSI_FILE_CONSTANT) { |
| 129 |
file = reg->DimIndirect.File; |
| 130 |
index = reg->DimIndirect.Index; |
| 131 |
imm_swz_index = reg->Dimension.Index; |
| 132 |
} |
| 133 |
else if (reg->Register.File == TGSI_FILE_SAMPLER) { |
| 134 |
file = reg->Indirect.File; |
| 135 |
index = reg->Indirect.Index; |
| 136 |
imm_swz_index = reg->Register.Index; |
| 137 |
} |
| 138 |
tgsi_transform_src_reg(&inst.Src[0], file, |
| 139 |
index, TGSI_SWIZZLE_X, |
| 140 |
TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X); |
| 141 |
tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_IMMEDIATE, |
| 142 |
imm_index + (imm_swz_index / 4), |
| 143 |
imm_swz_index % 4, |
| 144 |
imm_swz_index % 4, |
| 145 |
imm_swz_index % 4, |
| 146 |
imm_swz_index % 4); |
| 147 |
ctx->emit_instruction(ctx, &inst); |
| 148 |
|
| 149 |
|
| 150 |
inst = tgsi_default_full_instruction(); |
| 151 |
inst.Instruction.Opcode = TGSI_OPCODE_MOV; |
| 152 |
inst.Instruction.NumDstRegs = 1; |
| 153 |
tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY, |
| 154 |
tmp_loopIdx, TGSI_WRITEMASK_X); |
| 155 |
inst.Instruction.NumSrcRegs = 1; |
| 156 |
tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_IMMEDIATE, |
| 157 |
imm_index, TGSI_SWIZZLE_X, |
| 158 |
TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, |
| 159 |
TGSI_SWIZZLE_X); |
| 160 |
ctx->emit_instruction(ctx, &inst); |
| 161 |
|
| 162 |
for (i = 0; i < dc->num_iterations; i++) { |
| 163 |
bool out_of_bound_index = false; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if ((reg->Register.File == TGSI_FILE_CONSTANT) && |
| 180 |
(!reg->Register.Indirect && |
| 181 |
(reg->Register.Index > dc->const_buf_range[i]))) { |
| 182 |
out_of_bound_index = true; |
| 183 |
} |
| 184 |
|
| 185 |
if (!out_of_bound_index) { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
inst = tgsi_default_full_instruction(); |
| 204 |
inst.Instruction.Opcode = TGSI_OPCODE_USEQ; |
| 205 |
inst.Instruction.NumDstRegs = 1; |
| 206 |
tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY, |
| 207 |
tmp_cond, TGSI_WRITEMASK_X); |
| 208 |
inst.Instruction.NumSrcRegs = 2; |
| 209 |
tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY, |
| 210 |
tmp_arrayIdx, TGSI_SWIZZLE_X, |
| 211 |
TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, |
| 212 |
TGSI_SWIZZLE_X); |
| 213 |
tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_TEMPORARY, |
| 214 |
tmp_loopIdx, TGSI_SWIZZLE_X, |
| 215 |
TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, |
| 216 |
TGSI_SWIZZLE_X); |
| 217 |
ctx->emit_instruction(ctx, &inst); |
| 218 |
|
| 219 |
inst = tgsi_default_full_instruction(); |
| 220 |
inst.Instruction.Opcode = TGSI_OPCODE_UIF; |
| 221 |
inst.Instruction.NumDstRegs = 0; |
| 222 |
inst.Instruction.NumSrcRegs = 1; |
| 223 |
tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY, |
| 224 |
tmp_cond, TGSI_SWIZZLE_X, |
| 225 |
TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, |
| 226 |
TGSI_SWIZZLE_X); |
| 227 |
ctx->emit_instruction(ctx, &inst); |
| 228 |
|
| 229 |
|
| 230 |
inst = *orig_inst; |
| 231 |
for (j = 0; j < inst.Instruction.NumSrcRegs; j++) { |
| 232 |
if (inst.Src[j].Dimension.Indirect && |
| 233 |
inst.Src[j].Register.File == TGSI_FILE_CONSTANT) { |
| 234 |
inst.Src[j].Register.Dimension = 1; |
| 235 |
inst.Src[j].Dimension.Index = i; |
| 236 |
inst.Src[j].Dimension.Indirect = 0; |
| 237 |
} |
| 238 |
else if (inst.Src[j].Register.Indirect && |
| 239 |
inst.Src[j].Register.File == TGSI_FILE_SAMPLER) { |
| 240 |
inst.Src[j].Register.Indirect = 0; |
| 241 |
inst.Src[j].Register.Index = i; |
| 242 |
} |
| 243 |
} |
| 244 |
ctx->emit_instruction(ctx, &inst); |
| 245 |
|
| 246 |
inst = tgsi_default_full_instruction(); |
| 247 |
inst.Instruction.Opcode = TGSI_OPCODE_ENDIF; |
| 248 |
inst.Instruction.NumDstRegs = 0; |
| 249 |
inst.Instruction.NumSrcRegs = 0; |
| 250 |
ctx->emit_instruction(ctx, &inst); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
inst = tgsi_default_full_instruction(); |
| 258 |
inst.Instruction.Opcode = TGSI_OPCODE_UADD; |
| 259 |
inst.Instruction.NumDstRegs = 1; |
| 260 |
tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY, |
| 261 |
tmp_loopIdx, TGSI_WRITEMASK_X); |
| 262 |
inst.Instruction.NumSrcRegs = 2; |
| 263 |
tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY, |
| 264 |
tmp_loopIdx, TGSI_SWIZZLE_X, |
| 265 |
TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X); |
| 266 |
tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_IMMEDIATE, imm_index, |
| 267 |
TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y, |
| 268 |
TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y); |
| 269 |
|
| 270 |
ctx->emit_instruction(ctx, &inst); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static void |
| 279 |
dIndexing_inst(struct tgsi_transform_context *ctx, |
| 280 |
struct tgsi_full_instruction *inst) |
| 281 |
{ |
| 282 |
int i; |
| 283 |
bool indexing = false; |
| 284 |
struct dIndexing_transform_context *dc = dIndexing_transform_context(ctx); |
| 285 |
|
| 286 |
for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { |
| 287 |
struct tgsi_full_src_register *src; |
| 288 |
src = &inst->Src[i]; |
| 289 |
|
| 290 |
if ((src->Dimension.Indirect && |
| 291 |
src->Register.File == TGSI_FILE_CONSTANT) || |
| 292 |
(src->Register.Indirect && |
| 293 |
src->Register.File == TGSI_FILE_SAMPLER)) { |
| 294 |
|
| 295 |
if (indexing) |
| 296 |
assert("More than one src has dynamic indexing"); |
| 297 |
|
| 298 |
if (src->Register.File == TGSI_FILE_CONSTANT) |
| 299 |
dc->num_iterations = dc->num_const_bufs; |
| 300 |
else |
| 301 |
dc->num_iterations = dc->num_samplers; |
| 302 |
|
| 303 |
remove_dynamic_indexes(ctx, inst, src); |
| 304 |
indexing = true; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (!indexing) { |
| 309 |
ctx->emit_instruction(ctx, inst); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
struct tgsi_token * |
| 328 |
tgsi_remove_dynamic_indexing(const struct tgsi_token *tokens_in, |
| 329 |
unsigned const_buffers_declared_bitmask, |
| 330 |
unsigned samplers_declared_bitmask, |
| 331 |
unsigned imm_count) |
| 332 |
{ |
| 333 |
struct dIndexing_transform_context transform; |
| 334 |
const unsigned num_new_tokens = 1000; |
| 335 |
const unsigned new_len = tgsi_num_tokens(tokens_in) + num_new_tokens; |
| 336 |
|
| 337 |
|
| 338 |
memset(&transform, 0, sizeof(transform)); |
| 339 |
transform.base.transform_declaration = dIndexing_decl; |
| 340 |
transform.base.transform_instruction = dIndexing_inst; |
| 341 |
transform.base.prolog = dIndexing_prolog; |
| 342 |
|
| 343 |
transform.orig_num_tmp = 0; |
| 344 |
transform.orig_num_imm = imm_count; |
| 345 |
|
| 346 |
transform.num_const_bufs = log2(const_buffers_declared_bitmask + 1); |
| 347 |
transform.num_samplers = log2(samplers_declared_bitmask + 1); |
| 348 |
transform.num_iterations = 0; |
| 349 |
|
| 350 |
return tgsi_transform_shader(tokens_in, new_len, &transform.base); |
| 351 |
} |
| 352 |
|
| 353 |
|