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