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 #include "nine_helpers.h"
27 #include "nine_pipe.h"
28 #include "nine_dump.h"
30 #include "pipe/p_state.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_screen.h"
33 #include "util/u_inlines.h"
34 #include "util/u_resource.h"
36 #define DBG_CHANNEL DBG_TEXTURE
39 NineTexture9_ctor( struct NineTexture9
*This
,
40 struct NineUnknownParams
*pParams
,
41 UINT Width
, UINT Height
, UINT Levels
,
45 HANDLE
*pSharedHandle
)
47 struct pipe_screen
*screen
= pParams
->device
->screen
;
48 struct pipe_resource
*info
= &This
->base
.base
.info
;
49 struct pipe_resource
*resource
;
52 D3DSURFACE_DESC sfdesc
;
54 void *user_buffer
= NULL
;
56 DBG("(%p) Width=%u Height=%u Levels=%u Usage=%s Format=%s Pool=%s "
57 "pSharedHandle=%p\n", This
, Width
, Height
, Levels
,
58 nine_D3DUSAGE_to_str(Usage
),
59 d3dformat_to_string(Format
), nine_D3DPOOL_to_str(Pool
), pSharedHandle
);
61 user_assert(!(Usage
& D3DUSAGE_AUTOGENMIPMAP
) ||
62 (Pool
!= D3DPOOL_SYSTEMMEM
&& Levels
<= 1), D3DERR_INVALIDCALL
);
64 /* TODO: implement buffer sharing (should work with cross process too)
66 * Gem names may have fit but they're depreciated and won't work on render-nodes.
67 * One solution is to use shm buffers. We would use a /dev/shm file, fill the first
68 * values to tell it is a nine buffer, the size, which function created it, etc,
69 * and then it would contain the data. The handle would be a number, corresponding to
70 * the file to read (/dev/shm/nine-share-4 for example would be 4).
72 * Wine just ignores the argument, which works only if the app creates the handle
73 * and won't use it. Instead of failing, we support that situation by putting an
74 * invalid handle, that we would fail to import. Please note that we don't advertise
75 * the flag indicating the support for that feature, but apps seem to not care.
77 user_assert(!pSharedHandle
||
78 Pool
== D3DPOOL_SYSTEMMEM
||
79 Pool
== D3DPOOL_DEFAULT
, D3DERR_INVALIDCALL
);
81 if (pSharedHandle
&& Pool
== D3DPOOL_DEFAULT
) {
82 if (!*pSharedHandle
) {
83 DBG("Creating Texture with invalid handle. Importing will fail\n.");
84 *pSharedHandle
= (HANDLE
)1; /* Wine would keep it NULL */
87 ERR("Application tries to use cross-process sharing feature. Nine "
88 "doesn't support it");
89 return D3DERR_INVALIDCALL
;
93 if (Usage
& D3DUSAGE_AUTOGENMIPMAP
)
96 pf
= d3d9_to_pipe_format(Format
);
97 if (Format
!= D3DFMT_NULL
&& (pf
== PIPE_FORMAT_NONE
||
98 !screen
->is_format_supported(screen
, pf
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
))) {
99 return D3DERR_INVALIDCALL
;
102 info
->screen
= screen
;
103 info
->target
= PIPE_TEXTURE_2D
;
105 info
->width0
= Width
;
106 info
->height0
= Height
;
109 info
->last_level
= Levels
- 1;
111 info
->last_level
= util_logbase2(MAX2(Width
, Height
));
112 info
->array_size
= 1;
113 info
->nr_samples
= 0;
114 info
->bind
= PIPE_BIND_SAMPLER_VIEW
;
115 info
->usage
= PIPE_USAGE_DEFAULT
;
118 if (Usage
& D3DUSAGE_RENDERTARGET
)
119 info
->bind
|= PIPE_BIND_RENDER_TARGET
;
120 if (Usage
& D3DUSAGE_DEPTHSTENCIL
)
121 info
->bind
|= PIPE_BIND_DEPTH_STENCIL
;
123 if (Usage
& D3DUSAGE_DYNAMIC
) {
124 info
->usage
= PIPE_USAGE_DYNAMIC
;
126 PIPE_BIND_TRANSFER_READ
|
127 PIPE_BIND_TRANSFER_WRITE
;
130 info
->bind
|= PIPE_BIND_SHARED
;
132 if (Pool
== D3DPOOL_SYSTEMMEM
)
133 info
->usage
= PIPE_USAGE_STAGING
;
135 if (pSharedHandle
&& *pSharedHandle
) { /* Pool == D3DPOOL_SYSTEMMEM */
136 user_buffer
= (void *)*pSharedHandle
;
139 This
->surfaces
= CALLOC(info
->last_level
+ 1, sizeof(*This
->surfaces
));
141 return E_OUTOFMEMORY
;
143 hr
= NineBaseTexture9_ctor(&This
->base
, pParams
, NULL
, D3DRTYPE_TEXTURE
, Format
, Pool
, Usage
);
146 This
->base
.pstype
= (Height
== 1) ? 1 : 0;
148 /* Create all the surfaces right away.
149 * They manage backing storage, and transfers (LockRect) are deferred
152 sfdesc
.Format
= Format
;
153 sfdesc
.Type
= D3DRTYPE_SURFACE
;
154 sfdesc
.Usage
= Usage
;
156 sfdesc
.MultiSampleType
= D3DMULTISAMPLE_NONE
;
157 sfdesc
.MultiSampleQuality
= 0;
159 if (Pool
== D3DPOOL_SYSTEMMEM
)
162 resource
= This
->base
.base
.resource
;
164 for (l
= 0; l
<= info
->last_level
; ++l
) {
165 sfdesc
.Width
= u_minify(Width
, l
);
166 sfdesc
.Height
= u_minify(Height
, l
);
168 hr
= NineSurface9_new(This
->base
.base
.base
.device
, NineUnknown(This
),
169 resource
, user_buffer
,
170 D3DRTYPE_TEXTURE
, l
, 0,
171 &sfdesc
, &This
->surfaces
[l
]);
176 This
->dirty_rect
.depth
= 1; /* widht == 0 means empty, depth stays 1 */
178 if (pSharedHandle
&& !*pSharedHandle
) {/* Pool == D3DPOOL_SYSTEMMEM */
179 *pSharedHandle
= This
->surfaces
[0]->data
;
186 NineTexture9_dtor( struct NineTexture9
*This
)
190 if (This
->surfaces
) {
191 /* The surfaces should have 0 references and be unbound now. */
192 for (l
= 0; l
<= This
->base
.base
.info
.last_level
; ++l
)
193 NineUnknown_Destroy(&This
->surfaces
[l
]->base
.base
);
194 FREE(This
->surfaces
);
197 NineBaseTexture9_dtor(&This
->base
);
201 NineTexture9_GetLevelDesc( struct NineTexture9
*This
,
203 D3DSURFACE_DESC
*pDesc
)
205 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
206 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
209 *pDesc
= This
->surfaces
[Level
]->desc
;
215 NineTexture9_GetSurfaceLevel( struct NineTexture9
*This
,
217 IDirect3DSurface9
**ppSurfaceLevel
)
219 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
220 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
223 NineUnknown_AddRef(NineUnknown(This
->surfaces
[Level
]));
224 *ppSurfaceLevel
= (IDirect3DSurface9
*)This
->surfaces
[Level
];
230 NineTexture9_LockRect( struct NineTexture9
*This
,
232 D3DLOCKED_RECT
*pLockedRect
,
236 DBG("This=%p Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
237 This
, Level
, pLockedRect
, pRect
, Flags
);
239 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
240 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
243 return NineSurface9_LockRect(This
->surfaces
[Level
], pLockedRect
,
248 NineTexture9_UnlockRect( struct NineTexture9
*This
,
251 DBG("This=%p Level=%u\n", This
, Level
);
253 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
255 return NineSurface9_UnlockRect(This
->surfaces
[Level
]);
259 NineTexture9_AddDirtyRect( struct NineTexture9
*This
,
260 const RECT
*pDirtyRect
)
262 DBG("This=%p pDirtyRect=%p[(%u,%u)-(%u,%u)]\n", This
, pDirtyRect
,
263 pDirtyRect
? pDirtyRect
->left
: 0, pDirtyRect
? pDirtyRect
->top
: 0,
264 pDirtyRect
? pDirtyRect
->right
: 0, pDirtyRect
? pDirtyRect
->bottom
: 0);
266 /* Tracking dirty regions on DEFAULT or SYSTEMMEM resources is pointless,
267 * because we always write to the final storage. Just marked it dirty in
268 * case we need to generate mip maps.
270 if (This
->base
.base
.pool
!= D3DPOOL_MANAGED
) {
271 if (This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
272 This
->base
.dirty_mip
= TRUE
;
275 This
->base
.dirty
= TRUE
;
277 BASETEX_REGISTER_UPDATE(&This
->base
);
280 u_box_origin_2d(This
->base
.base
.info
.width0
,
281 This
->base
.base
.info
.height0
, &This
->dirty_rect
);
284 rect_to_pipe_box_clamp(&box
, pDirtyRect
);
285 u_box_union_2d(&This
->dirty_rect
, &This
->dirty_rect
, &box
);
290 IDirect3DTexture9Vtbl NineTexture9_vtable
= {
291 (void *)NineUnknown_QueryInterface
,
292 (void *)NineUnknown_AddRef
,
293 (void *)NineUnknown_Release
,
294 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
295 (void *)NineResource9_SetPrivateData
,
296 (void *)NineResource9_GetPrivateData
,
297 (void *)NineResource9_FreePrivateData
,
298 (void *)NineResource9_SetPriority
,
299 (void *)NineResource9_GetPriority
,
300 (void *)NineBaseTexture9_PreLoad
,
301 (void *)NineResource9_GetType
,
302 (void *)NineBaseTexture9_SetLOD
,
303 (void *)NineBaseTexture9_GetLOD
,
304 (void *)NineBaseTexture9_GetLevelCount
,
305 (void *)NineBaseTexture9_SetAutoGenFilterType
,
306 (void *)NineBaseTexture9_GetAutoGenFilterType
,
307 (void *)NineBaseTexture9_GenerateMipSubLevels
,
308 (void *)NineTexture9_GetLevelDesc
,
309 (void *)NineTexture9_GetSurfaceLevel
,
310 (void *)NineTexture9_LockRect
,
311 (void *)NineTexture9_UnlockRect
,
312 (void *)NineTexture9_AddDirtyRect
315 static const GUID
*NineTexture9_IIDs
[] = {
316 &IID_IDirect3DTexture9
,
317 &IID_IDirect3DBaseTexture9
,
318 &IID_IDirect3DResource9
,
324 NineTexture9_new( struct NineDevice9
*pDevice
,
325 UINT Width
, UINT Height
, UINT Levels
,
329 struct NineTexture9
**ppOut
,
330 HANDLE
*pSharedHandle
)
332 NINE_DEVICE_CHILD_NEW(Texture9
, ppOut
, pDevice
,
333 Width
, Height
, Levels
,
334 Usage
, Format
, Pool
, pSharedHandle
);