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                     pipe->bind_fs_state(pipe, NULL);
122                 pipe->delete_fs_state(pipe, var->cso);
123                 FREE(var->const_ranges);
124             }
125             var = var->next;
126         } while (var);
127 
128         if (This->ff_cso) {
129             if (This->ff_cso == This->base.device->context.cso_shader.ps)
130                 pipe->bind_fs_state(pipe, NULL);
131             pipe->delete_fs_state(pipe, This->ff_cso);
132         }
133     }
134     nine_shader_variants_free(&This->variant);
135 
136     nine_shader_constant_combination_free(This->c_combinations);
137 
138     FREE((void *)This->byte_code.tokens); /* const_cast */
139 
140     NineUnknown_dtor(&This->base);
141 }
142 
143 HRESULT NINE_WINAPI
NinePixelShader9_GetFunction(struct NinePixelShader9 * This,void * pData,UINT * pSizeOfData)144 NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
145                               void *pData,
146                               UINT *pSizeOfData )
147 {
148     DBG("This=%p pData=%p pSizeOfData=%p\n", This, pData, pSizeOfData);
149 
150     user_assert(pSizeOfData, D3DERR_INVALIDCALL);
151 
152     if (!pData) {
153         *pSizeOfData = This->byte_code.size;
154         return D3D_OK;
155     }
156     user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
157 
158     memcpy(pData, This->byte_code.tokens, This->byte_code.size);
159 
160     return D3D_OK;
161 }
162 
163 void *
NinePixelShader9_GetVariant(struct NinePixelShader9 * This,unsigned ** const_ranges,unsigned * const_used_size)164 NinePixelShader9_GetVariant( struct NinePixelShader9 *This,
165                              unsigned **const_ranges,
166                              unsigned *const_used_size )
167 {
168     /* GetVariant is called from nine_context, thus we can
169      * get pipe directly */
170     struct pipe_context *pipe = This->base.device->context.pipe;
171     void *cso;
172     uint64_t key;
173 
174     key = This->next_key;
175     if (key == This->last_key) {
176         *const_ranges = This->last_const_ranges;
177         *const_used_size = This->last_const_used_size;
178         return This->last_cso;
179     }
180 
181     cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
182     if (!cso) {
183         struct NineDevice9 *device = This->base.device;
184         struct nine_shader_info info;
185         HRESULT hr;
186 
187         info.type = PIPE_SHADER_FRAGMENT;
188         info.const_i_base = NINE_CONST_I_BASE(device->max_ps_const_f) / 16;
189         info.const_b_base = NINE_CONST_B_BASE(device->max_ps_const_f) / 16;
190         info.byte_code = This->byte_code.tokens;
191         info.sampler_mask_shadow = key & 0xffff;
192         /* intended overlap with sampler_mask_shadow */
193         if (unlikely(This->byte_code.version < 0x20)) {
194             if (This->byte_code.version < 0x14) {
195                 info.sampler_ps1xtypes = (key >> 4) & 0xff;
196                 info.projected = (key >> 12) & 0xff;
197             } else {
198                 info.sampler_ps1xtypes = (key >> 6) & 0xfff;
199                 info.projected = 0;
200             }
201         } else {
202             info.sampler_ps1xtypes = 0;
203             info.projected = 0;
204         }
205         info.fog_enable = device->context.rs[D3DRS_FOGENABLE];
206         info.fog_mode = device->context.rs[D3DRS_FOGTABLEMODE];
207         info.force_color_in_centroid = (key >> 22) & 1;
208         info.add_constants_defs.c_combination =
209             nine_shader_constant_combination_get(This->c_combinations, (key >> 24) & 0xff);
210         info.add_constants_defs.int_const_added = &This->int_slots_used;
211         info.add_constants_defs.bool_const_added = &This->bool_slots_used;
212         info.fetch4 = key >> 32 ;
213         info.process_vertices = false;
214         info.swvp_on = false;
215 
216         hr = nine_translate_shader(This->base.device, &info, pipe);
217         if (FAILED(hr))
218             return NULL;
219         nine_shader_variant_add(&This->variant, key, info.cso,
220                                 info.const_ranges, info.const_used_size);
221         cso = info.cso;
222         *const_ranges = info.const_ranges;
223         *const_used_size = info.const_used_size;
224     }
225 
226     This->last_key = key;
227     This->last_cso = cso;
228     This->last_const_ranges = *const_ranges;
229     This->last_const_used_size = *const_used_size;
230 
231     return cso;
232 }
233 
234 IDirect3DPixelShader9Vtbl NinePixelShader9_vtable = {
235     (void *)NineUnknown_QueryInterface,
236     (void *)NineUnknown_AddRef,
237     (void *)NineUnknown_Release,
238     (void *)NineUnknown_GetDevice,
239     (void *)NinePixelShader9_GetFunction
240 };
241 
242 static const GUID *NinePixelShader9_IIDs[] = {
243     &IID_IDirect3DPixelShader9,
244     &IID_IUnknown,
245     NULL
246 };
247 
248 HRESULT
NinePixelShader9_new(struct NineDevice9 * pDevice,struct NinePixelShader9 ** ppOut,const DWORD * pFunction,void * cso)249 NinePixelShader9_new( struct NineDevice9 *pDevice,
250                       struct NinePixelShader9 **ppOut,
251                       const DWORD *pFunction, void *cso )
252 {
253     if (cso) { /* ff shader. Needs to start with bind count */
254         NINE_DEVICE_CHILD_BIND_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
255     } else {
256         NINE_DEVICE_CHILD_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
257     }
258 }
259