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
33HRESULT
34NinePixelShader9_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.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;
67    info.swvp_on = false;
68
69    pipe = nine_context_get_pipe_acquire(device);
70    hr = nine_translate_shader(device, &info, pipe);
71    nine_context_get_pipe_release(device);
72    if (FAILED(hr))
73        return hr;
74    This->byte_code.version = info.version;
75
76    This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
77    if (!This->byte_code.tokens)
78        return E_OUTOFMEMORY;
79    This->byte_code.size = info.byte_size;
80
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;
87    This->last_key = 0;
88
89    This->sampler_mask = info.sampler_mask;
90    This->rt_mask = info.rt_mask;
91    This->bumpenvmat_needed = info.bumpenvmat_needed;
92
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));
95
96    This->const_int_slots = info.const_int_slots;
97    This->const_bool_slots = info.const_bool_slots;
98
99    This->c_combinations = NULL;
100
101    /* no constant relative addressing for ps */
102    assert(info.lconstf.data == NULL);
103    assert(info.lconstf.ranges == NULL);
104
105    return D3D_OK;
106}
107
108void
109NinePixelShader9_dtor( struct NinePixelShader9 *This )
110{
111    DBG("This=%p\n", This);
112
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;
116
117        do {
118            if (var->cso) {
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);
123            }
124            var = var->next;
125        } while (var);
126
127        if (This->ff_cso) {
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);
131        }
132    }
133    nine_shader_variants_free(&This->variant);
134
135    nine_shader_constant_combination_free(This->c_combinations);
136
137    FREE((void *)This->byte_code.tokens); /* const_cast */
138
139    NineUnknown_dtor(&This->base);
140}
141
142HRESULT NINE_WINAPI
143NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
144                              void *pData,
145                              UINT *pSizeOfData )
146{
147    DBG("This=%p pData=%p pSizeOfData=%p\n", This, pData, pSizeOfData);
148
149    user_assert(pSizeOfData, D3DERR_INVALIDCALL);
150
151    if (!pData) {
152        *pSizeOfData = This->byte_code.size;
153        return D3D_OK;
154    }
155    user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
156
157    memcpy(pData, This->byte_code.tokens, This->byte_code.size);
158
159    return D3D_OK;
160}
161
162void *
163NinePixelShader9_GetVariant( struct NinePixelShader9 *This,
164                             unsigned **const_ranges,
165                             unsigned *const_used_size )
166{
167    /* GetVariant is called from nine_context, thus we can
168     * get pipe directly */
169    struct pipe_context *pipe = This->base.device->context.pipe;
170    void *cso;
171    uint64_t key;
172
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;
178    }
179
180    cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key);
181    if (!cso) {
182        struct NineDevice9 *device = This->base.device;
183        struct nine_shader_info info;
184        HRESULT hr;
185
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;
196            } else {
197                info.sampler_ps1xtypes = (key >> 6) & 0xfff;
198                info.projected = 0;
199            }
200        } else {
201            info.sampler_ps1xtypes = 0;
202            info.projected = 0;
203        }
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;
213
214        hr = nine_translate_shader(This->base.device, &info, pipe);
215        if (FAILED(hr))
216            return NULL;
217        nine_shader_variant_add(&This->variant, key, info.cso,
218                                info.const_ranges, info.const_used_size);
219        cso = info.cso;
220        *const_ranges = info.const_ranges;
221        *const_used_size = info.const_used_size;
222    }
223
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;
228
229    return cso;
230}
231
232IDirect3DPixelShader9Vtbl NinePixelShader9_vtable = {
233    (void *)NineUnknown_QueryInterface,
234    (void *)NineUnknown_AddRef,
235    (void *)NineUnknown_Release,
236    (void *)NineUnknown_GetDevice,
237    (void *)NinePixelShader9_GetFunction
238};
239
240static const GUID *NinePixelShader9_IIDs[] = {
241    &IID_IDirect3DPixelShader9,
242    &IID_IUnknown,
243    NULL
244};
245
246HRESULT
247NinePixelShader9_new( struct NineDevice9 *pDevice,
248                      struct NinePixelShader9 **ppOut,
249                      const DWORD *pFunction, void *cso )
250{
251    if (cso) { /* ff shader. Needs to start with bind count */
252        NINE_DEVICE_CHILD_BIND_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
253    } else {
254        NINE_DEVICE_CHILD_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
255    }
256}
257