st/nine: Fix sm3 relative addressing for non-debug build
[mesa.git] / src / gallium / state_trackers / nine / volumetexture9.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 "device9.h"
24 #include "volumetexture9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27
28 #define DBG_CHANNEL DBG_VOLUMETEXTURE
29
30 static HRESULT
31 NineVolumeTexture9_ctor( struct NineVolumeTexture9 *This,
32 struct NineUnknownParams *pParams,
33 UINT Width, UINT Height, UINT Depth, UINT Levels,
34 DWORD Usage,
35 D3DFORMAT Format,
36 D3DPOOL Pool,
37 HANDLE *pSharedHandle )
38 {
39 struct pipe_resource *info = &This->base.base.info;
40 struct pipe_screen *screen = pParams->device->screen;
41 enum pipe_format pf;
42 unsigned l;
43 D3DVOLUME_DESC voldesc;
44 HRESULT hr;
45
46 DBG("This=%p pParams=%p Width=%u Height=%u Depth=%u Levels=%u "
47 "Usage=%d Format=%d Pool=%d pSharedHandle=%p\n",
48 This, pParams, Width, Height, Depth, Levels,
49 Usage, Format, Pool, pSharedHandle);
50
51 /* An IDirect3DVolume9 cannot be bound as a render target can it ? */
52 user_assert(!(Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)),
53 D3DERR_INVALIDCALL);
54 user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP) ||
55 (Pool != D3DPOOL_SYSTEMMEM && Levels <= 1), D3DERR_INVALIDCALL);
56
57 user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */
58
59 if (Usage & D3DUSAGE_AUTOGENMIPMAP)
60 Levels = 0;
61
62 pf = d3d9_to_pipe_format(Format);
63 if (pf == PIPE_FORMAT_NONE ||
64 !screen->is_format_supported(screen, pf, PIPE_TEXTURE_3D, 0, PIPE_BIND_SAMPLER_VIEW)) {
65 return D3DERR_INVALIDCALL;
66 }
67
68 /* We support ATI1 and ATI2 hacks only for 2D textures */
69 if (Format == D3DFMT_ATI1 || Format == D3DFMT_ATI2)
70 return D3DERR_INVALIDCALL;
71
72 info->screen = pParams->device->screen;
73 info->target = PIPE_TEXTURE_3D;
74 info->format = pf;
75 info->width0 = Width;
76 info->height0 = Height;
77 info->depth0 = Depth;
78 if (Levels)
79 info->last_level = Levels - 1;
80 else
81 info->last_level = util_logbase2(MAX2(MAX2(Width, Height), Depth));
82 info->array_size = 1;
83 info->nr_samples = 0;
84 info->bind = PIPE_BIND_SAMPLER_VIEW;
85 info->usage = PIPE_USAGE_DEFAULT;
86 info->flags = 0;
87
88 if (Usage & D3DUSAGE_DYNAMIC) {
89 info->usage = PIPE_USAGE_DYNAMIC;
90 info->bind |=
91 PIPE_BIND_TRANSFER_READ |
92 PIPE_BIND_TRANSFER_WRITE;
93 }
94
95 This->volumes = CALLOC(info->last_level + 1, sizeof(*This->volumes));
96 if (!This->volumes)
97 return E_OUTOFMEMORY;
98 This->base.pstype = 3;
99
100 hr = NineBaseTexture9_ctor(&This->base, pParams, NULL,
101 D3DRTYPE_VOLUMETEXTURE, Format, Pool, Usage);
102 if (FAILED(hr))
103 return hr;
104
105 voldesc.Format = Format;
106 voldesc.Type = D3DRTYPE_VOLUME;
107 voldesc.Usage = Usage;
108 voldesc.Pool = Pool;
109 for (l = 0; l <= info->last_level; ++l) {
110 voldesc.Width = u_minify(Width, l);
111 voldesc.Height = u_minify(Height, l);
112 voldesc.Depth = u_minify(Depth, l);
113
114 hr = NineVolume9_new(This->base.base.base.device, NineUnknown(This),
115 This->base.base.resource, l,
116 &voldesc, &This->volumes[l]);
117 if (FAILED(hr))
118 return hr;
119 }
120
121 return D3D_OK;
122 }
123
124 static void
125 NineVolumeTexture9_dtor( struct NineVolumeTexture9 *This )
126 {
127 unsigned l;
128
129 DBG("This=%p\n", This);
130
131 if (This->volumes) {
132 for (l = 0; l < This->base.base.info.last_level; ++l)
133 NineUnknown_Destroy(&This->volumes[l]->base);
134 FREE(This->volumes);
135 }
136
137 NineBaseTexture9_dtor(&This->base);
138 }
139
140 HRESULT WINAPI
141 NineVolumeTexture9_GetLevelDesc( struct NineVolumeTexture9 *This,
142 UINT Level,
143 D3DVOLUME_DESC *pDesc )
144 {
145 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
146 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
147 D3DERR_INVALIDCALL);
148
149 *pDesc = This->volumes[Level]->desc;
150
151 return D3D_OK;
152 }
153
154 HRESULT WINAPI
155 NineVolumeTexture9_GetVolumeLevel( struct NineVolumeTexture9 *This,
156 UINT Level,
157 IDirect3DVolume9 **ppVolumeLevel )
158 {
159 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
160 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
161 D3DERR_INVALIDCALL);
162
163 NineUnknown_AddRef(NineUnknown(This->volumes[Level]));
164 *ppVolumeLevel = (IDirect3DVolume9 *)This->volumes[Level];
165
166 return D3D_OK;
167 }
168
169 HRESULT WINAPI
170 NineVolumeTexture9_LockBox( struct NineVolumeTexture9 *This,
171 UINT Level,
172 D3DLOCKED_BOX *pLockedVolume,
173 const D3DBOX *pBox,
174 DWORD Flags )
175 {
176 DBG("This=%p Level=%u pLockedVolume=%p pBox=%p Flags=%d\n",
177 This, Level, pLockedVolume, pBox, Flags);
178
179 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
180 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
181 D3DERR_INVALIDCALL);
182
183 return NineVolume9_LockBox(This->volumes[Level], pLockedVolume, pBox,
184 Flags);
185 }
186
187 HRESULT WINAPI
188 NineVolumeTexture9_UnlockBox( struct NineVolumeTexture9 *This,
189 UINT Level )
190 {
191 DBG("This=%p Level=%u\n", This, Level);
192
193 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
194
195 return NineVolume9_UnlockBox(This->volumes[Level]);
196 }
197
198 HRESULT WINAPI
199 NineVolumeTexture9_AddDirtyBox( struct NineVolumeTexture9 *This,
200 const D3DBOX *pDirtyBox )
201 {
202 DBG("This=%p pDirtybox=%p\n", This, pDirtyBox);
203
204 if (This->base.base.pool != D3DPOOL_MANAGED) {
205 if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP)
206 This->base.dirty_mip = TRUE;
207 return D3D_OK;
208 }
209 This->base.dirty = TRUE;
210
211 BASETEX_REGISTER_UPDATE(&This->base);
212
213 if (!pDirtyBox) {
214 This->dirty_box.x = 0;
215 This->dirty_box.y = 0;
216 This->dirty_box.z = 0;
217 This->dirty_box.width = This->base.base.info.width0;
218 This->dirty_box.height = This->base.base.info.height0;
219 This->dirty_box.depth = This->base.base.info.depth0;
220 } else {
221 struct pipe_box box;
222 d3dbox_to_pipe_box(&box, pDirtyBox);
223 u_box_union_3d(&This->dirty_box, &This->dirty_box, &box);
224 }
225 return D3D_OK;
226 }
227
228 IDirect3DVolumeTexture9Vtbl NineVolumeTexture9_vtable = {
229 (void *)NineUnknown_QueryInterface,
230 (void *)NineUnknown_AddRef,
231 (void *)NineUnknown_Release,
232 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
233 (void *)NineResource9_SetPrivateData,
234 (void *)NineResource9_GetPrivateData,
235 (void *)NineResource9_FreePrivateData,
236 (void *)NineResource9_SetPriority,
237 (void *)NineResource9_GetPriority,
238 (void *)NineBaseTexture9_PreLoad,
239 (void *)NineResource9_GetType,
240 (void *)NineBaseTexture9_SetLOD,
241 (void *)NineBaseTexture9_GetLOD,
242 (void *)NineBaseTexture9_GetLevelCount,
243 (void *)NineBaseTexture9_SetAutoGenFilterType,
244 (void *)NineBaseTexture9_GetAutoGenFilterType,
245 (void *)NineBaseTexture9_GenerateMipSubLevels,
246 (void *)NineVolumeTexture9_GetLevelDesc,
247 (void *)NineVolumeTexture9_GetVolumeLevel,
248 (void *)NineVolumeTexture9_LockBox,
249 (void *)NineVolumeTexture9_UnlockBox,
250 (void *)NineVolumeTexture9_AddDirtyBox
251 };
252
253 static const GUID *NineVolumeTexture9_IIDs[] = {
254 &IID_IDirect3DVolumeTexture9,
255 &IID_IDirect3DBaseTexture9,
256 &IID_IDirect3DResource9,
257 &IID_IUnknown,
258 NULL
259 };
260
261 HRESULT
262 NineVolumeTexture9_new( struct NineDevice9 *pDevice,
263 UINT Width, UINT Height, UINT Depth, UINT Levels,
264 DWORD Usage,
265 D3DFORMAT Format,
266 D3DPOOL Pool,
267 struct NineVolumeTexture9 **ppOut,
268 HANDLE *pSharedHandle )
269 {
270 NINE_DEVICE_CHILD_NEW(VolumeTexture9, ppOut, pDevice,
271 Width, Height, Depth, Levels,
272 Usage, Format, Pool, pSharedHandle);
273 }
274