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. */
23 #include "c99_alloca.h"
28 #include "nine_helpers.h"
29 #include "nine_pipe.h"
30 #include "nine_dump.h"
32 #include "pipe/p_state.h"
33 #include "pipe/p_context.h"
34 #include "pipe/p_screen.h"
35 #include "util/u_inlines.h"
36 #include "util/u_resource.h"
38 #define DBG_CHANNEL DBG_TEXTURE
41 NineTexture9_ctor( struct NineTexture9
*This
,
42 struct NineUnknownParams
*pParams
,
43 UINT Width
, UINT Height
, UINT Levels
,
47 HANDLE
*pSharedHandle
)
49 struct pipe_screen
*screen
= pParams
->device
->screen
;
50 struct pipe_resource
*info
= &This
->base
.base
.info
;
51 struct pipe_resource
*resource
;
53 unsigned *level_offsets
;
55 D3DSURFACE_DESC sfdesc
;
57 void *user_buffer
= NULL
, *user_buffer_for_level
;
59 DBG("(%p) Width=%u Height=%u Levels=%u Usage=%s Format=%s Pool=%s "
60 "pSharedHandle=%p\n", This
, Width
, Height
, Levels
,
61 nine_D3DUSAGE_to_str(Usage
),
62 d3dformat_to_string(Format
), nine_D3DPOOL_to_str(Pool
), pSharedHandle
);
64 user_assert(!(Usage
& D3DUSAGE_AUTOGENMIPMAP
) ||
65 (Pool
!= D3DPOOL_SYSTEMMEM
&& Levels
<= 1), D3DERR_INVALIDCALL
);
67 /* TODO: implement buffer sharing (should work with cross process too)
69 * Gem names may have fit but they're depreciated and won't work on render-nodes.
70 * One solution is to use shm buffers. We would use a /dev/shm file, fill the first
71 * values to tell it is a nine buffer, the size, which function created it, etc,
72 * and then it would contain the data. The handle would be a number, corresponding to
73 * the file to read (/dev/shm/nine-share-4 for example would be 4).
75 * Wine just ignores the argument, which works only if the app creates the handle
76 * and won't use it. Instead of failing, we support that situation by putting an
77 * invalid handle, that we would fail to import. Please note that we don't advertise
78 * the flag indicating the support for that feature, but apps seem to not care.
80 user_assert(!pSharedHandle
||
81 Pool
== D3DPOOL_SYSTEMMEM
||
82 Pool
== D3DPOOL_DEFAULT
, D3DERR_INVALIDCALL
);
84 if (pSharedHandle
&& Pool
== D3DPOOL_DEFAULT
) {
85 if (!*pSharedHandle
) {
86 DBG("Creating Texture with invalid handle. Importing will fail\n.");
87 *pSharedHandle
= (HANDLE
)1; /* Wine would keep it NULL */
90 ERR("Application tries to use cross-process sharing feature. Nine "
91 "doesn't support it");
92 return D3DERR_INVALIDCALL
;
96 if (Usage
& D3DUSAGE_AUTOGENMIPMAP
)
99 pf
= d3d9_to_pipe_format_checked(screen
, Format
, PIPE_TEXTURE_2D
, 0,
100 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
101 if (Format
!= D3DFMT_NULL
&& pf
== PIPE_FORMAT_NONE
)
102 return D3DERR_INVALIDCALL
;
104 info
->screen
= screen
;
105 info
->target
= PIPE_TEXTURE_2D
;
107 info
->width0
= Width
;
108 info
->height0
= Height
;
111 info
->last_level
= Levels
- 1;
113 info
->last_level
= util_logbase2(MAX2(Width
, Height
));
114 info
->array_size
= 1;
115 info
->nr_samples
= 0;
116 info
->bind
= PIPE_BIND_SAMPLER_VIEW
;
117 info
->usage
= PIPE_USAGE_DEFAULT
;
120 if (Usage
& D3DUSAGE_RENDERTARGET
)
121 info
->bind
|= PIPE_BIND_RENDER_TARGET
;
122 if (Usage
& D3DUSAGE_DEPTHSTENCIL
)
123 info
->bind
|= PIPE_BIND_DEPTH_STENCIL
;
125 if (Usage
& D3DUSAGE_DYNAMIC
) {
126 info
->usage
= PIPE_USAGE_DYNAMIC
;
128 PIPE_BIND_TRANSFER_READ
|
129 PIPE_BIND_TRANSFER_WRITE
;
132 if (Usage
& D3DUSAGE_SOFTWAREPROCESSING
)
133 DBG("Application asked for Software Vertex Processing, "
134 "but this is unimplemented\n");
137 info
->bind
|= PIPE_BIND_SHARED
;
139 if (Pool
== D3DPOOL_SYSTEMMEM
)
140 info
->usage
= PIPE_USAGE_STAGING
;
142 if (pSharedHandle
&& *pSharedHandle
) { /* Pool == D3DPOOL_SYSTEMMEM */
143 user_buffer
= (void *)*pSharedHandle
;
144 level_offsets
= alloca(sizeof(unsigned) * (info
->last_level
+ 1));
145 (void) nine_format_get_size_and_offsets(pf
, level_offsets
,
148 } else if (Pool
!= D3DPOOL_DEFAULT
) {
149 level_offsets
= alloca(sizeof(unsigned) * (info
->last_level
+ 1));
150 user_buffer
= MALLOC(
151 nine_format_get_size_and_offsets(pf
, level_offsets
,
154 This
->managed_buffer
= user_buffer
;
155 if (!This
->managed_buffer
)
156 return E_OUTOFMEMORY
;
159 This
->surfaces
= CALLOC(info
->last_level
+ 1, sizeof(*This
->surfaces
));
161 return E_OUTOFMEMORY
;
163 hr
= NineBaseTexture9_ctor(&This
->base
, pParams
, NULL
, D3DRTYPE_TEXTURE
, Format
, Pool
, Usage
);
166 This
->base
.pstype
= (Height
== 1) ? 1 : 0;
168 /* Create all the surfaces right away.
169 * They manage backing storage, and transfers (LockRect) are deferred
172 sfdesc
.Format
= Format
;
173 sfdesc
.Type
= D3DRTYPE_SURFACE
;
174 sfdesc
.Usage
= Usage
;
176 sfdesc
.MultiSampleType
= D3DMULTISAMPLE_NONE
;
177 sfdesc
.MultiSampleQuality
= 0;
179 if (Pool
== D3DPOOL_SYSTEMMEM
)
182 resource
= This
->base
.base
.resource
;
184 for (l
= 0; l
<= info
->last_level
; ++l
) {
185 sfdesc
.Width
= u_minify(Width
, l
);
186 sfdesc
.Height
= u_minify(Height
, l
);
187 /* Some apps expect the memory to be allocated in
188 * continous blocks */
189 user_buffer_for_level
= user_buffer
? user_buffer
+
190 level_offsets
[l
] : NULL
;
192 hr
= NineSurface9_new(This
->base
.base
.base
.device
, NineUnknown(This
),
193 resource
, user_buffer_for_level
,
194 D3DRTYPE_TEXTURE
, l
, 0,
195 &sfdesc
, &This
->surfaces
[l
]);
200 This
->dirty_rect
.depth
= 1; /* widht == 0 means empty, depth stays 1 */
202 if (pSharedHandle
&& !*pSharedHandle
) {/* Pool == D3DPOOL_SYSTEMMEM */
203 *pSharedHandle
= This
->surfaces
[0]->data
;
210 NineTexture9_dtor( struct NineTexture9
*This
)
214 if (This
->surfaces
) {
215 /* The surfaces should have 0 references and be unbound now. */
216 for (l
= 0; l
<= This
->base
.base
.info
.last_level
; ++l
)
217 NineUnknown_Destroy(&This
->surfaces
[l
]->base
.base
);
218 FREE(This
->surfaces
);
221 if (This
->managed_buffer
)
222 FREE(This
->managed_buffer
);
224 NineBaseTexture9_dtor(&This
->base
);
228 NineTexture9_GetLevelDesc( struct NineTexture9
*This
,
230 D3DSURFACE_DESC
*pDesc
)
232 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
233 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
236 *pDesc
= This
->surfaces
[Level
]->desc
;
242 NineTexture9_GetSurfaceLevel( struct NineTexture9
*This
,
244 IDirect3DSurface9
**ppSurfaceLevel
)
246 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
247 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
250 NineUnknown_AddRef(NineUnknown(This
->surfaces
[Level
]));
251 *ppSurfaceLevel
= (IDirect3DSurface9
*)This
->surfaces
[Level
];
257 NineTexture9_LockRect( struct NineTexture9
*This
,
259 D3DLOCKED_RECT
*pLockedRect
,
263 DBG("This=%p Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
264 This
, Level
, pLockedRect
, pRect
, Flags
);
266 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
267 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
270 return NineSurface9_LockRect(This
->surfaces
[Level
], pLockedRect
,
275 NineTexture9_UnlockRect( struct NineTexture9
*This
,
278 DBG("This=%p Level=%u\n", This
, Level
);
280 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
282 return NineSurface9_UnlockRect(This
->surfaces
[Level
]);
286 NineTexture9_AddDirtyRect( struct NineTexture9
*This
,
287 const RECT
*pDirtyRect
)
289 DBG("This=%p pDirtyRect=%p[(%u,%u)-(%u,%u)]\n", This
, pDirtyRect
,
290 pDirtyRect
? pDirtyRect
->left
: 0, pDirtyRect
? pDirtyRect
->top
: 0,
291 pDirtyRect
? pDirtyRect
->right
: 0, pDirtyRect
? pDirtyRect
->bottom
: 0);
293 /* Tracking dirty regions on DEFAULT or SYSTEMMEM resources is pointless,
294 * because we always write to the final storage. Just marked it dirty in
295 * case we need to generate mip maps.
297 if (This
->base
.base
.pool
!= D3DPOOL_MANAGED
) {
298 if (This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
299 This
->base
.dirty_mip
= TRUE
;
302 This
->base
.managed
.dirty
= TRUE
;
304 BASETEX_REGISTER_UPDATE(&This
->base
);
307 u_box_origin_2d(This
->base
.base
.info
.width0
,
308 This
->base
.base
.info
.height0
, &This
->dirty_rect
);
311 rect_to_pipe_box_clamp(&box
, pDirtyRect
);
312 u_box_union_2d(&This
->dirty_rect
, &This
->dirty_rect
, &box
);
313 (void) u_box_clip_2d(&This
->dirty_rect
, &This
->dirty_rect
,
314 This
->base
.base
.info
.width0
,
315 This
->base
.base
.info
.height0
);
320 IDirect3DTexture9Vtbl NineTexture9_vtable
= {
321 (void *)NineUnknown_QueryInterface
,
322 (void *)NineUnknown_AddRef
,
323 (void *)NineUnknown_Release
,
324 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
325 (void *)NineResource9_SetPrivateData
,
326 (void *)NineResource9_GetPrivateData
,
327 (void *)NineResource9_FreePrivateData
,
328 (void *)NineResource9_SetPriority
,
329 (void *)NineResource9_GetPriority
,
330 (void *)NineBaseTexture9_PreLoad
,
331 (void *)NineResource9_GetType
,
332 (void *)NineBaseTexture9_SetLOD
,
333 (void *)NineBaseTexture9_GetLOD
,
334 (void *)NineBaseTexture9_GetLevelCount
,
335 (void *)NineBaseTexture9_SetAutoGenFilterType
,
336 (void *)NineBaseTexture9_GetAutoGenFilterType
,
337 (void *)NineBaseTexture9_GenerateMipSubLevels
,
338 (void *)NineTexture9_GetLevelDesc
,
339 (void *)NineTexture9_GetSurfaceLevel
,
340 (void *)NineTexture9_LockRect
,
341 (void *)NineTexture9_UnlockRect
,
342 (void *)NineTexture9_AddDirtyRect
345 static const GUID
*NineTexture9_IIDs
[] = {
346 &IID_IDirect3DTexture9
,
347 &IID_IDirect3DBaseTexture9
,
348 &IID_IDirect3DResource9
,
354 NineTexture9_new( struct NineDevice9
*pDevice
,
355 UINT Width
, UINT Height
, UINT Levels
,
359 struct NineTexture9
**ppOut
,
360 HANDLE
*pSharedHandle
)
362 NINE_DEVICE_CHILD_NEW(Texture9
, ppOut
, pDevice
,
363 Width
, Height
, Levels
,
364 Usage
, Format
, Pool
, pSharedHandle
);