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                     pipe->bind_vs_state(pipe, NULL);
139                 pipe->delete_vs_state(pipe, var->cso);
140                 FREE(var->const_ranges);
141             }
142             var = var->next;
143         } while (var);
144 
145         while (var_so && var_so->vdecl) {
146             if (var_so->cso) {
147                 This->base.device->pipe_sw->delete_vs_state(This->base.device->pipe_sw, var_so->cso);
148             }
149             var_so = var_so->next;
150         }
151 
152         if (This->ff_cso) {
153             if (This->ff_cso == This->base.device->context.cso_shader.vs)
154                 pipe->bind_vs_state(pipe, NULL);
155             pipe->delete_vs_state(pipe, This->ff_cso);
156         }
157     }
158     nine_shader_variants_free(&This->variant);
159     nine_shader_variants_so_free(&This->variant_so);
160 
161     nine_shader_constant_combination_free(This->c_combinations);
162 
163     FREE((void *)This->byte_code.tokens); /* const_cast */
164 
165     FREE(This->lconstf.data);
166     FREE(This->lconstf.ranges);
167 
168     NineUnknown_dtor(&This->base);
169 }
170 
171 HRESULT NINE_WINAPI
NineVertexShader9_GetFunction(struct NineVertexShader9 * This,void * pData,UINT * pSizeOfData)172 NineVertexShader9_GetFunction( struct NineVertexShader9 *This,
173                                void *pData,
174                                UINT *pSizeOfData )
175 {
176     user_assert(pSizeOfData, D3DERR_INVALIDCALL);
177 
178     if (!pData) {
179         *pSizeOfData = This->byte_code.size;
180         return D3D_OK;
181     }
182     user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
183 
184     memcpy(pData, This->byte_code.tokens, This->byte_code.size);
185 
186     return D3D_OK;
187 }
188 
189 void *
NineVertexShader9_GetVariant(struct NineVertexShader9 * This,unsigned ** const_ranges,unsigned * const_used_size)190 NineVertexShader9_GetVariant( struct NineVertexShader9 *This,
191                               unsigned **const_ranges,
192                               unsigned *const_used_size )
193 {
194     /* GetVariant is called from nine_context, thus we can
195      * get pipe directly */
196     struct pipe_context *pipe = This->base.device->context.pipe;
197     void *cso;
198     uint64_t key;
199 
200     key = This->next_key;
201     if (key == This->last_key) {
202         *const_ranges = This->last_const_ranges;
203         *const_used_size = This->last_const_used_size;
204         return This->last_cso;
205     }
206 
207     cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
208     if (!cso) {
209         struct NineDevice9 *device = This->base.device;
210         struct nine_shader_info info;
211         HRESULT hr;
212 
213         info.type = PIPE_SHADER_VERTEX;
214         info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
215         info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
216         info.byte_code = This->byte_code.tokens;
217         info.sampler_mask_shadow = key & 0xf;
218         info.fetch4 = 0x0;
219         info.fog_enable = device->context.rs[D3DRS_FOGENABLE];
220         info.point_size_min = asfloat(device->context.rs[D3DRS_POINTSIZE_MIN]);
221         info.point_size_max = asfloat(device->context.rs[D3DRS_POINTSIZE_MAX]);
222         info.add_constants_defs.c_combination =
223             nine_shader_constant_combination_get(This->c_combinations, (key >> 16) & 0xff);
224         info.add_constants_defs.int_const_added = &This->int_slots_used;
225         info.add_constants_defs.bool_const_added = &This->bool_slots_used;
226         info.swvp_on = device->context.swvp;
227         info.process_vertices = false;
228 
229         hr = nine_translate_shader(This->base.device, &info, pipe);
230         if (FAILED(hr))
231             return NULL;
232         nine_shader_variant_add(&This->variant, key, info.cso,
233                                 info.const_ranges, info.const_used_size);
234         cso = info.cso;
235         *const_ranges = info.const_ranges;
236         *const_used_size = info.const_used_size;
237     }
238 
239     This->last_key = key;
240     This->last_cso = cso;
241     This->last_const_ranges = *const_ranges;
242     This->last_const_used_size = *const_used_size;
243 
244     return cso;
245 }
246 
247 void *
NineVertexShader9_GetVariantProcessVertices(struct NineVertexShader9 * This,struct NineVertexDeclaration9 * vdecl_out,struct pipe_stream_output_info * so)248 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9 *This,
249                                              struct NineVertexDeclaration9 *vdecl_out,
250                                              struct pipe_stream_output_info *so )
251 {
252     struct nine_shader_info info;
253     HRESULT hr;
254     void *cso;
255 
256     cso = nine_shader_variant_so_get(&This->variant_so, vdecl_out, so);
257     if (cso)
258         return cso;
259 
260     info.type = PIPE_SHADER_VERTEX;
261     info.const_i_base = 0;
262     info.const_b_base = 0;
263     info.byte_code = This->byte_code.tokens;
264     info.sampler_mask_shadow = 0;
265     info.fetch4 = 0x0;
266     info.fog_enable = false;
267     info.point_size_min = 0;
268     info.point_size_max = 0;
269     info.add_constants_defs.c_combination = NULL;
270     info.add_constants_defs.int_const_added = NULL;
271     info.add_constants_defs.bool_const_added = NULL;
272     info.swvp_on = true;
273     info.vdecl_out = vdecl_out;
274     info.process_vertices = true;
275     hr = nine_translate_shader(This->base.device, &info, This->base.device->pipe_sw);
276     if (FAILED(hr))
277         return NULL;
278     *so = info.so;
279     nine_shader_variant_so_add(&This->variant_so, vdecl_out, so, info.cso);
280     return info.cso;
281 }
282 
283 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable = {
284     (void *)NineUnknown_QueryInterface,
285     (void *)NineUnknown_AddRef,
286     (void *)NineUnknown_Release,
287     (void *)NineUnknown_GetDevice,
288     (void *)NineVertexShader9_GetFunction
289 };
290 
291 static const GUID *NineVertexShader9_IIDs[] = {
292     &IID_IDirect3DVertexShader9,
293     &IID_IUnknown,
294     NULL
295 };
296 
297 HRESULT
NineVertexShader9_new(struct NineDevice9 * pDevice,struct NineVertexShader9 ** ppOut,const DWORD * pFunction,void * cso)298 NineVertexShader9_new( struct NineDevice9 *pDevice,
299                        struct NineVertexShader9 **ppOut,
300                        const DWORD *pFunction, void *cso )
301 {
302     if (cso) {
303         NINE_DEVICE_CHILD_BIND_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
304     } else {
305         NINE_DEVICE_CHILD_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
306     }
307 }
308