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