comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / d3dx9_36 / preshader.c
blob2287f15fb39c2f1baaa8faff5e059284ec58e2a2
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_ADD,
33 PRESHADER_OP_MUL,
34 PRESHADER_OP_DOT,
35 PRESHADER_OP_NEG,
36 PRESHADER_OP_RCP,
37 PRESHADER_OP_LT,
38 PRESHADER_OP_FRC,
39 PRESHADER_OP_MIN,
40 PRESHADER_OP_MAX,
41 PRESHADER_OP_GE,
42 PRESHADER_OP_CMP,
43 PRESHADER_OP_SIN,
44 PRESHADER_OP_COS,
45 PRESHADER_OP_RSQ,
48 typedef double (*pres_op_func)(double *args, int n);
50 static double pres_mov(double *args, int n) {return args[0];}
51 static double pres_add(double *args, int n) {return args[0] + args[1];}
52 static double pres_mul(double *args, int n) {return args[0] * args[1];}
53 static double pres_dot(double *args, int n)
55 int i;
56 double sum;
58 sum = 0.0;
59 for (i = 0; i < n; ++i)
60 sum += args[i] * args[i + n];
61 return sum;
63 static double pres_neg(double *args, int n) {return -args[0];}
64 static double pres_rcp(double *args, int n) {return 1.0 / args[0];}
65 static double pres_lt(double *args, int n) {return args[0] < args[1] ? 1.0 : 0.0;}
66 static double pres_ge(double *args, int n) {return args[0] >= args[1] ? 1.0 : 0.0;}
67 static double pres_frc(double *args, int n) {return args[0] - floor(args[0]);}
68 static double pres_min(double *args, int n) {return fmin(args[0], args[1]);}
69 static double pres_max(double *args, int n) {return fmax(args[0], args[1]);}
70 static double pres_cmp(double *args, int n) {return args[0] < 0.0 ? args[2] : args[1];}
71 static double pres_sin(double *args, int n) {return sin(args[0]);}
72 static double pres_cos(double *args, int n) {return cos(args[0]);}
73 static double pres_rsq(double *args, int n)
75 double v;
77 v = fabs(args[0]);
78 if (v == 0.0)
79 return INFINITY;
80 else
81 return 1.0 / sqrt(v);
84 #define PRES_OPCODE_MASK 0x7ff00000
85 #define PRES_OPCODE_SHIFT 20
86 #define PRES_SCALAR_FLAG 0x80000000
87 #define PRES_NCOMP_MASK 0x0000ffff
89 #define FOURCC_PRES 0x53455250
90 #define FOURCC_CLIT 0x54494c43
91 #define FOURCC_FXLC 0x434c5846
92 #define FOURCC_PRSI 0x49535250
93 #define PRES_SIGN 0x46580000
95 struct op_info
97 unsigned int opcode;
98 char mnem[8];
99 unsigned int input_count;
100 BOOL func_all_comps;
101 pres_op_func func;
104 static const struct op_info pres_op_info[] =
106 {0x000, "nop", 0, 0, NULL }, /* PRESHADER_OP_NOP */
107 {0x100, "mov", 1, 0, pres_mov}, /* PRESHADER_OP_MOV */
108 {0x204, "add", 2, 0, pres_add}, /* PRESHADER_OP_ADD */
109 {0x205, "mul", 2, 0, pres_mul}, /* PRESHADER_OP_MUL */
110 {0x500, "dot", 2, 1, pres_dot}, /* PRESHADER_OP_DOT */
111 {0x101, "neg", 1, 0, pres_neg}, /* PRESHADER_OP_NEG */
112 {0x103, "rcp", 1, 0, pres_rcp}, /* PRESHADER_OP_RCP */
113 {0x202, "lt", 2, 0, pres_lt }, /* PRESHADER_OP_LT */
114 {0x104, "frc", 1, 0, pres_frc}, /* PRESHADER_OP_FRC */
115 {0x200, "min", 2, 0, pres_min}, /* PRESHADER_OP_MIN */
116 {0x201, "max", 2, 0, pres_max}, /* PRESHADER_OP_MAX */
117 {0x203, "ge", 2, 0, pres_ge }, /* PRESHADER_OP_GE */
118 {0x300, "cmp", 3, 0, pres_cmp}, /* PRESHADER_OP_CMP */
119 {0x108, "sin", 1, 0, pres_sin}, /* PRESHADER_OP_SIN */
120 {0x109, "cos", 1, 0, pres_cos}, /* PRESHADER_OP_COS */
121 {0x107, "rsq", 1, 0, pres_rsq}, /* PRESHADER_OP_RSQ */
124 enum pres_value_type
126 PRES_VT_FLOAT,
127 PRES_VT_DOUBLE,
128 PRES_VT_INT,
129 PRES_VT_BOOL
132 static const struct
134 unsigned int component_size;
135 unsigned int reg_component_count;
136 enum pres_value_type type;
138 table_info[] =
140 {sizeof(double), 1, PRES_VT_DOUBLE}, /* PRES_REGTAB_IMMED */
141 {sizeof(float), 4, PRES_VT_FLOAT }, /* PRES_REGTAB_CONST */
142 {sizeof(float), 4, PRES_VT_FLOAT }, /* PRES_REGTAB_OCONST */
143 {sizeof(BOOL), 1, PRES_VT_BOOL }, /* PRES_REGTAB_OBCONST */
144 {sizeof(int), 4, PRES_VT_INT, }, /* PRES_REGTAB_OICONST */
145 /* TODO: use double precision for 64 bit */
146 {sizeof(float), 4, PRES_VT_FLOAT } /* PRES_REGTAB_TEMP */
149 static const char *table_symbol[] =
151 "imm", "c", "oc", "ob", "oi", "r", "(null)",
154 static const enum pres_reg_tables pres_regset2table[] =
156 PRES_REGTAB_OBCONST, /* D3DXRS_BOOL */
157 PRES_REGTAB_OICONST, /* D3DXRS_INT4 */
158 PRES_REGTAB_CONST, /* D3DXRS_FLOAT4 */
159 PRES_REGTAB_COUNT, /* D3DXRS_SAMPLER */
162 static const enum pres_reg_tables shad_regset2table[] =
164 PRES_REGTAB_OBCONST, /* D3DXRS_BOOL */
165 PRES_REGTAB_OICONST, /* D3DXRS_INT4 */
166 PRES_REGTAB_OCONST, /* D3DXRS_FLOAT4 */
167 PRES_REGTAB_COUNT, /* D3DXRS_SAMPLER */
170 struct d3dx_pres_operand
172 enum pres_reg_tables table;
173 /* offset is component index, not register index, e. g.
174 offset for component c3.y is 13 (3 * 4 + 1) */
175 unsigned int offset;
178 #define MAX_INPUTS_COUNT 3
180 struct d3dx_pres_ins
182 enum pres_ops op;
183 /* first input argument is scalar,
184 scalar component is propagated */
185 BOOL scalar_op;
186 unsigned int component_count;
187 struct d3dx_pres_operand inputs[MAX_INPUTS_COUNT];
188 struct d3dx_pres_operand output;
191 static unsigned int get_reg_offset(unsigned int table, unsigned int offset)
193 return offset / table_info[table].reg_component_count;
196 #define PRES_BITMASK_BLOCK_SIZE (sizeof(unsigned int) * 8)
198 static HRESULT regstore_alloc_table(struct d3dx_regstore *rs, unsigned int table)
200 unsigned int size;
202 size = rs->table_sizes[table] * table_info[table].reg_component_count * table_info[table].component_size;
203 if (size)
205 rs->tables[table] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
206 rs->table_value_set[table] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
207 sizeof(*rs->table_value_set[table]) *
208 ((rs->table_sizes[table] + PRES_BITMASK_BLOCK_SIZE - 1) / PRES_BITMASK_BLOCK_SIZE));
209 if (!rs->tables[table] || !rs->table_value_set[table])
210 return E_OUTOFMEMORY;
212 return D3D_OK;
215 static void regstore_free_tables(struct d3dx_regstore *rs)
217 unsigned int i;
219 for (i = 0; i < PRES_REGTAB_COUNT; ++i)
221 HeapFree(GetProcessHeap(), 0, rs->tables[i]);
222 HeapFree(GetProcessHeap(), 0, rs->table_value_set[i]);
226 static void regstore_set_values(struct d3dx_regstore *rs, unsigned int table, void *data,
227 unsigned int start_offset, unsigned int count)
229 unsigned int block_idx, start, end, start_block, end_block;
231 if (!count)
232 return;
234 memcpy((BYTE *)rs->tables[table] + start_offset * table_info[table].component_size,
235 data, count * table_info[table].component_size);
237 start = get_reg_offset(table, start_offset);
238 start_block = start / PRES_BITMASK_BLOCK_SIZE;
239 start -= start_block * PRES_BITMASK_BLOCK_SIZE;
240 end = get_reg_offset(table, start_offset + count - 1);
241 end_block = end / PRES_BITMASK_BLOCK_SIZE;
242 end = (end_block + 1) * PRES_BITMASK_BLOCK_SIZE - 1 - end;
244 if (start_block == end_block)
246 rs->table_value_set[table][start_block] |= (~0u << start) & (~0u >> end);
248 else
250 rs->table_value_set[table][start_block] |= ~0u << start;
252 for (block_idx = start_block + 1; block_idx < end_block; ++block_idx)
253 rs->table_value_set[table][block_idx] = ~0u;
255 rs->table_value_set[table][end_block] |= ~0u >> end;
259 static unsigned int regstore_is_val_set_reg(struct d3dx_regstore *rs, unsigned int table, unsigned int reg_idx)
261 return rs->table_value_set[table][reg_idx / PRES_BITMASK_BLOCK_SIZE] &
262 (1u << (reg_idx % PRES_BITMASK_BLOCK_SIZE));
265 static double regstore_get_double(struct d3dx_regstore *rs, unsigned int table, unsigned int offset)
267 BYTE *p;
269 p = (BYTE *)rs->tables[table] + table_info[table].component_size * offset;
270 switch (table_info[table].type)
272 case PRES_VT_FLOAT:
273 return *(float *)p;
274 case PRES_VT_DOUBLE:
275 return *(double *)p;
276 default:
277 FIXME("Unexpected preshader input from table %u.\n", table);
278 return NAN;
282 static void regstore_set_double(struct d3dx_regstore *rs, unsigned int table, unsigned int offset, double v)
284 BYTE *p;
285 unsigned int reg_idx;
287 p = (BYTE *)rs->tables[table] + table_info[table].component_size * offset;
288 switch (table_info[table].type)
290 case PRES_VT_FLOAT : *(float *)p = v; break;
291 case PRES_VT_DOUBLE: *(double *)p = v; break;
292 case PRES_VT_INT : *(int *)p = lrint(v); break;
293 case PRES_VT_BOOL : *(BOOL *)p = !!v; break;
295 reg_idx = get_reg_offset(table, offset);
296 rs->table_value_set[table][reg_idx / PRES_BITMASK_BLOCK_SIZE] |=
297 1u << (reg_idx % PRES_BITMASK_BLOCK_SIZE);
300 static void regstore_reset_table(struct d3dx_regstore *rs, unsigned int table)
302 unsigned int size;
304 size = rs->table_sizes[table] * table_info[table].reg_component_count * table_info[table].component_size;
306 memset(rs->tables[table], 0, size);
307 memset(rs->table_value_set[table], 0,
308 sizeof(*rs->table_value_set[table]) *
309 ((rs->table_sizes[table] + PRES_BITMASK_BLOCK_SIZE - 1) / PRES_BITMASK_BLOCK_SIZE));
312 static void dump_bytecode(void *data, unsigned int size)
314 unsigned int *bytecode = (unsigned int *)data;
315 unsigned int i, j, n;
317 size /= sizeof(*bytecode);
318 i = 0;
319 while (i < size)
321 n = min(size - i, 8);
322 for (j = 0; j < n; ++j)
323 TRACE("0x%08x,", bytecode[i + j]);
324 i += n;
325 TRACE("\n");
329 static unsigned int *find_bytecode_comment(unsigned int *ptr, unsigned int count,
330 unsigned int fourcc, unsigned int *size)
332 /* Provide at least one value in comment section on non-NULL return. */
333 while (count > 2 && (*ptr & 0xffff) == 0xfffe)
335 unsigned int section_size;
337 section_size = (*ptr >> 16);
338 if (!section_size || section_size + 1 > count)
339 break;
340 if (*(ptr + 1) == fourcc)
342 *size = section_size;
343 return ptr + 2;
345 count -= section_size + 1;
346 ptr += section_size + 1;
348 return NULL;
351 static unsigned int *parse_pres_arg(unsigned int *ptr, unsigned int count, struct d3dx_pres_operand *opr)
353 static const enum pres_reg_tables reg_table[8] =
355 PRES_REGTAB_COUNT, PRES_REGTAB_IMMED, PRES_REGTAB_CONST, PRES_REGTAB_COUNT,
356 PRES_REGTAB_OCONST, PRES_REGTAB_OBCONST, PRES_REGTAB_OICONST, PRES_REGTAB_TEMP
359 if (count < 3)
361 WARN("Byte code buffer ends unexpectedly.\n");
362 return NULL;
365 if (*ptr)
367 FIXME("Relative addressing not supported yet, word %#x.\n", *ptr);
368 return NULL;
370 ++ptr;
372 if (*ptr >= ARRAY_SIZE(reg_table) || reg_table[*ptr] == PRES_REGTAB_COUNT)
374 FIXME("Unsupported register table %#x.\n", *ptr);
375 return NULL;
377 opr->table = reg_table[*ptr++];
378 opr->offset = *ptr++;
380 if (opr->table == PRES_REGTAB_OBCONST)
381 opr->offset /= 4;
382 return ptr;
385 static unsigned int *parse_pres_ins(unsigned int *ptr, unsigned int count, struct d3dx_pres_ins *ins)
387 unsigned int ins_code, ins_raw;
388 unsigned int input_count;
389 unsigned int i;
391 if (count < 2)
393 WARN("Byte code buffer ends unexpectedly.\n");
394 return NULL;
397 ins_raw = *ptr++;
398 ins_code = (ins_raw & PRES_OPCODE_MASK) >> PRES_OPCODE_SHIFT;
399 ins->component_count = ins_raw & PRES_NCOMP_MASK;
400 ins->scalar_op = !!(ins_raw & PRES_SCALAR_FLAG);
402 if (ins->component_count < 1 || ins->component_count > 4)
404 FIXME("Unsupported number of components %u.\n", ins->component_count);
405 return NULL;
407 input_count = *ptr++;
408 count -= 2;
409 for (i = 0; i < ARRAY_SIZE(pres_op_info); ++i)
410 if (ins_code == pres_op_info[i].opcode)
411 break;
412 if (i == ARRAY_SIZE(pres_op_info))
414 FIXME("Unknown opcode %#x, raw %#x.\n", ins_code, ins_raw);
415 return NULL;
417 ins->op = i;
418 if (input_count > ARRAY_SIZE(ins->inputs) || input_count != pres_op_info[i].input_count)
420 FIXME("Actual input args %u, expected %u, instruction %s.\n", input_count,
421 pres_op_info[i].input_count, pres_op_info[i].mnem);
422 return NULL;
424 for (i = 0; i < input_count; ++i)
426 unsigned int *p;
428 p = parse_pres_arg(ptr, count, &ins->inputs[i]);
429 if (!p)
430 return NULL;
431 count -= p - ptr;
432 ptr = p;
434 ptr = parse_pres_arg(ptr, count, &ins->output);
435 return ptr;
438 static HRESULT get_ctab_constant_desc(ID3DXConstantTable *ctab, D3DXHANDLE hc, D3DXCONSTANT_DESC *desc)
440 D3DXCONSTANT_DESC buffer[2];
441 HRESULT hr;
442 unsigned int count;
444 count = ARRAY_SIZE(buffer);
445 if (FAILED(hr = ID3DXConstantTable_GetConstantDesc(ctab, hc, buffer, &count)))
447 FIXME("Could not get constant desc, hr %#x.\n", hr);
448 return hr;
450 else if (count != 1)
452 FIXME("Unexpected constant descriptors count %u.\n", count);
453 return D3DERR_INVALIDCALL;
455 *desc = buffer[0];
456 return D3D_OK;
459 static HRESULT get_constants_desc(unsigned int *byte_code, struct d3dx_const_tab *out, struct d3dx9_base_effect *base)
461 ID3DXConstantTable *ctab;
462 D3DXCONSTANT_DESC *cdesc;
463 struct d3dx_parameter **inputs_param;
464 D3DXCONSTANTTABLE_DESC desc;
465 HRESULT hr;
466 D3DXHANDLE hc;
467 unsigned int i;
469 out->inputs = cdesc = NULL;
470 out->ctab = NULL;
471 out->inputs_param = NULL;
472 out->input_count = 0;
473 inputs_param = NULL;
474 hr = D3DXGetShaderConstantTable(byte_code, &ctab);
475 if (FAILED(hr) || !ctab)
477 TRACE("Could not get CTAB data, hr %#x.\n", hr);
478 /* returning OK, shaders and preshaders without CTAB are valid */
479 return D3D_OK;
481 if (FAILED(hr = ID3DXConstantTable_GetDesc(ctab, &desc)))
483 FIXME("Could not get CTAB desc, hr %#x.\n", hr);
484 goto err_out;
487 cdesc = HeapAlloc(GetProcessHeap(), 0, sizeof(*cdesc) * desc.Constants);
488 inputs_param = HeapAlloc(GetProcessHeap(), 0, sizeof(*inputs_param) * desc.Constants);
489 if (!cdesc || !inputs_param)
491 hr = E_OUTOFMEMORY;
492 goto err_out;
495 for (i = 0; i < desc.Constants; ++i)
497 hc = ID3DXConstantTable_GetConstant(ctab, NULL, i);
498 if (!hc)
500 FIXME("Null constant handle.\n");
501 goto err_out;
503 if (FAILED(hr = get_ctab_constant_desc(ctab, hc, &cdesc[i])))
504 goto err_out;
505 inputs_param[i] = get_parameter_by_name(base, NULL, cdesc[i].Name);
506 if (cdesc[i].Class == D3DXPC_OBJECT)
507 TRACE("Object %s, parameter %p.\n", cdesc[i].Name, inputs_param[i]);
508 else if (!inputs_param[i])
509 ERR("Could not find parameter %s in effect.\n", cdesc[i].Name);
511 out->input_count = desc.Constants;
512 out->inputs = cdesc;
513 out->inputs_param = inputs_param;
514 out->ctab = ctab;
515 return D3D_OK;
517 err_out:
518 HeapFree(GetProcessHeap(), 0, cdesc);
519 HeapFree(GetProcessHeap(), 0, inputs_param);
520 if (ctab)
521 ID3DXConstantTable_Release(ctab);
522 return hr;
525 static void update_table_size(unsigned int *table_sizes, unsigned int table, unsigned int max_register)
527 if (table < PRES_REGTAB_COUNT)
528 table_sizes[table] = max(table_sizes[table], max_register + 1);
531 static void update_table_sizes_consts(unsigned int *table_sizes, struct d3dx_const_tab *ctab)
533 unsigned int i, table, max_register;
535 for (i = 0; i < ctab->input_count; ++i)
537 if (!ctab->inputs[i].RegisterCount)
538 continue;
539 max_register = ctab->inputs[i].RegisterIndex + ctab->inputs[i].RegisterCount - 1;
540 table = ctab->regset2table[ctab->inputs[i].RegisterSet];
541 update_table_size(table_sizes, table, max_register);
545 static void dump_arg(struct d3dx_regstore *rs, const struct d3dx_pres_operand *arg, int component_count)
547 static const char *xyzw_str = "xyzw";
548 unsigned int i, table;
550 table = arg->table;
551 if (table == PRES_REGTAB_IMMED)
553 TRACE("(");
554 for (i = 0; i < component_count; ++i)
555 TRACE(i < component_count - 1 ? "%.16e, " : "%.16e",
556 ((double *)rs->tables[PRES_REGTAB_IMMED])[arg->offset + i]);
557 TRACE(")");
559 else
561 TRACE("%s%u.", table_symbol[table], get_reg_offset(table, arg->offset));
562 for (i = 0; i < component_count; ++i)
563 TRACE("%c", xyzw_str[(arg->offset + i) % 4]);
567 static void dump_registers(struct d3dx_const_tab *ctab)
569 unsigned int table, i;
571 for (i = 0; i < ctab->input_count; ++i)
573 table = ctab->regset2table[ctab->inputs[i].RegisterSet];
574 TRACE("// %-12s %s%-4u %u\n", ctab->inputs_param[i] ? ctab->inputs_param[i]->name : "(nil)",
575 table_symbol[table], ctab->inputs[i].RegisterIndex, ctab->inputs[i].RegisterCount);
579 static void dump_ins(struct d3dx_regstore *rs, const struct d3dx_pres_ins *ins)
581 unsigned int i;
583 TRACE(" %s ", pres_op_info[ins->op].mnem);
584 dump_arg(rs, &ins->output, pres_op_info[ins->op].func_all_comps ? 1 : ins->component_count);
585 for (i = 0; i < pres_op_info[ins->op].input_count; ++i)
587 TRACE(", ");
588 dump_arg(rs, &ins->inputs[i], ins->scalar_op && !i ? 1 : ins->component_count);
590 TRACE("\n");
593 static void dump_preshader(struct d3dx_preshader *pres)
595 unsigned int i;
597 TRACE("// Preshader registers:\n");
598 dump_registers(&pres->inputs);
599 TRACE(" preshader\n");
600 for (i = 0; i < pres->ins_count; ++i)
601 dump_ins(&pres->regs, &pres->ins[i]);
604 static HRESULT parse_preshader(struct d3dx_preshader *pres, unsigned int *ptr, unsigned int count, struct d3dx9_base_effect *base)
606 unsigned int *p;
607 unsigned int i, j, const_count;
608 double *dconst;
609 HRESULT hr;
610 unsigned int saved_word;
611 unsigned int section_size;
613 TRACE("Preshader version %#x.\n", *ptr & 0xffff);
615 if (!count)
617 WARN("Unexpected end of byte code buffer.\n");
618 return D3DXERR_INVALIDDATA;
621 p = find_bytecode_comment(ptr + 1, count - 1, FOURCC_CLIT, &section_size);
622 if (p)
624 const_count = *p++;
625 if (const_count > (section_size - 1) / (sizeof(double) / sizeof(unsigned int)))
627 WARN("Byte code buffer ends unexpectedly.\n");
628 return D3DXERR_INVALIDDATA;
630 dconst = (double *)p;
632 else
634 const_count = 0;
635 dconst = NULL;
637 TRACE("%u double constants.\n", const_count);
639 p = find_bytecode_comment(ptr + 1, count - 1, FOURCC_FXLC, &section_size);
640 if (!p)
642 WARN("Could not find preshader code.\n");
643 return D3D_OK;
645 pres->ins_count = *p++;
646 --section_size;
647 if (pres->ins_count > UINT_MAX / sizeof(*pres->ins))
649 WARN("Invalid instruction count %u.\n", pres->ins_count);
650 return D3DXERR_INVALIDDATA;
652 TRACE("%u instructions.\n", pres->ins_count);
653 pres->ins = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pres->ins) * pres->ins_count);
654 if (!pres->ins)
655 return E_OUTOFMEMORY;
656 for (i = 0; i < pres->ins_count; ++i)
658 unsigned int *ptr_next;
660 ptr_next = parse_pres_ins(p, section_size, &pres->ins[i]);
661 if (!ptr_next)
662 return D3DXERR_INVALIDDATA;
663 section_size -= ptr_next - p;
664 p = ptr_next;
667 saved_word = *ptr;
668 *ptr = 0xfffe0000;
669 hr = get_constants_desc(ptr, &pres->inputs, base);
670 *ptr = saved_word;
671 if (FAILED(hr))
672 return hr;
674 pres->inputs.regset2table = pres_regset2table;
676 pres->regs.table_sizes[PRES_REGTAB_IMMED] = const_count;
678 for (i = 0; i < pres->ins_count; ++i)
680 for (j = 0; j < pres_op_info[pres->ins[i].op].input_count; ++j)
681 update_table_size(pres->regs.table_sizes, pres->ins[i].inputs[j].table,
682 get_reg_offset(pres->ins[i].inputs[j].table,
683 pres->ins[i].inputs[j].offset + pres->ins[i].component_count - 1));
685 update_table_size(pres->regs.table_sizes, pres->ins[i].output.table,
686 get_reg_offset(pres->ins[i].output.table,
687 pres->ins[i].output.offset + pres->ins[i].component_count - 1));
689 update_table_sizes_consts(pres->regs.table_sizes, &pres->inputs);
690 if (FAILED(regstore_alloc_table(&pres->regs, PRES_REGTAB_IMMED)))
691 return E_OUTOFMEMORY;
692 regstore_set_values(&pres->regs, PRES_REGTAB_IMMED, dconst, 0, const_count);
694 return D3D_OK;
697 void d3dx_create_param_eval(struct d3dx9_base_effect *base_effect, void *byte_code, unsigned int byte_code_size,
698 D3DXPARAMETER_TYPE type, struct d3dx_param_eval **peval_out)
700 struct d3dx_param_eval *peval;
701 unsigned int *ptr;
702 HRESULT hr;
703 unsigned int i;
704 BOOL shader;
705 unsigned int count, pres_size;
707 TRACE("base_effect %p, byte_code %p, byte_code_size %u, type %u, peval_out %p.\n",
708 base_effect, byte_code, byte_code_size, type, peval_out);
710 count = byte_code_size / sizeof(unsigned int);
711 if (!byte_code || !count)
713 *peval_out = NULL;
714 return;
717 peval = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*peval));
718 if (!peval)
719 goto err_out;
721 peval->param_type = type;
722 switch (type)
724 case D3DXPT_VERTEXSHADER:
725 case D3DXPT_PIXELSHADER:
726 shader = TRUE;
727 break;
728 default:
729 shader = FALSE;
730 break;
732 peval->shader_inputs.regset2table = shad_regset2table;
734 ptr = (unsigned int *)byte_code;
735 if (shader)
737 if ((*ptr & 0xfffe0000) != 0xfffe0000)
739 FIXME("Invalid shader signature %#x.\n", *ptr);
740 goto err_out;
742 TRACE("Shader version %#x.\n", *ptr & 0xffff);
744 if (FAILED(hr = get_constants_desc(ptr, &peval->shader_inputs, base_effect)))
746 FIXME("Could not get shader constant table, hr %#x.\n", hr);
747 goto err_out;
749 update_table_sizes_consts(peval->pres.regs.table_sizes, &peval->shader_inputs);
750 ptr = find_bytecode_comment(ptr + 1, count - 1, FOURCC_PRES, &pres_size);
751 if (!ptr)
752 TRACE("No preshader found.\n");
754 else
756 pres_size = count;
759 if (ptr && FAILED(parse_preshader(&peval->pres, ptr, pres_size, base_effect)))
761 FIXME("Failed parsing preshader, byte code for analysis follows.\n");
762 dump_bytecode(byte_code, byte_code_size);
763 goto err_out;
766 for (i = PRES_REGTAB_FIRST_SHADER; i < PRES_REGTAB_COUNT; ++i)
768 if (FAILED(regstore_alloc_table(&peval->pres.regs, i)))
769 goto err_out;
772 if (TRACE_ON(d3dx))
774 dump_bytecode(byte_code, byte_code_size);
775 dump_preshader(&peval->pres);
776 if (shader)
778 TRACE("// Shader registers:\n");
779 dump_registers(&peval->shader_inputs);
782 *peval_out = peval;
783 TRACE("Created parameter evaluator %p.\n", *peval_out);
784 return;
786 err_out:
787 FIXME("Error creating parameter evaluator.\n");
788 d3dx_free_param_eval(peval);
789 *peval_out = NULL;
792 static void d3dx_free_const_tab(struct d3dx_const_tab *ctab)
794 HeapFree(GetProcessHeap(), 0, ctab->inputs);
795 HeapFree(GetProcessHeap(), 0, ctab->inputs_param);
796 if (ctab->ctab)
797 ID3DXConstantTable_Release(ctab->ctab);
800 static void d3dx_free_preshader(struct d3dx_preshader *pres)
802 HeapFree(GetProcessHeap(), 0, pres->ins);
804 regstore_free_tables(&pres->regs);
805 d3dx_free_const_tab(&pres->inputs);
808 void d3dx_free_param_eval(struct d3dx_param_eval *peval)
810 TRACE("peval %p.\n", peval);
812 if (!peval)
813 return;
815 d3dx_free_preshader(&peval->pres);
816 d3dx_free_const_tab(&peval->shader_inputs);
817 HeapFree(GetProcessHeap(), 0, peval);
820 static HRESULT set_constants_param(struct d3dx_regstore *rs, struct d3dx_const_tab *const_tab,
821 D3DXHANDLE hc, struct d3dx_parameter *param)
823 ID3DXConstantTable *ctab = const_tab->ctab;
824 D3DXCONSTANT_DESC desc;
825 unsigned int const_count, param_count, i, j, n, table, start_offset;
826 unsigned int minor, major, major_stride, param_offset;
827 BOOL transpose, get_element;
829 if (FAILED(get_ctab_constant_desc(ctab, hc, &desc)))
830 return D3DERR_INVALIDCALL;
832 if (param->element_count)
834 param_count = param->element_count;
835 const_count = desc.Elements;
836 get_element = TRUE;
838 else
840 if (desc.Elements > 1)
842 FIXME("Unexpected number of constant elements %u.\n", desc.Elements);
843 return D3DERR_INVALIDCALL;
845 param_count = param->member_count;
846 const_count = desc.StructMembers;
847 get_element = FALSE;
849 if (const_count != param_count)
851 FIXME("Number of elements or struct members differs between parameter (%u) and constant (%u).\n",
852 param_count, const_count);
853 return D3DERR_INVALIDCALL;
855 if (const_count)
857 HRESULT hr, ret;
858 D3DXHANDLE hc_element;
860 ret = D3D_OK;
861 for (i = 0; i < const_count; ++i)
863 if (get_element)
864 hc_element = ID3DXConstantTable_GetConstantElement(ctab, hc, i);
865 else
866 hc_element = ID3DXConstantTable_GetConstant(ctab, hc, i);
867 if (!hc_element)
869 FIXME("Could not get constant.\n");
870 hr = D3DERR_INVALIDCALL;
872 else
874 hr = set_constants_param(rs, const_tab, hc_element, &param->members[i]);
876 if (FAILED(hr))
877 ret = hr;
879 return ret;
882 transpose = (desc.Class == D3DXPC_MATRIX_COLUMNS && param->class == D3DXPC_MATRIX_ROWS)
883 || (param->class == D3DXPC_MATRIX_COLUMNS && desc.Class == D3DXPC_MATRIX_ROWS);
884 if (desc.Class == D3DXPC_MATRIX_COLUMNS)
886 major = param->columns;
887 minor = param->rows;
889 else
891 major = param->rows;
892 minor = param->columns;
895 TRACE("Constant %s, rows %u, columns %u, class %u, bytes %u.\n",
896 debugstr_a(desc.Name), desc.Rows, desc.Columns, desc.Class, desc.Bytes);
897 TRACE("Parameter %s, rows %u, columns %u, class %u, flags %#x, bytes %u, transpose %#x.\n",
898 debugstr_a(param->name), param->rows, param->columns, param->class,
899 param->flags, param->bytes, transpose);
901 if (desc.RegisterSet >= ARRAY_SIZE(shad_regset2table))
903 FIXME("Unknown register set %u.\n", desc.RegisterSet);
904 return D3DERR_INVALIDCALL;
906 table = const_tab->regset2table[desc.RegisterSet];
907 if (table >= PRES_REGTAB_COUNT)
909 ERR("Unexpected register set %u.\n", desc.RegisterSet);
910 return D3DERR_INVALIDCALL;
912 start_offset = desc.RegisterIndex * table_info[table].reg_component_count;
913 major_stride = max(minor, table_info[table].reg_component_count);
914 n = min(major * major_stride,
915 desc.RegisterCount * table_info[table].reg_component_count + major_stride - 1) / major_stride;
916 for (i = 0; i < n; ++i)
918 for (j = 0; j < minor; ++j)
920 unsigned int out;
921 unsigned int *in;
922 unsigned int offset;
924 offset = start_offset + i * major_stride + j;
925 if (offset / table_info[table].reg_component_count >= rs->table_sizes[table])
927 if (table_info[table].reg_component_count != 1)
928 FIXME("Output offset exceeds table size, name %s, component %u.\n", desc.Name, i);
929 break;
931 if (transpose)
932 param_offset = i + j * major;
933 else
934 param_offset = i * minor + j;
935 if (param_offset * sizeof(unsigned int) >= param->bytes)
937 WARN("Parameter data is too short, name %s, component %u.\n", desc.Name, i);
938 break;
941 in = (unsigned int *)param->data + param_offset;
942 /* TODO: store data transfer / convert operation instead of performing an operation
943 from here, to move this to parsing stage */
944 switch (table_info[table].type)
946 case PRES_VT_FLOAT: set_number(&out, D3DXPT_FLOAT, in, param->type); break;
947 case PRES_VT_INT: set_number(&out, D3DXPT_INT, in, param->type); break;
948 case PRES_VT_BOOL: set_number(&out, D3DXPT_BOOL, in, param->type); break;
949 default:
950 FIXME("Unexpected type %#x.\n", table_info[table].type);
951 break;
953 regstore_set_values(rs, table, &out, offset, 1);
957 return D3D_OK;
960 static HRESULT set_constants(struct d3dx_regstore *rs, struct d3dx_const_tab *const_tab)
962 unsigned int i;
963 HRESULT hr, ret;
964 D3DXHANDLE hc;
966 ret = D3D_OK;
967 for (i = 0; i < const_tab->input_count; ++i)
969 if (!const_tab->inputs_param[i] || const_tab->inputs_param[i]->class == D3DXPC_OBJECT)
970 continue;
971 hc = ID3DXConstantTable_GetConstant(const_tab->ctab, NULL, i);
972 if (hc)
974 hr = set_constants_param(rs, const_tab, hc, const_tab->inputs_param[i]);
976 else
978 FIXME("Could not get constant, index %u.\n", i);
979 hr = D3DERR_INVALIDCALL;
981 if (FAILED(hr))
982 ret = hr;
984 return ret;
987 static double exec_get_arg(struct d3dx_regstore *rs, const struct d3dx_pres_ins *ins,
988 const struct d3dx_pres_operand *opr, unsigned int comp)
990 if (!regstore_is_val_set_reg(rs, opr->table, (opr->offset + comp) / table_info[opr->table].reg_component_count))
992 WARN("Using uninitialized input ");
993 dump_arg(rs, opr, comp);
994 TRACE(".\n");
995 dump_ins(rs, ins);
997 return regstore_get_double(rs, opr->table, opr->offset + comp);
1000 static void exec_set_arg(struct d3dx_regstore *rs, const struct d3dx_pres_operand *opr,
1001 unsigned int comp, double res)
1003 regstore_set_double(rs, opr->table, opr->offset + comp, res);
1006 #define ARGS_ARRAY_SIZE 8
1007 static HRESULT execute_preshader(struct d3dx_preshader *pres)
1009 unsigned int i, j, k;
1010 double args[ARGS_ARRAY_SIZE];
1011 double res;
1013 for (i = 0; i < pres->ins_count; ++i)
1015 const struct d3dx_pres_ins *ins;
1016 const struct op_info *oi;
1018 ins = &pres->ins[i];
1019 oi = &pres_op_info[ins->op];
1020 if (oi->func_all_comps)
1022 if (oi->input_count * ins->component_count > ARGS_ARRAY_SIZE)
1024 FIXME("Too many arguments (%u) for one instruction.\n", oi->input_count * ins->component_count);
1025 return E_FAIL;
1027 for (k = 0; k < oi->input_count; ++k)
1028 for (j = 0; j < ins->component_count; ++j)
1029 args[k * ins->component_count + j] = exec_get_arg(&pres->regs, ins, &ins->inputs[k],
1030 ins->scalar_op && !k ? 0 : j);
1031 res = oi->func(args, ins->component_count);
1033 /* only 'dot' instruction currently falls here */
1034 exec_set_arg(&pres->regs, &ins->output, 0, res);
1036 else
1038 for (j = 0; j < ins->component_count; ++j)
1040 for (k = 0; k < oi->input_count; ++k)
1041 args[k] = exec_get_arg(&pres->regs, ins, &ins->inputs[k], ins->scalar_op && !k ? 0 : j);
1042 res = oi->func(args, ins->component_count);
1043 exec_set_arg(&pres->regs, &ins->output, j, res);
1047 return D3D_OK;
1050 HRESULT d3dx_evaluate_parameter(struct d3dx_param_eval *peval, const struct d3dx_parameter *param, void *param_value)
1052 HRESULT hr;
1053 unsigned int i;
1054 unsigned int elements, elements_param, elements_table;
1055 float *oc;
1057 TRACE("peval %p, param %p, param_value %p.\n", peval, param, param_value);
1059 if (FAILED(hr = set_constants(&peval->pres.regs, &peval->pres.inputs)))
1060 return hr;
1062 if (FAILED(hr = execute_preshader(&peval->pres)))
1063 return hr;
1065 elements_table = table_info[PRES_REGTAB_OCONST].reg_component_count
1066 * peval->pres.regs.table_sizes[PRES_REGTAB_OCONST];
1067 elements_param = param->bytes / sizeof(unsigned int);
1068 elements = min(elements_table, elements_param);
1069 oc = (float *)peval->pres.regs.tables[PRES_REGTAB_OCONST];
1070 for (i = 0; i < elements; ++i)
1071 set_number((unsigned int *)param_value + i, param->type, oc + i, D3DXPT_FLOAT);
1072 return D3D_OK;
1075 static HRESULT set_shader_constants_device(struct IDirect3DDevice9 *device, struct d3dx_regstore *rs,
1076 D3DXPARAMETER_TYPE type, enum pres_reg_tables table)
1078 unsigned int start, count;
1079 void *ptr;
1080 HRESULT hr, result;
1082 result = D3D_OK;
1083 start = 0;
1084 while (start < rs->table_sizes[table])
1086 count = 0;
1087 while (start < rs->table_sizes[table] && !regstore_is_val_set_reg(rs, table, start))
1088 ++start;
1089 while (start + count < rs->table_sizes[table] && regstore_is_val_set_reg(rs, table, start + count))
1090 ++count;
1091 if (!count)
1092 break;
1093 TRACE("Setting %u constants at %u.\n", count, start);
1094 ptr = (BYTE *)rs->tables[table] + start * table_info[table].reg_component_count
1095 * table_info[table].component_size;
1096 if (type == D3DXPT_VERTEXSHADER)
1098 switch(table)
1100 case PRES_REGTAB_OCONST:
1101 hr = IDirect3DDevice9_SetVertexShaderConstantF(device, start, (const float *)ptr, count);
1102 break;
1103 case PRES_REGTAB_OICONST:
1104 hr = IDirect3DDevice9_SetVertexShaderConstantI(device, start, (const int *)ptr, count);
1105 break;
1106 case PRES_REGTAB_OBCONST:
1107 hr = IDirect3DDevice9_SetVertexShaderConstantB(device, start, (const BOOL *)ptr, count);
1108 break;
1109 default:
1110 FIXME("Unexpected register table %u.\n", table);
1111 return D3DERR_INVALIDCALL;
1114 else if (type == D3DXPT_PIXELSHADER)
1116 switch(table)
1118 case PRES_REGTAB_OCONST:
1119 hr = IDirect3DDevice9_SetPixelShaderConstantF(device, start, (const float *)ptr, count);
1120 break;
1121 case PRES_REGTAB_OICONST:
1122 hr = IDirect3DDevice9_SetPixelShaderConstantI(device, start, (const int *)ptr, count);
1123 break;
1124 case PRES_REGTAB_OBCONST:
1125 hr = IDirect3DDevice9_SetPixelShaderConstantB(device, start, (const BOOL *)ptr, count);
1126 break;
1127 default:
1128 FIXME("Unexpected register table %u.\n", table);
1129 return D3DERR_INVALIDCALL;
1132 else
1134 FIXME("Unexpected parameter type %u.\n", type);
1135 return D3DERR_INVALIDCALL;
1138 if (FAILED(hr))
1140 ERR("Setting constants failed, type %u, table %u, hr %#x.\n", type, table, hr);
1141 result = hr;
1143 start += count;
1145 regstore_reset_table(rs, table);
1146 return result;
1149 HRESULT d3dx_param_eval_set_shader_constants(struct IDirect3DDevice9 *device, struct d3dx_param_eval *peval)
1151 static const enum pres_reg_tables set_tables[] =
1152 {PRES_REGTAB_OCONST, PRES_REGTAB_OICONST, PRES_REGTAB_OBCONST};
1153 HRESULT hr, result;
1154 struct d3dx_preshader *pres = &peval->pres;
1155 struct d3dx_regstore *rs = &pres->regs;
1156 unsigned int i;
1158 TRACE("device %p, peval %p, param_type %u.\n", device, peval, peval->param_type);
1160 if (FAILED(hr = set_constants(rs, &pres->inputs)))
1161 return hr;
1162 if (FAILED(hr = execute_preshader(pres)))
1163 return hr;
1164 if (FAILED(hr = set_constants(rs, &peval->shader_inputs)))
1165 return hr;
1167 result = D3D_OK;
1168 for (i = 0; i < ARRAY_SIZE(set_tables); ++i)
1170 if (FAILED(hr = set_shader_constants_device(device, rs, peval->param_type, set_tables[i])))
1171 result = hr;
1173 return result;