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 "pixelshader9.h"
27
28 #include "device9.h"
29 #include "pipe/p_context.h"
30
31 #define DBG_CHANNEL DBG_PIXELSHADER
32
33 HRESULT
NinePixelShader9_ctor(struct NinePixelShader9 * This,struct NineUnknownParams * pParams,const DWORD * pFunction,void * cso)34 NinePixelShader9_ctor( struct NinePixelShader9 *This,
35 struct NineUnknownParams *pParams,
36 const DWORD *pFunction, void *cso )
37 {
38 struct NineDevice9 *device;
39 struct nine_shader_info info;
40 struct pipe_context *pipe;
41 HRESULT hr;
42
43 DBG("This=%p pParams=%p pFunction=%p cso=%p\n", This, pParams, pFunction, cso);
44
45 hr = NineUnknown_ctor(&This->base, pParams);
46 if (FAILED(hr))
47 return hr;
48
49 if (cso) {
50 This->ff_cso = cso;
51 return D3D_OK;
52 }
53 device = This->base.device;
54
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.fetch4 = 0x0;
61 info.sampler_ps1xtypes = 0x0;
62 info.fog_enable = 0;
63 info.projected = 0;
64 info.add_constants_defs.c_combination = NULL;
65 info.add_constants_defs.int_const_added = NULL;
66 info.add_constants_defs.bool_const_added = NULL;
67 info.process_vertices = false;
68 info.swvp_on = false;
69
70 pipe = nine_context_get_pipe_acquire(device);
71 hr = nine_translate_shader(device, &info, pipe);
72 nine_context_get_pipe_release(device);
73 if (FAILED(hr))
74 return hr;
75 This->byte_code.version = info.version;
76
77 This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
78 if (!This->byte_code.tokens)
79 return E_OUTOFMEMORY;
80 This->byte_code.size = info.byte_size;
81
82 This->variant.cso = info.cso;
83 This->variant.const_ranges = info.const_ranges;
84 This->variant.const_used_size = info.const_used_size;
85 This->last_cso = info.cso;
86 This->last_const_ranges = info.const_ranges;
87 This->last_const_used_size = info.const_used_size;
88 This->last_key = 0;
89
90 This->sampler_mask = info.sampler_mask;
91 This->rt_mask = info.rt_mask;
92 This->bumpenvmat_needed = info.bumpenvmat_needed;
93
94 memcpy(This->int_slots_used, info.int_slots_used, sizeof(This->int_slots_used));
95 memcpy(This->bool_slots_used, info.bool_slots_used, sizeof(This->bool_slots_used));
96
97 This->const_int_slots = info.const_int_slots;
98 This->const_bool_slots = info.const_bool_slots;
99
100 This->c_combinations = NULL;
101
102 /* no constant relative addressing for ps */
103 assert(info.lconstf.data == NULL);
104 assert(info.lconstf.ranges == NULL);
105
106 return D3D_OK;
107 }
108
109 void
NinePixelShader9_dtor(struct NinePixelShader9 * This)110 NinePixelShader9_dtor( struct NinePixelShader9 *This )
111 {
112 DBG("This=%p\n", This);
113
114 if (This->base.device) {
115 struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.device);
116 struct nine_shader_variant *var = &This->variant;
117
118 do {
119 if (var->cso) {
120 if (This->base.device->context.cso_shader.ps == var->cso) {
121 /* unbind because it is illegal to delete something bound */
122 pipe->bind_fs_state(pipe, NULL);
123 /* This will rebind cso_shader.ps in case somehow actually
124 * an identical shader with same cso is bound */
125 This->base.device->context.commit |= NINE_STATE_COMMIT_PS;
126 }
127 pipe->delete_fs_state(pipe, var->cso);
128 FREE(var->const_ranges);
129 }
130 var = var->next;
131 } while (var);
132
133 if (This->ff_cso) {
134 if (This->ff_cso == This->base.device->context.cso_shader.ps) {
135 pipe->bind_fs_state(pipe, NULL);
136 This->base.device->context.commit |= NINE_STATE_COMMIT_PS;
137 }
138 pipe->delete_fs_state(pipe, This->ff_cso);
139 }
140 }
141 nine_shader_variants_free(&This->variant);
142
143 nine_shader_constant_combination_free(This->c_combinations);
144
145 FREE((void *)This->byte_code.tokens); /* const_cast */
146
147 NineUnknown_dtor(&This->base);
148 }
149
150 HRESULT NINE_WINAPI
NinePixelShader9_GetFunction(struct NinePixelShader9 * This,void * pData,UINT * pSizeOfData)151 NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
152 void *pData,
153 UINT *pSizeOfData )
154 {
155 DBG("This=%p pData=%p pSizeOfData=%p\n", This, pData, pSizeOfData);
156
157 user_assert(pSizeOfData, D3DERR_INVALIDCALL);
158
159 if (!pData) {
160 *pSizeOfData = This->byte_code.size;
161 return D3D_OK;
162 }
163 user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
164
165 memcpy(pData, This->byte_code.tokens, This->byte_code.size);
166
167 return D3D_OK;
168 }
169
170 void *
NinePixelShader9_GetVariant(struct NinePixelShader9 * This,unsigned ** const_ranges,unsigned * const_used_size)171 NinePixelShader9_GetVariant( struct NinePixelShader9 *This,
172 unsigned **const_ranges,
173 unsigned *const_used_size )
174 {
175 /* GetVariant is called from nine_context, thus we can
176 * get pipe directly */
177 struct pipe_context *pipe = This->base.device->context.pipe;
178 void *cso;
179 uint64_t key;
180
181 key = This->next_key;
182 if (key == This->last_key) {
183 *const_ranges = This->last_const_ranges;
184 *const_used_size = This->last_const_used_size;
185 return This->last_cso;
186 }
187
188 cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
189 if (!cso) {
190 struct NineDevice9 *device = This->base.device;
191 struct nine_shader_info info;
192 HRESULT hr;
193
194 info.type = PIPE_SHADER_FRAGMENT;
195 info.const_i_base = NINE_CONST_I_BASE(device->max_ps_const_f) / 16;
196 info.const_b_base = NINE_CONST_B_BASE(device->max_ps_const_f) / 16;
197 info.byte_code = This->byte_code.tokens;
198 info.sampler_mask_shadow = key & 0xffff;
199 /* intended overlap with sampler_mask_shadow */
200 if (unlikely(This->byte_code.version < 0x20)) {
201 if (This->byte_code.version < 0x14) {
202 info.sampler_ps1xtypes = (key >> 4) & 0xff;
203 info.projected = (key >> 12) & 0xff;
204 } else {
205 info.sampler_ps1xtypes = (key >> 6) & 0xfff;
206 info.projected = 0;
207 }
208 } else {
209 info.sampler_ps1xtypes = 0;
210 info.projected = 0;
211 }
212 info.fog_enable = device->context.rs[D3DRS_FOGENABLE];
213 info.fog_mode = device->context.rs[D3DRS_FOGTABLEMODE];
214 info.force_color_in_centroid = (key >> 22) & 1;
215 info.add_constants_defs.c_combination =
216 nine_shader_constant_combination_get(This->c_combinations, (key >> 24) & 0xff);
217 info.add_constants_defs.int_const_added = &This->int_slots_used;
218 info.add_constants_defs.bool_const_added = &This->bool_slots_used;
219 info.fetch4 = key >> 32 ;
220 info.process_vertices = false;
221 info.swvp_on = false;
222
223 hr = nine_translate_shader(This->base.device, &info, pipe);
224 if (FAILED(hr))
225 return NULL;
226 nine_shader_variant_add(&This->variant, key, info.cso,
227 info.const_ranges, info.const_used_size);
228 cso = info.cso;
229 *const_ranges = info.const_ranges;
230 *const_used_size = info.const_used_size;
231 }
232
233 This->last_key = key;
234 This->last_cso = cso;
235 This->last_const_ranges = *const_ranges;
236 This->last_const_used_size = *const_used_size;
237
238 return cso;
239 }
240
241 IDirect3DPixelShader9Vtbl NinePixelShader9_vtable = {
242 (void *)NineUnknown_QueryInterface,
243 (void *)NineUnknown_AddRef,
244 (void *)NineUnknown_Release,
245 (void *)NineUnknown_GetDevice,
246 (void *)NinePixelShader9_GetFunction
247 };
248
249 static const GUID *NinePixelShader9_IIDs[] = {
250 &IID_IDirect3DPixelShader9,
251 &IID_IUnknown,
252 NULL
253 };
254
255 HRESULT
NinePixelShader9_new(struct NineDevice9 * pDevice,struct NinePixelShader9 ** ppOut,const DWORD * pFunction,void * cso)256 NinePixelShader9_new( struct NineDevice9 *pDevice,
257 struct NinePixelShader9 **ppOut,
258 const DWORD *pFunction, void *cso )
259 {
260 if (cso) { /* ff shader. Needs to start with bind count */
261 NINE_DEVICE_CHILD_BIND_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
262 } else {
263 NINE_DEVICE_CHILD_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
264 }
265 }
266