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.sampler_ps1xtypes = 0x0;
61     info.fog_enable = 0;
62     info.projected = 0;
63     info.process_vertices = false;
64 
65     pipe = nine_context_get_pipe_acquire(device);
66     hr = nine_translate_shader(device, &info, pipe);
67     nine_context_get_pipe_release(device);
68     if (FAILED(hr))
69         return hr;
70     This->byte_code.version = info.version;
71 
72     This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
73     if (!This->byte_code.tokens)
74         return E_OUTOFMEMORY;
75     This->byte_code.size = info.byte_size;
76 
77     This->variant.cso = info.cso;
78     This->last_cso = info.cso;
79     This->last_key = 0;
80 
81     This->sampler_mask = info.sampler_mask;
82     This->rt_mask = info.rt_mask;
83     This->const_used_size = info.const_used_size;
84     This->bumpenvmat_needed = info.bumpenvmat_needed;
85     /* no constant relative addressing for ps */
86     assert(info.lconstf.data == NULL);
87     assert(info.lconstf.ranges == NULL);
88 
89     return D3D_OK;
90 }
91 
92 void
NinePixelShader9_dtor(struct NinePixelShader9 * This)93 NinePixelShader9_dtor( struct NinePixelShader9 *This )
94 {
95     DBG("This=%p\n", This);
96 
97     if (This->base.device) {
98         struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.device);
99         struct nine_shader_variant *var = &This->variant;
100 
101         do {
102             if (var->cso) {
103                 if (This->base.device->context.cso_shader.ps == var->cso)
104                     pipe->bind_fs_state(pipe, NULL);
105                 pipe->delete_fs_state(pipe, var->cso);
106             }
107             var = var->next;
108         } while (var);
109 
110         if (This->ff_cso) {
111             if (This->ff_cso == This->base.device->context.cso_shader.ps)
112                 pipe->bind_fs_state(pipe, NULL);
113             pipe->delete_fs_state(pipe, This->ff_cso);
114         }
115     }
116     nine_shader_variants_free(&This->variant);
117 
118     FREE((void *)This->byte_code.tokens); /* const_cast */
119 
120     NineUnknown_dtor(&This->base);
121 }
122 
123 HRESULT NINE_WINAPI
NinePixelShader9_GetFunction(struct NinePixelShader9 * This,void * pData,UINT * pSizeOfData)124 NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
125                               void *pData,
126                               UINT *pSizeOfData )
127 {
128     DBG("This=%p pData=%p pSizeOfData=%p\n", This, pData, pSizeOfData);
129 
130     user_assert(pSizeOfData, D3DERR_INVALIDCALL);
131 
132     if (!pData) {
133         *pSizeOfData = This->byte_code.size;
134         return D3D_OK;
135     }
136     user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
137 
138     memcpy(pData, This->byte_code.tokens, This->byte_code.size);
139 
140     return D3D_OK;
141 }
142 
143 void *
NinePixelShader9_GetVariant(struct NinePixelShader9 * This)144 NinePixelShader9_GetVariant( struct NinePixelShader9 *This )
145 {
146     /* GetVariant is called from nine_context, thus we can
147      * get pipe directly */
148     struct pipe_context *pipe = This->base.device->context.pipe;
149     void *cso;
150     uint64_t key;
151 
152     key = This->next_key;
153     if (key == This->last_key)
154         return This->last_cso;
155 
156     cso = nine_shader_variant_get(&This->variant, key);
157     if (!cso) {
158         struct NineDevice9 *device = This->base.device;
159         struct nine_shader_info info;
160         HRESULT hr;
161 
162         info.type = PIPE_SHADER_FRAGMENT;
163         info.const_i_base = NINE_CONST_I_BASE(device->max_ps_const_f) / 16;
164         info.const_b_base = NINE_CONST_B_BASE(device->max_ps_const_f) / 16;
165         info.byte_code = This->byte_code.tokens;
166         info.sampler_mask_shadow = key & 0xffff;
167         info.sampler_ps1xtypes = key;
168         info.fog_enable = device->context.rs[D3DRS_FOGENABLE];
169         info.fog_mode = device->context.rs[D3DRS_FOGTABLEMODE];
170         info.force_color_in_centroid = key >> 34 & 1;
171         info.projected = (key >> 48) & 0xffff;
172         info.process_vertices = false;
173 
174         hr = nine_translate_shader(This->base.device, &info, pipe);
175         if (FAILED(hr))
176             return NULL;
177         nine_shader_variant_add(&This->variant, key, info.cso);
178         cso = info.cso;
179     }
180 
181     This->last_key = key;
182     This->last_cso = cso;
183 
184     return cso;
185 }
186 
187 IDirect3DPixelShader9Vtbl NinePixelShader9_vtable = {
188     (void *)NineUnknown_QueryInterface,
189     (void *)NineUnknown_AddRef,
190     (void *)NineUnknown_Release,
191     (void *)NineUnknown_GetDevice,
192     (void *)NinePixelShader9_GetFunction
193 };
194 
195 static const GUID *NinePixelShader9_IIDs[] = {
196     &IID_IDirect3DPixelShader9,
197     &IID_IUnknown,
198     NULL
199 };
200 
201 HRESULT
NinePixelShader9_new(struct NineDevice9 * pDevice,struct NinePixelShader9 ** ppOut,const DWORD * pFunction,void * cso)202 NinePixelShader9_new( struct NineDevice9 *pDevice,
203                       struct NinePixelShader9 **ppOut,
204                       const DWORD *pFunction, void *cso )
205 {
206     NINE_DEVICE_CHILD_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
207 }
208