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. */
25 #include "basetexture9.h" /* for marking dirty */
26 #include "nine_helpers.h"
27 #include "nine_pipe.h"
28 #include "nine_dump.h"
30 #include "util/u_hash_table.h"
31 #include "util/u_format.h"
32 #include "util/u_surface.h"
33 #include "nine_pdata.h"
35 #define DBG_CHANNEL DBG_VOLUME
39 NineVolume9_AllocateData( struct NineVolume9
*This
)
41 unsigned size
= This
->layer_stride
* This
->desc
.Depth
;
43 DBG("(%p(This=%p),level=%u) Allocating 0x%x bytes of system memory.\n",
44 This
->base
.container
, This
, This
->level
, size
);
46 This
->data
= (uint8_t *)MALLOC(size
);
53 NineVolume9_ctor( struct NineVolume9
*This
,
54 struct NineUnknownParams
*pParams
,
55 struct NineUnknown
*pContainer
,
56 struct pipe_resource
*pResource
,
58 D3DVOLUME_DESC
*pDesc
)
62 assert(pContainer
); /* stand-alone volumes can't be created */
64 DBG("This=%p pContainer=%p pDevice=%p pResource=%p Level=%u pDesc=%p\n",
65 This
, pContainer
, pParams
->device
, pResource
, Level
, pDesc
);
67 /* Mark this as a special surface held by another internal resource. */
68 pParams
->container
= pContainer
;
70 user_assert(!(pDesc
->Usage
& D3DUSAGE_DYNAMIC
) ||
71 (pDesc
->Pool
!= D3DPOOL_MANAGED
), D3DERR_INVALIDCALL
);
73 assert(pResource
|| pDesc
->Pool
!= D3DPOOL_DEFAULT
);
75 hr
= NineUnknown_ctor(&This
->base
, pParams
);
79 This
->pdata
= util_hash_table_create(ht_guid_hash
, ht_guid_compare
);
83 pipe_resource_reference(&This
->resource
, pResource
);
85 This
->pipe
= pParams
->device
->pipe
;
86 This
->transfer
= NULL
;
90 This
->level_actual
= Level
;
93 This
->info
.screen
= pParams
->device
->screen
;
94 This
->info
.target
= PIPE_TEXTURE_3D
;
95 This
->info
.format
= d3d9_to_pipe_format(pDesc
->Format
);
96 This
->info
.width0
= pDesc
->Width
;
97 This
->info
.height0
= pDesc
->Height
;
98 This
->info
.depth0
= pDesc
->Depth
;
99 This
->info
.last_level
= 0;
100 This
->info
.array_size
= 1;
101 This
->info
.nr_samples
= 0;
102 This
->info
.usage
= PIPE_USAGE_DEFAULT
;
103 This
->info
.bind
= PIPE_BIND_SAMPLER_VIEW
;
104 This
->info
.flags
= 0;
106 This
->stride
= util_format_get_stride(This
->info
.format
, pDesc
->Width
);
107 This
->stride
= align(This
->stride
, 4);
108 This
->layer_stride
= util_format_get_2d_size(This
->info
.format
,
109 This
->stride
, pDesc
->Height
);
111 if (pDesc
->Pool
== D3DPOOL_SYSTEMMEM
)
112 This
->info
.usage
= PIPE_USAGE_STAGING
;
114 if (!This
->resource
) {
115 hr
= NineVolume9_AllocateData(This
);
123 NineVolume9_dtor( struct NineVolume9
*This
)
125 DBG("This=%p\n", This
);
128 NineVolume9_UnlockBox(This
);
133 pipe_resource_reference(&This
->resource
, NULL
);
135 NineUnknown_dtor(&This
->base
);
139 NineVolume9_GetContainer( struct NineVolume9
*This
,
143 if (!NineUnknown(This
)->container
)
144 return E_NOINTERFACE
;
145 return NineUnknown_QueryInterface(NineUnknown(This
)->container
, riid
, ppContainer
);
149 NineVolume9_MarkContainerDirty( struct NineVolume9
*This
)
151 struct NineBaseTexture9
*tex
;
153 /* This is always contained by a NineVolumeTexture9. */
154 GUID id
= IID_IDirect3DVolumeTexture9
;
156 assert(NineUnknown_QueryInterface(This
->base
.container
, ref
, (void **)&tex
)
158 assert(NineUnknown_Release(NineUnknown(tex
)) != 0);
161 tex
= NineBaseTexture9(This
->base
.container
);
163 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
166 if (This
->desc
.Usage
& D3DUSAGE_AUTOGENMIPMAP
)
167 tex
->dirty_mip
= TRUE
;
169 BASETEX_REGISTER_UPDATE(tex
);
173 NineVolume9_GetDesc( struct NineVolume9
*This
,
174 D3DVOLUME_DESC
*pDesc
)
176 user_assert(pDesc
!= NULL
, E_POINTER
);
181 static INLINE boolean
182 NineVolume9_IsDirty(struct NineVolume9
*This
)
184 return This
->dirty_box
[0].width
!= 0;
188 NineVolume9_AddDirtyRegion( struct NineVolume9
*This
,
189 const struct pipe_box
*box
)
191 struct pipe_box cover_a
, cover_b
;
195 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
,
196 This
->desc
.Depth
, &This
->dirty_box
[0]);
197 memset(&This
->dirty_box
[1], 0, sizeof(This
->dirty_box
[1]));
200 if (!This
->dirty_box
[0].width
) {
201 This
->dirty_box
[0] = *box
;
205 u_box_union_3d(&cover_a
, &This
->dirty_box
[0], box
);
206 vol
[0] = u_box_volume_3d(&cover_a
);
208 if (This
->dirty_box
[1].width
== 0) {
209 vol
[1] = u_box_volume_3d(&This
->dirty_box
[0]);
210 if (vol
[0] > (vol
[1] * 1.5f
))
211 This
->dirty_box
[1] = *box
;
213 This
->dirty_box
[0] = cover_a
;
215 u_box_union_3d(&cover_b
, &This
->dirty_box
[1], box
);
216 vol
[1] = u_box_volume_3d(&cover_b
);
219 This
->dirty_box
[1] = cover_b
;
221 This
->dirty_box
[0] = cover_a
;
225 static INLINE
uint8_t *
226 NineVolume9_GetSystemMemPointer(struct NineVolume9
*This
, int x
, int y
, int z
)
228 unsigned x_offset
= util_format_get_stride(This
->info
.format
, x
);
230 y
= util_format_get_nblocksy(This
->info
.format
, y
);
233 return This
->data
+ (z
* This
->layer_stride
+ y
* This
->stride
+ x_offset
);
237 NineVolume9_LockBox( struct NineVolume9
*This
,
238 D3DLOCKED_BOX
*pLockedVolume
,
242 struct pipe_resource
*resource
= This
->resource
;
246 DBG("This=%p(%p) pLockedVolume=%p pBox=%p[%u..%u,%u..%u,%u..%u] Flags=%s\n",
247 This
, This
->base
.container
, pLockedVolume
, pBox
,
248 pBox
? pBox
->Left
: 0, pBox
? pBox
->Right
: 0,
249 pBox
? pBox
->Top
: 0, pBox
? pBox
->Bottom
: 0,
250 pBox
? pBox
->Front
: 0, pBox
? pBox
->Back
: 0,
251 nine_D3DLOCK_to_str(Flags
));
253 user_assert(This
->desc
.Pool
!= D3DPOOL_DEFAULT
||
254 (This
->desc
.Usage
& D3DUSAGE_DYNAMIC
), D3DERR_INVALIDCALL
);
256 user_assert(!((Flags
& D3DLOCK_DISCARD
) && (Flags
& D3DLOCK_READONLY
)),
259 user_assert(This
->lock_count
== 0, D3DERR_INVALIDCALL
);
260 user_assert(pLockedVolume
, E_POINTER
);
262 if (pBox
&& This
->desc
.Pool
== D3DPOOL_DEFAULT
&&
263 util_format_is_compressed(This
->info
.format
)) {
264 const unsigned w
= util_format_get_blockwidth(This
->info
.format
);
265 const unsigned h
= util_format_get_blockheight(This
->info
.format
);
266 user_assert(!(pBox
->Left
% w
) && !(pBox
->Right
% w
) &&
267 !(pBox
->Top
% h
) && !(pBox
->Bottom
% h
),
271 if (Flags
& D3DLOCK_DISCARD
) {
272 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
274 usage
= (Flags
& D3DLOCK_READONLY
) ?
275 PIPE_TRANSFER_READ
: PIPE_TRANSFER_READ_WRITE
;
277 if (Flags
& D3DLOCK_DONOTWAIT
)
278 usage
|= PIPE_TRANSFER_DONTBLOCK
;
281 d3dbox_to_pipe_box(&box
, pBox
);
282 if (u_box_clip_2d(&box
, &box
, This
->desc
.Width
, This
->desc
.Height
) < 0) {
283 DBG("Locked volume intersection empty.\n");
284 return D3DERR_INVALIDCALL
;
287 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
, This
->desc
.Depth
,
292 pLockedVolume
->RowPitch
= This
->stride
;
293 pLockedVolume
->SlicePitch
= This
->layer_stride
;
294 pLockedVolume
->pBits
=
295 NineVolume9_GetSystemMemPointer(This
, box
.x
, box
.y
, box
.z
);
297 pLockedVolume
->pBits
=
298 This
->pipe
->transfer_map(This
->pipe
, resource
, This
->level
, usage
,
299 &box
, &This
->transfer
);
300 if (!This
->transfer
) {
301 if (Flags
& D3DLOCK_DONOTWAIT
)
302 return D3DERR_WASSTILLDRAWING
;
303 return D3DERR_DRIVERINTERNALERROR
;
305 pLockedVolume
->RowPitch
= This
->transfer
->stride
;
306 pLockedVolume
->SlicePitch
= This
->transfer
->layer_stride
;
309 if (!(Flags
& (D3DLOCK_NO_DIRTY_UPDATE
| D3DLOCK_READONLY
))) {
310 NineVolume9_MarkContainerDirty(This
);
311 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
312 NineVolume9_AddDirtyRegion(This
, &box
);
320 NineVolume9_UnlockBox( struct NineVolume9
*This
)
322 DBG("This=%p lock_count=%u\n", This
, This
->lock_count
);
323 user_assert(This
->lock_count
, D3DERR_INVALIDCALL
);
324 if (This
->transfer
) {
325 This
->pipe
->transfer_unmap(This
->pipe
, This
->transfer
);
326 This
->transfer
= NULL
;
334 NineVolume9_CopyVolume( struct NineVolume9
*This
,
335 struct NineVolume9
*From
,
336 unsigned dstx
, unsigned dsty
, unsigned dstz
,
337 struct pipe_box
*pSrcBox
)
339 struct pipe_context
*pipe
= This
->pipe
;
340 struct pipe_resource
*r_dst
= This
->resource
;
341 struct pipe_resource
*r_src
= From
->resource
;
342 struct pipe_transfer
*transfer
;
343 struct pipe_box src_box
;
344 struct pipe_box dst_box
;
346 const uint8_t *p_src
;
348 DBG("This=%p From=%p dstx=%u dsty=%u dstz=%u pSrcBox=%p\n",
349 This
, From
, dstx
, dsty
, dstz
, pSrcBox
);
351 user_assert(This
->desc
.Format
== From
->desc
.Format
, D3DERR_INVALIDCALL
);
358 /* make sure it doesn't range outside the source volume */
359 user_assert(pSrcBox
->x
>= 0 &&
360 (pSrcBox
->width
- pSrcBox
->x
) <= From
->desc
.Width
&&
362 (pSrcBox
->height
- pSrcBox
->y
) <= From
->desc
.Height
&&
364 (pSrcBox
->depth
- pSrcBox
->z
) <= From
->desc
.Depth
,
371 src_box
.width
= From
->desc
.Width
;
372 src_box
.height
= From
->desc
.Height
;
373 src_box
.depth
= From
->desc
.Depth
;
376 dst_box
.width
= This
->desc
.Width
- dst_box
.x
;
377 dst_box
.height
= This
->desc
.Height
- dst_box
.y
;
378 dst_box
.depth
= This
->desc
.Depth
- dst_box
.z
;
380 user_assert(src_box
.width
<= dst_box
.width
&&
381 src_box
.height
<= dst_box
.height
&&
382 src_box
.depth
<= dst_box
.depth
, D3DERR_INVALIDCALL
);
384 dst_box
.width
= src_box
.width
;
385 dst_box
.height
= src_box
.height
;
386 dst_box
.depth
= src_box
.depth
;
388 /* Don't copy to device memory of managed resources.
389 * We don't want to download it back again later.
391 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
394 /* Don't copy from stale device memory of managed resources.
395 * Also, don't copy between system and device if we don't have to.
397 if (From
->desc
.Pool
== D3DPOOL_MANAGED
) {
398 if (!r_dst
|| NineVolume9_IsDirty(From
))
402 if (r_dst
&& r_src
) {
403 pipe
->resource_copy_region(pipe
,
405 dst_box
.x
, dst_box
.y
, dst_box
.z
,
410 p_src
= NineVolume9_GetSystemMemPointer(From
,
411 src_box
.x
, src_box
.y
, src_box
.z
);
413 pipe
->transfer_inline_write(pipe
, r_dst
, This
->level
,
414 0, /* WRITE|DISCARD are implicit */
416 From
->stride
, From
->layer_stride
);
419 p_dst
= NineVolume9_GetSystemMemPointer(This
, 0, 0, 0);
420 p_src
= pipe
->transfer_map(pipe
, r_src
, From
->level
,
422 &src_box
, &transfer
);
424 return D3DERR_DRIVERINTERNALERROR
;
426 util_copy_box(p_dst
, This
->info
.format
,
427 This
->stride
, This
->layer_stride
,
428 dst_box
.x
, dst_box
.y
, dst_box
.z
,
429 dst_box
.width
, dst_box
.height
, dst_box
.depth
,
431 transfer
->stride
, transfer
->layer_stride
,
432 src_box
.x
, src_box
.y
, src_box
.z
);
434 pipe
->transfer_unmap(pipe
, transfer
);
436 p_dst
= NineVolume9_GetSystemMemPointer(This
, 0, 0, 0);
437 p_src
= NineVolume9_GetSystemMemPointer(From
, 0, 0, 0);
439 util_copy_box(p_dst
, This
->info
.format
,
440 This
->stride
, This
->layer_stride
,
441 dst_box
.x
, dst_box
.y
, dst_box
.z
,
442 dst_box
.width
, dst_box
.height
, dst_box
.depth
,
444 From
->stride
, From
->layer_stride
,
445 src_box
.x
, src_box
.y
, src_box
.z
);
448 if (This
->desc
.Pool
== D3DPOOL_DEFAULT
||
449 This
->desc
.Pool
== D3DPOOL_MANAGED
)
450 NineVolume9_MarkContainerDirty(This
);
451 if (!r_dst
&& This
->resource
)
452 NineVolume9_AddDirtyRegion(This
, &dst_box
);
458 NineVolume9_UploadSelf( struct NineVolume9
*This
)
460 struct pipe_context
*pipe
= This
->pipe
;
461 struct pipe_resource
*res
= This
->resource
;
465 DBG("This=%p dirty=%i data=%p res=%p\n", This
, NineVolume9_IsDirty(This
),
468 assert(This
->desc
.Pool
== D3DPOOL_MANAGED
);
470 if (!NineVolume9_IsDirty(This
))
474 for (i
= 0; i
< Elements(This
->dirty_box
); ++i
) {
475 const struct pipe_box
*box
= &This
->dirty_box
[i
];
478 ptr
= NineVolume9_GetSystemMemPointer(This
, box
->x
, box
->y
, box
->z
);
480 pipe
->transfer_inline_write(pipe
, res
, This
->level
,
482 box
, ptr
, This
->stride
, This
->layer_stride
);
484 NineVolume9_ClearDirtyRegion(This
);
490 IDirect3DVolume9Vtbl NineVolume9_vtable
= {
491 (void *)NineUnknown_QueryInterface
,
492 (void *)NineUnknown_AddRef
,
493 (void *)NineUnknown_Release
,
494 (void *)NineUnknown_GetDevice
, /* actually part of Volume9 iface */
495 (void *)NineVolume9_SetPrivateData
,
496 (void *)NineVolume9_GetPrivateData
,
497 (void *)NineVolume9_FreePrivateData
,
498 (void *)NineVolume9_GetContainer
,
499 (void *)NineVolume9_GetDesc
,
500 (void *)NineVolume9_LockBox
,
501 (void *)NineVolume9_UnlockBox
504 static const GUID
*NineVolume9_IIDs
[] = {
505 &IID_IDirect3DVolume9
,
511 NineVolume9_new( struct NineDevice9
*pDevice
,
512 struct NineUnknown
*pContainer
,
513 struct pipe_resource
*pResource
,
515 D3DVOLUME_DESC
*pDesc
,
516 struct NineVolume9
**ppOut
)
518 NINE_DEVICE_CHILD_NEW(Volume9
, ppOut
, pDevice
, /* args */
519 pContainer
, pResource
, Level
, pDesc
);
523 /*** The boring stuff. TODO: Unify with Resource. ***/
526 NineVolume9_SetPrivateData( struct NineVolume9
*This
,
533 struct pheader
*header
;
534 const void *user_data
= pData
;
536 DBG("This=%p refguid=%p pData=%p SizeOfData=%d Flags=%d\n",
537 This
, refguid
, pData
, SizeOfData
, Flags
);
539 if (Flags
& D3DSPD_IUNKNOWN
)
540 user_assert(SizeOfData
== sizeof(IUnknown
*), D3DERR_INVALIDCALL
);
542 /* data consists of a header and the actual data. avoiding 2 mallocs */
543 header
= CALLOC_VARIANT_LENGTH_STRUCT(pheader
, SizeOfData
-1);
544 if (!header
) { return E_OUTOFMEMORY
; }
545 header
->unknown
= (Flags
& D3DSPD_IUNKNOWN
) ? TRUE
: FALSE
;
547 /* if the refguid already exists, delete it */
548 NineVolume9_FreePrivateData(This
, refguid
);
550 /* IUnknown special case */
551 if (header
->unknown
) {
552 /* here the pointer doesn't point to the data we want, so point at the
553 * pointer making what we eventually copy is the pointer itself */
557 header
->size
= SizeOfData
;
558 memcpy(header
->data
, user_data
, header
->size
);
560 err
= util_hash_table_set(This
->pdata
, refguid
, header
);
561 if (err
== PIPE_OK
) {
562 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header
->data
); }
567 if (err
== PIPE_ERROR_OUT_OF_MEMORY
) { return E_OUTOFMEMORY
; }
569 return D3DERR_DRIVERINTERNALERROR
;
573 NineVolume9_GetPrivateData( struct NineVolume9
*This
,
578 struct pheader
*header
;
580 user_assert(pSizeOfData
, E_POINTER
);
582 header
= util_hash_table_get(This
->pdata
, refguid
);
583 if (!header
) { return D3DERR_NOTFOUND
; }
586 *pSizeOfData
= header
->size
;
589 if (*pSizeOfData
< header
->size
) {
590 return D3DERR_MOREDATA
;
593 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header
->data
); }
594 memcpy(pData
, header
->data
, header
->size
);
600 NineVolume9_FreePrivateData( struct NineVolume9
*This
,
603 struct pheader
*header
;
605 DBG("This=%p refguid=%p\n", This
, refguid
);
607 header
= util_hash_table_get(This
->pdata
, refguid
);
608 if (!header
) { return D3DERR_NOTFOUND
; }
610 ht_guid_delete(NULL
, header
, NULL
);
611 util_hash_table_remove(This
->pdata
, refguid
);