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 "pixelshader9.h"
29 #include "pipe/p_context.h"
31 #define DBG_CHANNEL DBG_PIXELSHADER
34 NinePixelShader9_ctor( struct NinePixelShader9
*This
,
35 struct NineUnknownParams
*pParams
,
36 const DWORD
*pFunction
, void *cso
)
38 struct NineDevice9
*device
;
39 struct nine_shader_info info
;
40 struct pipe_context
*pipe
;
43 DBG("This=%p pParams=%p pFunction=%p cso=%p\n", This
, pParams
, pFunction
, cso
);
45 hr
= NineUnknown_ctor(&This
->base
, pParams
);
53 device
= This
->base
.device
;
55 info
.type
= PIPE_SHADER_FRAGMENT
;
56 info
.byte_code
= pFunction
;
57 info
.const_i_base
= NINE_CONST_I_BASE(device
->max_ps_const_f
) / 16;
58 info
.const_b_base
= NINE_CONST_B_BASE(device
->max_ps_const_f
) / 16;
59 info
.sampler_mask_shadow
= 0x0;
60 info
.sampler_ps1xtypes
= 0x0;
63 info
.add_constants_defs
.c_combination
= NULL
;
64 info
.add_constants_defs
.int_const_added
= NULL
;
65 info
.add_constants_defs
.bool_const_added
= NULL
;
66 info
.process_vertices
= false;
69 pipe
= nine_context_get_pipe_acquire(device
);
70 hr
= nine_translate_shader(device
, &info
, pipe
);
71 nine_context_get_pipe_release(device
);
74 This
->byte_code
.version
= info
.version
;
76 This
->byte_code
.tokens
= mem_dup(pFunction
, info
.byte_size
);
77 if (!This
->byte_code
.tokens
)
79 This
->byte_code
.size
= info
.byte_size
;
81 This
->variant
.cso
= info
.cso
;
82 This
->variant
.const_ranges
= info
.const_ranges
;
83 This
->variant
.const_used_size
= info
.const_used_size
;
84 This
->last_cso
= info
.cso
;
85 This
->last_const_ranges
= info
.const_ranges
;
86 This
->last_const_used_size
= info
.const_used_size
;
89 This
->sampler_mask
= info
.sampler_mask
;
90 This
->rt_mask
= info
.rt_mask
;
91 This
->bumpenvmat_needed
= info
.bumpenvmat_needed
;
93 memcpy(This
->int_slots_used
, info
.int_slots_used
, sizeof(This
->int_slots_used
));
94 memcpy(This
->bool_slots_used
, info
.bool_slots_used
, sizeof(This
->bool_slots_used
));
96 This
->const_int_slots
= info
.const_int_slots
;
97 This
->const_bool_slots
= info
.const_bool_slots
;
99 This
->c_combinations
= NULL
;
101 /* no constant relative addressing for ps */
102 assert(info
.lconstf
.data
== NULL
);
103 assert(info
.lconstf
.ranges
== NULL
);
109 NinePixelShader9_dtor( struct NinePixelShader9
*This
)
111 DBG("This=%p\n", This
);
113 if (This
->base
.device
) {
114 struct pipe_context
*pipe
= nine_context_get_pipe_multithread(This
->base
.device
);
115 struct nine_shader_variant
*var
= &This
->variant
;
119 if (This
->base
.device
->context
.cso_shader
.ps
== var
->cso
)
120 pipe
->bind_fs_state(pipe
, NULL
);
121 pipe
->delete_fs_state(pipe
, var
->cso
);
122 FREE(var
->const_ranges
);
128 if (This
->ff_cso
== This
->base
.device
->context
.cso_shader
.ps
)
129 pipe
->bind_fs_state(pipe
, NULL
);
130 pipe
->delete_fs_state(pipe
, This
->ff_cso
);
133 nine_shader_variants_free(&This
->variant
);
135 nine_shader_constant_combination_free(This
->c_combinations
);
137 FREE((void *)This
->byte_code
.tokens
); /* const_cast */
139 NineUnknown_dtor(&This
->base
);
143 NinePixelShader9_GetFunction( struct NinePixelShader9
*This
,
147 DBG("This=%p pData=%p pSizeOfData=%p\n", This
, pData
, pSizeOfData
);
149 user_assert(pSizeOfData
, D3DERR_INVALIDCALL
);
152 *pSizeOfData
= This
->byte_code
.size
;
155 user_assert(*pSizeOfData
>= This
->byte_code
.size
, D3DERR_INVALIDCALL
);
157 memcpy(pData
, This
->byte_code
.tokens
, This
->byte_code
.size
);
163 NinePixelShader9_GetVariant( struct NinePixelShader9
*This
,
164 unsigned **const_ranges
,
165 unsigned *const_used_size
)
167 /* GetVariant is called from nine_context, thus we can
168 * get pipe directly */
169 struct pipe_context
*pipe
= This
->base
.device
->context
.pipe
;
173 key
= This
->next_key
;
174 if (key
== This
->last_key
) {
175 *const_ranges
= This
->last_const_ranges
;
176 *const_used_size
= This
->last_const_used_size
;
177 return This
->last_cso
;
180 cso
= nine_shader_variant_get(&This
->variant
, const_ranges
, const_used_size
, key
);
182 struct NineDevice9
*device
= This
->base
.device
;
183 struct nine_shader_info info
;
186 info
.type
= PIPE_SHADER_FRAGMENT
;
187 info
.const_i_base
= NINE_CONST_I_BASE(device
->max_ps_const_f
) / 16;
188 info
.const_b_base
= NINE_CONST_B_BASE(device
->max_ps_const_f
) / 16;
189 info
.byte_code
= This
->byte_code
.tokens
;
190 info
.sampler_mask_shadow
= key
& 0xffff;
191 /* intended overlap with sampler_mask_shadow */
192 if (unlikely(This
->byte_code
.version
< 0x20)) {
193 if (This
->byte_code
.version
< 0x14) {
194 info
.sampler_ps1xtypes
= (key
>> 4) & 0xff;
195 info
.projected
= (key
>> 12) & 0xff;
197 info
.sampler_ps1xtypes
= (key
>> 6) & 0xfff;
201 info
.sampler_ps1xtypes
= 0;
204 info
.fog_enable
= device
->context
.rs
[D3DRS_FOGENABLE
];
205 info
.fog_mode
= device
->context
.rs
[D3DRS_FOGTABLEMODE
];
206 info
.force_color_in_centroid
= (key
>> 22) & 1;
207 info
.add_constants_defs
.c_combination
=
208 nine_shader_constant_combination_get(This
->c_combinations
, (key
>> 24) & 0xff);
209 info
.add_constants_defs
.int_const_added
= &This
->int_slots_used
;
210 info
.add_constants_defs
.bool_const_added
= &This
->bool_slots_used
;
211 info
.process_vertices
= false;
212 info
.swvp_on
= false;
214 hr
= nine_translate_shader(This
->base
.device
, &info
, pipe
);
217 nine_shader_variant_add(&This
->variant
, key
, info
.cso
,
218 info
.const_ranges
, info
.const_used_size
);
220 *const_ranges
= info
.const_ranges
;
221 *const_used_size
= info
.const_used_size
;
224 This
->last_key
= key
;
225 This
->last_cso
= cso
;
226 This
->last_const_ranges
= *const_ranges
;
227 This
->last_const_used_size
= *const_used_size
;
232 IDirect3DPixelShader9Vtbl NinePixelShader9_vtable
= {
233 (void *)NineUnknown_QueryInterface
,
234 (void *)NineUnknown_AddRef
,
235 (void *)NineUnknown_Release
,
236 (void *)NineUnknown_GetDevice
,
237 (void *)NinePixelShader9_GetFunction
240 static const GUID
*NinePixelShader9_IIDs
[] = {
241 &IID_IDirect3DPixelShader9
,
247 NinePixelShader9_new( struct NineDevice9
*pDevice
,
248 struct NinePixelShader9
**ppOut
,
249 const DWORD
*pFunction
, void *cso
)
251 if (cso
) { /* ff shader. Needs to start with bind count */
252 NINE_DEVICE_CHILD_BIND_NEW(PixelShader9
, ppOut
, pDevice
, pFunction
, cso
);
254 NINE_DEVICE_CHILD_NEW(PixelShader9
, ppOut
, pDevice
, pFunction
, cso
);