2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
23 #include "nine_helpers.h"
24 #include "nine_shader.h"
26 #include "vertexdeclaration9.h"
27 #include "vertexshader9.h"
30 #include "pipe/p_context.h"
31 #include "cso_cache/cso_context.h"
33 #define DBG_CHANNEL DBG_VERTEXSHADER
36 NineVertexShader9_ctor( struct NineVertexShader9
*This
,
37 struct NineUnknownParams
*pParams
,
38 const DWORD
*pFunction
, void *cso
)
40 struct NineDevice9
*device
;
41 struct nine_shader_info info
;
42 struct pipe_context
*pipe
;
46 DBG("This=%p pParams=%p pFunction=%p cso=%p\n",
47 This
, pParams
, pFunction
, cso
);
49 hr
= NineUnknown_ctor(&This
->base
, pParams
);
58 device
= This
->base
.device
;
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;
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;
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. */
78 hr
= nine_translate_shader(device
, &info
, pipe
);
80 nine_context_get_pipe_release(device
);
81 if (hr
== D3DERR_INVALIDCALL
)
82 ERR("Encountered buggy shader\n");
85 This
->byte_code
.version
= info
.version
;
86 This
->swvp_only
= info
.swvp_on
;
88 This
->byte_code
.tokens
= mem_dup(pFunction
, info
.byte_size
);
89 if (!This
->byte_code
.tokens
)
91 This
->byte_code
.size
= info
.byte_size
;
93 This
->variant
.cso
= info
.cso
;
94 This
->last_cso
= info
.cso
;
95 This
->last_key
= (uint32_t) (info
.swvp_on
<< 9);
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
;
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
;
111 NineVertexShader9_dtor( struct NineVertexShader9
*This
)
113 DBG("This=%p\n", This
);
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
;
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
);
129 while (var_so
&& var_so
->vdecl
) {
131 cso_delete_vertex_shader(This
->base
.device
->cso_sw
, var_so
->cso
);
133 var_so
= var_so
->next
;
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
);
142 nine_shader_variants_free(&This
->variant
);
143 nine_shader_variants_so_free(&This
->variant_so
);
145 FREE((void *)This
->byte_code
.tokens
); /* const_cast */
147 FREE(This
->lconstf
.data
);
148 FREE(This
->lconstf
.ranges
);
150 NineUnknown_dtor(&This
->base
);
154 NineVertexShader9_GetFunction( struct NineVertexShader9
*This
,
158 user_assert(pSizeOfData
, D3DERR_INVALIDCALL
);
161 *pSizeOfData
= This
->byte_code
.size
;
164 user_assert(*pSizeOfData
>= This
->byte_code
.size
, D3DERR_INVALIDCALL
);
166 memcpy(pData
, This
->byte_code
.tokens
, This
->byte_code
.size
);
172 NineVertexShader9_GetVariant( struct NineVertexShader9
*This
)
174 /* GetVariant is called from nine_context, thus we can
175 * get pipe directly */
176 struct pipe_context
*pipe
= This
->base
.device
->context
.pipe
;
180 key
= This
->next_key
;
181 if (key
== This
->last_key
)
182 return This
->last_cso
;
184 cso
= nine_shader_variant_get(&This
->variant
, key
);
186 struct NineDevice9
*device
= This
->base
.device
;
187 struct nine_shader_info info
;
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;
201 hr
= nine_translate_shader(This
->base
.device
, &info
, pipe
);
204 nine_shader_variant_add(&This
->variant
, key
, info
.cso
);
208 This
->last_key
= key
;
209 This
->last_cso
= cso
;
215 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9
*This
,
216 struct NineVertexDeclaration9
*vdecl_out
,
217 struct pipe_stream_output_info
*so
)
219 struct nine_shader_info info
;
223 cso
= nine_shader_variant_so_get(&This
->variant_so
, vdecl_out
, so
);
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;
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
);
242 nine_shader_variant_so_add(&This
->variant_so
, vdecl_out
, so
, info
.cso
);
246 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable
= {
247 (void *)NineUnknown_QueryInterface
,
248 (void *)NineUnknown_AddRef
,
249 (void *)NineUnknown_Release
,
250 (void *)NineUnknown_GetDevice
,
251 (void *)NineVertexShader9_GetFunction
254 static const GUID
*NineVertexShader9_IIDs
[] = {
255 &IID_IDirect3DVertexShader9
,
261 NineVertexShader9_new( struct NineDevice9
*pDevice
,
262 struct NineVertexShader9
**ppOut
,
263 const DWORD
*pFunction
, void *cso
)
266 NINE_DEVICE_CHILD_BIND_NEW(VertexShader9
, ppOut
, pDevice
, pFunction
, cso
);
268 NINE_DEVICE_CHILD_NEW(VertexShader9
, ppOut
, pDevice
, pFunction
, cso
);