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. */
26 /* for marking dirty */
27 #include "basetexture9.h"
29 #include "cubetexture9.h"
31 #include "nine_helpers.h"
32 #include "nine_pipe.h"
33 #include "nine_dump.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "pipe/p_state.h"
39 #include "util/u_math.h"
40 #include "util/u_inlines.h"
41 #include "util/u_surface.h"
43 #define DBG_CHANNEL DBG_SURFACE
45 #define is_ATI1_ATI2(format) (format == PIPE_FORMAT_RGTC1_UNORM || format == PIPE_FORMAT_RGTC2_UNORM)
48 NineSurface9_ctor( struct NineSurface9
*This
,
49 struct NineUnknownParams
*pParams
,
50 struct NineUnknown
*pContainer
,
51 struct pipe_resource
*pResource
,
56 D3DSURFACE_DESC
*pDesc
)
59 union pipe_color_union rgba
= {0};
60 struct pipe_surface
*surf
;
61 struct pipe_context
*pipe
= pParams
->device
->pipe
;
63 DBG("This=%p pDevice=%p pResource=%p Level=%u Layer=%u pDesc=%p\n",
64 This
, pParams
->device
, pResource
, Level
, Layer
, 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
|| (user_buffer
&& pDesc
->Pool
!= D3DPOOL_DEFAULT
) ||
73 (!pContainer
&& pDesc
->Pool
!= D3DPOOL_DEFAULT
) ||
74 pDesc
->Format
== D3DFMT_NULL
);
76 assert(!pResource
|| !user_buffer
);
77 assert(!user_buffer
|| pDesc
->Pool
!= D3DPOOL_DEFAULT
);
78 /* The only way we can have !pContainer is being created
79 * from create_zs_or_rt_surface with params 0 0 0 */
80 assert(pContainer
|| (Level
== 0 && Layer
== 0 && TextureType
== 0));
82 This
->data
= (uint8_t *)user_buffer
;
84 This
->base
.info
.screen
= pParams
->device
->screen
;
85 This
->base
.info
.target
= PIPE_TEXTURE_2D
;
86 This
->base
.info
.width0
= pDesc
->Width
;
87 This
->base
.info
.height0
= pDesc
->Height
;
88 This
->base
.info
.depth0
= 1;
89 This
->base
.info
.last_level
= 0;
90 This
->base
.info
.array_size
= 1;
91 This
->base
.info
.nr_samples
= pDesc
->MultiSampleType
;
92 This
->base
.info
.usage
= PIPE_USAGE_DEFAULT
;
93 This
->base
.info
.bind
= PIPE_BIND_SAMPLER_VIEW
;
94 This
->base
.info
.flags
= 0;
95 This
->base
.info
.format
= d3d9_to_pipe_format_checked(This
->base
.info
.screen
,
97 This
->base
.info
.target
,
98 This
->base
.info
.nr_samples
,
102 if (pDesc
->Usage
& D3DUSAGE_RENDERTARGET
)
103 This
->base
.info
.bind
|= PIPE_BIND_RENDER_TARGET
;
104 if (pDesc
->Usage
& D3DUSAGE_DEPTHSTENCIL
)
105 This
->base
.info
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
107 /* Ram buffer with no parent. Has to allocate the resource itself */
108 if (!pResource
&& !pContainer
) {
109 assert(!user_buffer
);
110 This
->data
= align_malloc(
111 nine_format_get_level_alloc_size(This
->base
.info
.format
,
116 return E_OUTOFMEMORY
;
119 if (pDesc
->Pool
== D3DPOOL_SYSTEMMEM
) {
120 This
->base
.info
.usage
= PIPE_USAGE_STAGING
;
123 if (pResource
&& (pDesc
->Usage
& D3DUSAGE_DYNAMIC
))
124 pResource
->flags
|= NINE_RESOURCE_FLAG_LOCKABLE
;
127 hr
= NineResource9_ctor(&This
->base
, pParams
, pResource
, FALSE
, D3DRTYPE_SURFACE
,
128 pDesc
->Pool
, pDesc
->Usage
);
132 This
->pipe
= This
->base
.base
.device
->pipe
;
133 This
->transfer
= NULL
;
135 This
->texture
= TextureType
;
137 This
->level_actual
= Level
;
141 This
->stride
= nine_format_get_stride(This
->base
.info
.format
, pDesc
->Width
);
143 if (pResource
&& NineSurface9_IsOffscreenPlain(This
))
144 pResource
->flags
|= NINE_RESOURCE_FLAG_LOCKABLE
;
146 /* TODO: investigate what else exactly needs to be cleared */
147 if (This
->base
.resource
&& (pDesc
->Usage
& D3DUSAGE_RENDERTARGET
)) {
148 surf
= NineSurface9_GetSurface(This
, 0);
149 pipe
->clear_render_target(pipe
, surf
, &rgba
, 0, 0, pDesc
->Width
, pDesc
->Height
);
152 NineSurface9_Dump(This
);
158 NineSurface9_dtor( struct NineSurface9
*This
)
161 NineSurface9_UnlockRect(This
);
163 pipe_surface_reference(&This
->surface
[0], NULL
);
164 pipe_surface_reference(&This
->surface
[1], NULL
);
166 /* Release system memory when we have to manage it (no parent) */
167 if (!This
->base
.base
.container
&& This
->data
)
169 NineResource9_dtor(&This
->base
);
172 struct pipe_surface
*
173 NineSurface9_CreatePipeSurface( struct NineSurface9
*This
, const int sRGB
)
175 struct pipe_context
*pipe
= This
->pipe
;
176 struct pipe_screen
*screen
= pipe
->screen
;
177 struct pipe_resource
*resource
= This
->base
.resource
;
178 struct pipe_surface templ
;
179 enum pipe_format srgb_format
;
181 assert(This
->desc
.Pool
== D3DPOOL_DEFAULT
);
184 srgb_format
= util_format_srgb(resource
->format
);
185 if (sRGB
&& srgb_format
!= PIPE_FORMAT_NONE
&&
186 screen
->is_format_supported(screen
, srgb_format
,
187 resource
->target
, 0, resource
->bind
))
188 templ
.format
= srgb_format
;
190 templ
.format
= resource
->format
;
191 templ
.u
.tex
.level
= This
->level
;
192 templ
.u
.tex
.first_layer
= This
->layer
;
193 templ
.u
.tex
.last_layer
= This
->layer
;
195 This
->surface
[sRGB
] = pipe
->create_surface(pipe
, resource
, &templ
);
196 assert(This
->surface
[sRGB
]);
197 return This
->surface
[sRGB
];
202 NineSurface9_Dump( struct NineSurface9
*This
)
204 struct NineBaseTexture9
*tex
;
205 GUID id
= IID_IDirect3DBaseTexture9
;
208 DBG("\nNineSurface9(%p->%p/%p): Pool=%s Type=%s Usage=%s\n"
209 "Dims=%ux%u Format=%s Stride=%u Lockable=%i\n"
210 "Level=%u(%u), Layer=%u\n", This
, This
->base
.resource
, This
->data
,
211 nine_D3DPOOL_to_str(This
->desc
.Pool
),
212 nine_D3DRTYPE_to_str(This
->desc
.Type
),
213 nine_D3DUSAGE_to_str(This
->desc
.Usage
),
214 This
->desc
.Width
, This
->desc
.Height
,
215 d3dformat_to_string(This
->desc
.Format
), This
->stride
,
216 This
->base
.resource
&&
217 (This
->base
.resource
->flags
& NINE_RESOURCE_FLAG_LOCKABLE
),
218 This
->level
, This
->level_actual
, This
->layer
);
220 if (!This
->base
.base
.container
)
222 NineUnknown_QueryInterface(This
->base
.base
.container
, ref
, (void **)&tex
);
224 NineBaseTexture9_Dump(tex
);
225 NineUnknown_Release(NineUnknown(tex
));
231 NineSurface9_GetContainer( struct NineSurface9
*This
,
236 if (!NineUnknown(This
)->container
)
237 return E_NOINTERFACE
;
238 hr
= NineUnknown_QueryInterface(NineUnknown(This
)->container
, riid
, ppContainer
);
240 DBG("QueryInterface FAILED!\n");
245 NineSurface9_MarkContainerDirty( struct NineSurface9
*This
)
248 struct NineBaseTexture9
*tex
=
249 NineBaseTexture9(This
->base
.base
.container
);
251 assert(This
->texture
== D3DRTYPE_TEXTURE
||
252 This
->texture
== D3DRTYPE_CUBETEXTURE
);
253 if (This
->base
.pool
== D3DPOOL_MANAGED
)
254 tex
->managed
.dirty
= TRUE
;
256 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
257 tex
->dirty_mip
= TRUE
;
259 BASETEX_REGISTER_UPDATE(tex
);
264 NineSurface9_GetDesc( struct NineSurface9
*This
,
265 D3DSURFACE_DESC
*pDesc
)
267 user_assert(pDesc
!= NULL
, E_POINTER
);
272 /* Add the dirty rects to the source texture */
274 NineSurface9_AddDirtyRect( struct NineSurface9
*This
,
275 const struct pipe_box
*box
)
279 DBG("This=%p box=%p\n", This
, box
);
281 assert (This
->base
.pool
!= D3DPOOL_MANAGED
||
282 This
->texture
== D3DRTYPE_CUBETEXTURE
||
283 This
->texture
== D3DRTYPE_TEXTURE
);
285 if (This
->base
.pool
== D3DPOOL_DEFAULT
)
288 /* Add a dirty rect to level 0 of the parent texture */
289 dirty_rect
.left
= box
->x
<< This
->level_actual
;
290 dirty_rect
.right
= dirty_rect
.left
+ (box
->width
<< This
->level_actual
);
291 dirty_rect
.top
= box
->y
<< This
->level_actual
;
292 dirty_rect
.bottom
= dirty_rect
.top
+ (box
->height
<< This
->level_actual
);
294 if (This
->texture
== D3DRTYPE_TEXTURE
) {
295 struct NineTexture9
*tex
=
296 NineTexture9(This
->base
.base
.container
);
298 NineTexture9_AddDirtyRect(tex
, &dirty_rect
);
299 } else if (This
->texture
== D3DRTYPE_CUBETEXTURE
) {
300 struct NineCubeTexture9
*ctex
=
301 NineCubeTexture9(This
->base
.base
.container
);
303 NineCubeTexture9_AddDirtyRect(ctex
, This
->layer
, &dirty_rect
);
307 static inline uint8_t *
308 NineSurface9_GetSystemMemPointer(struct NineSurface9
*This
, int x
, int y
)
310 unsigned x_offset
= util_format_get_stride(This
->base
.info
.format
, x
);
312 y
= util_format_get_nblocksy(This
->base
.info
.format
, y
);
315 return This
->data
+ (y
* This
->stride
+ x_offset
);
319 NineSurface9_LockRect( struct NineSurface9
*This
,
320 D3DLOCKED_RECT
*pLockedRect
,
324 struct pipe_resource
*resource
= This
->base
.resource
;
328 DBG("This=%p pLockedRect=%p pRect=%p[%u..%u,%u..%u] Flags=%s\n", This
,
330 pRect
? pRect
->left
: 0, pRect
? pRect
->right
: 0,
331 pRect
? pRect
->top
: 0, pRect
? pRect
->bottom
: 0,
332 nine_D3DLOCK_to_str(Flags
));
333 NineSurface9_Dump(This
);
335 /* check if it's already locked */
336 user_assert(This
->lock_count
== 0, D3DERR_INVALIDCALL
);
338 /* set pBits to NULL after lock_count check */
339 user_assert(pLockedRect
, E_POINTER
);
340 pLockedRect
->pBits
= NULL
;
343 user_assert(This
->base
.pool
!= D3DPOOL_DEFAULT
||
344 (resource
&& (resource
->flags
& NINE_RESOURCE_FLAG_LOCKABLE
)),
347 user_assert(!(Flags
& ~(D3DLOCK_DISCARD
|
349 D3DLOCK_NO_DIRTY_UPDATE
|
350 D3DLOCK_NOOVERWRITE
|
351 D3DLOCK_NOSYSLOCK
| /* ignored */
352 D3DLOCK_READONLY
)), D3DERR_INVALIDCALL
);
353 user_assert(!((Flags
& D3DLOCK_DISCARD
) && (Flags
& D3DLOCK_READONLY
)),
356 user_assert(This
->desc
.MultiSampleType
== D3DMULTISAMPLE_NONE
,
359 if (pRect
&& This
->desc
.Pool
== D3DPOOL_DEFAULT
&&
360 compressed_format (This
->desc
.Format
)) {
361 const unsigned w
= util_format_get_blockwidth(This
->base
.info
.format
);
362 const unsigned h
= util_format_get_blockheight(This
->base
.info
.format
);
363 user_assert((pRect
->left
== 0 && pRect
->right
== This
->desc
.Width
&&
364 pRect
->top
== 0 && pRect
->bottom
== This
->desc
.Height
) ||
365 (!(pRect
->left
% w
) && !(pRect
->right
% w
) &&
366 !(pRect
->top
% h
) && !(pRect
->bottom
% h
)),
370 if (Flags
& D3DLOCK_DISCARD
) {
371 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
373 usage
= (Flags
& D3DLOCK_READONLY
) ?
374 PIPE_TRANSFER_READ
: PIPE_TRANSFER_READ_WRITE
;
376 if (Flags
& D3DLOCK_DONOTWAIT
)
377 usage
|= PIPE_TRANSFER_DONTBLOCK
;
380 /* Windows XP accepts invalid locking rectangles, Windows 7 rejects
381 * them. Use Windows XP behaviour for now. */
382 rect_to_pipe_box(&box
, pRect
);
384 u_box_origin_2d(This
->desc
.Width
, This
->desc
.Height
, &box
);
387 user_warn(This
->desc
.Format
== D3DFMT_NULL
);
390 DBG("returning system memory\n");
391 /* ATI1 and ATI2 need special handling, because of d3d9 bug.
392 * We must advertise to the application as if it is uncompressed
393 * and bpp 8, and the app has a workaround to work with the fact
394 * that it is actually compressed. */
395 if (is_ATI1_ATI2(This
->base
.info
.format
)) {
396 pLockedRect
->Pitch
= This
->desc
.Width
;
397 pLockedRect
->pBits
= This
->data
+ box
.y
* This
->desc
.Width
+ box
.x
;
399 pLockedRect
->Pitch
= This
->stride
;
400 pLockedRect
->pBits
= NineSurface9_GetSystemMemPointer(This
,
405 DBG("mapping pipe_resource %p (level=%u usage=%x)\n",
406 resource
, This
->level
, usage
);
408 pLockedRect
->pBits
= This
->pipe
->transfer_map(This
->pipe
, resource
,
409 This
->level
, usage
, &box
,
411 if (!This
->transfer
) {
412 DBG("transfer_map failed\n");
413 if (Flags
& D3DLOCK_DONOTWAIT
)
414 return D3DERR_WASSTILLDRAWING
;
415 return D3DERR_INVALIDCALL
;
417 pLockedRect
->Pitch
= This
->transfer
->stride
;
420 if (!(Flags
& (D3DLOCK_NO_DIRTY_UPDATE
| D3DLOCK_READONLY
))) {
421 NineSurface9_MarkContainerDirty(This
);
422 NineSurface9_AddDirtyRect(This
, &box
);
430 NineSurface9_UnlockRect( struct NineSurface9
*This
)
432 DBG("This=%p lock_count=%u\n", This
, This
->lock_count
);
433 user_assert(This
->lock_count
, D3DERR_INVALIDCALL
);
434 if (This
->transfer
) {
435 This
->pipe
->transfer_unmap(This
->pipe
, This
->transfer
);
436 This
->transfer
= NULL
;
443 NineSurface9_GetDC( struct NineSurface9
*This
,
446 STUB(D3DERR_INVALIDCALL
);
450 NineSurface9_ReleaseDC( struct NineSurface9
*This
,
453 STUB(D3DERR_INVALIDCALL
);
456 IDirect3DSurface9Vtbl NineSurface9_vtable
= {
457 (void *)NineUnknown_QueryInterface
,
458 (void *)NineUnknown_AddRef
,
459 (void *)NineUnknown_Release
,
460 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
461 (void *)NineResource9_SetPrivateData
,
462 (void *)NineResource9_GetPrivateData
,
463 (void *)NineResource9_FreePrivateData
,
464 (void *)NineResource9_SetPriority
,
465 (void *)NineResource9_GetPriority
,
466 (void *)NineResource9_PreLoad
,
467 (void *)NineResource9_GetType
,
468 (void *)NineSurface9_GetContainer
,
469 (void *)NineSurface9_GetDesc
,
470 (void *)NineSurface9_LockRect
,
471 (void *)NineSurface9_UnlockRect
,
472 (void *)NineSurface9_GetDC
,
473 (void *)NineSurface9_ReleaseDC
476 /* When this function is called, we have already checked
477 * The copy regions fit the surfaces */
479 NineSurface9_CopyMemToDefault( struct NineSurface9
*This
,
480 struct NineSurface9
*From
,
481 const POINT
*pDestPoint
,
482 const RECT
*pSourceRect
)
484 struct pipe_context
*pipe
= This
->pipe
;
485 struct pipe_resource
*r_dst
= This
->base
.resource
;
486 struct pipe_box dst_box
;
487 const uint8_t *p_src
;
488 int src_x
, src_y
, dst_x
, dst_y
, copy_width
, copy_height
;
490 assert(This
->base
.pool
== D3DPOOL_DEFAULT
&&
491 From
->base
.pool
== D3DPOOL_SYSTEMMEM
);
494 dst_x
= pDestPoint
->x
;
495 dst_y
= pDestPoint
->y
;
502 src_x
= pSourceRect
->left
;
503 src_y
= pSourceRect
->top
;
504 copy_width
= pSourceRect
->right
- pSourceRect
->left
;
505 copy_height
= pSourceRect
->bottom
- pSourceRect
->top
;
509 copy_width
= From
->desc
.Width
;
510 copy_height
= From
->desc
.Height
;
513 u_box_2d_zslice(dst_x
, dst_y
, This
->layer
,
514 copy_width
, copy_height
, &dst_box
);
516 p_src
= NineSurface9_GetSystemMemPointer(From
, src_x
, src_y
);
518 pipe
->transfer_inline_write(pipe
, r_dst
, This
->level
,
519 0, /* WRITE|DISCARD are implicit */
520 &dst_box
, p_src
, From
->stride
, 0);
522 NineSurface9_MarkContainerDirty(This
);
526 NineSurface9_CopyDefaultToMem( struct NineSurface9
*This
,
527 struct NineSurface9
*From
)
529 struct pipe_context
*pipe
= This
->pipe
;
530 struct pipe_resource
*r_src
= From
->base
.resource
;
531 struct pipe_transfer
*transfer
;
532 struct pipe_box src_box
;
534 const uint8_t *p_src
;
536 assert(This
->base
.pool
== D3DPOOL_SYSTEMMEM
&&
537 From
->base
.pool
== D3DPOOL_DEFAULT
);
539 assert(This
->desc
.Width
== From
->desc
.Width
);
540 assert(This
->desc
.Height
== From
->desc
.Height
);
542 u_box_origin_2d(This
->desc
.Width
, This
->desc
.Height
, &src_box
);
543 src_box
.z
= From
->layer
;
545 p_src
= pipe
->transfer_map(pipe
, r_src
, From
->level
,
547 &src_box
, &transfer
);
548 p_dst
= NineSurface9_GetSystemMemPointer(This
, 0, 0);
550 assert (p_src
&& p_dst
);
552 util_copy_rect(p_dst
, This
->base
.info
.format
,
554 This
->desc
.Width
, This
->desc
.Height
,
556 transfer
->stride
, 0, 0);
558 pipe
->transfer_unmap(pipe
, transfer
);
562 /* Gladly, rendering to a MANAGED surface is not permitted, so we will
563 * never have to do the reverse, i.e. download the surface.
566 NineSurface9_UploadSelf( struct NineSurface9
*This
,
567 const struct pipe_box
*damaged
)
569 struct pipe_context
*pipe
= This
->pipe
;
570 struct pipe_resource
*res
= This
->base
.resource
;
574 DBG("This=%p damaged=%p\n", This
, damaged
);
576 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
586 box
.width
= This
->desc
.Width
;
587 box
.height
= This
->desc
.Height
;
591 ptr
= NineSurface9_GetSystemMemPointer(This
, box
.x
, box
.y
);
593 pipe
->transfer_inline_write(pipe
, res
, This
->level
, 0,
594 &box
, ptr
, This
->stride
, 0);
600 NineSurface9_SetResourceResize( struct NineSurface9
*This
,
601 struct pipe_resource
*resource
)
603 assert(This
->level
== 0 && This
->level_actual
== 0);
604 assert(!This
->lock_count
);
605 assert(This
->desc
.Pool
== D3DPOOL_DEFAULT
);
606 assert(!This
->texture
);
608 pipe_resource_reference(&This
->base
.resource
, resource
);
610 This
->desc
.Width
= This
->base
.info
.width0
= resource
->width0
;
611 This
->desc
.Height
= This
->base
.info
.height0
= resource
->height0
;
612 This
->desc
.MultiSampleType
= This
->base
.info
.nr_samples
= resource
->nr_samples
;
614 This
->stride
= nine_format_get_stride(This
->base
.info
.format
,
617 pipe_surface_reference(&This
->surface
[0], NULL
);
618 pipe_surface_reference(&This
->surface
[1], NULL
);
622 static const GUID
*NineSurface9_IIDs
[] = {
623 &IID_IDirect3DSurface9
,
624 &IID_IDirect3DResource9
,
630 NineSurface9_new( struct NineDevice9
*pDevice
,
631 struct NineUnknown
*pContainer
,
632 struct pipe_resource
*pResource
,
637 D3DSURFACE_DESC
*pDesc
,
638 struct NineSurface9
**ppOut
)
640 NINE_DEVICE_CHILD_NEW(Surface9
, ppOut
, pDevice
, /* args */
641 pContainer
, pResource
, user_buffer
,
642 TextureType
, Level
, Layer
, pDesc
);