2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "nine_helpers.h"
24 #include "nine_shader.h"
26 #include "vertexdeclaration9.h"
27 #include "vertexshader9.h"
30 #include "pipe/p_context.h"
31 #include "cso_cache/cso_context.h"
33 #define DBG_CHANNEL DBG_VERTEXSHADER
36 NineVertexShader9_ctor( struct NineVertexShader9
*This
,
37 struct NineUnknownParams
*pParams
,
38 const DWORD
*pFunction
, void *cso
)
40 struct NineDevice9
*device
;
41 struct nine_shader_info info
;
42 struct pipe_context
*pipe
;
46 DBG("This=%p pParams=%p pFunction=%p cso=%p\n",
47 This
, pParams
, pFunction
, cso
);
49 hr
= NineUnknown_ctor(&This
->base
, pParams
);
58 device
= This
->base
.device
;
60 info
.type
= PIPE_SHADER_VERTEX
;
61 info
.byte_code
= pFunction
;
62 info
.const_i_base
= NINE_CONST_I_BASE(device
->max_vs_const_f
) / 16;
63 info
.const_b_base
= NINE_CONST_B_BASE(device
->max_vs_const_f
) / 16;
64 info
.sampler_mask_shadow
= 0x0;
65 info
.sampler_ps1xtypes
= 0x0;
67 info
.point_size_min
= 0;
68 info
.point_size_max
= 0;
69 info
.add_constants_defs
.c_combination
= NULL
;
70 info
.add_constants_defs
.int_const_added
= NULL
;
71 info
.add_constants_defs
.bool_const_added
= NULL
;
72 info
.swvp_on
= !!(device
->params
.BehaviorFlags
& D3DCREATE_SOFTWARE_VERTEXPROCESSING
);
73 info
.process_vertices
= false;
75 pipe
= nine_context_get_pipe_acquire(device
);
76 hr
= nine_translate_shader(device
, &info
, pipe
);
77 if (hr
== D3DERR_INVALIDCALL
&&
78 (device
->params
.BehaviorFlags
& D3DCREATE_MIXED_VERTEXPROCESSING
)) {
79 /* Retry with a swvp shader. It will require swvp to be on. */
81 hr
= nine_translate_shader(device
, &info
, pipe
);
83 nine_context_get_pipe_release(device
);
84 if (hr
== D3DERR_INVALIDCALL
)
85 ERR("Encountered buggy shader\n");
88 This
->byte_code
.version
= info
.version
;
89 This
->swvp_only
= info
.swvp_on
;
91 This
->byte_code
.tokens
= mem_dup(pFunction
, info
.byte_size
);
92 if (!This
->byte_code
.tokens
)
94 This
->byte_code
.size
= info
.byte_size
;
96 This
->variant
.cso
= info
.cso
;
97 This
->variant
.const_ranges
= info
.const_ranges
;
98 This
->variant
.const_used_size
= info
.const_used_size
;
99 This
->last_cso
= info
.cso
;
100 This
->last_const_ranges
= info
.const_ranges
;
101 This
->last_const_used_size
= info
.const_used_size
;
102 This
->last_key
= (uint32_t) (info
.swvp_on
<< 9);
104 This
->lconstf
= info
.lconstf
;
105 This
->sampler_mask
= info
.sampler_mask
;
106 This
->position_t
= info
.position_t
;
107 This
->point_size
= info
.point_size
;
109 memcpy(This
->int_slots_used
, info
.int_slots_used
, sizeof(This
->int_slots_used
));
110 memcpy(This
->bool_slots_used
, info
.bool_slots_used
, sizeof(This
->bool_slots_used
));
112 This
->const_int_slots
= info
.const_int_slots
;
113 This
->const_bool_slots
= info
.const_bool_slots
;
115 This
->c_combinations
= NULL
;
117 for (i
= 0; i
< info
.num_inputs
&& i
< ARRAY_SIZE(This
->input_map
); ++i
)
118 This
->input_map
[i
].ndecl
= info
.input_map
[i
];
119 This
->num_inputs
= i
;
125 NineVertexShader9_dtor( struct NineVertexShader9
*This
)
127 DBG("This=%p\n", This
);
129 if (This
->base
.device
) {
130 struct pipe_context
*pipe
= nine_context_get_pipe_multithread(This
->base
.device
);
131 struct nine_shader_variant
*var
= &This
->variant
;
132 struct nine_shader_variant_so
*var_so
= &This
->variant_so
;
136 if (This
->base
.device
->context
.cso_shader
.vs
== var
->cso
)
137 pipe
->bind_vs_state(pipe
, NULL
);
138 pipe
->delete_vs_state(pipe
, var
->cso
);
139 FREE(var
->const_ranges
);
144 while (var_so
&& var_so
->vdecl
) {
146 This
->base
.device
->pipe_sw
->delete_vs_state(This
->base
.device
->pipe_sw
, var_so
->cso
);
148 var_so
= var_so
->next
;
152 if (This
->ff_cso
== This
->base
.device
->context
.cso_shader
.vs
)
153 pipe
->bind_vs_state(pipe
, NULL
);
154 pipe
->delete_vs_state(pipe
, This
->ff_cso
);
157 nine_shader_variants_free(&This
->variant
);
158 nine_shader_variants_so_free(&This
->variant_so
);
160 nine_shader_constant_combination_free(This
->c_combinations
);
162 FREE((void *)This
->byte_code
.tokens
); /* const_cast */
164 FREE(This
->lconstf
.data
);
165 FREE(This
->lconstf
.ranges
);
167 NineUnknown_dtor(&This
->base
);
171 NineVertexShader9_GetFunction( struct NineVertexShader9
*This
,
175 user_assert(pSizeOfData
, D3DERR_INVALIDCALL
);
178 *pSizeOfData
= This
->byte_code
.size
;
181 user_assert(*pSizeOfData
>= This
->byte_code
.size
, D3DERR_INVALIDCALL
);
183 memcpy(pData
, This
->byte_code
.tokens
, This
->byte_code
.size
);
189 NineVertexShader9_GetVariant( struct NineVertexShader9
*This
,
190 unsigned **const_ranges
,
191 unsigned *const_used_size
)
193 /* GetVariant is called from nine_context, thus we can
194 * get pipe directly */
195 struct pipe_context
*pipe
= This
->base
.device
->context
.pipe
;
199 key
= This
->next_key
;
200 if (key
== This
->last_key
) {
201 *const_ranges
= This
->last_const_ranges
;
202 *const_used_size
= This
->last_const_used_size
;
203 return This
->last_cso
;
206 cso
= nine_shader_variant_get(&This
->variant
, const_ranges
, const_used_size
, key
);
208 struct NineDevice9
*device
= This
->base
.device
;
209 struct nine_shader_info info
;
212 info
.type
= PIPE_SHADER_VERTEX
;
213 info
.const_i_base
= NINE_CONST_I_BASE(device
->max_vs_const_f
) / 16;
214 info
.const_b_base
= NINE_CONST_B_BASE(device
->max_vs_const_f
) / 16;
215 info
.byte_code
= This
->byte_code
.tokens
;
216 info
.sampler_mask_shadow
= key
& 0xf;
217 info
.fog_enable
= device
->context
.rs
[D3DRS_FOGENABLE
];
218 info
.point_size_min
= asfloat(device
->context
.rs
[D3DRS_POINTSIZE_MIN
]);
219 info
.point_size_max
= asfloat(device
->context
.rs
[D3DRS_POINTSIZE_MAX
]);
220 info
.add_constants_defs
.c_combination
=
221 nine_shader_constant_combination_get(This
->c_combinations
, (key
>> 16) & 0xff);
222 info
.add_constants_defs
.int_const_added
= &This
->int_slots_used
;
223 info
.add_constants_defs
.bool_const_added
= &This
->bool_slots_used
;
224 info
.swvp_on
= device
->context
.swvp
;
225 info
.process_vertices
= false;
227 hr
= nine_translate_shader(This
->base
.device
, &info
, pipe
);
230 nine_shader_variant_add(&This
->variant
, key
, info
.cso
,
231 info
.const_ranges
, info
.const_used_size
);
233 *const_ranges
= info
.const_ranges
;
234 *const_used_size
= info
.const_used_size
;
237 This
->last_key
= key
;
238 This
->last_cso
= cso
;
239 This
->last_const_ranges
= *const_ranges
;
240 This
->last_const_used_size
= *const_used_size
;
246 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9
*This
,
247 struct NineVertexDeclaration9
*vdecl_out
,
248 struct pipe_stream_output_info
*so
)
250 struct nine_shader_info info
;
254 cso
= nine_shader_variant_so_get(&This
->variant_so
, vdecl_out
, so
);
258 info
.type
= PIPE_SHADER_VERTEX
;
259 info
.const_i_base
= 0;
260 info
.const_b_base
= 0;
261 info
.byte_code
= This
->byte_code
.tokens
;
262 info
.sampler_mask_shadow
= 0;
263 info
.fog_enable
= false;
264 info
.point_size_min
= 0;
265 info
.point_size_max
= 0;
266 info
.add_constants_defs
.c_combination
= NULL
;
267 info
.add_constants_defs
.int_const_added
= NULL
;
268 info
.add_constants_defs
.bool_const_added
= NULL
;
270 info
.vdecl_out
= vdecl_out
;
271 info
.process_vertices
= true;
272 hr
= nine_translate_shader(This
->base
.device
, &info
, This
->base
.device
->pipe_sw
);
276 nine_shader_variant_so_add(&This
->variant_so
, vdecl_out
, so
, info
.cso
);
280 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable
= {
281 (void *)NineUnknown_QueryInterface
,
282 (void *)NineUnknown_AddRef
,
283 (void *)NineUnknown_Release
,
284 (void *)NineUnknown_GetDevice
,
285 (void *)NineVertexShader9_GetFunction
288 static const GUID
*NineVertexShader9_IIDs
[] = {
289 &IID_IDirect3DVertexShader9
,
295 NineVertexShader9_new( struct NineDevice9
*pDevice
,
296 struct NineVertexShader9
**ppOut
,
297 const DWORD
*pFunction
, void *cso
)
300 NINE_DEVICE_CHILD_BIND_NEW(VertexShader9
, ppOut
, pDevice
, pFunction
, cso
);
302 NINE_DEVICE_CHILD_NEW(VertexShader9
, ppOut
, pDevice
, pFunction
, cso
);