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
20 #include "wine/port.h"
22 #include "d3dx9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
47 PRESHADER_OP_DOTSWIZ6
,
48 PRESHADER_OP_DOTSWIZ8
,
51 typedef double (*pres_op_func
)(double *args
, int n
);
53 static double pres_mov(double *args
, int n
) {return args
[0];}
54 static double pres_add(double *args
, int n
) {return args
[0] + args
[1];}
55 static double pres_mul(double *args
, int n
) {return args
[0] * args
[1];}
56 static double pres_dot(double *args
, int n
)
62 for (i
= 0; i
< n
; ++i
)
63 sum
+= args
[i
] * args
[i
+ n
];
67 static double pres_dotswiz6(double *args
, int n
)
69 return pres_dot(args
, 3);
72 static double pres_dotswiz8(double *args
, int n
)
74 return pres_dot(args
, 4);
77 static double pres_neg(double *args
, int n
) {return -args
[0];}
78 static double pres_rcp(double *args
, int n
) {return 1.0 / args
[0];}
79 static double pres_lt(double *args
, int n
) {return args
[0] < args
[1] ? 1.0 : 0.0;}
80 static double pres_ge(double *args
, int n
) {return args
[0] >= args
[1] ? 1.0 : 0.0;}
81 static double pres_frc(double *args
, int n
) {return args
[0] - floor(args
[0]);}
82 static double pres_min(double *args
, int n
) {return fmin(args
[0], args
[1]);}
83 static double pres_max(double *args
, int n
) {return fmax(args
[0], args
[1]);}
84 static double pres_cmp(double *args
, int n
) {return args
[0] < 0.0 ? args
[2] : args
[1];}
85 static double pres_sin(double *args
, int n
) {return sin(args
[0]);}
86 static double pres_cos(double *args
, int n
) {return cos(args
[0]);}
87 static double pres_rsq(double *args
, int n
)
97 static double pres_exp(double *args
, int n
) {return pow(2.0, args
[0]);}
99 #define PRES_OPCODE_MASK 0x7ff00000
100 #define PRES_OPCODE_SHIFT 20
101 #define PRES_SCALAR_FLAG 0x80000000
102 #define PRES_NCOMP_MASK 0x0000ffff
104 #define FOURCC_PRES 0x53455250
105 #define FOURCC_CLIT 0x54494c43
106 #define FOURCC_FXLC 0x434c5846
107 #define FOURCC_PRSI 0x49535250
108 #define PRES_SIGN 0x46580000
114 unsigned int input_count
;
119 static const struct op_info pres_op_info
[] =
121 {0x000, "nop", 0, 0, NULL
}, /* PRESHADER_OP_NOP */
122 {0x100, "mov", 1, 0, pres_mov
}, /* PRESHADER_OP_MOV */
123 {0x204, "add", 2, 0, pres_add
}, /* PRESHADER_OP_ADD */
124 {0x205, "mul", 2, 0, pres_mul
}, /* PRESHADER_OP_MUL */
125 {0x500, "dot", 2, 1, pres_dot
}, /* PRESHADER_OP_DOT */
126 {0x101, "neg", 1, 0, pres_neg
}, /* PRESHADER_OP_NEG */
127 {0x103, "rcp", 1, 0, pres_rcp
}, /* PRESHADER_OP_RCP */
128 {0x202, "lt", 2, 0, pres_lt
}, /* PRESHADER_OP_LT */
129 {0x104, "frc", 1, 0, pres_frc
}, /* PRESHADER_OP_FRC */
130 {0x200, "min", 2, 0, pres_min
}, /* PRESHADER_OP_MIN */
131 {0x201, "max", 2, 0, pres_max
}, /* PRESHADER_OP_MAX */
132 {0x203, "ge", 2, 0, pres_ge
}, /* PRESHADER_OP_GE */
133 {0x300, "cmp", 3, 0, pres_cmp
}, /* PRESHADER_OP_CMP */
134 {0x108, "sin", 1, 0, pres_sin
}, /* PRESHADER_OP_SIN */
135 {0x109, "cos", 1, 0, pres_cos
}, /* PRESHADER_OP_COS */
136 {0x107, "rsq", 1, 0, pres_rsq
}, /* PRESHADER_OP_RSQ */
137 {0x105, "exp", 1, 0, pres_exp
}, /* PRESHADER_OP_EXP */
138 {0x70e, "d3ds_dotswiz", 6, 0, pres_dotswiz6
}, /* PRESHADER_OP_DOTSWIZ6 */
139 {0x70e, "d3ds_dotswiz", 8, 0, pres_dotswiz8
}, /* PRESHADER_OP_DOTSWIZ8 */
152 unsigned int component_size
;
153 unsigned int reg_component_count
;
154 enum pres_value_type type
;
158 {sizeof(double), 1, PRES_VT_DOUBLE
}, /* PRES_REGTAB_IMMED */
159 {sizeof(float), 4, PRES_VT_FLOAT
}, /* PRES_REGTAB_CONST */
160 {sizeof(float), 4, PRES_VT_FLOAT
}, /* PRES_REGTAB_OCONST */
161 {sizeof(BOOL
), 1, PRES_VT_BOOL
}, /* PRES_REGTAB_OBCONST */
162 {sizeof(int), 4, PRES_VT_INT
, }, /* PRES_REGTAB_OICONST */
163 /* TODO: use double precision for 64 bit */
164 {sizeof(float), 4, PRES_VT_FLOAT
} /* PRES_REGTAB_TEMP */
167 static const char *table_symbol
[] =
169 "imm", "c", "oc", "ob", "oi", "r", "(null)",
172 static const enum pres_reg_tables pres_regset2table
[] =
174 PRES_REGTAB_OBCONST
, /* D3DXRS_BOOL */
175 PRES_REGTAB_OICONST
, /* D3DXRS_INT4 */
176 PRES_REGTAB_CONST
, /* D3DXRS_FLOAT4 */
177 PRES_REGTAB_COUNT
, /* D3DXRS_SAMPLER */
180 static const enum pres_reg_tables shad_regset2table
[] =
182 PRES_REGTAB_OBCONST
, /* D3DXRS_BOOL */
183 PRES_REGTAB_OICONST
, /* D3DXRS_INT4 */
184 PRES_REGTAB_OCONST
, /* D3DXRS_FLOAT4 */
185 PRES_REGTAB_COUNT
, /* D3DXRS_SAMPLER */
190 enum pres_reg_tables table
;
191 /* offset is component index, not register index, e. g.
192 offset for component c3.y is 13 (3 * 4 + 1) */
196 struct d3dx_pres_operand
198 struct d3dx_pres_reg reg
;
199 struct d3dx_pres_reg index_reg
;
202 #define MAX_INPUTS_COUNT 8
207 /* first input argument is scalar,
208 scalar component is propagated */
210 unsigned int component_count
;
211 struct d3dx_pres_operand inputs
[MAX_INPUTS_COUNT
];
212 struct d3dx_pres_operand output
;
215 static unsigned int get_reg_offset(unsigned int table
, unsigned int offset
)
217 return offset
/ table_info
[table
].reg_component_count
;
220 #define PRES_BITMASK_BLOCK_SIZE (sizeof(unsigned int) * 8)
222 static HRESULT
init_set_constants(struct d3dx_const_tab
*const_tab
, ID3DXConstantTable
*ctab
);
224 static HRESULT
regstore_alloc_table(struct d3dx_regstore
*rs
, unsigned int table
)
228 size
= rs
->table_sizes
[table
] * table_info
[table
].reg_component_count
* table_info
[table
].component_size
;
231 rs
->tables
[table
] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
232 rs
->table_value_set
[table
] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
233 sizeof(*rs
->table_value_set
[table
]) *
234 ((rs
->table_sizes
[table
] + PRES_BITMASK_BLOCK_SIZE
- 1) / PRES_BITMASK_BLOCK_SIZE
));
235 if (!rs
->tables
[table
] || !rs
->table_value_set
[table
])
236 return E_OUTOFMEMORY
;
241 static void regstore_free_tables(struct d3dx_regstore
*rs
)
245 for (i
= 0; i
< PRES_REGTAB_COUNT
; ++i
)
247 HeapFree(GetProcessHeap(), 0, rs
->tables
[i
]);
248 HeapFree(GetProcessHeap(), 0, rs
->table_value_set
[i
]);
252 static void regstore_set_values(struct d3dx_regstore
*rs
, unsigned int table
, void *data
,
253 unsigned int start_offset
, unsigned int count
)
255 unsigned int block_idx
, start
, end
, start_block
, end_block
;
260 memcpy((BYTE
*)rs
->tables
[table
] + start_offset
* table_info
[table
].component_size
,
261 data
, count
* table_info
[table
].component_size
);
263 start
= get_reg_offset(table
, start_offset
);
264 start_block
= start
/ PRES_BITMASK_BLOCK_SIZE
;
265 start
-= start_block
* PRES_BITMASK_BLOCK_SIZE
;
266 end
= get_reg_offset(table
, start_offset
+ count
- 1);
267 end_block
= end
/ PRES_BITMASK_BLOCK_SIZE
;
268 end
= (end_block
+ 1) * PRES_BITMASK_BLOCK_SIZE
- 1 - end
;
270 if (start_block
== end_block
)
272 rs
->table_value_set
[table
][start_block
] |= (~0u << start
) & (~0u >> end
);
276 rs
->table_value_set
[table
][start_block
] |= ~0u << start
;
278 for (block_idx
= start_block
+ 1; block_idx
< end_block
; ++block_idx
)
279 rs
->table_value_set
[table
][block_idx
] = ~0u;
281 rs
->table_value_set
[table
][end_block
] |= ~0u >> end
;
285 static unsigned int regstore_is_val_set_reg(struct d3dx_regstore
*rs
, unsigned int table
, unsigned int reg_idx
)
287 return rs
->table_value_set
[table
][reg_idx
/ PRES_BITMASK_BLOCK_SIZE
] &
288 (1u << (reg_idx
% PRES_BITMASK_BLOCK_SIZE
));
291 static double regstore_get_double(struct d3dx_regstore
*rs
, unsigned int table
, unsigned int offset
)
295 p
= (BYTE
*)rs
->tables
[table
] + table_info
[table
].component_size
* offset
;
296 switch (table_info
[table
].type
)
303 FIXME("Unexpected preshader input from table %u.\n", table
);
308 static void regstore_set_double(struct d3dx_regstore
*rs
, unsigned int table
, unsigned int offset
, double v
)
311 unsigned int reg_idx
;
313 p
= (BYTE
*)rs
->tables
[table
] + table_info
[table
].component_size
* offset
;
314 switch (table_info
[table
].type
)
316 case PRES_VT_FLOAT
: *(float *)p
= v
; break;
317 case PRES_VT_DOUBLE
: *(double *)p
= v
; break;
318 case PRES_VT_INT
: *(int *)p
= lrint(v
); break;
319 case PRES_VT_BOOL
: *(BOOL
*)p
= !!v
; break;
321 reg_idx
= get_reg_offset(table
, offset
);
322 rs
->table_value_set
[table
][reg_idx
/ PRES_BITMASK_BLOCK_SIZE
] |=
323 1u << (reg_idx
% PRES_BITMASK_BLOCK_SIZE
);
326 static void regstore_reset_table(struct d3dx_regstore
*rs
, unsigned int table
)
330 size
= rs
->table_sizes
[table
] * table_info
[table
].reg_component_count
* table_info
[table
].component_size
;
332 memset(rs
->tables
[table
], 0, size
);
333 memset(rs
->table_value_set
[table
], 0,
334 sizeof(*rs
->table_value_set
[table
]) *
335 ((rs
->table_sizes
[table
] + PRES_BITMASK_BLOCK_SIZE
- 1) / PRES_BITMASK_BLOCK_SIZE
));
338 static void dump_bytecode(void *data
, unsigned int size
)
340 unsigned int *bytecode
= (unsigned int *)data
;
341 unsigned int i
, j
, n
;
343 size
/= sizeof(*bytecode
);
347 n
= min(size
- i
, 8);
348 for (j
= 0; j
< n
; ++j
)
349 TRACE("0x%08x,", bytecode
[i
+ j
]);
355 static unsigned int *find_bytecode_comment(unsigned int *ptr
, unsigned int count
,
356 unsigned int fourcc
, unsigned int *size
)
358 /* Provide at least one value in comment section on non-NULL return. */
359 while (count
> 2 && (*ptr
& 0xffff) == 0xfffe)
361 unsigned int section_size
;
363 section_size
= (*ptr
>> 16);
364 if (!section_size
|| section_size
+ 1 > count
)
366 if (*(ptr
+ 1) == fourcc
)
368 *size
= section_size
;
371 count
-= section_size
+ 1;
372 ptr
+= section_size
+ 1;
377 static unsigned int *parse_pres_reg(unsigned int *ptr
, struct d3dx_pres_reg
*reg
)
379 static const enum pres_reg_tables reg_table
[8] =
381 PRES_REGTAB_COUNT
, PRES_REGTAB_IMMED
, PRES_REGTAB_CONST
, PRES_REGTAB_COUNT
,
382 PRES_REGTAB_OCONST
, PRES_REGTAB_OBCONST
, PRES_REGTAB_OICONST
, PRES_REGTAB_TEMP
385 if (*ptr
>= ARRAY_SIZE(reg_table
) || reg_table
[*ptr
] == PRES_REGTAB_COUNT
)
387 FIXME("Unsupported register table %#x.\n", *ptr
);
391 reg
->table
= reg_table
[*ptr
++];
392 reg
->offset
= *ptr
++;
396 static unsigned int *parse_pres_arg(unsigned int *ptr
, unsigned int count
, struct d3dx_pres_operand
*opr
)
398 if (count
< 3 || (*ptr
&& count
< 5))
400 WARN("Byte code buffer ends unexpectedly, count %u.\n", count
);
408 FIXME("Unknown relative addressing flag, word %#x.\n", *ptr
);
411 ptr
= parse_pres_reg(ptr
+ 1, &opr
->index_reg
);
417 opr
->index_reg
.table
= PRES_REGTAB_COUNT
;
421 ptr
= parse_pres_reg(ptr
, &opr
->reg
);
423 if (opr
->reg
.table
== PRES_REGTAB_OBCONST
)
424 opr
->reg
.offset
/= 4;
428 static unsigned int *parse_pres_ins(unsigned int *ptr
, unsigned int count
, struct d3dx_pres_ins
*ins
)
430 unsigned int ins_code
, ins_raw
;
431 unsigned int input_count
;
436 WARN("Byte code buffer ends unexpectedly.\n");
441 ins_code
= (ins_raw
& PRES_OPCODE_MASK
) >> PRES_OPCODE_SHIFT
;
442 ins
->component_count
= ins_raw
& PRES_NCOMP_MASK
;
443 ins
->scalar_op
= !!(ins_raw
& PRES_SCALAR_FLAG
);
445 if (ins
->component_count
< 1 || ins
->component_count
> 4)
447 FIXME("Unsupported number of components %u.\n", ins
->component_count
);
450 input_count
= *ptr
++;
452 for (i
= 0; i
< ARRAY_SIZE(pres_op_info
); ++i
)
453 if (ins_code
== pres_op_info
[i
].opcode
&& input_count
== pres_op_info
[i
].input_count
)
455 if (i
== ARRAY_SIZE(pres_op_info
))
457 FIXME("Unknown opcode %#x, input_count %u, raw %#x.\n", ins_code
, input_count
, ins_raw
);
461 if (input_count
> ARRAY_SIZE(ins
->inputs
))
463 FIXME("Actual input args count %u exceeds inputs array size, instruction %s.\n", input_count
,
464 pres_op_info
[i
].mnem
);
467 for (i
= 0; i
< input_count
; ++i
)
471 p
= parse_pres_arg(ptr
, count
, &ins
->inputs
[i
]);
477 ptr
= parse_pres_arg(ptr
, count
, &ins
->output
);
478 if (ins
->output
.index_reg
.table
!= PRES_REGTAB_COUNT
)
480 FIXME("Relative addressing in output register not supported.\n");
487 static HRESULT
get_ctab_constant_desc(ID3DXConstantTable
*ctab
, D3DXHANDLE hc
, D3DXCONSTANT_DESC
*desc
)
489 D3DXCONSTANT_DESC buffer
[2];
493 count
= ARRAY_SIZE(buffer
);
494 if (FAILED(hr
= ID3DXConstantTable_GetConstantDesc(ctab
, hc
, buffer
, &count
)))
496 FIXME("Could not get constant desc, hr %#x.\n", hr
);
501 FIXME("Unexpected constant descriptors count %u.\n", count
);
502 return D3DERR_INVALIDCALL
;
508 static HRESULT
get_constants_desc(unsigned int *byte_code
, struct d3dx_const_tab
*out
, struct d3dx9_base_effect
*base
)
510 ID3DXConstantTable
*ctab
;
511 D3DXCONSTANT_DESC
*cdesc
;
512 struct d3dx_parameter
**inputs_param
;
513 D3DXCONSTANTTABLE_DESC desc
;
518 out
->inputs
= cdesc
= NULL
;
519 out
->inputs_param
= NULL
;
520 out
->input_count
= 0;
522 hr
= D3DXGetShaderConstantTable(byte_code
, &ctab
);
523 if (FAILED(hr
) || !ctab
)
525 TRACE("Could not get CTAB data, hr %#x.\n", hr
);
526 /* returning OK, shaders and preshaders without CTAB are valid */
529 if (FAILED(hr
= ID3DXConstantTable_GetDesc(ctab
, &desc
)))
531 FIXME("Could not get CTAB desc, hr %#x.\n", hr
);
535 cdesc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*cdesc
) * desc
.Constants
);
536 inputs_param
= HeapAlloc(GetProcessHeap(), 0, sizeof(*inputs_param
) * desc
.Constants
);
537 if (!cdesc
|| !inputs_param
)
543 for (i
= 0; i
< desc
.Constants
; ++i
)
545 hc
= ID3DXConstantTable_GetConstant(ctab
, NULL
, i
);
548 FIXME("Null constant handle.\n");
551 if (FAILED(hr
= get_ctab_constant_desc(ctab
, hc
, &cdesc
[i
])))
553 inputs_param
[i
] = get_parameter_by_name(base
, NULL
, cdesc
[i
].Name
);
554 if (cdesc
[i
].Class
== D3DXPC_OBJECT
)
555 TRACE("Object %s, parameter %p.\n", cdesc
[i
].Name
, inputs_param
[i
]);
556 else if (!inputs_param
[i
])
557 WARN("Could not find parameter %s in effect.\n", cdesc
[i
].Name
);
559 out
->input_count
= desc
.Constants
;
561 out
->inputs_param
= inputs_param
;
562 hr
= init_set_constants(out
, ctab
);
563 ID3DXConstantTable_Release(ctab
);
566 HeapFree(GetProcessHeap(), 0, cdesc
);
567 HeapFree(GetProcessHeap(), 0, inputs_param
);
569 ID3DXConstantTable_Release(ctab
);
573 static void update_table_size(unsigned int *table_sizes
, unsigned int table
, unsigned int max_register
)
575 if (table
< PRES_REGTAB_COUNT
)
576 table_sizes
[table
] = max(table_sizes
[table
], max_register
+ 1);
579 static void update_table_sizes_consts(unsigned int *table_sizes
, struct d3dx_const_tab
*ctab
)
581 unsigned int i
, table
, max_register
;
583 for (i
= 0; i
< ctab
->input_count
; ++i
)
585 if (!ctab
->inputs
[i
].RegisterCount
)
587 max_register
= ctab
->inputs
[i
].RegisterIndex
+ ctab
->inputs
[i
].RegisterCount
- 1;
588 table
= ctab
->regset2table
[ctab
->inputs
[i
].RegisterSet
];
589 update_table_size(table_sizes
, table
, max_register
);
593 static void dump_arg(struct d3dx_regstore
*rs
, const struct d3dx_pres_operand
*arg
, int component_count
)
595 static const char *xyzw_str
= "xyzw";
596 unsigned int i
, table
;
598 table
= arg
->reg
.table
;
599 if (table
== PRES_REGTAB_IMMED
&& arg
->index_reg
.table
== PRES_REGTAB_COUNT
)
602 for (i
= 0; i
< component_count
; ++i
)
603 TRACE(i
< component_count
- 1 ? "%.16e, " : "%.16e",
604 ((double *)rs
->tables
[PRES_REGTAB_IMMED
])[arg
->reg
.offset
+ i
]);
609 if (arg
->index_reg
.table
== PRES_REGTAB_COUNT
)
611 TRACE("%s%u.", table_symbol
[table
], get_reg_offset(table
, arg
->reg
.offset
));
615 unsigned int index_reg
;
617 index_reg
= get_reg_offset(arg
->index_reg
.table
, arg
->index_reg
.offset
);
618 TRACE("%s[%u + %s%u.%c].", table_symbol
[table
], get_reg_offset(table
, arg
->reg
.offset
),
619 table_symbol
[arg
->index_reg
.table
], index_reg
,
620 xyzw_str
[arg
->index_reg
.offset
621 - index_reg
* table_info
[arg
->index_reg
.table
].reg_component_count
]);
623 for (i
= 0; i
< component_count
; ++i
)
624 TRACE("%c", xyzw_str
[(arg
->reg
.offset
+ i
) % 4]);
628 static void dump_registers(struct d3dx_const_tab
*ctab
)
630 unsigned int table
, i
;
632 for (i
= 0; i
< ctab
->input_count
; ++i
)
634 table
= ctab
->regset2table
[ctab
->inputs
[i
].RegisterSet
];
635 TRACE("// %-12s %s%-4u %u\n", ctab
->inputs_param
[i
] ? ctab
->inputs_param
[i
]->name
: "(nil)",
636 table_symbol
[table
], ctab
->inputs
[i
].RegisterIndex
, ctab
->inputs
[i
].RegisterCount
);
640 static void dump_ins(struct d3dx_regstore
*rs
, const struct d3dx_pres_ins
*ins
)
644 TRACE("%s ", pres_op_info
[ins
->op
].mnem
);
645 dump_arg(rs
, &ins
->output
, pres_op_info
[ins
->op
].func_all_comps
? 1 : ins
->component_count
);
646 for (i
= 0; i
< pres_op_info
[ins
->op
].input_count
; ++i
)
649 dump_arg(rs
, &ins
->inputs
[i
], ins
->scalar_op
&& !i
? 1 : ins
->component_count
);
654 static void dump_preshader(struct d3dx_preshader
*pres
)
656 unsigned int i
, immediate_count
= pres
->regs
.table_sizes
[PRES_REGTAB_IMMED
];
657 const double *immediates
= pres
->regs
.tables
[PRES_REGTAB_IMMED
];
660 TRACE("// Immediates:\n");
661 for (i
= 0; i
< immediate_count
; ++i
)
665 TRACE("%.8e", immediates
[i
]);
671 TRACE("// Preshader registers:\n");
672 dump_registers(&pres
->inputs
);
673 TRACE("preshader\n");
674 for (i
= 0; i
< pres
->ins_count
; ++i
)
675 dump_ins(&pres
->regs
, &pres
->ins
[i
]);
678 static HRESULT
parse_preshader(struct d3dx_preshader
*pres
, unsigned int *ptr
, unsigned int count
, struct d3dx9_base_effect
*base
)
681 unsigned int i
, j
, const_count
;
684 unsigned int saved_word
;
685 unsigned int section_size
;
687 TRACE("Preshader version %#x.\n", *ptr
& 0xffff);
691 WARN("Unexpected end of byte code buffer.\n");
692 return D3DXERR_INVALIDDATA
;
695 p
= find_bytecode_comment(ptr
+ 1, count
- 1, FOURCC_CLIT
, §ion_size
);
699 if (const_count
> (section_size
- 1) / (sizeof(double) / sizeof(unsigned int)))
701 WARN("Byte code buffer ends unexpectedly.\n");
702 return D3DXERR_INVALIDDATA
;
704 dconst
= (double *)p
;
711 TRACE("%u double constants.\n", const_count
);
713 p
= find_bytecode_comment(ptr
+ 1, count
- 1, FOURCC_FXLC
, §ion_size
);
716 WARN("Could not find preshader code.\n");
719 pres
->ins_count
= *p
++;
721 if (pres
->ins_count
> UINT_MAX
/ sizeof(*pres
->ins
))
723 WARN("Invalid instruction count %u.\n", pres
->ins_count
);
724 return D3DXERR_INVALIDDATA
;
726 TRACE("%u instructions.\n", pres
->ins_count
);
727 pres
->ins
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pres
->ins
) * pres
->ins_count
);
729 return E_OUTOFMEMORY
;
730 for (i
= 0; i
< pres
->ins_count
; ++i
)
732 unsigned int *ptr_next
;
734 ptr_next
= parse_pres_ins(p
, section_size
, &pres
->ins
[i
]);
736 return D3DXERR_INVALIDDATA
;
737 section_size
-= ptr_next
- p
;
741 pres
->inputs
.regset2table
= pres_regset2table
;
745 hr
= get_constants_desc(ptr
, &pres
->inputs
, base
);
750 pres
->regs
.table_sizes
[PRES_REGTAB_IMMED
] = const_count
;
752 for (i
= 0; i
< pres
->ins_count
; ++i
)
754 for (j
= 0; j
< pres_op_info
[pres
->ins
[i
].op
].input_count
; ++j
)
756 enum pres_reg_tables table
;
757 unsigned int reg_idx
;
759 if (pres
->ins
[i
].inputs
[j
].index_reg
.table
== PRES_REGTAB_COUNT
)
761 table
= pres
->ins
[i
].inputs
[j
].reg
.table
;
762 reg_idx
= get_reg_offset(table
, pres
->ins
[i
].inputs
[j
].reg
.offset
763 + pres
->ins
[i
].component_count
- 1);
767 table
= pres
->ins
[i
].inputs
[j
].index_reg
.table
;
768 reg_idx
= get_reg_offset(table
, pres
->ins
[i
].inputs
[j
].index_reg
.offset
);
770 update_table_size(pres
->regs
.table_sizes
, table
, reg_idx
);
772 update_table_size(pres
->regs
.table_sizes
, pres
->ins
[i
].output
.reg
.table
,
773 get_reg_offset(pres
->ins
[i
].output
.reg
.table
,
774 pres
->ins
[i
].output
.reg
.offset
+ pres
->ins
[i
].component_count
- 1));
776 update_table_sizes_consts(pres
->regs
.table_sizes
, &pres
->inputs
);
777 if (FAILED(regstore_alloc_table(&pres
->regs
, PRES_REGTAB_IMMED
)))
778 return E_OUTOFMEMORY
;
779 regstore_set_values(&pres
->regs
, PRES_REGTAB_IMMED
, dconst
, 0, const_count
);
784 void d3dx_create_param_eval(struct d3dx9_base_effect
*base_effect
, void *byte_code
, unsigned int byte_code_size
,
785 D3DXPARAMETER_TYPE type
, struct d3dx_param_eval
**peval_out
)
787 struct d3dx_param_eval
*peval
;
792 unsigned int count
, pres_size
;
794 TRACE("base_effect %p, byte_code %p, byte_code_size %u, type %u, peval_out %p.\n",
795 base_effect
, byte_code
, byte_code_size
, type
, peval_out
);
797 count
= byte_code_size
/ sizeof(unsigned int);
798 if (!byte_code
|| !count
)
804 peval
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*peval
));
808 peval
->param_type
= type
;
811 case D3DXPT_VERTEXSHADER
:
812 case D3DXPT_PIXELSHADER
:
819 peval
->shader_inputs
.regset2table
= shad_regset2table
;
821 ptr
= (unsigned int *)byte_code
;
824 if ((*ptr
& 0xfffe0000) != 0xfffe0000)
826 FIXME("Invalid shader signature %#x.\n", *ptr
);
829 TRACE("Shader version %#x.\n", *ptr
& 0xffff);
831 if (FAILED(hr
= get_constants_desc(ptr
, &peval
->shader_inputs
, base_effect
)))
833 FIXME("Could not get shader constant table, hr %#x.\n", hr
);
836 update_table_sizes_consts(peval
->pres
.regs
.table_sizes
, &peval
->shader_inputs
);
837 ptr
= find_bytecode_comment(ptr
+ 1, count
- 1, FOURCC_PRES
, &pres_size
);
839 TRACE("No preshader found.\n");
846 if (ptr
&& FAILED(parse_preshader(&peval
->pres
, ptr
, pres_size
, base_effect
)))
848 FIXME("Failed parsing preshader, byte code for analysis follows.\n");
849 dump_bytecode(byte_code
, byte_code_size
);
853 for (i
= PRES_REGTAB_FIRST_SHADER
; i
< PRES_REGTAB_COUNT
; ++i
)
855 if (FAILED(regstore_alloc_table(&peval
->pres
.regs
, i
)))
861 dump_bytecode(byte_code
, byte_code_size
);
862 dump_preshader(&peval
->pres
);
865 TRACE("// Shader registers:\n");
866 dump_registers(&peval
->shader_inputs
);
870 TRACE("Created parameter evaluator %p.\n", *peval_out
);
874 FIXME("Error creating parameter evaluator.\n");
875 d3dx_free_param_eval(peval
);
879 static void d3dx_free_const_tab(struct d3dx_const_tab
*ctab
)
881 HeapFree(GetProcessHeap(), 0, ctab
->inputs
);
882 HeapFree(GetProcessHeap(), 0, ctab
->inputs_param
);
883 HeapFree(GetProcessHeap(), 0, ctab
->const_set
);
886 static void d3dx_free_preshader(struct d3dx_preshader
*pres
)
888 HeapFree(GetProcessHeap(), 0, pres
->ins
);
890 regstore_free_tables(&pres
->regs
);
891 d3dx_free_const_tab(&pres
->inputs
);
894 void d3dx_free_param_eval(struct d3dx_param_eval
*peval
)
896 TRACE("peval %p.\n", peval
);
901 d3dx_free_preshader(&peval
->pres
);
902 d3dx_free_const_tab(&peval
->shader_inputs
);
903 HeapFree(GetProcessHeap(), 0, peval
);
906 static void set_constants(struct d3dx_regstore
*rs
, struct d3dx_const_tab
*const_tab
, BOOL update_all
)
908 unsigned int const_idx
;
910 for (const_idx
= 0; const_idx
< const_tab
->const_set_count
; ++const_idx
)
912 struct d3dx_const_param_eval_output
*const_set
= &const_tab
->const_set
[const_idx
];
913 unsigned int table
= const_set
->table
;
914 struct d3dx_parameter
*param
= const_set
->param
;
915 enum pres_value_type table_type
= table_info
[table
].type
;
916 unsigned int i
, j
, n
, start_offset
;
917 unsigned int minor
, major
, major_stride
, param_offset
;
921 if (!(update_all
|| is_param_dirty(param
)))
924 transpose
= (const_set
->constant_class
== D3DXPC_MATRIX_COLUMNS
&& param
->class == D3DXPC_MATRIX_ROWS
)
925 || (param
->class == D3DXPC_MATRIX_COLUMNS
&& const_set
->constant_class
== D3DXPC_MATRIX_ROWS
);
926 if (const_set
->constant_class
== D3DXPC_MATRIX_COLUMNS
)
928 major
= param
->columns
;
934 minor
= param
->columns
;
936 start_offset
= const_set
->register_index
* table_info
[table
].reg_component_count
;
937 major_stride
= max(minor
, table_info
[table
].reg_component_count
);
938 n
= min(major
* major_stride
,
939 const_set
->register_count
* table_info
[table
].reg_component_count
+ major_stride
- 1) / major_stride
;
941 if (((param
->type
== D3DXPT_FLOAT
&& table_type
== PRES_VT_FLOAT
)
942 || (param
->type
== D3DXPT_INT
&& table_type
== PRES_VT_INT
)
943 || (param
->type
== D3DXPT_BOOL
&& table_type
== PRES_VT_BOOL
))
944 && !transpose
&& minor
== major_stride
945 && count
== table_info
[table
].reg_component_count
* const_set
->register_count
946 && count
* sizeof(unsigned int) <= param
->bytes
)
948 regstore_set_values(rs
, table
, param
->data
, start_offset
, count
);
952 for (i
= 0; i
< n
; ++i
)
954 for (j
= 0; j
< minor
; ++j
)
960 offset
= start_offset
+ i
* major_stride
+ j
;
961 if (offset
/ table_info
[table
].reg_component_count
>= rs
->table_sizes
[table
])
963 if (table_info
[table
].reg_component_count
!= 1)
964 FIXME("Output offset exceeds table size, name %s, component %u.\n",
965 debugstr_a(param
->name
), i
);
969 param_offset
= i
+ j
* major
;
971 param_offset
= i
* minor
+ j
;
972 if (param_offset
* sizeof(unsigned int) >= param
->bytes
)
974 WARN("Parameter data is too short, name %s, component %u.\n", debugstr_a(param
->name
), i
);
978 in
= (unsigned int *)param
->data
+ param_offset
;
981 case PRES_VT_FLOAT
: set_number(&out
, D3DXPT_FLOAT
, in
, param
->type
); break;
982 case PRES_VT_INT
: set_number(&out
, D3DXPT_INT
, in
, param
->type
); break;
983 case PRES_VT_BOOL
: set_number(&out
, D3DXPT_BOOL
, in
, param
->type
); break;
985 FIXME("Unexpected type %#x.\n", table_info
[table
].type
);
988 regstore_set_values(rs
, table
, &out
, offset
, 1);
994 #define INITIAL_CONST_SET_SIZE 16
996 static HRESULT
append_const_set(struct d3dx_const_tab
*const_tab
, struct d3dx_const_param_eval_output
*set
)
998 if (const_tab
->const_set_count
>= const_tab
->const_set_size
)
1000 unsigned int new_size
;
1001 struct d3dx_const_param_eval_output
*new_alloc
;
1003 if (!const_tab
->const_set_size
)
1005 new_size
= INITIAL_CONST_SET_SIZE
;
1006 new_alloc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*const_tab
->const_set
) * new_size
);
1009 ERR("Out of memory.\n");
1010 return E_OUTOFMEMORY
;
1015 new_size
= const_tab
->const_set_size
* 2;
1016 new_alloc
= HeapReAlloc(GetProcessHeap(), 0, const_tab
->const_set
,
1017 sizeof(*const_tab
->const_set
) * new_size
);
1020 ERR("Out of memory.\n");
1021 return E_OUTOFMEMORY
;
1024 const_tab
->const_set
= new_alloc
;
1025 const_tab
->const_set_size
= new_size
;
1027 const_tab
->const_set
[const_tab
->const_set_count
++] = *set
;
1031 static HRESULT
init_set_constants_param(struct d3dx_const_tab
*const_tab
, ID3DXConstantTable
*ctab
,
1032 D3DXHANDLE hc
, struct d3dx_parameter
*param
)
1034 D3DXCONSTANT_DESC desc
;
1035 unsigned int const_count
, param_count
, i
;
1037 struct d3dx_const_param_eval_output const_set
;
1040 if (FAILED(get_ctab_constant_desc(ctab
, hc
, &desc
)))
1041 return D3DERR_INVALIDCALL
;
1043 if (param
->element_count
)
1045 param_count
= param
->element_count
;
1046 const_count
= desc
.Elements
;
1051 if (desc
.Elements
> 1)
1053 FIXME("Unexpected number of constant elements %u.\n", desc
.Elements
);
1054 return D3DERR_INVALIDCALL
;
1056 param_count
= param
->member_count
;
1057 const_count
= desc
.StructMembers
;
1058 get_element
= FALSE
;
1060 if (const_count
!= param_count
)
1062 FIXME("Number of elements or struct members differs between parameter (%u) and constant (%u).\n",
1063 param_count
, const_count
);
1064 return D3DERR_INVALIDCALL
;
1069 D3DXHANDLE hc_element
;
1072 for (i
= 0; i
< const_count
; ++i
)
1075 hc_element
= ID3DXConstantTable_GetConstantElement(ctab
, hc
, i
);
1077 hc_element
= ID3DXConstantTable_GetConstant(ctab
, hc
, i
);
1080 FIXME("Could not get constant.\n");
1081 hr
= D3DERR_INVALIDCALL
;
1085 hr
= init_set_constants_param(const_tab
, ctab
, hc_element
, ¶m
->members
[i
]);
1093 TRACE("Constant %s, rows %u, columns %u, class %u, bytes %u.\n",
1094 debugstr_a(desc
.Name
), desc
.Rows
, desc
.Columns
, desc
.Class
, desc
.Bytes
);
1095 TRACE("Parameter %s, rows %u, columns %u, class %u, flags %#x, bytes %u.\n",
1096 debugstr_a(param
->name
), param
->rows
, param
->columns
, param
->class,
1097 param
->flags
, param
->bytes
);
1099 const_set
.param
= param
;
1100 const_set
.constant_class
= desc
.Class
;
1101 if (desc
.RegisterSet
>= ARRAY_SIZE(shad_regset2table
))
1103 FIXME("Unknown register set %u.\n", desc
.RegisterSet
);
1104 return D3DERR_INVALIDCALL
;
1106 const_set
.register_index
= desc
.RegisterIndex
;
1107 const_set
.table
= const_tab
->regset2table
[desc
.RegisterSet
];
1108 if (const_set
.table
>= PRES_REGTAB_COUNT
)
1110 ERR("Unexpected register set %u.\n", desc
.RegisterSet
);
1111 return D3DERR_INVALIDCALL
;
1113 const_set
.register_count
= desc
.RegisterCount
;
1114 if (FAILED(hr
= append_const_set(const_tab
, &const_set
)))
1120 static HRESULT
init_set_constants(struct d3dx_const_tab
*const_tab
, ID3DXConstantTable
*ctab
)
1127 for (i
= 0; i
< const_tab
->input_count
; ++i
)
1129 if (!const_tab
->inputs_param
[i
] || const_tab
->inputs_param
[i
]->class == D3DXPC_OBJECT
)
1131 hc
= ID3DXConstantTable_GetConstant(ctab
, NULL
, i
);
1134 hr
= init_set_constants_param(const_tab
, ctab
, hc
, const_tab
->inputs_param
[i
]);
1138 FIXME("Could not get constant, index %u.\n", i
);
1139 hr
= D3DERR_INVALIDCALL
;
1145 if (const_tab
->const_set_count
)
1147 const_tab
->const_set
= HeapReAlloc(GetProcessHeap(), 0, const_tab
->const_set
,
1148 sizeof(*const_tab
->const_set
) * const_tab
->const_set_count
);
1149 if (!const_tab
->const_set
)
1151 ERR("Out of memory.\n");
1152 return E_OUTOFMEMORY
;
1154 const_tab
->const_set_size
= const_tab
->const_set_count
;
1159 static double exec_get_reg_value(struct d3dx_regstore
*rs
, enum pres_reg_tables table
, unsigned int offset
)
1161 if (!regstore_is_val_set_reg(rs
, table
, offset
/ table_info
[table
].reg_component_count
))
1162 WARN("Using uninitialized input, table %u, offset %u.\n", table
, offset
);
1164 return regstore_get_double(rs
, table
, offset
);
1167 static double exec_get_arg(struct d3dx_regstore
*rs
, const struct d3dx_pres_operand
*opr
, unsigned int comp
)
1169 unsigned int offset
, base_index
, reg_index
, table
;
1171 table
= opr
->reg
.table
;
1173 if (opr
->index_reg
.table
== PRES_REGTAB_COUNT
)
1176 base_index
= lrint(exec_get_reg_value(rs
, opr
->index_reg
.table
, opr
->index_reg
.offset
));
1178 /* '4' is used instead of reg_component_count, as immediate constants (which have
1179 * reg_component_count of 1) are still indexed as 4 values according to the tests. */
1180 offset
= base_index
* 4 + opr
->reg
.offset
+ comp
;
1181 reg_index
= offset
/ table_info
[table
].reg_component_count
;
1183 if (reg_index
>= rs
->table_sizes
[table
])
1185 unsigned int wrap_size
;
1187 if (table
== PRES_REGTAB_CONST
)
1189 /* As it can be guessed from tests, offset into floating constant table is wrapped
1190 * to the nearest power of 2 and not to the actual table size. */
1191 for (wrap_size
= 1; wrap_size
< rs
->table_sizes
[table
]; wrap_size
<<= 1)
1196 wrap_size
= rs
->table_sizes
[table
];
1198 WARN("Wrapping register index %u, table %u, wrap_size %u, table size %u.\n",
1199 reg_index
, table
, wrap_size
, rs
->table_sizes
[table
]);
1200 reg_index
%= wrap_size
;
1202 if (reg_index
>= rs
->table_sizes
[table
])
1205 offset
= reg_index
* table_info
[table
].reg_component_count
1206 + offset
% table_info
[table
].reg_component_count
;
1209 return exec_get_reg_value(rs
, table
, offset
);
1212 static void exec_set_arg(struct d3dx_regstore
*rs
, const struct d3dx_pres_reg
*reg
,
1213 unsigned int comp
, double res
)
1215 regstore_set_double(rs
, reg
->table
, reg
->offset
+ comp
, res
);
1218 #define ARGS_ARRAY_SIZE 8
1219 static HRESULT
execute_preshader(struct d3dx_preshader
*pres
)
1221 unsigned int i
, j
, k
;
1222 double args
[ARGS_ARRAY_SIZE
];
1225 for (i
= 0; i
< pres
->ins_count
; ++i
)
1227 const struct d3dx_pres_ins
*ins
;
1228 const struct op_info
*oi
;
1230 ins
= &pres
->ins
[i
];
1231 oi
= &pres_op_info
[ins
->op
];
1232 if (oi
->func_all_comps
)
1234 if (oi
->input_count
* ins
->component_count
> ARGS_ARRAY_SIZE
)
1236 FIXME("Too many arguments (%u) for one instruction.\n", oi
->input_count
* ins
->component_count
);
1239 for (k
= 0; k
< oi
->input_count
; ++k
)
1240 for (j
= 0; j
< ins
->component_count
; ++j
)
1241 args
[k
* ins
->component_count
+ j
] = exec_get_arg(&pres
->regs
, &ins
->inputs
[k
],
1242 ins
->scalar_op
&& !k
? 0 : j
);
1243 res
= oi
->func(args
, ins
->component_count
);
1245 /* only 'dot' instruction currently falls here */
1246 exec_set_arg(&pres
->regs
, &ins
->output
.reg
, 0, res
);
1250 for (j
= 0; j
< ins
->component_count
; ++j
)
1252 for (k
= 0; k
< oi
->input_count
; ++k
)
1253 args
[k
] = exec_get_arg(&pres
->regs
, &ins
->inputs
[k
], ins
->scalar_op
&& !k
? 0 : j
);
1254 res
= oi
->func(args
, ins
->component_count
);
1255 exec_set_arg(&pres
->regs
, &ins
->output
.reg
, j
, res
);
1262 static BOOL
is_const_tab_input_dirty(struct d3dx_const_tab
*ctab
)
1266 for (i
= 0; i
< ctab
->const_set_count
; ++i
)
1268 if (is_param_dirty(ctab
->const_set
[i
].param
))
1274 BOOL
is_param_eval_input_dirty(struct d3dx_param_eval
*peval
)
1276 return is_const_tab_input_dirty(&peval
->pres
.inputs
)
1277 || is_const_tab_input_dirty(&peval
->shader_inputs
);
1280 HRESULT
d3dx_evaluate_parameter(struct d3dx_param_eval
*peval
, const struct d3dx_parameter
*param
,
1281 void *param_value
, BOOL update_all
)
1285 unsigned int elements
, elements_param
, elements_table
;
1288 TRACE("peval %p, param %p, param_value %p.\n", peval
, param
, param_value
);
1290 set_constants(&peval
->pres
.regs
, &peval
->pres
.inputs
, update_all
);
1292 if (FAILED(hr
= execute_preshader(&peval
->pres
)))
1295 elements_table
= table_info
[PRES_REGTAB_OCONST
].reg_component_count
1296 * peval
->pres
.regs
.table_sizes
[PRES_REGTAB_OCONST
];
1297 elements_param
= param
->bytes
/ sizeof(unsigned int);
1298 elements
= min(elements_table
, elements_param
);
1299 oc
= (float *)peval
->pres
.regs
.tables
[PRES_REGTAB_OCONST
];
1300 for (i
= 0; i
< elements
; ++i
)
1301 set_number((unsigned int *)param_value
+ i
, param
->type
, oc
+ i
, D3DXPT_FLOAT
);
1305 static HRESULT
set_shader_constants_device(struct IDirect3DDevice9
*device
, struct d3dx_regstore
*rs
,
1306 D3DXPARAMETER_TYPE type
, enum pres_reg_tables table
)
1308 unsigned int start
, count
;
1314 while (start
< rs
->table_sizes
[table
])
1317 while (start
< rs
->table_sizes
[table
] && !regstore_is_val_set_reg(rs
, table
, start
))
1319 while (start
+ count
< rs
->table_sizes
[table
] && regstore_is_val_set_reg(rs
, table
, start
+ count
))
1323 TRACE("Setting %u constants at %u.\n", count
, start
);
1324 ptr
= (BYTE
*)rs
->tables
[table
] + start
* table_info
[table
].reg_component_count
1325 * table_info
[table
].component_size
;
1326 if (type
== D3DXPT_VERTEXSHADER
)
1330 case PRES_REGTAB_OCONST
:
1331 hr
= IDirect3DDevice9_SetVertexShaderConstantF(device
, start
, (const float *)ptr
, count
);
1333 case PRES_REGTAB_OICONST
:
1334 hr
= IDirect3DDevice9_SetVertexShaderConstantI(device
, start
, (const int *)ptr
, count
);
1336 case PRES_REGTAB_OBCONST
:
1337 hr
= IDirect3DDevice9_SetVertexShaderConstantB(device
, start
, (const BOOL
*)ptr
, count
);
1340 FIXME("Unexpected register table %u.\n", table
);
1341 return D3DERR_INVALIDCALL
;
1344 else if (type
== D3DXPT_PIXELSHADER
)
1348 case PRES_REGTAB_OCONST
:
1349 hr
= IDirect3DDevice9_SetPixelShaderConstantF(device
, start
, (const float *)ptr
, count
);
1351 case PRES_REGTAB_OICONST
:
1352 hr
= IDirect3DDevice9_SetPixelShaderConstantI(device
, start
, (const int *)ptr
, count
);
1354 case PRES_REGTAB_OBCONST
:
1355 hr
= IDirect3DDevice9_SetPixelShaderConstantB(device
, start
, (const BOOL
*)ptr
, count
);
1358 FIXME("Unexpected register table %u.\n", table
);
1359 return D3DERR_INVALIDCALL
;
1364 FIXME("Unexpected parameter type %u.\n", type
);
1365 return D3DERR_INVALIDCALL
;
1370 ERR("Setting constants failed, type %u, table %u, hr %#x.\n", type
, table
, hr
);
1375 regstore_reset_table(rs
, table
);
1379 HRESULT
d3dx_param_eval_set_shader_constants(struct IDirect3DDevice9
*device
, struct d3dx_param_eval
*peval
,
1382 static const enum pres_reg_tables set_tables
[] =
1383 {PRES_REGTAB_OCONST
, PRES_REGTAB_OICONST
, PRES_REGTAB_OBCONST
};
1385 struct d3dx_preshader
*pres
= &peval
->pres
;
1386 struct d3dx_regstore
*rs
= &pres
->regs
;
1389 TRACE("device %p, peval %p, param_type %u.\n", device
, peval
, peval
->param_type
);
1391 if (update_all
|| is_const_tab_input_dirty(&pres
->inputs
))
1393 set_constants(rs
, &pres
->inputs
, update_all
);
1394 if (FAILED(hr
= execute_preshader(pres
)))
1398 set_constants(rs
, &peval
->shader_inputs
, update_all
);
1400 for (i
= 0; i
< ARRAY_SIZE(set_tables
); ++i
)
1402 if (FAILED(hr
= set_shader_constants_device(device
, rs
, peval
->param_type
, set_tables
[i
])))