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
);
130 pipe_resource_reference(&This
->resource
, NULL
);
132 NineUnknown_dtor(&This
->base
);
136 NineVolume9_GetContainer( struct NineVolume9
*This
,
140 if (!NineUnknown(This
)->container
)
141 return E_NOINTERFACE
;
142 return NineUnknown_QueryInterface(NineUnknown(This
)->container
, riid
, ppContainer
);
146 NineVolume9_MarkContainerDirty( struct NineVolume9
*This
)
148 struct NineBaseTexture9
*tex
;
150 /* This is always contained by a NineVolumeTexture9. */
151 GUID id
= IID_IDirect3DVolumeTexture9
;
153 assert(NineUnknown_QueryInterface(This
->base
.container
, ref
, (void **)&tex
)
155 assert(NineUnknown_Release(NineUnknown(tex
)) != 0);
158 tex
= NineBaseTexture9(This
->base
.container
);
160 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
163 if (This
->desc
.Usage
& D3DUSAGE_AUTOGENMIPMAP
)
164 tex
->dirty_mip
= TRUE
;
166 BASETEX_REGISTER_UPDATE(tex
);
170 NineVolume9_GetDesc( struct NineVolume9
*This
,
171 D3DVOLUME_DESC
*pDesc
)
173 user_assert(pDesc
!= NULL
, E_POINTER
);
178 static INLINE boolean
179 NineVolume9_IsDirty(struct NineVolume9
*This
)
181 return This
->dirty_box
[0].width
!= 0;
185 NineVolume9_AddDirtyRegion( struct NineVolume9
*This
,
186 const struct pipe_box
*box
)
188 struct pipe_box cover_a
, cover_b
;
192 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
,
193 This
->desc
.Depth
, &This
->dirty_box
[0]);
194 memset(&This
->dirty_box
[1], 0, sizeof(This
->dirty_box
[1]));
197 if (!This
->dirty_box
[0].width
) {
198 This
->dirty_box
[0] = *box
;
202 u_box_union_3d(&cover_a
, &This
->dirty_box
[0], box
);
203 vol
[0] = u_box_volume_3d(&cover_a
);
205 if (This
->dirty_box
[1].width
== 0) {
206 vol
[1] = u_box_volume_3d(&This
->dirty_box
[0]);
207 if (vol
[0] > (vol
[1] * 1.5f
))
208 This
->dirty_box
[1] = *box
;
210 This
->dirty_box
[0] = cover_a
;
212 u_box_union_3d(&cover_b
, &This
->dirty_box
[1], box
);
213 vol
[1] = u_box_volume_3d(&cover_b
);
216 This
->dirty_box
[1] = cover_b
;
218 This
->dirty_box
[0] = cover_a
;
222 static INLINE
uint8_t *
223 NineVolume9_GetSystemMemPointer(struct NineVolume9
*This
, int x
, int y
, int z
)
225 unsigned x_offset
= util_format_get_stride(This
->info
.format
, x
);
227 y
= util_format_get_nblocksy(This
->info
.format
, y
);
230 return This
->data
+ (z
* This
->layer_stride
+ y
* This
->stride
+ x_offset
);
234 NineVolume9_LockBox( struct NineVolume9
*This
,
235 D3DLOCKED_BOX
*pLockedVolume
,
239 struct pipe_resource
*resource
= This
->resource
;
243 DBG("This=%p(%p) pLockedVolume=%p pBox=%p[%u..%u,%u..%u,%u..%u] Flags=%s\n",
244 This
, This
->base
.container
, pLockedVolume
, pBox
,
245 pBox
? pBox
->Left
: 0, pBox
? pBox
->Right
: 0,
246 pBox
? pBox
->Top
: 0, pBox
? pBox
->Bottom
: 0,
247 pBox
? pBox
->Front
: 0, pBox
? pBox
->Back
: 0,
248 nine_D3DLOCK_to_str(Flags
));
250 user_assert(This
->desc
.Pool
!= D3DPOOL_DEFAULT
||
251 (This
->desc
.Usage
& D3DUSAGE_DYNAMIC
), D3DERR_INVALIDCALL
);
253 user_assert(!((Flags
& D3DLOCK_DISCARD
) && (Flags
& D3DLOCK_READONLY
)),
256 user_assert(This
->lock_count
== 0, D3DERR_INVALIDCALL
);
257 user_assert(pLockedVolume
, E_POINTER
);
259 if (pBox
&& This
->desc
.Pool
== D3DPOOL_DEFAULT
&&
260 util_format_is_compressed(This
->info
.format
)) {
261 const unsigned w
= util_format_get_blockwidth(This
->info
.format
);
262 const unsigned h
= util_format_get_blockheight(This
->info
.format
);
263 user_assert(!(pBox
->Left
% w
) && !(pBox
->Right
% w
) &&
264 !(pBox
->Top
% h
) && !(pBox
->Bottom
% h
),
268 if (Flags
& D3DLOCK_DISCARD
) {
269 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
271 usage
= (Flags
& D3DLOCK_READONLY
) ?
272 PIPE_TRANSFER_READ
: PIPE_TRANSFER_READ_WRITE
;
274 if (Flags
& D3DLOCK_DONOTWAIT
)
275 usage
|= PIPE_TRANSFER_DONTBLOCK
;
278 d3dbox_to_pipe_box(&box
, pBox
);
279 if (u_box_clip_2d(&box
, &box
, This
->desc
.Width
, This
->desc
.Height
) < 0) {
280 DBG("Locked volume intersection empty.\n");
281 return D3DERR_INVALIDCALL
;
284 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
, This
->desc
.Depth
,
289 pLockedVolume
->RowPitch
= This
->stride
;
290 pLockedVolume
->SlicePitch
= This
->layer_stride
;
291 pLockedVolume
->pBits
=
292 NineVolume9_GetSystemMemPointer(This
, box
.x
, box
.y
, box
.z
);
294 pLockedVolume
->pBits
=
295 This
->pipe
->transfer_map(This
->pipe
, resource
, This
->level
, usage
,
296 &box
, &This
->transfer
);
297 if (!This
->transfer
) {
298 if (Flags
& D3DLOCK_DONOTWAIT
)
299 return D3DERR_WASSTILLDRAWING
;
300 return D3DERR_DRIVERINTERNALERROR
;
302 pLockedVolume
->RowPitch
= This
->transfer
->stride
;
303 pLockedVolume
->SlicePitch
= This
->transfer
->layer_stride
;
306 if (!(Flags
& (D3DLOCK_NO_DIRTY_UPDATE
| D3DLOCK_READONLY
))) {
307 NineVolume9_MarkContainerDirty(This
);
308 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
309 NineVolume9_AddDirtyRegion(This
, &box
);
317 NineVolume9_UnlockBox( struct NineVolume9
*This
)
319 DBG("This=%p lock_count=%u\n", This
, This
->lock_count
);
320 user_assert(This
->lock_count
, D3DERR_INVALIDCALL
);
321 if (This
->transfer
) {
322 This
->pipe
->transfer_unmap(This
->pipe
, This
->transfer
);
323 This
->transfer
= NULL
;
331 NineVolume9_CopyVolume( struct NineVolume9
*This
,
332 struct NineVolume9
*From
,
333 unsigned dstx
, unsigned dsty
, unsigned dstz
,
334 struct pipe_box
*pSrcBox
)
336 struct pipe_context
*pipe
= This
->pipe
;
337 struct pipe_resource
*r_dst
= This
->resource
;
338 struct pipe_resource
*r_src
= From
->resource
;
339 struct pipe_transfer
*transfer
;
340 struct pipe_box src_box
;
341 struct pipe_box dst_box
;
343 const uint8_t *p_src
;
345 DBG("This=%p From=%p dstx=%u dsty=%u dstz=%u pSrcBox=%p\n",
346 This
, From
, dstx
, dsty
, dstz
, pSrcBox
);
348 user_assert(This
->desc
.Format
== From
->desc
.Format
, D3DERR_INVALIDCALL
);
355 /* make sure it doesn't range outside the source volume */
356 user_assert(pSrcBox
->x
>= 0 &&
357 (pSrcBox
->width
- pSrcBox
->x
) <= From
->desc
.Width
&&
359 (pSrcBox
->height
- pSrcBox
->y
) <= From
->desc
.Height
&&
361 (pSrcBox
->depth
- pSrcBox
->z
) <= From
->desc
.Depth
,
368 src_box
.width
= From
->desc
.Width
;
369 src_box
.height
= From
->desc
.Height
;
370 src_box
.depth
= From
->desc
.Depth
;
373 dst_box
.width
= This
->desc
.Width
- dst_box
.x
;
374 dst_box
.height
= This
->desc
.Height
- dst_box
.y
;
375 dst_box
.depth
= This
->desc
.Depth
- dst_box
.z
;
377 user_assert(src_box
.width
<= dst_box
.width
&&
378 src_box
.height
<= dst_box
.height
&&
379 src_box
.depth
<= dst_box
.depth
, D3DERR_INVALIDCALL
);
381 dst_box
.width
= src_box
.width
;
382 dst_box
.height
= src_box
.height
;
383 dst_box
.depth
= src_box
.depth
;
385 /* Don't copy to device memory of managed resources.
386 * We don't want to download it back again later.
388 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
391 /* Don't copy from stale device memory of managed resources.
392 * Also, don't copy between system and device if we don't have to.
394 if (From
->desc
.Pool
== D3DPOOL_MANAGED
) {
395 if (!r_dst
|| NineVolume9_IsDirty(From
))
399 if (r_dst
&& r_src
) {
400 pipe
->resource_copy_region(pipe
,
402 dst_box
.x
, dst_box
.y
, dst_box
.z
,
407 p_src
= NineVolume9_GetSystemMemPointer(From
,
408 src_box
.x
, src_box
.y
, src_box
.z
);
410 pipe
->transfer_inline_write(pipe
, r_dst
, This
->level
,
411 0, /* WRITE|DISCARD are implicit */
413 From
->stride
, From
->layer_stride
);
416 p_dst
= NineVolume9_GetSystemMemPointer(This
, 0, 0, 0);
417 p_src
= pipe
->transfer_map(pipe
, r_src
, From
->level
,
419 &src_box
, &transfer
);
421 return D3DERR_DRIVERINTERNALERROR
;
423 util_copy_box(p_dst
, This
->info
.format
,
424 This
->stride
, This
->layer_stride
,
425 dst_box
.x
, dst_box
.y
, dst_box
.z
,
426 dst_box
.width
, dst_box
.height
, dst_box
.depth
,
428 transfer
->stride
, transfer
->layer_stride
,
429 src_box
.x
, src_box
.y
, src_box
.z
);
431 pipe
->transfer_unmap(pipe
, transfer
);
433 p_dst
= NineVolume9_GetSystemMemPointer(This
, 0, 0, 0);
434 p_src
= NineVolume9_GetSystemMemPointer(From
, 0, 0, 0);
436 util_copy_box(p_dst
, This
->info
.format
,
437 This
->stride
, This
->layer_stride
,
438 dst_box
.x
, dst_box
.y
, dst_box
.z
,
439 dst_box
.width
, dst_box
.height
, dst_box
.depth
,
441 From
->stride
, From
->layer_stride
,
442 src_box
.x
, src_box
.y
, src_box
.z
);
445 if (This
->desc
.Pool
== D3DPOOL_DEFAULT
||
446 This
->desc
.Pool
== D3DPOOL_MANAGED
)
447 NineVolume9_MarkContainerDirty(This
);
448 if (!r_dst
&& This
->resource
)
449 NineVolume9_AddDirtyRegion(This
, &dst_box
);
455 NineVolume9_UploadSelf( struct NineVolume9
*This
)
457 struct pipe_context
*pipe
= This
->pipe
;
458 struct pipe_resource
*res
= This
->resource
;
462 DBG("This=%p dirty=%i data=%p res=%p\n", This
, NineVolume9_IsDirty(This
),
465 assert(This
->desc
.Pool
== D3DPOOL_MANAGED
);
467 if (!NineVolume9_IsDirty(This
))
471 for (i
= 0; i
< Elements(This
->dirty_box
); ++i
) {
472 const struct pipe_box
*box
= &This
->dirty_box
[i
];
475 ptr
= NineVolume9_GetSystemMemPointer(This
, box
->x
, box
->y
, box
->z
);
477 pipe
->transfer_inline_write(pipe
, res
, This
->level
,
479 box
, ptr
, This
->stride
, This
->layer_stride
);
481 NineVolume9_ClearDirtyRegion(This
);
487 IDirect3DVolume9Vtbl NineVolume9_vtable
= {
488 (void *)NineUnknown_QueryInterface
,
489 (void *)NineUnknown_AddRef
,
490 (void *)NineUnknown_Release
,
491 (void *)NineUnknown_GetDevice
, /* actually part of Volume9 iface */
492 (void *)NineVolume9_SetPrivateData
,
493 (void *)NineVolume9_GetPrivateData
,
494 (void *)NineVolume9_FreePrivateData
,
495 (void *)NineVolume9_GetContainer
,
496 (void *)NineVolume9_GetDesc
,
497 (void *)NineVolume9_LockBox
,
498 (void *)NineVolume9_UnlockBox
501 static const GUID
*NineVolume9_IIDs
[] = {
502 &IID_IDirect3DVolume9
,
508 NineVolume9_new( struct NineDevice9
*pDevice
,
509 struct NineUnknown
*pContainer
,
510 struct pipe_resource
*pResource
,
512 D3DVOLUME_DESC
*pDesc
,
513 struct NineVolume9
**ppOut
)
515 NINE_DEVICE_CHILD_NEW(Volume9
, ppOut
, pDevice
, /* args */
516 pContainer
, pResource
, Level
, pDesc
);
520 /*** The boring stuff. TODO: Unify with Resource. ***/
523 NineVolume9_SetPrivateData( struct NineVolume9
*This
,
530 struct pheader
*header
;
531 const void *user_data
= pData
;
533 DBG("This=%p refguid=%p pData=%p SizeOfData=%d Flags=%d\n",
534 This
, refguid
, pData
, SizeOfData
, Flags
);
536 if (Flags
& D3DSPD_IUNKNOWN
)
537 user_assert(SizeOfData
== sizeof(IUnknown
*), D3DERR_INVALIDCALL
);
539 /* data consists of a header and the actual data. avoiding 2 mallocs */
540 header
= CALLOC_VARIANT_LENGTH_STRUCT(pheader
, SizeOfData
-1);
541 if (!header
) { return E_OUTOFMEMORY
; }
542 header
->unknown
= (Flags
& D3DSPD_IUNKNOWN
) ? TRUE
: FALSE
;
544 /* if the refguid already exists, delete it */
545 NineVolume9_FreePrivateData(This
, refguid
);
547 /* IUnknown special case */
548 if (header
->unknown
) {
549 /* here the pointer doesn't point to the data we want, so point at the
550 * pointer making what we eventually copy is the pointer itself */
554 header
->size
= SizeOfData
;
555 memcpy(header
->data
, user_data
, header
->size
);
557 err
= util_hash_table_set(This
->pdata
, refguid
, header
);
558 if (err
== PIPE_OK
) {
559 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header
->data
); }
564 if (err
== PIPE_ERROR_OUT_OF_MEMORY
) { return E_OUTOFMEMORY
; }
566 return D3DERR_DRIVERINTERNALERROR
;
570 NineVolume9_GetPrivateData( struct NineVolume9
*This
,
575 struct pheader
*header
;
577 user_assert(pSizeOfData
, E_POINTER
);
579 header
= util_hash_table_get(This
->pdata
, refguid
);
580 if (!header
) { return D3DERR_NOTFOUND
; }
583 *pSizeOfData
= header
->size
;
586 if (*pSizeOfData
< header
->size
) {
587 return D3DERR_MOREDATA
;
590 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header
->data
); }
591 memcpy(pData
, header
->data
, header
->size
);
597 NineVolume9_FreePrivateData( struct NineVolume9
*This
,
600 struct pheader
*header
;
602 DBG("This=%p refguid=%p\n", This
, refguid
);
604 header
= util_hash_table_get(This
->pdata
, refguid
);
605 if (!header
) { return D3DERR_NOTFOUND
; }
607 ht_guid_delete(NULL
, header
, NULL
);
608 util_hash_table_remove(This
->pdata
, refguid
);