2 * Copyright 2009 Henri Verbeet for CodeWeavers
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 "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
26 #define WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT 24
27 #define WINED3D_SM4_INSTRUCTION_LENGTH_MASK (0xf << WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT)
29 #define WINED3D_SM4_OPCODE_MASK 0xff
31 #define WINED3D_SM4_REGISTER_TYPE_SHIFT 12
32 #define WINED3D_SM4_REGISTER_TYPE_MASK (0xf << WINED3D_SM4_REGISTER_TYPE_SHIFT)
34 #define WINED3D_SM4_IMMCONST_TYPE_SHIFT 0
35 #define WINED3D_SM4_IMMCONST_TYPE_MASK (0x3 << WINED3D_SM4_IMMCONST_TYPE_SHIFT)
37 #define WINED3D_SM4_WRITEMASK_SHIFT 4
38 #define WINED3D_SM4_WRITEMASK_MASK (0xf << WINED3D_SM4_WRITEMASK_SHIFT)
40 #define WINED3D_SM4_SWIZZLE_SHIFT 4
41 #define WINED3D_SM4_SWIZZLE_MASK (0xff << WINED3D_SM4_SWIZZLE_SHIFT)
43 enum wined3d_sm4_opcode
45 WINED3D_SM4_OP_ADD = 0x00,
46 WINED3D_SM4_OP_EXP = 0x19,
47 WINED3D_SM4_OP_MOV = 0x36,
48 WINED3D_SM4_OP_MUL = 0x38,
49 WINED3D_SM4_OP_RET = 0x3e,
50 WINED3D_SM4_OP_SINCOS = 0x4d,
53 enum wined3d_sm4_register_type
55 WINED3D_SM4_RT_TEMP = 0x0,
56 WINED3D_SM4_RT_INPUT = 0x1,
57 WINED3D_SM4_RT_OUTPUT = 0x2,
58 WINED3D_SM4_RT_IMMCONST = 0x4,
61 enum wined3d_sm4_immconst_type
63 WINED3D_SM4_IMMCONST_FLOAT = 0x1,
64 WINED3D_SM4_IMMCONST_FLOAT4 = 0x2,
67 struct wined3d_sm4_data
72 struct wined3d_sm4_opcode_info
74 enum wined3d_sm4_opcode opcode;
75 enum WINED3D_SHADER_INSTRUCTION_HANDLER handler_idx;
80 static const struct wined3d_sm4_opcode_info opcode_table[] =
82 {WINED3D_SM4_OP_ADD, WINED3DSIH_ADD, 1, 2},
83 {WINED3D_SM4_OP_EXP, WINED3DSIH_EXP, 1, 1},
84 {WINED3D_SM4_OP_MOV, WINED3DSIH_MOV, 1, 1},
85 {WINED3D_SM4_OP_MUL, WINED3DSIH_MUL, 1, 2},
86 {WINED3D_SM4_OP_RET, WINED3DSIH_RET, 0, 0},
87 {WINED3D_SM4_OP_SINCOS, WINED3DSIH_SINCOS, 1, 2},
90 static const WINED3DSHADER_PARAM_REGISTER_TYPE register_type_table[] =
92 /* WINED3D_SM4_RT_TEMP */ WINED3DSPR_TEMP,
93 /* WINED3D_SM4_RT_INPUT */ WINED3DSPR_INPUT,
94 /* WINED3D_SM4_RT_OUTPUT */ WINED3DSPR_OUTPUT,
96 /* WINED3D_SM4_RT_IMMCONST */ WINED3DSPR_IMMCONST,
99 static const struct wined3d_sm4_opcode_info *get_opcode_info(enum wined3d_sm4_opcode opcode)
103 for (i = 0; i < sizeof(opcode_table) / sizeof(*opcode_table); ++i)
105 if (opcode == opcode_table[i].opcode) return &opcode_table[i];
111 static void *shader_sm4_init(const DWORD *byte_code)
113 struct wined3d_sm4_data *priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv));
116 ERR("Failed to allocate private data\n");
123 static void shader_sm4_free(void *data)
125 HeapFree(GetProcessHeap(), 0, data);
128 static void shader_sm4_read_header(void *data, const DWORD **ptr, DWORD *shader_version)
130 struct wined3d_sm4_data *priv = data;
133 TRACE("version: 0x%08x\n", **ptr);
134 *shader_version = *(*ptr)++;
135 TRACE("token count: %u\n", **ptr);
136 priv->end += *(*ptr)++;
139 static void shader_sm4_read_opcode(void *data, const DWORD **ptr, struct wined3d_shader_instruction *ins,
140 UINT *param_size, DWORD shader_version)
142 const struct wined3d_sm4_opcode_info *opcode_info;
143 DWORD token = *(*ptr)++;
144 DWORD opcode = token & WINED3D_SM4_OPCODE_MASK;
146 *param_size = ((token & WINED3D_SM4_INSTRUCTION_LENGTH_MASK) >> WINED3D_SM4_INSTRUCTION_LENGTH_SHIFT) - 1;
148 opcode_info = get_opcode_info(opcode);
151 FIXME("Unrecognized opcode %#x, token 0x%08x\n", opcode, token);
152 ins->handler_idx = WINED3DSIH_TABLE_SIZE;
156 ins->handler_idx = opcode_info->handler_idx;
160 ins->dst_count = opcode_info->dst_count;
161 ins->src_count = opcode_info->src_count;
164 static void shader_sm4_read_src_param(const DWORD **ptr, struct wined3d_shader_src_param *src_param,
165 struct wined3d_shader_src_param *src_rel_addr, DWORD shader_version)
167 DWORD token = *(*ptr)++;
168 enum wined3d_sm4_register_type register_type;
170 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
171 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
173 FIXME("Unhandled register type %#x\n", register_type);
174 src_param->register_type = WINED3DSPR_TEMP;
178 src_param->register_type = register_type_table[register_type];
181 if (register_type == WINED3D_SM4_RT_IMMCONST)
183 enum wined3d_sm4_immconst_type immconst_type =
184 (token & WINED3D_SM4_IMMCONST_TYPE_MASK) >> WINED3D_SM4_IMMCONST_TYPE_SHIFT;
185 src_param->swizzle = WINED3DSP_NOSWIZZLE;
187 switch(immconst_type)
189 case WINED3D_SM4_IMMCONST_FLOAT:
190 src_param->immconst_type = WINED3D_IMMCONST_FLOAT;
191 memcpy(src_param->immconst_data, *ptr, 1 * sizeof(DWORD));
195 case WINED3D_SM4_IMMCONST_FLOAT4:
196 src_param->immconst_type = WINED3D_IMMCONST_FLOAT4;
197 memcpy(src_param->immconst_data, *ptr, 4 * sizeof(DWORD));
202 FIXME("Unhandled immediate constant type %#x\n", immconst_type);
208 src_param->register_idx = *(*ptr)++;
209 src_param->swizzle = (token & WINED3D_SM4_SWIZZLE_MASK) >> WINED3D_SM4_SWIZZLE_SHIFT;
212 src_param->modifiers = 0;
213 src_param->rel_addr = NULL;
216 static void shader_sm4_read_dst_param(const DWORD **ptr, struct wined3d_shader_dst_param *dst_param,
217 struct wined3d_shader_src_param *dst_rel_addr, DWORD shader_version)
219 DWORD token = *(*ptr)++;
220 UINT register_idx = *(*ptr)++;
221 enum wined3d_sm4_register_type register_type;
223 register_type = (token & WINED3D_SM4_REGISTER_TYPE_MASK) >> WINED3D_SM4_REGISTER_TYPE_SHIFT;
224 if (register_type >= sizeof(register_type_table) / sizeof(*register_type_table))
226 FIXME("Unhandled register type %#x\n", register_type);
227 dst_param->register_type = WINED3DSPR_TEMP;
231 dst_param->register_type = register_type_table[register_type];
234 dst_param->register_idx = register_idx;
235 dst_param->write_mask = (token & WINED3D_SM4_WRITEMASK_MASK) >> WINED3D_SM4_WRITEMASK_SHIFT;
236 dst_param->modifiers = 0;
237 dst_param->shift = 0;
238 dst_param->rel_addr = NULL;
241 static void shader_sm4_read_semantic(const DWORD **ptr, struct wined3d_shader_semantic *semantic)
243 FIXME("ptr %p, semantic %p stub!\n", ptr, semantic);
246 static void shader_sm4_read_comment(const DWORD **ptr, const char **comment)
248 FIXME("ptr %p, comment %p stub!\n", ptr, comment);
252 static BOOL shader_sm4_is_end(void *data, const DWORD **ptr)
254 struct wined3d_sm4_data *priv = data;
255 return *ptr == priv->end;
258 const struct wined3d_shader_frontend sm4_shader_frontend =
262 shader_sm4_read_header,
263 shader_sm4_read_opcode,
264 shader_sm4_read_src_param,
265 shader_sm4_read_dst_param,
266 shader_sm4_read_semantic,
267 shader_sm4_read_comment,