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