1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "nine_helpers.h"
24 #include "nine_shader.h"
25
26 #include "vertexdeclaration9.h"
27 #include "vertexshader9.h"
28
29 #include "device9.h"
30 #include "pipe/p_context.h"
31 #include "cso_cache/cso_context.h"
32
33 #define DBG_CHANNEL DBG_VERTEXSHADER
34
35 HRESULT
NineVertexShader9_ctor(struct NineVertexShader9 * This,struct NineUnknownParams * pParams,const DWORD * pFunction,void * cso)36 NineVertexShader9_ctor( struct NineVertexShader9 *This,
37 struct NineUnknownParams *pParams,
38 const DWORD *pFunction, void *cso )
39 {
40 struct NineDevice9 *device;
41 struct nine_shader_info info;
42 struct pipe_context *pipe;
43 HRESULT hr;
44 unsigned i;
45
46 DBG("This=%p pParams=%p pFunction=%p cso=%p\n",
47 This, pParams, pFunction, cso);
48
49 hr = NineUnknown_ctor(&This->base, pParams);
50 if (FAILED(hr))
51 return hr;
52
53 if (cso) {
54 This->ff_cso = cso;
55 return D3D_OK;
56 }
57
58 device = This->base.device;
59
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.fetch4 = 0x0;
66 info.sampler_ps1xtypes = 0x0;
67 info.fog_enable = 0;
68 info.point_size_min = 0;
69 info.point_size_max = 0;
70 info.clip_plane_emulation = 0;
71 info.add_constants_defs.c_combination = NULL;
72 info.add_constants_defs.int_const_added = NULL;
73 info.add_constants_defs.bool_const_added = NULL;
74 info.swvp_on = !!(device->params.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING);
75 info.process_vertices = false;
76
77 pipe = nine_context_get_pipe_acquire(device);
78 hr = nine_translate_shader(device, &info, pipe);
79 if (hr == D3DERR_INVALIDCALL &&
80 (device->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING)) {
81 /* Retry with a swvp shader. It will require swvp to be on. */
82 info.swvp_on = true;
83 hr = nine_translate_shader(device, &info, pipe);
84 }
85 nine_context_get_pipe_release(device);
86 if (hr == D3DERR_INVALIDCALL)
87 ERR("Encountered buggy shader\n");
88 if (FAILED(hr))
89 return hr;
90 This->byte_code.version = info.version;
91 This->swvp_only = info.swvp_on;
92
93 This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
94 if (!This->byte_code.tokens)
95 return E_OUTOFMEMORY;
96 This->byte_code.size = info.byte_size;
97
98 This->variant.cso = info.cso;
99 This->variant.const_ranges = info.const_ranges;
100 This->variant.const_used_size = info.const_used_size;
101 This->last_cso = info.cso;
102 This->last_const_ranges = info.const_ranges;
103 This->last_const_used_size = info.const_used_size;
104 This->last_key = (uint32_t) (info.swvp_on << 9);
105
106 This->lconstf = info.lconstf;
107 This->sampler_mask = info.sampler_mask;
108 This->position_t = info.position_t;
109 This->point_size = info.point_size;
110
111 memcpy(This->int_slots_used, info.int_slots_used, sizeof(This->int_slots_used));
112 memcpy(This->bool_slots_used, info.bool_slots_used, sizeof(This->bool_slots_used));
113
114 This->const_int_slots = info.const_int_slots;
115 This->const_bool_slots = info.const_bool_slots;
116
117 This->c_combinations = NULL;
118
119 for (i = 0; i < info.num_inputs && i < ARRAY_SIZE(This->input_map); ++i)
120 This->input_map[i].ndecl = info.input_map[i];
121 This->num_inputs = i;
122
123 return D3D_OK;
124 }
125
126 void
NineVertexShader9_dtor(struct NineVertexShader9 * This)127 NineVertexShader9_dtor( struct NineVertexShader9 *This )
128 {
129 DBG("This=%p\n", This);
130
131 if (This->base.device) {
132 struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.device);
133 struct nine_shader_variant *var = &This->variant;
134 struct nine_shader_variant_so *var_so = &This->variant_so;
135
136 do {
137 if (var->cso) {
138 if (This->base.device->context.cso_shader.vs == var->cso) {
139 /* unbind because it is illegal to delete something bound */
140 pipe->bind_vs_state(pipe, NULL);
141 /* This will rebind cso_shader.vs in case somehow actually
142 * an identical shader with same cso is bound */
143 This->base.device->context.commit |= NINE_STATE_COMMIT_VS;
144 }
145 pipe->delete_vs_state(pipe, var->cso);
146 FREE(var->const_ranges);
147 }
148 var = var->next;
149 } while (var);
150
151 while (var_so && var_so->vdecl) {
152 if (var_so->cso) {
153 This->base.device->pipe_sw->delete_vs_state(This->base.device->pipe_sw, var_so->cso);
154 }
155 var_so = var_so->next;
156 }
157
158 if (This->ff_cso) {
159 if (This->ff_cso == This->base.device->context.cso_shader.vs) {
160 pipe->bind_vs_state(pipe, NULL);
161 This->base.device->context.commit |= NINE_STATE_COMMIT_VS;
162 }
163 pipe->delete_vs_state(pipe, This->ff_cso);
164 }
165 }
166 nine_shader_variants_free(&This->variant);
167 nine_shader_variants_so_free(&This->variant_so);
168
169 nine_shader_constant_combination_free(This->c_combinations);
170
171 FREE((void *)This->byte_code.tokens); /* const_cast */
172
173 FREE(This->lconstf.data);
174 FREE(This->lconstf.ranges);
175
176 NineUnknown_dtor(&This->base);
177 }
178
179 HRESULT NINE_WINAPI
NineVertexShader9_GetFunction(struct NineVertexShader9 * This,void * pData,UINT * pSizeOfData)180 NineVertexShader9_GetFunction( struct NineVertexShader9 *This,
181 void *pData,
182 UINT *pSizeOfData )
183 {
184 user_assert(pSizeOfData, D3DERR_INVALIDCALL);
185
186 if (!pData) {
187 *pSizeOfData = This->byte_code.size;
188 return D3D_OK;
189 }
190 user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
191
192 memcpy(pData, This->byte_code.tokens, This->byte_code.size);
193
194 return D3D_OK;
195 }
196
197 void *
NineVertexShader9_GetVariant(struct NineVertexShader9 * This,unsigned ** const_ranges,unsigned * const_used_size)198 NineVertexShader9_GetVariant( struct NineVertexShader9 *This,
199 unsigned **const_ranges,
200 unsigned *const_used_size )
201 {
202 /* GetVariant is called from nine_context, thus we can
203 * get pipe directly */
204 struct pipe_context *pipe = This->base.device->context.pipe;
205 void *cso;
206 uint64_t key;
207
208 key = This->next_key;
209 if (key == This->last_key) {
210 *const_ranges = This->last_const_ranges;
211 *const_used_size = This->last_const_used_size;
212 return This->last_cso;
213 }
214
215 cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
216 if (!cso) {
217 struct NineDevice9 *device = This->base.device;
218 struct nine_shader_info info;
219 HRESULT hr;
220
221 info.type = PIPE_SHADER_VERTEX;
222 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
223 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
224 info.byte_code = This->byte_code.tokens;
225 info.sampler_mask_shadow = key & 0xf;
226 info.fetch4 = 0x0;
227 info.fog_enable = device->context.rs[D3DRS_FOGENABLE];
228 info.point_size_min = asfloat(device->context.rs[D3DRS_POINTSIZE_MIN]);
229 info.point_size_max = asfloat(device->context.rs[D3DRS_POINTSIZE_MAX]);
230 info.clip_plane_emulation = (key >> 24) & 0xff;
231 info.add_constants_defs.c_combination =
232 nine_shader_constant_combination_get(This->c_combinations, (key >> 16) & 0xff);
233 info.add_constants_defs.int_const_added = &This->int_slots_used;
234 info.add_constants_defs.bool_const_added = &This->bool_slots_used;
235 info.swvp_on = device->context.swvp;
236 info.process_vertices = false;
237
238 hr = nine_translate_shader(This->base.device, &info, pipe);
239 if (FAILED(hr))
240 return NULL;
241 nine_shader_variant_add(&This->variant, key, info.cso,
242 info.const_ranges, info.const_used_size);
243 cso = info.cso;
244 *const_ranges = info.const_ranges;
245 *const_used_size = info.const_used_size;
246 }
247
248 This->last_key = key;
249 This->last_cso = cso;
250 This->last_const_ranges = *const_ranges;
251 This->last_const_used_size = *const_used_size;
252
253 return cso;
254 }
255
256 void *
NineVertexShader9_GetVariantProcessVertices(struct NineVertexShader9 * This,struct NineVertexDeclaration9 * vdecl_out,struct pipe_stream_output_info * so)257 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9 *This,
258 struct NineVertexDeclaration9 *vdecl_out,
259 struct pipe_stream_output_info *so )
260 {
261 struct nine_shader_info info;
262 HRESULT hr;
263 void *cso;
264
265 cso = nine_shader_variant_so_get(&This->variant_so, vdecl_out, so);
266 if (cso)
267 return cso;
268
269 info.type = PIPE_SHADER_VERTEX;
270 info.const_i_base = 0;
271 info.const_b_base = 0;
272 info.byte_code = This->byte_code.tokens;
273 info.sampler_mask_shadow = 0;
274 info.fetch4 = 0x0;
275 info.fog_enable = false;
276 info.point_size_min = 0;
277 info.point_size_max = 0;
278 info.add_constants_defs.c_combination = NULL;
279 info.add_constants_defs.int_const_added = NULL;
280 info.add_constants_defs.bool_const_added = NULL;
281 info.swvp_on = true;
282 info.vdecl_out = vdecl_out;
283 info.process_vertices = true;
284 hr = nine_translate_shader(This->base.device, &info, This->base.device->pipe_sw);
285 if (FAILED(hr))
286 return NULL;
287 *so = info.so;
288 nine_shader_variant_so_add(&This->variant_so, vdecl_out, so, info.cso);
289 return info.cso;
290 }
291
292 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable = {
293 (void *)NineUnknown_QueryInterface,
294 (void *)NineUnknown_AddRef,
295 (void *)NineUnknown_Release,
296 (void *)NineUnknown_GetDevice,
297 (void *)NineVertexShader9_GetFunction
298 };
299
300 static const GUID *NineVertexShader9_IIDs[] = {
301 &IID_IDirect3DVertexShader9,
302 &IID_IUnknown,
303 NULL
304 };
305
306 HRESULT
NineVertexShader9_new(struct NineDevice9 * pDevice,struct NineVertexShader9 ** ppOut,const DWORD * pFunction,void * cso)307 NineVertexShader9_new( struct NineDevice9 *pDevice,
308 struct NineVertexShader9 **ppOut,
309 const DWORD *pFunction, void *cso )
310 {
311 if (cso) {
312 NINE_DEVICE_CHILD_BIND_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
313 } else {
314 NINE_DEVICE_CHILD_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
315 }
316 }
317