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
;
45 DBG("This=%p pParams=%p pFunction=%p cso=%p\n",
46 This
, pParams
, pFunction
, cso
);
48 hr
= NineUnknown_ctor(&This
->base
, pParams
);
57 device
= This
->base
.device
;
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;
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;
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. */
76 hr
= nine_translate_shader(device
, &info
);
78 if (hr
== D3DERR_INVALIDCALL
)
79 ERR("Encountered buggy shader\n");
82 This
->byte_code
.version
= info
.version
;
83 This
->swvp_only
= info
.swvp_on
;
85 This
->byte_code
.tokens
= mem_dup(pFunction
, info
.byte_size
);
86 if (!This
->byte_code
.tokens
)
88 This
->byte_code
.size
= info
.byte_size
;
90 This
->variant
.cso
= info
.cso
;
91 This
->last_cso
= info
.cso
;
92 This
->last_key
= (uint32_t) (info
.swvp_on
<< 9);
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
;
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
;
108 NineVertexShader9_dtor( struct NineVertexShader9
*This
)
110 DBG("This=%p\n", This
);
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
;
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
);
126 while (var_so
&& var_so
->vdecl
) {
128 cso_delete_vertex_shader(This
->base
.device
->cso_sw
, var_so
->cso
);
130 var_so
= var_so
->next
;
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
);
139 nine_shader_variants_free(&This
->variant
);
140 nine_shader_variants_so_free(&This
->variant_so
);
142 FREE((void *)This
->byte_code
.tokens
); /* const_cast */
144 FREE(This
->lconstf
.data
);
145 FREE(This
->lconstf
.ranges
);
147 NineUnknown_dtor(&This
->base
);
151 NineVertexShader9_GetFunction( struct NineVertexShader9
*This
,
155 user_assert(pSizeOfData
, D3DERR_INVALIDCALL
);
158 *pSizeOfData
= This
->byte_code
.size
;
161 user_assert(*pSizeOfData
>= This
->byte_code
.size
, D3DERR_INVALIDCALL
);
163 memcpy(pData
, This
->byte_code
.tokens
, This
->byte_code
.size
);
169 NineVertexShader9_GetVariant( struct NineVertexShader9
*This
)
174 key
= This
->next_key
;
175 if (key
== This
->last_key
)
176 return This
->last_cso
;
178 cso
= nine_shader_variant_get(&This
->variant
, key
);
180 struct NineDevice9
*device
= This
->base
.device
;
181 struct nine_shader_info info
;
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;
195 hr
= nine_translate_shader(This
->base
.device
, &info
);
198 nine_shader_variant_add(&This
->variant
, key
, info
.cso
);
202 This
->last_key
= key
;
203 This
->last_cso
= cso
;
209 NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9
*This
,
210 struct NineVertexDeclaration9
*vdecl_out
,
211 struct pipe_stream_output_info
*so
)
213 struct nine_shader_info info
;
217 cso
= nine_shader_variant_so_get(&This
->variant_so
, vdecl_out
, so
);
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;
230 info
.vdecl_out
= vdecl_out
;
231 info
.process_vertices
= true;
232 hr
= nine_translate_shader(This
->base
.device
, &info
);
236 nine_shader_variant_so_add(&This
->variant_so
, vdecl_out
, so
, info
.cso
);
240 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable
= {
241 (void *)NineUnknown_QueryInterface
,
242 (void *)NineUnknown_AddRef
,
243 (void *)NineUnknown_Release
,
244 (void *)NineUnknown_GetDevice
,
245 (void *)NineVertexShader9_GetFunction
248 static const GUID
*NineVertexShader9_IIDs
[] = {
249 &IID_IDirect3DVertexShader9
,
255 NineVertexShader9_new( struct NineDevice9
*pDevice
,
256 struct NineVertexShader9
**ppOut
,
257 const DWORD
*pFunction
, void *cso
)
259 NINE_DEVICE_CHILD_NEW(VertexShader9
, ppOut
, pDevice
, pFunction
, cso
);