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