• 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.sampler_ps1xtypes = 0x0;
66     info.fog_enable = 0;
67     info.point_size_min = 0;
68     info.point_size_max = 0;
69     info.swvp_on = !!(device->params.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING);
70     info.process_vertices = false;
71 
72     pipe = nine_context_get_pipe_acquire(device);
73     hr = nine_translate_shader(device, &info, pipe);
74     if (hr == D3DERR_INVALIDCALL &&
75         (device->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING)) {
76         /* Retry with a swvp shader. It will require swvp to be on. */
77         info.swvp_on = true;
78         hr = nine_translate_shader(device, &info, pipe);
79     }
80     nine_context_get_pipe_release(device);
81     if (hr == D3DERR_INVALIDCALL)
82         ERR("Encountered buggy shader\n");
83     if (FAILED(hr))
84         return hr;
85     This->byte_code.version = info.version;
86     This->swvp_only = info.swvp_on;
87 
88     This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
89     if (!This->byte_code.tokens)
90         return E_OUTOFMEMORY;
91     This->byte_code.size = info.byte_size;
92 
93     This->variant.cso = info.cso;
94     This->last_cso = info.cso;
95     This->last_key = (uint32_t) (info.swvp_on << 9);
96 
97     This->const_used_size = info.const_used_size;
98     This->lconstf = info.lconstf;
99     This->sampler_mask = info.sampler_mask;
100     This->position_t = info.position_t;
101     This->point_size = info.point_size;
102 
103     for (i = 0; i < info.num_inputs && i < ARRAY_SIZE(This->input_map); ++i)
104         This->input_map[i].ndecl = info.input_map[i];
105     This->num_inputs = i;
106 
107     return D3D_OK;
108 }
109 
110 void
NineVertexShader9_dtor(struct NineVertexShader9 * This)111 NineVertexShader9_dtor( struct NineVertexShader9 *This )
112 {
113     DBG("This=%p\n", This);
114 
115     if (This->base.device) {
116         struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.device);
117         struct nine_shader_variant *var = &This->variant;
118         struct nine_shader_variant_so *var_so = &This->variant_so;
119 
120         do {
121             if (var->cso) {
122                 if (This->base.device->context.cso_shader.vs == var->cso)
123                     pipe->bind_vs_state(pipe, NULL);
124                 pipe->delete_vs_state(pipe, var->cso);
125             }
126             var = var->next;
127         } while (var);
128 
129         while (var_so && var_so->vdecl) {
130             if (var_so->cso) {
131                 cso_delete_vertex_shader(This->base.device->cso_sw, var_so->cso );
132             }
133             var_so = var_so->next;
134         }
135 
136         if (This->ff_cso) {
137             if (This->ff_cso == This->base.device->context.cso_shader.vs)
138                 pipe->bind_vs_state(pipe, NULL);
139             pipe->delete_vs_state(pipe, This->ff_cso);
140         }
141     }
142     nine_shader_variants_free(&This->variant);
143     nine_shader_variants_so_free(&This->variant_so);
144 
145     FREE((void *)This->byte_code.tokens); /* const_cast */
146 
147     FREE(This->lconstf.data);
148     FREE(This->lconstf.ranges);
149 
150     NineUnknown_dtor(&This->base);
151 }
152 
153 HRESULT NINE_WINAPI
NineVertexShader9_GetFunction(struct NineVertexShader9 * This,void * pData,UINT * pSizeOfData)154 NineVertexShader9_GetFunction( struct NineVertexShader9 *This,
155                                void *pData,
156                                UINT *pSizeOfData )
157 {
158     user_assert(pSizeOfData, D3DERR_INVALIDCALL);
159 
160     if (!pData) {
161         *pSizeOfData = This->byte_code.size;
162         return D3D_OK;
163     }
164     user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
165 
166     memcpy(pData, This->byte_code.tokens, This->byte_code.size);
167 
168     return D3D_OK;
169 }
170 
171 void *
NineVertexShader9_GetVariant(struct NineVertexShader9 * This)172 NineVertexShader9_GetVariant( struct NineVertexShader9 *This )
173 {
174     /* GetVariant is called from nine_context, thus we can
175      * get pipe directly */
176     struct pipe_context *pipe = This->base.device->context.pipe;
177     void *cso;
178     uint64_t key;
179 
180     key = This->next_key;
181     if (key == This->last_key)
182         return This->last_cso;
183 
184     cso = nine_shader_variant_get(&This->variant, key);
185     if (!cso) {
186         struct NineDevice9 *device = This->base.device;
187         struct nine_shader_info info;
188         HRESULT hr;
189 
190         info.type = PIPE_SHADER_VERTEX;
191         info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
192         info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
193         info.byte_code = This->byte_code.tokens;
194         info.sampler_mask_shadow = key & 0xf;
195         info.fog_enable = device->context.rs[D3DRS_FOGENABLE];
196         info.point_size_min = asfloat(device->context.rs[D3DRS_POINTSIZE_MIN]);
197         info.point_size_max = asfloat(device->context.rs[D3DRS_POINTSIZE_MAX]);
198         info.swvp_on = device->context.swvp;
199         info.process_vertices = false;
200 
201         hr = nine_translate_shader(This->base.device, &info, pipe);
202         if (FAILED(hr))
203             return NULL;
204         nine_shader_variant_add(&This->variant, key, info.cso);
205         cso = info.cso;
206     }
207 
208     This->last_key = key;
209     This->last_cso = cso;
210 
211     return cso;
212 }
213 
214 void *
NineVertexShader9_GetVariantProcessVertices(struct NineVertexShader9 * This,struct NineVertexDeclaration9 * vdecl_out,struct pipe_stream_output_info * so)215 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9 *This,
216                                              struct NineVertexDeclaration9 *vdecl_out,
217                                              struct pipe_stream_output_info *so )
218 {
219     struct nine_shader_info info;
220     HRESULT hr;
221     void *cso;
222 
223     cso = nine_shader_variant_so_get(&This->variant_so, vdecl_out, so);
224     if (cso)
225         return cso;
226 
227     info.type = PIPE_SHADER_VERTEX;
228     info.const_i_base = 0;
229     info.const_b_base = 0;
230     info.byte_code = This->byte_code.tokens;
231     info.sampler_mask_shadow = 0;
232     info.fog_enable = false;
233     info.point_size_min = 0;
234     info.point_size_max = 0;
235     info.swvp_on = true;
236     info.vdecl_out = vdecl_out;
237     info.process_vertices = true;
238     hr = nine_translate_shader(This->base.device, &info, This->base.device->pipe_sw);
239     if (FAILED(hr))
240         return NULL;
241     *so = info.so;
242     nine_shader_variant_so_add(&This->variant_so, vdecl_out, so, info.cso);
243     return info.cso;
244 }
245 
246 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable = {
247     (void *)NineUnknown_QueryInterface,
248     (void *)NineUnknown_AddRef,
249     (void *)NineUnknown_Release,
250     (void *)NineUnknown_GetDevice,
251     (void *)NineVertexShader9_GetFunction
252 };
253 
254 static const GUID *NineVertexShader9_IIDs[] = {
255     &IID_IDirect3DVertexShader9,
256     &IID_IUnknown,
257     NULL
258 };
259 
260 HRESULT
NineVertexShader9_new(struct NineDevice9 * pDevice,struct NineVertexShader9 ** ppOut,const DWORD * pFunction,void * cso)261 NineVertexShader9_new( struct NineDevice9 *pDevice,
262                        struct NineVertexShader9 **ppOut,
263                        const DWORD *pFunction, void *cso )
264 {
265     NINE_DEVICE_CHILD_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
266 }
267