2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
24 #include "volumetexture9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
28 #define DBG_CHANNEL DBG_VOLUMETEXTURE
31 NineVolumeTexture9_ctor( struct NineVolumeTexture9
*This
,
32 struct NineUnknownParams
*pParams
,
33 UINT Width
, UINT Height
, UINT Depth
, UINT Levels
,
37 HANDLE
*pSharedHandle
)
39 struct pipe_resource
*info
= &This
->base
.base
.info
;
40 struct pipe_screen
*screen
= pParams
->device
->screen
;
43 D3DVOLUME_DESC voldesc
;
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
);
51 user_assert(Width
&& Height
&& Depth
, D3DERR_INVALIDCALL
);
53 /* user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL); */
54 user_assert(!pSharedHandle
, D3DERR_INVALIDCALL
); /* TODO */
56 /* An IDirect3DVolume9 cannot be bound as a render target can it ? */
57 user_assert(!(Usage
& (D3DUSAGE_RENDERTARGET
| D3DUSAGE_DEPTHSTENCIL
)),
59 user_assert(!(Usage
& D3DUSAGE_AUTOGENMIPMAP
), D3DERR_INVALIDCALL
);
61 pf
= d3d9_to_pipe_format_checked(screen
, Format
, PIPE_TEXTURE_3D
, 0,
62 PIPE_BIND_SAMPLER_VIEW
, FALSE
,
63 Pool
== D3DPOOL_SCRATCH
);
65 if (pf
== PIPE_FORMAT_NONE
)
66 return D3DERR_INVALIDCALL
;
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
;
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
);
79 info
->screen
= pParams
->device
->screen
;
80 info
->target
= PIPE_TEXTURE_3D
;
83 info
->height0
= Height
;
86 info
->last_level
= Levels
- 1;
88 info
->last_level
= util_logbase2(MAX2(MAX2(Width
, Height
), Depth
));
91 info
->nr_storage_samples
= 0;
92 info
->bind
= PIPE_BIND_SAMPLER_VIEW
;
93 info
->usage
= PIPE_USAGE_DEFAULT
;
96 if (Usage
& D3DUSAGE_DYNAMIC
) {
97 info
->usage
= PIPE_USAGE_DYNAMIC
;
99 if (Usage
& D3DUSAGE_SOFTWAREPROCESSING
)
100 DBG("Application asked for Software Vertex Processing, "
101 "but this is unimplemented\n");
103 This
->volumes
= CALLOC(info
->last_level
+ 1, sizeof(*This
->volumes
));
105 return E_OUTOFMEMORY
;
106 This
->base
.pstype
= 3;
108 hr
= NineBaseTexture9_ctor(&This
->base
, pParams
, NULL
,
109 D3DRTYPE_VOLUMETEXTURE
, Format
, Pool
, Usage
);
113 voldesc
.Format
= Format
;
114 voldesc
.Type
= D3DRTYPE_VOLUME
;
115 voldesc
.Usage
= Usage
;
117 for (l
= 0; l
<= info
->last_level
; ++l
) {
118 voldesc
.Width
= u_minify(Width
, l
);
119 voldesc
.Height
= u_minify(Height
, l
);
120 voldesc
.Depth
= u_minify(Depth
, l
);
122 hr
= NineVolume9_new(This
->base
.base
.base
.device
, NineUnknown(This
),
123 This
->base
.base
.resource
, l
,
124 &voldesc
, &This
->volumes
[l
]);
129 /* Textures start initially dirty */
130 NineVolumeTexture9_AddDirtyBox(This
, NULL
);
136 NineVolumeTexture9_dtor( struct NineVolumeTexture9
*This
)
140 DBG("This=%p\n", This
);
143 for (l
= 0; l
<= This
->base
.base
.info
.last_level
; ++l
)
144 if (This
->volumes
[l
])
145 NineUnknown_Destroy(&This
->volumes
[l
]->base
);
149 NineBaseTexture9_dtor(&This
->base
);
153 NineVolumeTexture9_GetLevelDesc( struct NineVolumeTexture9
*This
,
155 D3DVOLUME_DESC
*pDesc
)
157 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
159 *pDesc
= This
->volumes
[Level
]->desc
;
165 NineVolumeTexture9_GetVolumeLevel( struct NineVolumeTexture9
*This
,
167 IDirect3DVolume9
**ppVolumeLevel
)
169 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
171 NineUnknown_AddRef(NineUnknown(This
->volumes
[Level
]));
172 *ppVolumeLevel
= (IDirect3DVolume9
*)This
->volumes
[Level
];
178 NineVolumeTexture9_LockBox( struct NineVolumeTexture9
*This
,
180 D3DLOCKED_BOX
*pLockedVolume
,
184 DBG("This=%p Level=%u pLockedVolume=%p pBox=%p Flags=%d\n",
185 This
, Level
, pLockedVolume
, pBox
, Flags
);
187 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
189 return NineVolume9_LockBox(This
->volumes
[Level
], pLockedVolume
, pBox
,
194 NineVolumeTexture9_UnlockBox( struct NineVolumeTexture9
*This
,
197 DBG("This=%p Level=%u\n", This
, Level
);
199 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
201 return NineVolume9_UnlockBox(This
->volumes
[Level
]);
205 NineVolumeTexture9_AddDirtyBox( struct NineVolumeTexture9
*This
,
206 const D3DBOX
*pDirtyBox
)
208 DBG("This=%p pDirtybox=%p\n", This
, pDirtyBox
);
210 if (This
->base
.base
.pool
== D3DPOOL_DEFAULT
) {
214 if (This
->base
.base
.pool
== D3DPOOL_MANAGED
) {
215 This
->base
.managed
.dirty
= TRUE
;
216 BASETEX_REGISTER_UPDATE(&This
->base
);
220 This
->dirty_box
.x
= 0;
221 This
->dirty_box
.y
= 0;
222 This
->dirty_box
.z
= 0;
223 This
->dirty_box
.width
= This
->base
.base
.info
.width0
;
224 This
->dirty_box
.height
= This
->base
.base
.info
.height0
;
225 This
->dirty_box
.depth
= This
->base
.base
.info
.depth0
;
227 if (This
->dirty_box
.width
== 0) {
228 d3dbox_to_pipe_box(&This
->dirty_box
, pDirtyBox
);
231 d3dbox_to_pipe_box(&box
, pDirtyBox
);
232 u_box_union_3d(&This
->dirty_box
, &This
->dirty_box
, &box
);
234 This
->dirty_box
.x
= MAX2(This
->dirty_box
.x
, 0);
235 This
->dirty_box
.y
= MAX2(This
->dirty_box
.y
, 0);
236 This
->dirty_box
.z
= MAX2(This
->dirty_box
.z
, 0);
237 This
->dirty_box
.width
= MIN2(This
->dirty_box
.width
,
238 This
->base
.base
.info
.width0
- This
->dirty_box
.x
);
239 This
->dirty_box
.height
= MIN2(This
->dirty_box
.height
,
240 This
->base
.base
.info
.height0
- This
->dirty_box
.y
);
241 This
->dirty_box
.depth
= MIN2(This
->dirty_box
.depth
,
242 This
->base
.base
.info
.depth0
- This
->dirty_box
.z
);
247 IDirect3DVolumeTexture9Vtbl NineVolumeTexture9_vtable
= {
248 (void *)NineUnknown_QueryInterface
,
249 (void *)NineUnknown_AddRef
,
250 (void *)NineUnknown_Release
,
251 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
252 (void *)NineUnknown_SetPrivateData
,
253 (void *)NineUnknown_GetPrivateData
,
254 (void *)NineUnknown_FreePrivateData
,
255 (void *)NineResource9_SetPriority
,
256 (void *)NineResource9_GetPriority
,
257 (void *)NineBaseTexture9_PreLoad
,
258 (void *)NineResource9_GetType
,
259 (void *)NineBaseTexture9_SetLOD
,
260 (void *)NineBaseTexture9_GetLOD
,
261 (void *)NineBaseTexture9_GetLevelCount
,
262 (void *)NineBaseTexture9_SetAutoGenFilterType
,
263 (void *)NineBaseTexture9_GetAutoGenFilterType
,
264 (void *)NineBaseTexture9_GenerateMipSubLevels
,
265 (void *)NineVolumeTexture9_GetLevelDesc
,
266 (void *)NineVolumeTexture9_GetVolumeLevel
,
267 (void *)NineVolumeTexture9_LockBox
,
268 (void *)NineVolumeTexture9_UnlockBox
,
269 (void *)NineVolumeTexture9_AddDirtyBox
272 static const GUID
*NineVolumeTexture9_IIDs
[] = {
273 &IID_IDirect3DVolumeTexture9
,
274 &IID_IDirect3DBaseTexture9
,
275 &IID_IDirect3DResource9
,
281 NineVolumeTexture9_new( struct NineDevice9
*pDevice
,
282 UINT Width
, UINT Height
, UINT Depth
, UINT Levels
,
286 struct NineVolumeTexture9
**ppOut
,
287 HANDLE
*pSharedHandle
)
289 NINE_DEVICE_CHILD_NEW(VolumeTexture9
, ppOut
, pDevice
,
290 Width
, Height
, Depth
, Levels
,
291 Usage
, Format
, Pool
, pSharedHandle
);