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