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