OSDN Git Service

e5b2b53148de7966f8e29a3cee9d86a4c07410eb
[android-x86/external-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), D3DERR_INVALIDCALL);
55
56     user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */
57
58     pf = d3d9_to_pipe_format_checked(screen, Format, PIPE_TEXTURE_3D, 0,
59                                      PIPE_BIND_SAMPLER_VIEW, FALSE);
60     if (pf == PIPE_FORMAT_NONE)
61         return D3DERR_INVALIDCALL;
62
63     /* We support ATI1 and ATI2 hacks only for 2D textures */
64     if (Format == D3DFMT_ATI1 || Format == D3DFMT_ATI2)
65         return D3DERR_INVALIDCALL;
66
67     if (compressed_format(Format)) {
68         const unsigned w = util_format_get_blockwidth(pf);
69         const unsigned h = util_format_get_blockheight(pf);
70         /* Compressed formats are not compressed on depth component */
71         user_assert(!(Width % w) && !(Height % h), D3DERR_INVALIDCALL);
72     }
73
74     info->screen = pParams->device->screen;
75     info->target = PIPE_TEXTURE_3D;
76     info->format = pf;
77     info->width0 = Width;
78     info->height0 = Height;
79     info->depth0 = Depth;
80     if (Levels)
81         info->last_level = Levels - 1;
82     else
83         info->last_level = util_logbase2(MAX2(MAX2(Width, Height), Depth));
84     info->array_size = 1;
85     info->nr_samples = 0;
86     info->bind = PIPE_BIND_SAMPLER_VIEW;
87     info->usage = PIPE_USAGE_DEFAULT;
88     info->flags = 0;
89
90     if (Usage & D3DUSAGE_DYNAMIC) {
91         info->usage = PIPE_USAGE_DYNAMIC;
92         info->bind |=
93             PIPE_BIND_TRANSFER_READ |
94             PIPE_BIND_TRANSFER_WRITE;
95     }
96     if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
97         DBG("Application asked for Software Vertex Processing, "
98             "but this is unimplemented\n");
99
100     This->volumes = CALLOC(info->last_level + 1, sizeof(*This->volumes));
101     if (!This->volumes)
102         return E_OUTOFMEMORY;
103     This->base.pstype = 3;
104
105     hr = NineBaseTexture9_ctor(&This->base, pParams, NULL,
106                                D3DRTYPE_VOLUMETEXTURE, Format, Pool, Usage);
107     if (FAILED(hr))
108         return hr;
109
110     voldesc.Format = Format;
111     voldesc.Type = D3DRTYPE_VOLUME;
112     voldesc.Usage = Usage;
113     voldesc.Pool = Pool;
114     for (l = 0; l <= info->last_level; ++l) {
115         voldesc.Width = u_minify(Width, l);
116         voldesc.Height = u_minify(Height, l);
117         voldesc.Depth = u_minify(Depth, l);
118
119         hr = NineVolume9_new(This->base.base.base.device, NineUnknown(This),
120                              This->base.base.resource, l,
121                              &voldesc, &This->volumes[l]);
122         if (FAILED(hr))
123             return hr;
124     }
125
126     /* Textures start initially dirty */
127     NineVolumeTexture9_AddDirtyBox(This, NULL);
128
129     return D3D_OK;
130 }
131
132 static void
133 NineVolumeTexture9_dtor( struct NineVolumeTexture9 *This )
134 {
135     unsigned l;
136
137     DBG("This=%p\n", This);
138
139     if (This->volumes) {
140         for (l = 0; l < This->base.base.info.last_level; ++l)
141             NineUnknown_Destroy(&This->volumes[l]->base);
142         FREE(This->volumes);
143     }
144
145     NineBaseTexture9_dtor(&This->base);
146 }
147
148 HRESULT WINAPI
149 NineVolumeTexture9_GetLevelDesc( struct NineVolumeTexture9 *This,
150                                  UINT Level,
151                                  D3DVOLUME_DESC *pDesc )
152 {
153     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
154
155     *pDesc = This->volumes[Level]->desc;
156
157     return D3D_OK;
158 }
159
160 HRESULT WINAPI
161 NineVolumeTexture9_GetVolumeLevel( struct NineVolumeTexture9 *This,
162                                    UINT Level,
163                                    IDirect3DVolume9 **ppVolumeLevel )
164 {
165     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
166
167     NineUnknown_AddRef(NineUnknown(This->volumes[Level]));
168     *ppVolumeLevel = (IDirect3DVolume9 *)This->volumes[Level];
169
170     return D3D_OK;
171 }
172
173 HRESULT WINAPI
174 NineVolumeTexture9_LockBox( struct NineVolumeTexture9 *This,
175                             UINT Level,
176                             D3DLOCKED_BOX *pLockedVolume,
177                             const D3DBOX *pBox,
178                             DWORD Flags )
179 {
180     DBG("This=%p Level=%u pLockedVolume=%p pBox=%p Flags=%d\n",
181         This, Level, pLockedVolume, pBox, Flags);
182
183     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
184
185     return NineVolume9_LockBox(This->volumes[Level], pLockedVolume, pBox,
186                                Flags);
187 }
188
189 HRESULT WINAPI
190 NineVolumeTexture9_UnlockBox( struct NineVolumeTexture9 *This,
191                               UINT Level )
192 {
193     DBG("This=%p Level=%u\n", This, Level);
194
195     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
196
197     return NineVolume9_UnlockBox(This->volumes[Level]);
198 }
199
200 HRESULT WINAPI
201 NineVolumeTexture9_AddDirtyBox( struct NineVolumeTexture9 *This,
202                                 const D3DBOX *pDirtyBox )
203 {
204     DBG("This=%p pDirtybox=%p\n", This, pDirtyBox);
205
206     if (This->base.base.pool == D3DPOOL_DEFAULT) {
207         return D3D_OK;
208     }
209
210     if (This->base.base.pool == D3DPOOL_MANAGED) {
211         This->base.managed.dirty = TRUE;
212         BASETEX_REGISTER_UPDATE(&This->base);
213     }
214
215     if (!pDirtyBox) {
216         This->dirty_box.x = 0;
217         This->dirty_box.y = 0;
218         This->dirty_box.z = 0;
219         This->dirty_box.width = This->base.base.info.width0;
220         This->dirty_box.height = This->base.base.info.height0;
221         This->dirty_box.depth = This->base.base.info.depth0;
222     } else {
223         struct pipe_box box;
224         d3dbox_to_pipe_box(&box, pDirtyBox);
225         u_box_union_3d(&This->dirty_box, &This->dirty_box, &box);
226         This->dirty_box.x = MAX2(This->dirty_box.x, 0);
227         This->dirty_box.y = MAX2(This->dirty_box.y, 0);
228         This->dirty_box.z = MAX2(This->dirty_box.z, 0);
229         This->dirty_box.width = MIN2(This->dirty_box.width,
230                                      This->base.base.info.width0 - This->dirty_box.x);
231         This->dirty_box.height = MIN2(This->dirty_box.height,
232                                      This->base.base.info.height0 - This->dirty_box.y);
233         This->dirty_box.depth = MIN2(This->dirty_box.depth,
234                                      This->base.base.info.depth0 - This->dirty_box.z);
235     }
236     return D3D_OK;
237 }
238
239 IDirect3DVolumeTexture9Vtbl NineVolumeTexture9_vtable = {
240     (void *)NineUnknown_QueryInterface,
241     (void *)NineUnknown_AddRef,
242     (void *)NineUnknown_Release,
243     (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
244     (void *)NineResource9_SetPrivateData,
245     (void *)NineResource9_GetPrivateData,
246     (void *)NineResource9_FreePrivateData,
247     (void *)NineResource9_SetPriority,
248     (void *)NineResource9_GetPriority,
249     (void *)NineBaseTexture9_PreLoad,
250     (void *)NineResource9_GetType,
251     (void *)NineBaseTexture9_SetLOD,
252     (void *)NineBaseTexture9_GetLOD,
253     (void *)NineBaseTexture9_GetLevelCount,
254     (void *)NineBaseTexture9_SetAutoGenFilterType,
255     (void *)NineBaseTexture9_GetAutoGenFilterType,
256     (void *)NineBaseTexture9_GenerateMipSubLevels,
257     (void *)NineVolumeTexture9_GetLevelDesc,
258     (void *)NineVolumeTexture9_GetVolumeLevel,
259     (void *)NineVolumeTexture9_LockBox,
260     (void *)NineVolumeTexture9_UnlockBox,
261     (void *)NineVolumeTexture9_AddDirtyBox
262 };
263
264 static const GUID *NineVolumeTexture9_IIDs[] = {
265     &IID_IDirect3DVolumeTexture9,
266     &IID_IDirect3DBaseTexture9,
267     &IID_IDirect3DResource9,
268     &IID_IUnknown,
269     NULL
270 };
271
272 HRESULT
273 NineVolumeTexture9_new( struct NineDevice9 *pDevice,
274                         UINT Width, UINT Height, UINT Depth, UINT Levels,
275                         DWORD Usage,
276                         D3DFORMAT Format,
277                         D3DPOOL Pool,
278                         struct NineVolumeTexture9 **ppOut,
279                         HANDLE *pSharedHandle )
280 {
281     NINE_DEVICE_CHILD_NEW(VolumeTexture9, ppOut, pDevice,
282                           Width, Height, Depth, Levels,
283                           Usage, Format, Pool, pSharedHandle);
284 }
285