d3dx9: Add 'asin' preshader opcode.
[wine.git] / dlls / d3dx9_36 / preshader.c
blob428215e4b956cf5b699f085189c22c61a9854d38
1 /*
2 * Copyright 2016 Paul Gofman
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include "d3dx9_private.h"
24 #include <float.h>
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28 enum pres_ops
30 PRESHADER_OP_NOP,
31 PRESHADER_OP_MOV,
32 PRESHADER_OP_NEG,
33 PRESHADER_OP_RCP,
34 PRESHADER_OP_FRC,
35 PRESHADER_OP_EXP,
36 PRESHADER_OP_LOG,
37 PRESHADER_OP_RSQ,
38 PRESHADER_OP_SIN,
39 PRESHADER_OP_COS,
40 PRESHADER_OP_ASIN,
41 PRESHADER_OP_MIN,
42 PRESHADER_OP_MAX,
43 PRESHADER_OP_LT,
44 PRESHADER_OP_GE,
45 PRESHADER_OP_ADD,
46 PRESHADER_OP_MUL,
47 PRESHADER_OP_CMP,
48 PRESHADER_OP_DOT,
49 PRESHADER_OP_DOTSWIZ6,
50 PRESHADER_OP_DOTSWIZ8,
53 typedef double (*pres_op_func)(double *args, int n);
55 static double to_signed_nan(double v)
57 static const union
59 ULONG64 ulong64_value;
60 double double_value;
62 signed_nan =
64 0xfff8000000000000
67 return isnan(v) ? signed_nan.double_value : v;
70 static double pres_mov(double *args, int n) {return args[0];}
71 static double pres_add(double *args, int n) {return args[0] + args[1];}
72 static double pres_mul(double *args, int n) {return args[0] * args[1];}
73 static double pres_dot(double *args, int n)
75 int i;
76 double sum;
78 sum = 0.0;
79 for (i = 0; i < n; ++i)
80 sum += args[i] * args[i + n];
81 return sum;
84 static double pres_dotswiz6(double *args, int n)
86 return pres_dot(args, 3);
89 static double pres_dotswiz8(double *args, int n)
91 return pres_dot(args, 4);
94 static double pres_neg(double *args, int n) {return -args[0];}
95 static double pres_rcp(double *args, int n) {return 1.0 / args[0];}
96 static double pres_lt(double *args, int n) {return args[0] < args[1] ? 1.0 : 0.0;}
97 static double pres_ge(double *args, int n) {return args[0] >= args[1] ? 1.0 : 0.0;}
98 static double pres_frc(double *args, int n) {return args[0] - floor(args[0]);}
99 static double pres_min(double *args, int n) {return fmin(args[0], args[1]);}
100 static double pres_max(double *args, int n) {return fmax(args[0], args[1]);}
101 static double pres_cmp(double *args, int n) {return args[0] < 0.0 ? args[2] : args[1];}
102 static double pres_sin(double *args, int n) {return sin(args[0]);}
103 static double pres_cos(double *args, int n) {return cos(args[0]);}
104 static double pres_rsq(double *args, int n)
106 double v;
108 v = fabs(args[0]);
109 if (v == 0.0)
110 return INFINITY;
111 else
112 return 1.0 / sqrt(v);
114 static double pres_exp(double *args, int n) {return pow(2.0, args[0]);}
115 static double pres_log(double *args, int n)
117 double v;
119 v = fabs(args[0]);
120 if (v == 0.0)
121 return 0.0;
122 else
123 #ifdef HAVE_LOG2
124 return log2(v);
125 #else
126 return log(v) / log(2);
127 #endif
129 static double pres_asin(double *args, int n) {return to_signed_nan(asin(args[0]));}
131 #define PRES_OPCODE_MASK 0x7ff00000
132 #define PRES_OPCODE_SHIFT 20
133 #define PRES_SCALAR_FLAG 0x80000000
134 #define PRES_NCOMP_MASK 0x0000ffff
136 #define FOURCC_PRES 0x53455250
137 #define FOURCC_CLIT 0x54494c43
138 #define FOURCC_FXLC 0x434c5846
139 #define FOURCC_PRSI 0x49535250
140 #define PRES_SIGN 0x46580000
142 struct op_info
144 unsigned int opcode;
145 char mnem[16];
146 unsigned int input_count;
147 BOOL func_all_comps;
148 pres_op_func func;
151 static const struct op_info pres_op_info[] =
153 {0x000, "nop", 0, 0, NULL }, /* PRESHADER_OP_NOP */
154 {0x100, "mov", 1, 0, pres_mov}, /* PRESHADER_OP_MOV */
155 {0x101, "neg", 1, 0, pres_neg}, /* PRESHADER_OP_NEG */
156 {0x103, "rcp", 1, 0, pres_rcp}, /* PRESHADER_OP_RCP */
157 {0x104, "frc", 1, 0, pres_frc}, /* PRESHADER_OP_FRC */
158 {0x105, "exp", 1, 0, pres_exp}, /* PRESHADER_OP_EXP */
159 {0x106, "log", 1, 0, pres_log}, /* PRESHADER_OP_LOG */
160 {0x107, "rsq", 1, 0, pres_rsq}, /* PRESHADER_OP_RSQ */
161 {0x108, "sin", 1, 0, pres_sin}, /* PRESHADER_OP_SIN */
162 {0x109, "cos", 1, 0, pres_cos}, /* PRESHADER_OP_COS */
163 {0x10a, "asin", 1, 0, pres_asin}, /* PRESHADER_OP_ASIN */
164 {0x200, "min", 2, 0, pres_min}, /* PRESHADER_OP_MIN */
165 {0x201, "max", 2, 0, pres_max}, /* PRESHADER_OP_MAX */
166 {0x202, "lt", 2, 0, pres_lt }, /* PRESHADER_OP_LT */
167 {0x203, "ge", 2, 0, pres_ge }, /* PRESHADER_OP_GE */
168 {0x204, "add", 2, 0, pres_add}, /* PRESHADER_OP_ADD */
169 {0x205, "mul", 2, 0, pres_mul}, /* PRESHADER_OP_MUL */
170 {0x300, "cmp", 3, 0, pres_cmp}, /* PRESHADER_OP_CMP */
171 {0x500, "dot", 2, 1, pres_dot}, /* PRESHADER_OP_DOT */
172 {0x70e, "d3ds_dotswiz", 6, 0, pres_dotswiz6}, /* PRESHADER_OP_DOTSWIZ6 */
173 {0x70e, "d3ds_dotswiz", 8, 0, pres_dotswiz8}, /* PRESHADER_OP_DOTSWIZ8 */
176 enum pres_value_type
178 PRES_VT_FLOAT,
179 PRES_VT_DOUBLE,
180 PRES_VT_INT,
181 PRES_VT_BOOL
184 static const struct
186 unsigned int component_size;
187 unsigned int reg_component_count;
188 enum pres_value_type type;
190 table_info[] =
192 {sizeof(double), 4, PRES_VT_DOUBLE}, /* PRES_REGTAB_IMMED */
193 {sizeof(float), 4, PRES_VT_FLOAT }, /* PRES_REGTAB_CONST */
194 {sizeof(float), 4, PRES_VT_FLOAT }, /* PRES_REGTAB_OCONST */
195 {sizeof(BOOL), 1, PRES_VT_BOOL }, /* PRES_REGTAB_OBCONST */
196 {sizeof(int), 4, PRES_VT_INT, }, /* PRES_REGTAB_OICONST */
197 /* TODO: use double precision for 64 bit */
198 {sizeof(float), 4, PRES_VT_FLOAT } /* PRES_REGTAB_TEMP */
201 static const char *table_symbol[] =
203 "imm", "c", "oc", "ob", "oi", "r", "(null)",
206 static const enum pres_reg_tables pres_regset2table[] =
208 PRES_REGTAB_OBCONST, /* D3DXRS_BOOL */
209 PRES_REGTAB_OICONST, /* D3DXRS_INT4 */
210 PRES_REGTAB_CONST, /* D3DXRS_FLOAT4 */
211 PRES_REGTAB_COUNT, /* D3DXRS_SAMPLER */
214 static const enum pres_reg_tables shad_regset2table[] =
216 PRES_REGTAB_OBCONST, /* D3DXRS_BOOL */
217 PRES_REGTAB_OICONST, /* D3DXRS_INT4 */
218 PRES_REGTAB_OCONST, /* D3DXRS_FLOAT4 */
219 PRES_REGTAB_COUNT, /* D3DXRS_SAMPLER */
222 struct d3dx_pres_reg
224 enum pres_reg_tables table;
225 /* offset is component index, not register index, e. g.
226 offset for component c3.y is 13 (3 * 4 + 1) */
227 unsigned int offset;
230 struct d3dx_pres_operand
232 struct d3dx_pres_reg reg;
233 struct d3dx_pres_reg index_reg;
236 #define MAX_INPUTS_COUNT 8
238 struct d3dx_pres_ins
240 enum pres_ops op;
241 /* first input argument is scalar,
242 scalar component is propagated */
243 BOOL scalar_op;
244 unsigned int component_count;
245 struct d3dx_pres_operand inputs[MAX_INPUTS_COUNT];
246 struct d3dx_pres_operand output;
249 static unsigned int get_reg_offset(unsigned int table, unsigned int offset)
251 return offset / table_info[table].reg_component_count;
254 #define PRES_BITMASK_BLOCK_SIZE (sizeof(unsigned int) * 8)
256 static HRESULT init_set_constants(struct d3dx_const_tab *const_tab, ID3DXConstantTable *ctab);
258 static HRESULT regstore_alloc_table(struct d3dx_regstore *rs, unsigned int table)
260 unsigned int size;
262 size = rs->table_sizes[table] * table_info[table].reg_component_count * table_info[table].component_size;
263 if (size)
265 rs->tables[table] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
266 rs->table_value_set[table] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
267 sizeof(*rs->table_value_set[table]) *
268 ((rs->table_sizes[table] + PRES_BITMASK_BLOCK_SIZE - 1) / PRES_BITMASK_BLOCK_SIZE));
269 if (!rs->tables[table] || !rs->table_value_set[table])
270 return E_OUTOFMEMORY;
272 return D3D_OK;
275 static void regstore_free_tables(struct d3dx_regstore *rs)
277 unsigned int i;
279 for (i = 0; i < PRES_REGTAB_COUNT; ++i)
281 HeapFree(GetProcessHeap(), 0, rs->tables[i]);
282 HeapFree(GetProcessHeap(), 0, rs->table_value_set[i]);
286 static void regstore_set_modified_reg(struct d3dx_regstore *rs, unsigned int table,
287 unsigned int start, unsigned int end)
289 unsigned int block_idx, start_block, end_block;
291 start_block = start / PRES_BITMASK_BLOCK_SIZE;
292 start -= start_block * PRES_BITMASK_BLOCK_SIZE;
293 end_block = end / PRES_BITMASK_BLOCK_SIZE;
294 end = (end_block + 1) * PRES_BITMASK_BLOCK_SIZE - 1 - end;
296 if (start_block == end_block)
298 rs->table_value_set[table][start_block] |= (~0u << start) & (~0u >> end);
300 else
302 rs->table_value_set[table][start_block] |= ~0u << start;
304 for (block_idx = start_block + 1; block_idx < end_block; ++block_idx)
305 rs->table_value_set[table][block_idx] = ~0u;
307 rs->table_value_set[table][end_block] |= ~0u >> end;
311 static void regstore_set_modified(struct d3dx_regstore *rs, unsigned int table,
312 unsigned int start_offset, unsigned int count)
314 if (!count)
315 return;
317 regstore_set_modified_reg(rs, table, get_reg_offset(table, start_offset),
318 get_reg_offset(table, start_offset + count - 1));
321 static void regstore_set_values(struct d3dx_regstore *rs, unsigned int table, void *data,
322 unsigned int start_offset, unsigned int count)
324 if (!count)
325 return;
327 memcpy((BYTE *)rs->tables[table] + start_offset * table_info[table].component_size,
328 data, count * table_info[table].component_size);
329 regstore_set_modified(rs, table, start_offset, count);
332 static unsigned int regstore_is_val_set_reg(struct d3dx_regstore *rs, unsigned int table, unsigned int reg_idx)
334 return rs->table_value_set[table][reg_idx / PRES_BITMASK_BLOCK_SIZE] &
335 (1u << (reg_idx % PRES_BITMASK_BLOCK_SIZE));
338 static double regstore_get_double(struct d3dx_regstore *rs, unsigned int table, unsigned int offset)
340 BYTE *p;
342 p = (BYTE *)rs->tables[table] + table_info[table].component_size * offset;
343 switch (table_info[table].type)
345 case PRES_VT_FLOAT:
346 return *(float *)p;
347 case PRES_VT_DOUBLE:
348 return *(double *)p;
349 default:
350 FIXME("Unexpected preshader input from table %u.\n", table);
351 return NAN;
355 static void regstore_set_double(struct d3dx_regstore *rs, unsigned int table, unsigned int offset, double v)
357 BYTE *p;
358 unsigned int reg_idx;
360 p = (BYTE *)rs->tables[table] + table_info[table].component_size * offset;
361 switch (table_info[table].type)
363 case PRES_VT_FLOAT : *(float *)p = v; break;
364 case PRES_VT_DOUBLE: *(double *)p = v; break;
365 case PRES_VT_INT : *(int *)p = lrint(v); break;
366 case PRES_VT_BOOL : *(BOOL *)p = !!v; break;
368 reg_idx = get_reg_offset(table, offset);
369 rs->table_value_set[table][reg_idx / PRES_BITMASK_BLOCK_SIZE] |=
370 1u << (reg_idx % PRES_BITMASK_BLOCK_SIZE);
373 static void regstore_reset_modified(struct d3dx_regstore *rs, unsigned int table)
375 memset(rs->table_value_set[table], 0,
376 sizeof(*rs->table_value_set[table]) *
377 ((rs->table_sizes[table] + PRES_BITMASK_BLOCK_SIZE - 1) / PRES_BITMASK_BLOCK_SIZE));
380 static void dump_bytecode(void *data, unsigned int size)
382 unsigned int *bytecode = (unsigned int *)data;
383 unsigned int i, j, n;
385 size /= sizeof(*bytecode);
386 i = 0;
387 while (i < size)
389 n = min(size - i, 8);
390 for (j = 0; j < n; ++j)
391 TRACE("0x%08x,", bytecode[i + j]);
392 i += n;
393 TRACE("\n");
397 static unsigned int *find_bytecode_comment(unsigned int *ptr, unsigned int count,
398 unsigned int fourcc, unsigned int *size)
400 /* Provide at least one value in comment section on non-NULL return. */
401 while (count > 2 && (*ptr & 0xffff) == 0xfffe)
403 unsigned int section_size;
405 section_size = (*ptr >> 16);
406 if (!section_size || section_size + 1 > count)
407 break;
408 if (*(ptr + 1) == fourcc)
410 *size = section_size;
411 return ptr + 2;
413 count -= section_size + 1;
414 ptr += section_size + 1;
416 return NULL;
419 static unsigned int *parse_pres_reg(unsigned int *ptr, struct d3dx_pres_reg *reg)
421 static const enum pres_reg_tables reg_table[8] =
423 PRES_REGTAB_COUNT, PRES_REGTAB_IMMED, PRES_REGTAB_CONST, PRES_REGTAB_COUNT,
424 PRES_REGTAB_OCONST, PRES_REGTAB_OBCONST, PRES_REGTAB_OICONST, PRES_REGTAB_TEMP
427 if (*ptr >= ARRAY_SIZE(reg_table) || reg_table[*ptr] == PRES_REGTAB_COUNT)
429 FIXME("Unsupported register table %#x.\n", *ptr);
430 return NULL;
433 reg->table = reg_table[*ptr++];
434 reg->offset = *ptr++;
435 return ptr;
438 static unsigned int *parse_pres_arg(unsigned int *ptr, unsigned int count, struct d3dx_pres_operand *opr)
440 if (count < 3 || (*ptr && count < 5))
442 WARN("Byte code buffer ends unexpectedly, count %u.\n", count);
443 return NULL;
446 if (*ptr)
448 if (*ptr != 1)
450 FIXME("Unknown relative addressing flag, word %#x.\n", *ptr);
451 return NULL;
453 ptr = parse_pres_reg(ptr + 1, &opr->index_reg);
454 if (!ptr)
455 return NULL;
457 else
459 opr->index_reg.table = PRES_REGTAB_COUNT;
460 ++ptr;
463 ptr = parse_pres_reg(ptr, &opr->reg);
465 if (opr->reg.table == PRES_REGTAB_OBCONST)
466 opr->reg.offset /= 4;
467 return ptr;
470 static unsigned int *parse_pres_ins(unsigned int *ptr, unsigned int count, struct d3dx_pres_ins *ins)
472 unsigned int ins_code, ins_raw;
473 unsigned int input_count;
474 unsigned int i;
476 if (count < 2)
478 WARN("Byte code buffer ends unexpectedly.\n");
479 return NULL;
482 ins_raw = *ptr++;
483 ins_code = (ins_raw & PRES_OPCODE_MASK) >> PRES_OPCODE_SHIFT;
484 ins->component_count = ins_raw & PRES_NCOMP_MASK;
485 ins->scalar_op = !!(ins_raw & PRES_SCALAR_FLAG);
487 if (ins->component_count < 1 || ins->component_count > 4)
489 FIXME("Unsupported number of components %u.\n", ins->component_count);
490 return NULL;
492 input_count = *ptr++;
493 count -= 2;
494 for (i = 0; i < ARRAY_SIZE(pres_op_info); ++i)
495 if (ins_code == pres_op_info[i].opcode && input_count == pres_op_info[i].input_count)
496 break;
497 if (i == ARRAY_SIZE(pres_op_info))
499 FIXME("Unknown opcode %#x, input_count %u, raw %#x.\n", ins_code, input_count, ins_raw);
500 return NULL;
502 ins->op = i;
503 if (input_count > ARRAY_SIZE(ins->inputs))
505 FIXME("Actual input args count %u exceeds inputs array size, instruction %s.\n", input_count,
506 pres_op_info[i].mnem);
507 return NULL;
509 for (i = 0; i < input_count; ++i)
511 unsigned int *p;
513 p = parse_pres_arg(ptr, count, &ins->inputs[i]);
514 if (!p)
515 return NULL;
516 count -= p - ptr;
517 ptr = p;
519 ptr = parse_pres_arg(ptr, count, &ins->output);
520 if (ins->output.index_reg.table != PRES_REGTAB_COUNT)
522 FIXME("Relative addressing in output register not supported.\n");
523 return NULL;
526 return ptr;
529 static HRESULT get_ctab_constant_desc(ID3DXConstantTable *ctab, D3DXHANDLE hc, D3DXCONSTANT_DESC *desc)
531 D3DXCONSTANT_DESC buffer[2];
532 HRESULT hr;
533 unsigned int count;
535 count = ARRAY_SIZE(buffer);
536 if (FAILED(hr = ID3DXConstantTable_GetConstantDesc(ctab, hc, buffer, &count)))
538 FIXME("Could not get constant desc, hr %#x.\n", hr);
539 return hr;
541 else if (count != 1)
543 FIXME("Unexpected constant descriptors count %u.\n", count);
544 return D3DERR_INVALIDCALL;
546 *desc = buffer[0];
547 return D3D_OK;
550 static HRESULT get_constants_desc(unsigned int *byte_code, struct d3dx_const_tab *out, struct d3dx9_base_effect *base)
552 ID3DXConstantTable *ctab;
553 D3DXCONSTANT_DESC *cdesc;
554 struct d3dx_parameter **inputs_param;
555 D3DXCONSTANTTABLE_DESC desc;
556 HRESULT hr;
557 D3DXHANDLE hc;
558 unsigned int i;
560 out->inputs = cdesc = NULL;
561 out->inputs_param = NULL;
562 out->input_count = 0;
563 inputs_param = NULL;
564 hr = D3DXGetShaderConstantTable(byte_code, &ctab);
565 if (FAILED(hr) || !ctab)
567 TRACE("Could not get CTAB data, hr %#x.\n", hr);
568 /* returning OK, shaders and preshaders without CTAB are valid */
569 return D3D_OK;
571 if (FAILED(hr = ID3DXConstantTable_GetDesc(ctab, &desc)))
573 FIXME("Could not get CTAB desc, hr %#x.\n", hr);
574 goto err_out;
577 cdesc = HeapAlloc(GetProcessHeap(), 0, sizeof(*cdesc) * desc.Constants);
578 inputs_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*inputs_param) * desc.Constants);
579 if (!cdesc || !inputs_param)
581 hr = E_OUTOFMEMORY;
582 goto err_out;
585 for (i = 0; i < desc.Constants; ++i)
587 hc = ID3DXConstantTable_GetConstant(ctab, NULL, i);
588 if (!hc)
590 FIXME("Null constant handle.\n");
591 goto err_out;
593 if (FAILED(hr = get_ctab_constant_desc(ctab, hc, &cdesc[i])))
594 goto err_out;
595 inputs_param[i] = get_parameter_by_name(base, NULL, cdesc[i].Name);
596 if (cdesc[i].Class == D3DXPC_OBJECT)
597 TRACE("Object %s, parameter %p.\n", cdesc[i].Name, inputs_param[i]);
598 else if (!inputs_param[i])
599 WARN("Could not find parameter %s in effect.\n", cdesc[i].Name);
601 out->input_count = desc.Constants;
602 out->inputs = cdesc;
603 out->inputs_param = inputs_param;
604 hr = init_set_constants(out, ctab);
605 ID3DXConstantTable_Release(ctab);
606 return hr;
607 err_out:
608 HeapFree(GetProcessHeap(), 0, cdesc);
609 HeapFree(GetProcessHeap(), 0, inputs_param);
610 if (ctab)
611 ID3DXConstantTable_Release(ctab);
612 return hr;
615 static void update_table_size(unsigned int *table_sizes, unsigned int table, unsigned int max_register)
617 if (table < PRES_REGTAB_COUNT)
618 table_sizes[table] = max(table_sizes[table], max_register + 1);
621 static void update_table_sizes_consts(unsigned int *table_sizes, struct d3dx_const_tab *ctab)
623 unsigned int i, table, max_register;
625 for (i = 0; i < ctab->input_count; ++i)
627 if (!ctab->inputs[i].RegisterCount)
628 continue;
629 max_register = ctab->inputs[i].RegisterIndex + ctab->inputs[i].RegisterCount - 1;
630 table = ctab->regset2table[ctab->inputs[i].RegisterSet];
631 update_table_size(table_sizes, table, max_register);
635 static void dump_arg(struct d3dx_regstore *rs, const struct d3dx_pres_operand *arg, int component_count)
637 static const char *xyzw_str = "xyzw";
638 unsigned int i, table;
640 table = arg->reg.table;
641 if (table == PRES_REGTAB_IMMED && arg->index_reg.table == PRES_REGTAB_COUNT)
643 TRACE("(");
644 for (i = 0; i < component_count; ++i)
645 TRACE(i < component_count - 1 ? "%.16e, " : "%.16e",
646 ((double *)rs->tables[PRES_REGTAB_IMMED])[arg->reg.offset + i]);
647 TRACE(")");
649 else
651 if (arg->index_reg.table == PRES_REGTAB_COUNT)
653 TRACE("%s%u.", table_symbol[table], get_reg_offset(table, arg->reg.offset));
655 else
657 unsigned int index_reg;
659 index_reg = get_reg_offset(arg->index_reg.table, arg->index_reg.offset);
660 TRACE("%s[%u + %s%u.%c].", table_symbol[table], get_reg_offset(table, arg->reg.offset),
661 table_symbol[arg->index_reg.table], index_reg,
662 xyzw_str[arg->index_reg.offset
663 - index_reg * table_info[arg->index_reg.table].reg_component_count]);
665 for (i = 0; i < component_count; ++i)
666 TRACE("%c", xyzw_str[(arg->reg.offset + i) % 4]);
670 static void dump_registers(struct d3dx_const_tab *ctab)
672 unsigned int table, i;
674 for (i = 0; i < ctab->input_count; ++i)
676 table = ctab->regset2table[ctab->inputs[i].RegisterSet];
677 TRACE("// %-12s %s%-4u %u\n", ctab->inputs_param[i] ? ctab->inputs_param[i]->name : "(nil)",
678 table_symbol[table], ctab->inputs[i].RegisterIndex, ctab->inputs[i].RegisterCount);
682 static void dump_ins(struct d3dx_regstore *rs, const struct d3dx_pres_ins *ins)
684 unsigned int i;
686 TRACE("%s ", pres_op_info[ins->op].mnem);
687 dump_arg(rs, &ins->output, pres_op_info[ins->op].func_all_comps ? 1 : ins->component_count);
688 for (i = 0; i < pres_op_info[ins->op].input_count; ++i)
690 TRACE(", ");
691 dump_arg(rs, &ins->inputs[i], ins->scalar_op && !i ? 1 : ins->component_count);
693 TRACE("\n");
696 static void dump_preshader(struct d3dx_preshader *pres)
698 unsigned int i, immediate_count = pres->regs.table_sizes[PRES_REGTAB_IMMED] * 4;
699 const double *immediates = pres->regs.tables[PRES_REGTAB_IMMED];
701 if (immediate_count)
702 TRACE("// Immediates:\n");
703 for (i = 0; i < immediate_count; ++i)
705 if (!(i % 4))
706 TRACE("// ");
707 TRACE("%.8e", immediates[i]);
708 if (i % 4 == 3)
709 TRACE("\n");
710 else
711 TRACE(", ");
713 TRACE("// Preshader registers:\n");
714 dump_registers(&pres->inputs);
715 TRACE("preshader\n");
716 for (i = 0; i < pres->ins_count; ++i)
717 dump_ins(&pres->regs, &pres->ins[i]);
720 static HRESULT parse_preshader(struct d3dx_preshader *pres, unsigned int *ptr, unsigned int count, struct d3dx9_base_effect *base)
722 unsigned int *p;
723 unsigned int i, j, const_count;
724 double *dconst;
725 HRESULT hr;
726 unsigned int saved_word;
727 unsigned int section_size;
729 TRACE("Preshader version %#x.\n", *ptr & 0xffff);
731 if (!count)
733 WARN("Unexpected end of byte code buffer.\n");
734 return D3DXERR_INVALIDDATA;
737 p = find_bytecode_comment(ptr + 1, count - 1, FOURCC_CLIT, &section_size);
738 if (p)
740 const_count = *p++;
741 if (const_count > (section_size - 1) / (sizeof(double) / sizeof(unsigned int)))
743 WARN("Byte code buffer ends unexpectedly.\n");
744 return D3DXERR_INVALIDDATA;
746 dconst = (double *)p;
748 else
750 const_count = 0;
751 dconst = NULL;
753 TRACE("%u double constants.\n", const_count);
755 p = find_bytecode_comment(ptr + 1, count - 1, FOURCC_FXLC, &section_size);
756 if (!p)
758 WARN("Could not find preshader code.\n");
759 return D3D_OK;
761 pres->ins_count = *p++;
762 --section_size;
763 if (pres->ins_count > UINT_MAX / sizeof(*pres->ins))
765 WARN("Invalid instruction count %u.\n", pres->ins_count);
766 return D3DXERR_INVALIDDATA;
768 TRACE("%u instructions.\n", pres->ins_count);
769 pres->ins = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pres->ins) * pres->ins_count);
770 if (!pres->ins)
771 return E_OUTOFMEMORY;
772 for (i = 0; i < pres->ins_count; ++i)
774 unsigned int *ptr_next;
776 ptr_next = parse_pres_ins(p, section_size, &pres->ins[i]);
777 if (!ptr_next)
778 return D3DXERR_INVALIDDATA;
779 section_size -= ptr_next - p;
780 p = ptr_next;
783 pres->inputs.regset2table = pres_regset2table;
785 saved_word = *ptr;
786 *ptr = 0xfffe0000;
787 hr = get_constants_desc(ptr, &pres->inputs, base);
788 *ptr = saved_word;
789 if (FAILED(hr))
790 return hr;
792 if (const_count % table_info[PRES_REGTAB_IMMED].reg_component_count)
794 FIXME("const_count %u is not a multiple of %u.\n", const_count,
795 table_info[PRES_REGTAB_IMMED].reg_component_count);
796 return D3DXERR_INVALIDDATA;
798 pres->regs.table_sizes[PRES_REGTAB_IMMED] = const_count
799 / table_info[PRES_REGTAB_IMMED].reg_component_count;
801 update_table_sizes_consts(pres->regs.table_sizes, &pres->inputs);
802 for (i = 0; i < pres->ins_count; ++i)
804 for (j = 0; j < pres_op_info[pres->ins[i].op].input_count; ++j)
806 enum pres_reg_tables table;
807 unsigned int reg_idx;
809 if (pres->ins[i].inputs[j].index_reg.table == PRES_REGTAB_COUNT)
811 unsigned int last_component_index = pres->ins[i].scalar_op && !j ? 0
812 : pres->ins[i].component_count - 1;
814 table = pres->ins[i].inputs[j].reg.table;
815 reg_idx = get_reg_offset(table, pres->ins[i].inputs[j].reg.offset
816 + last_component_index);
818 else
820 table = pres->ins[i].inputs[j].index_reg.table;
821 reg_idx = get_reg_offset(table, pres->ins[i].inputs[j].index_reg.offset);
823 if (reg_idx >= pres->regs.table_sizes[table])
825 FIXME("Out of bounds register index, i %u, j %u, table %u, reg_idx %u.",
826 i, j, table, reg_idx);
827 return D3DXERR_INVALIDDATA;
830 update_table_size(pres->regs.table_sizes, pres->ins[i].output.reg.table,
831 get_reg_offset(pres->ins[i].output.reg.table,
832 pres->ins[i].output.reg.offset + pres->ins[i].component_count - 1));
834 if (FAILED(regstore_alloc_table(&pres->regs, PRES_REGTAB_IMMED)))
835 return E_OUTOFMEMORY;
836 regstore_set_values(&pres->regs, PRES_REGTAB_IMMED, dconst, 0, const_count);
838 return D3D_OK;
841 void d3dx_create_param_eval(struct d3dx9_base_effect *base_effect, void *byte_code, unsigned int byte_code_size,
842 D3DXPARAMETER_TYPE type, struct d3dx_param_eval **peval_out, ULONG64 *version_counter)
844 struct d3dx_param_eval *peval;
845 unsigned int *ptr;
846 HRESULT hr;
847 unsigned int i;
848 BOOL shader;
849 unsigned int count, pres_size;
851 TRACE("base_effect %p, byte_code %p, byte_code_size %u, type %u, peval_out %p.\n",
852 base_effect, byte_code, byte_code_size, type, peval_out);
854 count = byte_code_size / sizeof(unsigned int);
855 if (!byte_code || !count)
857 *peval_out = NULL;
858 return;
861 peval = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*peval));
862 if (!peval)
863 goto err_out;
865 peval->version_counter = version_counter;
867 peval->param_type = type;
868 switch (type)
870 case D3DXPT_VERTEXSHADER:
871 case D3DXPT_PIXELSHADER:
872 shader = TRUE;
873 break;
874 default:
875 shader = FALSE;
876 break;
878 peval->shader_inputs.regset2table = shad_regset2table;
880 ptr = (unsigned int *)byte_code;
881 if (shader)
883 if ((*ptr & 0xfffe0000) != 0xfffe0000)
885 FIXME("Invalid shader signature %#x.\n", *ptr);
886 goto err_out;
888 TRACE("Shader version %#x.\n", *ptr & 0xffff);
890 if (FAILED(hr = get_constants_desc(ptr, &peval->shader_inputs, base_effect)))
892 FIXME("Could not get shader constant table, hr %#x.\n", hr);
893 goto err_out;
895 update_table_sizes_consts(peval->pres.regs.table_sizes, &peval->shader_inputs);
896 ptr = find_bytecode_comment(ptr + 1, count - 1, FOURCC_PRES, &pres_size);
897 if (!ptr)
898 TRACE("No preshader found.\n");
900 else
902 pres_size = count;
905 if (ptr && FAILED(parse_preshader(&peval->pres, ptr, pres_size, base_effect)))
907 FIXME("Failed parsing preshader, byte code for analysis follows.\n");
908 dump_bytecode(byte_code, byte_code_size);
909 goto err_out;
912 for (i = PRES_REGTAB_FIRST_SHADER; i < PRES_REGTAB_COUNT; ++i)
914 if (FAILED(regstore_alloc_table(&peval->pres.regs, i)))
915 goto err_out;
918 if (TRACE_ON(d3dx))
920 dump_bytecode(byte_code, byte_code_size);
921 dump_preshader(&peval->pres);
922 if (shader)
924 TRACE("// Shader registers:\n");
925 dump_registers(&peval->shader_inputs);
928 *peval_out = peval;
929 TRACE("Created parameter evaluator %p.\n", *peval_out);
930 return;
932 err_out:
933 FIXME("Error creating parameter evaluator.\n");
934 d3dx_free_param_eval(peval);
935 *peval_out = NULL;
938 static void d3dx_free_const_tab(struct d3dx_const_tab *ctab)
940 HeapFree(GetProcessHeap(), 0, ctab->inputs);
941 HeapFree(GetProcessHeap(), 0, ctab->inputs_param);
942 HeapFree(GetProcessHeap(), 0, ctab->const_set);
945 static void d3dx_free_preshader(struct d3dx_preshader *pres)
947 HeapFree(GetProcessHeap(), 0, pres->ins);
949 regstore_free_tables(&pres->regs);
950 d3dx_free_const_tab(&pres->inputs);
953 void d3dx_free_param_eval(struct d3dx_param_eval *peval)
955 TRACE("peval %p.\n", peval);
957 if (!peval)
958 return;
960 d3dx_free_preshader(&peval->pres);
961 d3dx_free_const_tab(&peval->shader_inputs);
962 HeapFree(GetProcessHeap(), 0, peval);
965 static void set_constants(struct d3dx_regstore *rs, struct d3dx_const_tab *const_tab,
966 ULONG64 new_update_version)
968 unsigned int const_idx;
970 for (const_idx = 0; const_idx < const_tab->const_set_count; ++const_idx)
972 struct d3dx_const_param_eval_output *const_set = &const_tab->const_set[const_idx];
973 unsigned int table = const_set->table;
974 struct d3dx_parameter *param = const_set->param;
975 enum pres_value_type table_type = table_info[table].type;
976 unsigned int i, j, n, start_offset;
977 unsigned int minor, major, major_stride, param_offset;
978 BOOL transpose;
979 unsigned int count;
981 if (!is_param_dirty(param, const_tab->update_version))
982 continue;
984 transpose = (const_set->constant_class == D3DXPC_MATRIX_COLUMNS && param->class == D3DXPC_MATRIX_ROWS)
985 || (param->class == D3DXPC_MATRIX_COLUMNS && const_set->constant_class == D3DXPC_MATRIX_ROWS);
986 if (const_set->constant_class == D3DXPC_MATRIX_COLUMNS)
988 major = param->columns;
989 minor = param->rows;
991 else
993 major = param->rows;
994 minor = param->columns;
996 start_offset = const_set->register_index * table_info[table].reg_component_count;
997 major_stride = max(minor, table_info[table].reg_component_count);
998 n = min(major * major_stride,
999 const_set->register_count * table_info[table].reg_component_count + major_stride - 1) / major_stride;
1000 count = n * minor;
1001 if (((param->type == D3DXPT_FLOAT && table_type == PRES_VT_FLOAT)
1002 || (param->type == D3DXPT_INT && table_type == PRES_VT_INT)
1003 || (param->type == D3DXPT_BOOL && table_type == PRES_VT_BOOL))
1004 && !transpose && minor == major_stride
1005 && count == table_info[table].reg_component_count * const_set->register_count
1006 && count * sizeof(unsigned int) <= param->bytes)
1008 regstore_set_values(rs, table, param->data, start_offset, count);
1009 continue;
1012 for (i = 0; i < n; ++i)
1014 for (j = 0; j < minor; ++j)
1016 unsigned int out;
1017 unsigned int *in;
1018 unsigned int offset;
1020 offset = start_offset + i * major_stride + j;
1021 if (offset / table_info[table].reg_component_count >= rs->table_sizes[table])
1023 if (table_info[table].reg_component_count != 1)
1024 FIXME("Output offset exceeds table size, name %s, component %u.\n",
1025 debugstr_a(param->name), i);
1026 break;
1028 if (transpose)
1029 param_offset = i + j * major;
1030 else
1031 param_offset = i * minor + j;
1032 if (param_offset * sizeof(unsigned int) >= param->bytes)
1034 WARN("Parameter data is too short, name %s, component %u.\n", debugstr_a(param->name), i);
1035 break;
1038 in = (unsigned int *)param->data + param_offset;
1039 switch (table_type)
1041 case PRES_VT_FLOAT: set_number(&out, D3DXPT_FLOAT, in, param->type); break;
1042 case PRES_VT_INT: set_number(&out, D3DXPT_INT, in, param->type); break;
1043 case PRES_VT_BOOL: set_number(&out, D3DXPT_BOOL, in, param->type); break;
1044 default:
1045 FIXME("Unexpected type %#x.\n", table_info[table].type);
1046 break;
1048 regstore_set_values(rs, table, &out, offset, 1);
1052 const_tab->update_version = new_update_version;
1055 #define INITIAL_CONST_SET_SIZE 16
1057 static HRESULT append_const_set(struct d3dx_const_tab *const_tab, struct d3dx_const_param_eval_output *set)
1059 if (const_tab->const_set_count >= const_tab->const_set_size)
1061 unsigned int new_size;
1062 struct d3dx_const_param_eval_output *new_alloc;
1064 if (!const_tab->const_set_size)
1066 new_size = INITIAL_CONST_SET_SIZE;
1067 new_alloc = HeapAlloc(GetProcessHeap(), 0, sizeof(*const_tab->const_set) * new_size);
1068 if (!new_alloc)
1070 ERR("Out of memory.\n");
1071 return E_OUTOFMEMORY;
1074 else
1076 new_size = const_tab->const_set_size * 2;
1077 new_alloc = HeapReAlloc(GetProcessHeap(), 0, const_tab->const_set,
1078 sizeof(*const_tab->const_set) * new_size);
1079 if (!new_alloc)
1081 ERR("Out of memory.\n");
1082 return E_OUTOFMEMORY;
1085 const_tab->const_set = new_alloc;
1086 const_tab->const_set_size = new_size;
1088 const_tab->const_set[const_tab->const_set_count++] = *set;
1089 return D3D_OK;
1092 static HRESULT init_set_constants_param(struct d3dx_const_tab *const_tab, ID3DXConstantTable *ctab,
1093 D3DXHANDLE hc, struct d3dx_parameter *param)
1095 D3DXCONSTANT_DESC desc;
1096 unsigned int const_count, param_count, i;
1097 BOOL get_element;
1098 struct d3dx_const_param_eval_output const_set;
1099 HRESULT hr;
1101 if (FAILED(get_ctab_constant_desc(ctab, hc, &desc)))
1102 return D3DERR_INVALIDCALL;
1104 if (param->element_count)
1106 param_count = param->element_count;
1107 const_count = desc.Elements;
1108 get_element = TRUE;
1110 else
1112 if (desc.Elements > 1)
1114 FIXME("Unexpected number of constant elements %u.\n", desc.Elements);
1115 return D3DERR_INVALIDCALL;
1117 param_count = param->member_count;
1118 const_count = desc.StructMembers;
1119 get_element = FALSE;
1121 if (const_count != param_count)
1123 FIXME("Number of elements or struct members differs between parameter (%u) and constant (%u).\n",
1124 param_count, const_count);
1125 return D3DERR_INVALIDCALL;
1127 if (const_count)
1129 HRESULT ret;
1130 D3DXHANDLE hc_element;
1132 ret = D3D_OK;
1133 for (i = 0; i < const_count; ++i)
1135 if (get_element)
1136 hc_element = ID3DXConstantTable_GetConstantElement(ctab, hc, i);
1137 else
1138 hc_element = ID3DXConstantTable_GetConstant(ctab, hc, i);
1139 if (!hc_element)
1141 FIXME("Could not get constant.\n");
1142 hr = D3DERR_INVALIDCALL;
1144 else
1146 hr = init_set_constants_param(const_tab, ctab, hc_element, &param->members[i]);
1148 if (FAILED(hr))
1149 ret = hr;
1151 return ret;
1154 TRACE("Constant %s, rows %u, columns %u, class %u, bytes %u.\n",
1155 debugstr_a(desc.Name), desc.Rows, desc.Columns, desc.Class, desc.Bytes);
1156 TRACE("Parameter %s, rows %u, columns %u, class %u, flags %#x, bytes %u.\n",
1157 debugstr_a(param->name), param->rows, param->columns, param->class,
1158 param->flags, param->bytes);
1160 const_set.param = param;
1161 const_set.constant_class = desc.Class;
1162 if (desc.RegisterSet >= ARRAY_SIZE(shad_regset2table))
1164 FIXME("Unknown register set %u.\n", desc.RegisterSet);
1165 return D3DERR_INVALIDCALL;
1167 const_set.register_index = desc.RegisterIndex;
1168 const_set.table = const_tab->regset2table[desc.RegisterSet];
1169 if (const_set.table >= PRES_REGTAB_COUNT)
1171 ERR("Unexpected register set %u.\n", desc.RegisterSet);
1172 return D3DERR_INVALIDCALL;
1174 const_set.register_count = desc.RegisterCount;
1175 if (FAILED(hr = append_const_set(const_tab, &const_set)))
1176 return hr;
1178 return D3D_OK;
1181 static HRESULT init_set_constants(struct d3dx_const_tab *const_tab, ID3DXConstantTable *ctab)
1183 unsigned int i;
1184 HRESULT hr, ret;
1185 D3DXHANDLE hc;
1187 ret = D3D_OK;
1188 for (i = 0; i < const_tab->input_count; ++i)
1190 if (!const_tab->inputs_param[i] || const_tab->inputs_param[i]->class == D3DXPC_OBJECT)
1191 continue;
1192 hc = ID3DXConstantTable_GetConstant(ctab, NULL, i);
1193 if (hc)
1195 hr = init_set_constants_param(const_tab, ctab, hc, const_tab->inputs_param[i]);
1197 else
1199 FIXME("Could not get constant, index %u.\n", i);
1200 hr = D3DERR_INVALIDCALL;
1202 if (FAILED(hr))
1203 ret = hr;
1206 if (const_tab->const_set_count)
1208 const_tab->const_set = HeapReAlloc(GetProcessHeap(), 0, const_tab->const_set,
1209 sizeof(*const_tab->const_set) * const_tab->const_set_count);
1210 if (!const_tab->const_set)
1212 ERR("Out of memory.\n");
1213 return E_OUTOFMEMORY;
1215 const_tab->const_set_size = const_tab->const_set_count;
1217 return ret;
1220 static double exec_get_reg_value(struct d3dx_regstore *rs, enum pres_reg_tables table, unsigned int offset)
1222 if (!regstore_is_val_set_reg(rs, table, offset / table_info[table].reg_component_count))
1223 WARN("Using uninitialized input, table %u, offset %u.\n", table, offset);
1225 return regstore_get_double(rs, table, offset);
1228 static double exec_get_arg(struct d3dx_regstore *rs, const struct d3dx_pres_operand *opr, unsigned int comp)
1230 unsigned int offset, base_index, reg_index, table;
1232 table = opr->reg.table;
1234 if (opr->index_reg.table == PRES_REGTAB_COUNT)
1235 base_index = 0;
1236 else
1237 base_index = lrint(exec_get_reg_value(rs, opr->index_reg.table, opr->index_reg.offset));
1239 offset = base_index * table_info[table].reg_component_count + opr->reg.offset + comp;
1240 reg_index = offset / table_info[table].reg_component_count;
1242 if (reg_index >= rs->table_sizes[table])
1244 unsigned int wrap_size;
1246 if (table == PRES_REGTAB_CONST)
1248 /* As it can be guessed from tests, offset into floating constant table is wrapped
1249 * to the nearest power of 2 and not to the actual table size. */
1250 for (wrap_size = 1; wrap_size < rs->table_sizes[table]; wrap_size <<= 1)
1253 else
1255 wrap_size = rs->table_sizes[table];
1257 WARN("Wrapping register index %u, table %u, wrap_size %u, table size %u.\n",
1258 reg_index, table, wrap_size, rs->table_sizes[table]);
1259 reg_index %= wrap_size;
1261 if (reg_index >= rs->table_sizes[table])
1262 return 0.0;
1264 offset = reg_index * table_info[table].reg_component_count
1265 + offset % table_info[table].reg_component_count;
1268 return exec_get_reg_value(rs, table, offset);
1271 static void exec_set_arg(struct d3dx_regstore *rs, const struct d3dx_pres_reg *reg,
1272 unsigned int comp, double res)
1274 regstore_set_double(rs, reg->table, reg->offset + comp, res);
1277 #define ARGS_ARRAY_SIZE 8
1278 static HRESULT execute_preshader(struct d3dx_preshader *pres)
1280 unsigned int i, j, k;
1281 double args[ARGS_ARRAY_SIZE];
1282 double res;
1284 for (i = 0; i < pres->ins_count; ++i)
1286 const struct d3dx_pres_ins *ins;
1287 const struct op_info *oi;
1289 ins = &pres->ins[i];
1290 oi = &pres_op_info[ins->op];
1291 if (oi->func_all_comps)
1293 if (oi->input_count * ins->component_count > ARGS_ARRAY_SIZE)
1295 FIXME("Too many arguments (%u) for one instruction.\n", oi->input_count * ins->component_count);
1296 return E_FAIL;
1298 for (k = 0; k < oi->input_count; ++k)
1299 for (j = 0; j < ins->component_count; ++j)
1300 args[k * ins->component_count + j] = exec_get_arg(&pres->regs, &ins->inputs[k],
1301 ins->scalar_op && !k ? 0 : j);
1302 res = oi->func(args, ins->component_count);
1304 /* only 'dot' instruction currently falls here */
1305 exec_set_arg(&pres->regs, &ins->output.reg, 0, res);
1307 else
1309 for (j = 0; j < ins->component_count; ++j)
1311 for (k = 0; k < oi->input_count; ++k)
1312 args[k] = exec_get_arg(&pres->regs, &ins->inputs[k], ins->scalar_op && !k ? 0 : j);
1313 res = oi->func(args, ins->component_count);
1314 exec_set_arg(&pres->regs, &ins->output.reg, j, res);
1318 return D3D_OK;
1321 static void set_preshader_modified(struct d3dx_preshader *pres)
1323 unsigned int i;
1325 for (i = 0; i < pres->ins_count; ++i)
1327 const struct d3dx_pres_ins *ins = &pres->ins[i];
1328 const struct d3dx_pres_reg *reg = &ins->output.reg;
1330 if (reg->table == PRES_REGTAB_TEMP)
1331 continue;
1333 regstore_set_modified(&pres->regs, reg->table, reg->offset,
1334 pres_op_info[ins->op].func_all_comps ? 1 : ins->component_count);
1338 static BOOL is_const_tab_input_dirty(struct d3dx_const_tab *ctab, ULONG64 update_version)
1340 unsigned int i;
1342 if (update_version == ULONG64_MAX)
1343 update_version = ctab->update_version;
1344 for (i = 0; i < ctab->input_count; ++i)
1346 if (ctab->inputs_param[i]
1347 && is_param_dirty(ctab->inputs_param[i], update_version))
1348 return TRUE;
1350 return FALSE;
1353 BOOL is_param_eval_input_dirty(struct d3dx_param_eval *peval, ULONG64 update_version)
1355 return is_const_tab_input_dirty(&peval->pres.inputs, update_version)
1356 || is_const_tab_input_dirty(&peval->shader_inputs, update_version);
1359 HRESULT d3dx_evaluate_parameter(struct d3dx_param_eval *peval, const struct d3dx_parameter *param,
1360 void *param_value)
1362 HRESULT hr;
1363 unsigned int i;
1364 unsigned int elements, elements_param, elements_table;
1365 float *oc;
1367 TRACE("peval %p, param %p, param_value %p.\n", peval, param, param_value);
1369 if (is_const_tab_input_dirty(&peval->pres.inputs, ULONG64_MAX))
1371 set_constants(&peval->pres.regs, &peval->pres.inputs,
1372 next_update_version(peval->version_counter));
1374 if (FAILED(hr = execute_preshader(&peval->pres)))
1375 return hr;
1378 elements_table = table_info[PRES_REGTAB_OCONST].reg_component_count
1379 * peval->pres.regs.table_sizes[PRES_REGTAB_OCONST];
1380 elements_param = param->bytes / sizeof(unsigned int);
1381 elements = min(elements_table, elements_param);
1382 oc = (float *)peval->pres.regs.tables[PRES_REGTAB_OCONST];
1383 for (i = 0; i < elements; ++i)
1384 set_number((unsigned int *)param_value + i, param->type, oc + i, D3DXPT_FLOAT);
1385 return D3D_OK;
1388 static HRESULT set_shader_constants_device(ID3DXEffectStateManager *manager, struct IDirect3DDevice9 *device,
1389 struct d3dx_regstore *rs, D3DXPARAMETER_TYPE type, enum pres_reg_tables table)
1391 unsigned int start, count;
1392 void *ptr;
1393 HRESULT hr, result;
1395 result = D3D_OK;
1396 start = 0;
1397 while (start < rs->table_sizes[table])
1399 count = 0;
1400 while (start < rs->table_sizes[table] && !regstore_is_val_set_reg(rs, table, start))
1401 ++start;
1402 while (start + count < rs->table_sizes[table] && regstore_is_val_set_reg(rs, table, start + count))
1403 ++count;
1404 if (!count)
1405 break;
1406 TRACE("Setting %u constants at %u.\n", count, start);
1407 ptr = (BYTE *)rs->tables[table] + start * table_info[table].reg_component_count
1408 * table_info[table].component_size;
1409 if (type == D3DXPT_VERTEXSHADER)
1411 switch(table)
1413 case PRES_REGTAB_OCONST:
1414 hr = SET_D3D_STATE_(manager, device, SetVertexShaderConstantF, start, (const float *)ptr, count);
1415 break;
1416 case PRES_REGTAB_OICONST:
1417 hr = SET_D3D_STATE_(manager, device, SetVertexShaderConstantI, start, (const int *)ptr, count);
1418 break;
1419 case PRES_REGTAB_OBCONST:
1420 hr = SET_D3D_STATE_(manager, device, SetVertexShaderConstantB, start, (const BOOL *)ptr, count);
1421 break;
1422 default:
1423 FIXME("Unexpected register table %u.\n", table);
1424 return D3DERR_INVALIDCALL;
1427 else if (type == D3DXPT_PIXELSHADER)
1429 switch(table)
1431 case PRES_REGTAB_OCONST:
1432 hr = SET_D3D_STATE_(manager, device, SetPixelShaderConstantF, start, (const float *)ptr, count);
1433 break;
1434 case PRES_REGTAB_OICONST:
1435 hr = SET_D3D_STATE_(manager, device, SetPixelShaderConstantI, start, (const int *)ptr, count);
1436 break;
1437 case PRES_REGTAB_OBCONST:
1438 hr = SET_D3D_STATE_(manager, device, SetPixelShaderConstantB, start, (const BOOL *)ptr, count);
1439 break;
1440 default:
1441 FIXME("Unexpected register table %u.\n", table);
1442 return D3DERR_INVALIDCALL;
1445 else
1447 FIXME("Unexpected parameter type %u.\n", type);
1448 return D3DERR_INVALIDCALL;
1451 if (FAILED(hr))
1453 ERR("Setting constants failed, type %u, table %u, hr %#x.\n", type, table, hr);
1454 result = hr;
1456 start += count;
1458 regstore_reset_modified(rs, table);
1459 return result;
1462 HRESULT d3dx_param_eval_set_shader_constants(ID3DXEffectStateManager *manager, struct IDirect3DDevice9 *device,
1463 struct d3dx_param_eval *peval, BOOL update_all)
1465 static const enum pres_reg_tables set_tables[] =
1466 {PRES_REGTAB_OCONST, PRES_REGTAB_OICONST, PRES_REGTAB_OBCONST};
1467 HRESULT hr, result;
1468 struct d3dx_preshader *pres = &peval->pres;
1469 struct d3dx_regstore *rs = &pres->regs;
1470 unsigned int i;
1471 ULONG64 new_update_version = next_update_version(peval->version_counter);
1472 BOOL update_device = update_all;
1474 TRACE("device %p, peval %p, param_type %u.\n", device, peval, peval->param_type);
1476 if (is_const_tab_input_dirty(&pres->inputs, ULONG64_MAX))
1478 set_constants(rs, &pres->inputs, new_update_version);
1479 if (FAILED(hr = execute_preshader(pres)))
1480 return hr;
1481 update_device = TRUE;
1484 if (is_const_tab_input_dirty(&peval->shader_inputs, ULONG64_MAX))
1486 set_constants(rs, &peval->shader_inputs, new_update_version);
1487 update_device = TRUE;
1489 result = D3D_OK;
1491 if (update_device)
1493 if (update_all)
1495 for (i = 0; i < peval->shader_inputs.input_count; ++i)
1497 unsigned int table;
1499 if (!peval->shader_inputs.inputs[i].RegisterCount)
1500 continue;
1501 table = peval->shader_inputs.regset2table[peval->shader_inputs.inputs[i].RegisterSet];
1502 if (table < PRES_REGTAB_COUNT)
1503 regstore_set_modified_reg(rs, table,
1504 peval->shader_inputs.inputs[i].RegisterIndex,
1505 peval->shader_inputs.inputs[i].RegisterIndex
1506 + peval->shader_inputs.inputs[i].RegisterCount - 1);
1508 set_preshader_modified(pres);
1511 for (i = 0; i < ARRAY_SIZE(set_tables); ++i)
1513 if (FAILED(hr = set_shader_constants_device(manager, device, rs, peval->param_type, set_tables[i])))
1514 result = hr;
1517 return result;