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 "volumetexture9.h"
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
29 #include "nine_dump.h"
31 #include "util/u_hash_table.h"
32 #include "util/u_format.h"
33 #include "util/u_surface.h"
34 #include "nine_pdata.h"
36 #define DBG_CHANNEL DBG_VOLUME
40 NineVolume9_AllocateData( struct NineVolume9
*This
)
42 unsigned size
= This
->layer_stride
* This
->desc
.Depth
;
44 DBG("(%p(This=%p),level=%u) Allocating 0x%x bytes of system memory.\n",
45 This
->base
.container
, This
, This
->level
, size
);
47 This
->data
= (uint8_t *)align_malloc(size
, 32);
54 NineVolume9_ctor( struct NineVolume9
*This
,
55 struct NineUnknownParams
*pParams
,
56 struct NineUnknown
*pContainer
,
57 struct pipe_resource
*pResource
,
59 D3DVOLUME_DESC
*pDesc
)
63 assert(pContainer
); /* stand-alone volumes can't be created */
65 DBG("This=%p pContainer=%p pDevice=%p pResource=%p Level=%u pDesc=%p\n",
66 This
, pContainer
, pParams
->device
, pResource
, Level
, pDesc
);
68 /* Mark this as a special surface held by another internal resource. */
69 pParams
->container
= pContainer
;
71 user_assert(!(pDesc
->Usage
& D3DUSAGE_DYNAMIC
) ||
72 (pDesc
->Pool
!= D3DPOOL_MANAGED
), D3DERR_INVALIDCALL
);
74 assert(pResource
|| pDesc
->Pool
!= D3DPOOL_DEFAULT
);
76 hr
= NineUnknown_ctor(&This
->base
, pParams
);
80 This
->pdata
= util_hash_table_create(ht_guid_hash
, ht_guid_compare
);
84 pipe_resource_reference(&This
->resource
, pResource
);
86 This
->pipe
= pParams
->device
->pipe
;
87 This
->transfer
= NULL
;
91 This
->level_actual
= Level
;
94 This
->info
.screen
= pParams
->device
->screen
;
95 This
->info
.target
= PIPE_TEXTURE_3D
;
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;
105 This
->info
.format
= d3d9_to_pipe_format_checked(This
->info
.screen
,
108 This
->info
.nr_samples
,
109 This
->info
.bind
, FALSE
);
111 if (This
->info
.format
== PIPE_FORMAT_NONE
)
112 return D3DERR_DRIVERINTERNALERROR
;
114 This
->stride
= util_format_get_stride(This
->info
.format
, pDesc
->Width
);
115 This
->stride
= align(This
->stride
, 4);
116 This
->layer_stride
= util_format_get_2d_size(This
->info
.format
,
117 This
->stride
, pDesc
->Height
);
119 if (pDesc
->Pool
== D3DPOOL_SYSTEMMEM
)
120 This
->info
.usage
= PIPE_USAGE_STAGING
;
122 if (!This
->resource
) {
123 hr
= NineVolume9_AllocateData(This
);
131 NineVolume9_dtor( struct NineVolume9
*This
)
133 DBG("This=%p\n", This
);
136 NineVolume9_UnlockBox(This
);
141 pipe_resource_reference(&This
->resource
, NULL
);
143 NineUnknown_dtor(&This
->base
);
147 NineVolume9_GetContainer( struct NineVolume9
*This
,
151 if (!NineUnknown(This
)->container
)
152 return E_NOINTERFACE
;
153 return NineUnknown_QueryInterface(NineUnknown(This
)->container
, riid
, ppContainer
);
157 NineVolume9_MarkContainerDirty( struct NineVolume9
*This
)
159 struct NineBaseTexture9
*tex
;
161 /* This is always contained by a NineVolumeTexture9. */
162 GUID id
= IID_IDirect3DVolumeTexture9
;
164 assert(NineUnknown_QueryInterface(This
->base
.container
, ref
, (void **)&tex
)
166 assert(NineUnknown_Release(NineUnknown(tex
)) != 0);
169 tex
= NineBaseTexture9(This
->base
.container
);
171 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
172 tex
->managed
.dirty
= TRUE
;
174 BASETEX_REGISTER_UPDATE(tex
);
178 NineVolume9_GetDesc( struct NineVolume9
*This
,
179 D3DVOLUME_DESC
*pDesc
)
181 user_assert(pDesc
!= NULL
, E_POINTER
);
187 NineVolume9_AddDirtyRegion( struct NineVolume9
*This
,
188 const struct pipe_box
*box
)
191 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
->base
.container
);
194 NineVolumeTexture9_AddDirtyBox(tex
, NULL
);
196 dirty_region
.Left
= box
->x
<< This
->level_actual
;
197 dirty_region
.Top
= box
->y
<< This
->level_actual
;
198 dirty_region
.Front
= box
->z
<< This
->level_actual
;
199 dirty_region
.Right
= dirty_region
.Left
+ (box
->width
<< This
->level_actual
);
200 dirty_region
.Bottom
= dirty_region
.Top
+ (box
->height
<< This
->level_actual
);
201 dirty_region
.Back
= dirty_region
.Front
+ (box
->depth
<< This
->level_actual
);
202 NineVolumeTexture9_AddDirtyBox(tex
, &dirty_region
);
206 static inline uint8_t *
207 NineVolume9_GetSystemMemPointer(struct NineVolume9
*This
, int x
, int y
, int z
)
209 unsigned x_offset
= util_format_get_stride(This
->info
.format
, x
);
211 y
= util_format_get_nblocksy(This
->info
.format
, y
);
214 return This
->data
+ (z
* This
->layer_stride
+ y
* This
->stride
+ x_offset
);
218 NineVolume9_LockBox( struct NineVolume9
*This
,
219 D3DLOCKED_BOX
*pLockedVolume
,
223 struct pipe_resource
*resource
= This
->resource
;
227 DBG("This=%p(%p) pLockedVolume=%p pBox=%p[%u..%u,%u..%u,%u..%u] Flags=%s\n",
228 This
, This
->base
.container
, pLockedVolume
, pBox
,
229 pBox
? pBox
->Left
: 0, pBox
? pBox
->Right
: 0,
230 pBox
? pBox
->Top
: 0, pBox
? pBox
->Bottom
: 0,
231 pBox
? pBox
->Front
: 0, pBox
? pBox
->Back
: 0,
232 nine_D3DLOCK_to_str(Flags
));
234 /* check if it's already locked */
235 user_assert(This
->lock_count
== 0, D3DERR_INVALIDCALL
);
237 /* set pBits to NULL after lock_count check */
238 user_assert(pLockedVolume
, E_POINTER
);
239 pLockedVolume
->pBits
= NULL
;
241 user_assert(This
->desc
.Pool
!= D3DPOOL_DEFAULT
||
242 (This
->desc
.Usage
& D3DUSAGE_DYNAMIC
), D3DERR_INVALIDCALL
);
244 user_assert(!((Flags
& D3DLOCK_DISCARD
) && (Flags
& D3DLOCK_READONLY
)),
247 if (pBox
&& compressed_format (This
->desc
.Format
)) { /* For volume all pools are checked */
248 const unsigned w
= util_format_get_blockwidth(This
->info
.format
);
249 const unsigned h
= util_format_get_blockheight(This
->info
.format
);
250 user_assert((pBox
->Left
== 0 && pBox
->Right
== This
->desc
.Width
&&
251 pBox
->Top
== 0 && pBox
->Bottom
== This
->desc
.Height
) ||
252 (!(pBox
->Left
% w
) && !(pBox
->Right
% w
) &&
253 !(pBox
->Top
% h
) && !(pBox
->Bottom
% h
)),
257 if (Flags
& D3DLOCK_DISCARD
) {
258 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
260 usage
= (Flags
& D3DLOCK_READONLY
) ?
261 PIPE_TRANSFER_READ
: PIPE_TRANSFER_READ_WRITE
;
263 if (Flags
& D3DLOCK_DONOTWAIT
)
264 usage
|= PIPE_TRANSFER_DONTBLOCK
;
267 d3dbox_to_pipe_box(&box
, pBox
);
268 if (u_box_clip_2d(&box
, &box
, This
->desc
.Width
, This
->desc
.Height
) < 0) {
269 DBG("Locked volume intersection empty.\n");
270 return D3DERR_INVALIDCALL
;
273 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
, This
->desc
.Depth
,
278 pLockedVolume
->RowPitch
= This
->stride
;
279 pLockedVolume
->SlicePitch
= This
->layer_stride
;
280 pLockedVolume
->pBits
=
281 NineVolume9_GetSystemMemPointer(This
, box
.x
, box
.y
, box
.z
);
283 pLockedVolume
->pBits
=
284 This
->pipe
->transfer_map(This
->pipe
, resource
, This
->level
, usage
,
285 &box
, &This
->transfer
);
286 if (!This
->transfer
) {
287 if (Flags
& D3DLOCK_DONOTWAIT
)
288 return D3DERR_WASSTILLDRAWING
;
289 return D3DERR_DRIVERINTERNALERROR
;
291 pLockedVolume
->RowPitch
= This
->transfer
->stride
;
292 pLockedVolume
->SlicePitch
= This
->transfer
->layer_stride
;
295 if (!(Flags
& (D3DLOCK_NO_DIRTY_UPDATE
| D3DLOCK_READONLY
))) {
296 NineVolume9_MarkContainerDirty(This
);
297 NineVolume9_AddDirtyRegion(This
, &box
);
305 NineVolume9_UnlockBox( struct NineVolume9
*This
)
307 DBG("This=%p lock_count=%u\n", This
, This
->lock_count
);
308 user_assert(This
->lock_count
, D3DERR_INVALIDCALL
);
309 if (This
->transfer
) {
310 This
->pipe
->transfer_unmap(This
->pipe
, This
->transfer
);
311 This
->transfer
= NULL
;
317 /* When this function is called, we have already checked
318 * The copy regions fit the volumes */
320 NineVolume9_CopyMemToDefault( struct NineVolume9
*This
,
321 struct NineVolume9
*From
,
322 unsigned dstx
, unsigned dsty
, unsigned dstz
,
323 struct pipe_box
*pSrcBox
)
325 struct pipe_context
*pipe
= This
->pipe
;
326 struct pipe_resource
*r_dst
= This
->resource
;
327 struct pipe_box src_box
;
328 struct pipe_box dst_box
;
329 const uint8_t *p_src
;
331 DBG("This=%p From=%p dstx=%u dsty=%u dstz=%u pSrcBox=%p\n",
332 This
, From
, dstx
, dsty
, dstz
, pSrcBox
);
334 assert(This
->desc
.Pool
== D3DPOOL_DEFAULT
&&
335 From
->desc
.Pool
== D3DPOOL_SYSTEMMEM
);
347 src_box
.width
= From
->desc
.Width
;
348 src_box
.height
= From
->desc
.Height
;
349 src_box
.depth
= From
->desc
.Depth
;
352 dst_box
.width
= src_box
.width
;
353 dst_box
.height
= src_box
.height
;
354 dst_box
.depth
= src_box
.depth
;
356 p_src
= NineVolume9_GetSystemMemPointer(From
,
357 src_box
.x
, src_box
.y
, src_box
.z
);
359 pipe
->transfer_inline_write(pipe
, r_dst
, This
->level
,
360 0, /* WRITE|DISCARD are implicit */
362 From
->stride
, From
->layer_stride
);
364 NineVolume9_MarkContainerDirty(This
);
370 NineVolume9_UploadSelf( struct NineVolume9
*This
,
371 const struct pipe_box
*damaged
)
373 struct pipe_context
*pipe
= This
->pipe
;
374 struct pipe_resource
*res
= This
->resource
;
378 DBG("This=%p damaged=%p data=%p res=%p\n", This
, damaged
,
381 assert(This
->desc
.Pool
== D3DPOOL_MANAGED
);
390 box
.width
= This
->desc
.Width
;
391 box
.height
= This
->desc
.Height
;
392 box
.depth
= This
->desc
.Depth
;
395 ptr
= NineVolume9_GetSystemMemPointer(This
, box
.x
, box
.y
, box
.z
);
397 pipe
->transfer_inline_write(pipe
, res
, This
->level
, 0, &box
,
398 ptr
, This
->stride
, This
->layer_stride
);
404 IDirect3DVolume9Vtbl NineVolume9_vtable
= {
405 (void *)NineUnknown_QueryInterface
,
406 (void *)NineUnknown_AddRef
,
407 (void *)NineUnknown_Release
,
408 (void *)NineUnknown_GetDevice
, /* actually part of Volume9 iface */
409 (void *)NineVolume9_SetPrivateData
,
410 (void *)NineVolume9_GetPrivateData
,
411 (void *)NineVolume9_FreePrivateData
,
412 (void *)NineVolume9_GetContainer
,
413 (void *)NineVolume9_GetDesc
,
414 (void *)NineVolume9_LockBox
,
415 (void *)NineVolume9_UnlockBox
418 static const GUID
*NineVolume9_IIDs
[] = {
419 &IID_IDirect3DVolume9
,
425 NineVolume9_new( struct NineDevice9
*pDevice
,
426 struct NineUnknown
*pContainer
,
427 struct pipe_resource
*pResource
,
429 D3DVOLUME_DESC
*pDesc
,
430 struct NineVolume9
**ppOut
)
432 NINE_DEVICE_CHILD_NEW(Volume9
, ppOut
, pDevice
, /* args */
433 pContainer
, pResource
, Level
, pDesc
);
437 /*** The boring stuff. TODO: Unify with Resource. ***/
440 NineVolume9_SetPrivateData( struct NineVolume9
*This
,
447 struct pheader
*header
;
448 const void *user_data
= pData
;
450 DBG("This=%p refguid=%p pData=%p SizeOfData=%d Flags=%d\n",
451 This
, refguid
, pData
, SizeOfData
, Flags
);
453 if (Flags
& D3DSPD_IUNKNOWN
)
454 user_assert(SizeOfData
== sizeof(IUnknown
*), D3DERR_INVALIDCALL
);
456 /* data consists of a header and the actual data. avoiding 2 mallocs */
457 header
= CALLOC_VARIANT_LENGTH_STRUCT(pheader
, SizeOfData
-1);
458 if (!header
) { return E_OUTOFMEMORY
; }
459 header
->unknown
= (Flags
& D3DSPD_IUNKNOWN
) ? TRUE
: FALSE
;
461 /* if the refguid already exists, delete it */
462 NineVolume9_FreePrivateData(This
, refguid
);
464 /* IUnknown special case */
465 if (header
->unknown
) {
466 /* here the pointer doesn't point to the data we want, so point at the
467 * pointer making what we eventually copy is the pointer itself */
471 header
->size
= SizeOfData
;
472 memcpy(header
->data
, user_data
, header
->size
);
474 err
= util_hash_table_set(This
->pdata
, refguid
, header
);
475 if (err
== PIPE_OK
) {
476 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header
->data
); }
481 if (err
== PIPE_ERROR_OUT_OF_MEMORY
) { return E_OUTOFMEMORY
; }
483 return D3DERR_DRIVERINTERNALERROR
;
487 NineVolume9_GetPrivateData( struct NineVolume9
*This
,
492 struct pheader
*header
;
494 user_assert(pSizeOfData
, E_POINTER
);
496 header
= util_hash_table_get(This
->pdata
, refguid
);
497 if (!header
) { return D3DERR_NOTFOUND
; }
500 *pSizeOfData
= header
->size
;
503 if (*pSizeOfData
< header
->size
) {
504 return D3DERR_MOREDATA
;
507 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header
->data
); }
508 memcpy(pData
, header
->data
, header
->size
);
514 NineVolume9_FreePrivateData( struct NineVolume9
*This
,
517 struct pheader
*header
;
519 DBG("This=%p refguid=%p\n", This
, refguid
);
521 header
= util_hash_table_get(This
->pdata
, refguid
);
522 if (!header
) { return D3DERR_NOTFOUND
; }
524 ht_guid_delete(NULL
, header
, NULL
);
525 util_hash_table_remove(This
->pdata
, refguid
);