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_format.h"
32 #include "util/u_surface.h"
34 #define DBG_CHANNEL DBG_VOLUME
38 NineVolume9_AllocateData( struct NineVolume9
*This
)
40 unsigned size
= This
->layer_stride
* This
->desc
.Depth
;
42 DBG("(%p(This=%p),level=%u) Allocating 0x%x bytes of system memory.\n",
43 This
->base
.container
, This
, This
->level
, size
);
45 This
->data
= (uint8_t *)align_calloc(size
, 32);
52 NineVolume9_ctor( struct NineVolume9
*This
,
53 struct NineUnknownParams
*pParams
,
54 struct NineUnknown
*pContainer
,
55 struct pipe_resource
*pResource
,
57 D3DVOLUME_DESC
*pDesc
)
61 assert(pContainer
); /* stand-alone volumes can't be created */
63 DBG("This=%p pContainer=%p pDevice=%p pResource=%p Level=%u pDesc=%p\n",
64 This
, pContainer
, pParams
->device
, pResource
, Level
, pDesc
);
66 /* Mark this as a special surface held by another internal resource. */
67 pParams
->container
= pContainer
;
69 user_assert(!(pDesc
->Usage
& D3DUSAGE_DYNAMIC
) ||
70 (pDesc
->Pool
!= D3DPOOL_MANAGED
), D3DERR_INVALIDCALL
);
72 assert(pResource
|| pDesc
->Pool
!= D3DPOOL_DEFAULT
);
74 hr
= NineUnknown_ctor(&This
->base
, pParams
);
78 pipe_resource_reference(&This
->resource
, pResource
);
80 This
->transfer
= NULL
;
84 This
->level_actual
= Level
;
87 This
->info
.screen
= pParams
->device
->screen
;
88 This
->info
.target
= PIPE_TEXTURE_3D
;
89 This
->info
.width0
= pDesc
->Width
;
90 This
->info
.height0
= pDesc
->Height
;
91 This
->info
.depth0
= pDesc
->Depth
;
92 This
->info
.last_level
= 0;
93 This
->info
.array_size
= 1;
94 This
->info
.nr_samples
= 0;
95 This
->info
.nr_storage_samples
= 0;
96 This
->info
.usage
= PIPE_USAGE_DEFAULT
;
97 This
->info
.bind
= PIPE_BIND_SAMPLER_VIEW
;
99 This
->info
.format
= d3d9_to_pipe_format_checked(This
->info
.screen
,
102 This
->info
.nr_samples
,
103 This
->info
.bind
, FALSE
,
104 pDesc
->Pool
== D3DPOOL_SCRATCH
);
106 if (This
->info
.format
== PIPE_FORMAT_NONE
)
107 return D3DERR_DRIVERINTERNALERROR
;
109 This
->stride
= util_format_get_stride(This
->info
.format
, pDesc
->Width
);
110 This
->stride
= align(This
->stride
, 4);
111 This
->layer_stride
= util_format_get_2d_size(This
->info
.format
,
112 This
->stride
, pDesc
->Height
);
114 /* Get true format */
115 This
->format_conversion
= d3d9_to_pipe_format_checked(This
->info
.screen
,
118 This
->info
.nr_samples
,
119 This
->info
.bind
, FALSE
,
121 if (This
->info
.format
!= This
->format_conversion
) {
122 This
->stride_conversion
= nine_format_get_stride(This
->format_conversion
,
124 This
->layer_stride_conversion
= util_format_get_2d_size(This
->format_conversion
,
125 This
->stride_conversion
,
127 This
->data_conversion
= align_calloc(This
->layer_stride_conversion
*
128 This
->desc
.Depth
, 32);
129 if (!This
->data_conversion
)
130 return E_OUTOFMEMORY
;
133 if (!This
->resource
) {
134 hr
= NineVolume9_AllocateData(This
);
142 NineVolume9_dtor( struct NineVolume9
*This
)
144 DBG("This=%p\n", This
);
146 if (This
->transfer
) {
147 struct pipe_context
*pipe
= nine_context_get_pipe_multithread(This
->base
.device
);
148 pipe
->transfer_unmap(pipe
, This
->transfer
);
149 This
->transfer
= NULL
;
152 /* Note: Following condition cannot happen currently, since we
153 * refcount the volume in the functions increasing
154 * pending_uploads_counter. */
155 if (p_atomic_read(&This
->pending_uploads_counter
))
156 nine_csmt_process(This
->base
.device
);
159 align_free(This
->data
);
160 if (This
->data_conversion
)
161 align_free(This
->data_conversion
);
163 pipe_resource_reference(&This
->resource
, NULL
);
165 NineUnknown_dtor(&This
->base
);
169 NineVolume9_GetContainer( struct NineVolume9
*This
,
175 DBG("This=%p riid=%p id=%s ppContainer=%p\n",
176 This
, riid
, riid
? GUID_sprintf(guid_str
, riid
) : "", ppContainer
);
180 if (!NineUnknown(This
)->container
)
181 return E_NOINTERFACE
;
182 return NineUnknown_QueryInterface(NineUnknown(This
)->container
, riid
, ppContainer
);
186 NineVolume9_MarkContainerDirty( struct NineVolume9
*This
)
188 struct NineBaseTexture9
*tex
;
190 /* This is always contained by a NineVolumeTexture9. */
191 GUID id
= IID_IDirect3DVolumeTexture9
;
193 assert(NineUnknown_QueryInterface(This
->base
.container
, ref
, (void **)&tex
)
195 assert(NineUnknown_Release(NineUnknown(tex
)) != 0);
198 tex
= NineBaseTexture9(This
->base
.container
);
200 if (This
->desc
.Pool
== D3DPOOL_MANAGED
)
201 tex
->managed
.dirty
= TRUE
;
203 BASETEX_REGISTER_UPDATE(tex
);
207 NineVolume9_GetDesc( struct NineVolume9
*This
,
208 D3DVOLUME_DESC
*pDesc
)
210 user_assert(pDesc
!= NULL
, E_POINTER
);
216 NineVolume9_AddDirtyRegion( struct NineVolume9
*This
,
217 const struct pipe_box
*box
)
220 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
->base
.container
);
223 NineVolumeTexture9_AddDirtyBox(tex
, NULL
);
225 dirty_region
.Left
= box
->x
<< This
->level_actual
;
226 dirty_region
.Top
= box
->y
<< This
->level_actual
;
227 dirty_region
.Front
= box
->z
<< This
->level_actual
;
228 dirty_region
.Right
= dirty_region
.Left
+ (box
->width
<< This
->level_actual
);
229 dirty_region
.Bottom
= dirty_region
.Top
+ (box
->height
<< This
->level_actual
);
230 dirty_region
.Back
= dirty_region
.Front
+ (box
->depth
<< This
->level_actual
);
231 NineVolumeTexture9_AddDirtyBox(tex
, &dirty_region
);
235 static inline uint8_t *
236 NineVolume9_GetSystemMemPointer(struct NineVolume9
*This
, int x
, int y
, int z
)
238 unsigned x_offset
= util_format_get_stride(This
->info
.format
, x
);
240 y
= util_format_get_nblocksy(This
->info
.format
, y
);
243 return This
->data
+ (z
* This
->layer_stride
+ y
* This
->stride
+ x_offset
);
247 NineVolume9_LockBox( struct NineVolume9
*This
,
248 D3DLOCKED_BOX
*pLockedVolume
,
252 struct pipe_context
*pipe
;
253 struct pipe_resource
*resource
= This
->resource
;
257 DBG("This=%p(%p) pLockedVolume=%p pBox=%p[%u..%u,%u..%u,%u..%u] Flags=%s\n",
258 This
, This
->base
.container
, pLockedVolume
, pBox
,
259 pBox
? pBox
->Left
: 0, pBox
? pBox
->Right
: 0,
260 pBox
? pBox
->Top
: 0, pBox
? pBox
->Bottom
: 0,
261 pBox
? pBox
->Front
: 0, pBox
? pBox
->Back
: 0,
262 nine_D3DLOCK_to_str(Flags
));
264 /* check if it's already locked */
265 user_assert(This
->lock_count
== 0, D3DERR_INVALIDCALL
);
267 /* set pBits to NULL after lock_count check */
268 user_assert(pLockedVolume
, E_POINTER
);
269 pLockedVolume
->pBits
= NULL
;
271 user_assert(This
->desc
.Pool
!= D3DPOOL_DEFAULT
||
272 (This
->desc
.Usage
& D3DUSAGE_DYNAMIC
), D3DERR_INVALIDCALL
);
274 user_assert(!((Flags
& D3DLOCK_DISCARD
) && (Flags
& D3DLOCK_READONLY
)),
277 if (pBox
&& compressed_format (This
->desc
.Format
)) { /* For volume all pools are checked */
278 const unsigned w
= util_format_get_blockwidth(This
->info
.format
);
279 const unsigned h
= util_format_get_blockheight(This
->info
.format
);
280 user_assert((pBox
->Left
== 0 && pBox
->Right
== This
->desc
.Width
&&
281 pBox
->Top
== 0 && pBox
->Bottom
== This
->desc
.Height
) ||
282 (!(pBox
->Left
% w
) && !(pBox
->Right
% w
) &&
283 !(pBox
->Top
% h
) && !(pBox
->Bottom
% h
)),
287 if (Flags
& D3DLOCK_DISCARD
) {
288 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
290 usage
= (Flags
& D3DLOCK_READONLY
) ?
291 PIPE_TRANSFER_READ
: PIPE_TRANSFER_READ_WRITE
;
293 if (Flags
& D3DLOCK_DONOTWAIT
)
294 usage
|= PIPE_TRANSFER_DONTBLOCK
;
297 user_assert(pBox
->Right
> pBox
->Left
, D3DERR_INVALIDCALL
);
298 user_assert(pBox
->Bottom
> pBox
->Top
, D3DERR_INVALIDCALL
);
299 user_assert(pBox
->Back
> pBox
->Front
, D3DERR_INVALIDCALL
);
300 user_assert(pBox
->Right
<= This
->desc
.Width
, D3DERR_INVALIDCALL
);
301 user_assert(pBox
->Bottom
<= This
->desc
.Height
, D3DERR_INVALIDCALL
);
302 user_assert(pBox
->Back
<= This
->desc
.Depth
, D3DERR_INVALIDCALL
);
304 d3dbox_to_pipe_box(&box
, pBox
);
305 if (u_box_clip_2d(&box
, &box
, This
->desc
.Width
, This
->desc
.Height
) < 0) {
306 DBG("Locked volume intersection empty.\n");
307 return D3DERR_INVALIDCALL
;
310 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
, This
->desc
.Depth
,
314 if (p_atomic_read(&This
->pending_uploads_counter
))
315 nine_csmt_process(This
->base
.device
);
317 if (This
->data_conversion
) {
318 /* For now we only have uncompressed formats here */
319 pLockedVolume
->RowPitch
= This
->stride_conversion
;
320 pLockedVolume
->SlicePitch
= This
->layer_stride_conversion
;
321 pLockedVolume
->pBits
= This
->data_conversion
+ box
.z
* This
->layer_stride_conversion
+
322 box
.y
* This
->stride_conversion
+
323 util_format_get_stride(This
->format_conversion
, box
.x
);
324 } else if (This
->data
) {
325 pLockedVolume
->RowPitch
= This
->stride
;
326 pLockedVolume
->SlicePitch
= This
->layer_stride
;
327 pLockedVolume
->pBits
=
328 NineVolume9_GetSystemMemPointer(This
, box
.x
, box
.y
, box
.z
);
330 bool no_refs
= !p_atomic_read(&This
->base
.bind
) &&
331 !p_atomic_read(&This
->base
.container
->bind
);
333 pipe
= nine_context_get_pipe_acquire(This
->base
.device
);
335 pipe
= NineDevice9_GetPipe(This
->base
.device
);
336 pLockedVolume
->pBits
=
337 pipe
->transfer_map(pipe
, resource
, This
->level
, usage
,
338 &box
, &This
->transfer
);
340 nine_context_get_pipe_release(This
->base
.device
);
341 if (!This
->transfer
) {
342 if (Flags
& D3DLOCK_DONOTWAIT
)
343 return D3DERR_WASSTILLDRAWING
;
344 return D3DERR_DRIVERINTERNALERROR
;
346 pLockedVolume
->RowPitch
= This
->transfer
->stride
;
347 pLockedVolume
->SlicePitch
= This
->transfer
->layer_stride
;
350 if (!(Flags
& (D3DLOCK_NO_DIRTY_UPDATE
| D3DLOCK_READONLY
))) {
351 NineVolume9_MarkContainerDirty(This
);
352 NineVolume9_AddDirtyRegion(This
, &box
);
360 NineVolume9_UnlockBox( struct NineVolume9
*This
)
362 struct pipe_context
*pipe
;
364 DBG("This=%p lock_count=%u\n", This
, This
->lock_count
);
365 user_assert(This
->lock_count
, D3DERR_INVALIDCALL
);
366 if (This
->transfer
) {
367 pipe
= nine_context_get_pipe_acquire(This
->base
.device
);
368 pipe
->transfer_unmap(pipe
, This
->transfer
);
369 This
->transfer
= NULL
;
370 nine_context_get_pipe_release(This
->base
.device
);
374 if (This
->data_conversion
) {
375 struct pipe_transfer
*transfer
;
376 uint8_t *dst
= This
->data
;
379 u_box_3d(0, 0, 0, This
->desc
.Width
, This
->desc
.Height
, This
->desc
.Depth
,
382 pipe
= NineDevice9_GetPipe(This
->base
.device
);
384 dst
= pipe
->transfer_map(pipe
,
387 PIPE_TRANSFER_WRITE
|
388 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
394 (void) util_format_translate_3d(This
->info
.format
,
395 dst
, This
->data
? This
->stride
: transfer
->stride
,
396 This
->data
? This
->layer_stride
: transfer
->layer_stride
,
398 This
->format_conversion
,
399 This
->data_conversion
,
400 This
->stride_conversion
,
401 This
->layer_stride_conversion
,
403 This
->desc
.Width
, This
->desc
.Height
,
407 pipe_transfer_unmap(pipe
, transfer
);
413 /* When this function is called, we have already checked
414 * The copy regions fit the volumes */
416 NineVolume9_CopyMemToDefault( struct NineVolume9
*This
,
417 struct NineVolume9
*From
,
418 unsigned dstx
, unsigned dsty
, unsigned dstz
,
419 struct pipe_box
*pSrcBox
)
421 struct pipe_resource
*r_dst
= This
->resource
;
422 struct pipe_box src_box
;
423 struct pipe_box dst_box
;
425 DBG("This=%p From=%p dstx=%u dsty=%u dstz=%u pSrcBox=%p\n",
426 This
, From
, dstx
, dsty
, dstz
, pSrcBox
);
428 assert(This
->desc
.Pool
== D3DPOOL_DEFAULT
&&
429 From
->desc
.Pool
== D3DPOOL_SYSTEMMEM
);
441 src_box
.width
= From
->desc
.Width
;
442 src_box
.height
= From
->desc
.Height
;
443 src_box
.depth
= From
->desc
.Depth
;
446 dst_box
.width
= src_box
.width
;
447 dst_box
.height
= src_box
.height
;
448 dst_box
.depth
= src_box
.depth
;
450 nine_context_box_upload(This
->base
.device
,
451 &From
->pending_uploads_counter
,
452 (struct NineUnknown
*)From
,
457 From
->data
, From
->stride
,
461 if (This
->data_conversion
)
462 (void) util_format_translate_3d(This
->format_conversion
,
463 This
->data_conversion
,
464 This
->stride_conversion
,
465 This
->layer_stride_conversion
,
468 From
->data
, From
->stride
,
470 src_box
.x
, src_box
.y
,
476 NineVolume9_MarkContainerDirty(This
);
482 NineVolume9_UploadSelf( struct NineVolume9
*This
,
483 const struct pipe_box
*damaged
)
485 struct pipe_resource
*res
= This
->resource
;
488 DBG("This=%p damaged=%p data=%p res=%p\n", This
, damaged
,
491 assert(This
->desc
.Pool
== D3DPOOL_MANAGED
);
500 box
.width
= This
->desc
.Width
;
501 box
.height
= This
->desc
.Height
;
502 box
.depth
= This
->desc
.Depth
;
505 nine_context_box_upload(This
->base
.device
,
506 &This
->pending_uploads_counter
,
507 (struct NineUnknown
*)This
,
512 This
->data
, This
->stride
,
520 IDirect3DVolume9Vtbl NineVolume9_vtable
= {
521 (void *)NineUnknown_QueryInterface
,
522 (void *)NineUnknown_AddRef
,
523 (void *)NineUnknown_Release
,
524 (void *)NineUnknown_GetDevice
, /* actually part of Volume9 iface */
525 (void *)NineUnknown_SetPrivateData
,
526 (void *)NineUnknown_GetPrivateData
,
527 (void *)NineUnknown_FreePrivateData
,
528 (void *)NineVolume9_GetContainer
,
529 (void *)NineVolume9_GetDesc
,
530 (void *)NineVolume9_LockBox
,
531 (void *)NineVolume9_UnlockBox
534 static const GUID
*NineVolume9_IIDs
[] = {
535 &IID_IDirect3DVolume9
,
541 NineVolume9_new( struct NineDevice9
*pDevice
,
542 struct NineUnknown
*pContainer
,
543 struct pipe_resource
*pResource
,
545 D3DVOLUME_DESC
*pDesc
,
546 struct NineVolume9
**ppOut
)
548 NINE_DEVICE_CHILD_NEW(Volume9
, ppOut
, pDevice
, /* args */
549 pContainer
, pResource
, Level
, pDesc
);