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"
26 #include "cubetexture9.h"
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
30 #define DBG_CHANNEL DBG_CUBETEXTURE
34 NineCubeTexture9_ctor( struct NineCubeTexture9
*This
,
35 struct NineUnknownParams
*pParams
,
36 UINT EdgeLength
, UINT Levels
,
40 HANDLE
*pSharedHandle
)
42 struct pipe_resource
*info
= &This
->base
.base
.info
;
43 struct pipe_screen
*screen
= pParams
->device
->screen
;
45 unsigned i
, l
, f
, offset
, face_size
= 0;
46 unsigned *level_offsets
;
47 D3DSURFACE_DESC sfdesc
;
51 DBG("This=%p pParams=%p EdgeLength=%u Levels=%u Usage=%d "
52 "Format=%d Pool=%d pSharedHandle=%p\n",
53 This
, pParams
, EdgeLength
, Levels
, Usage
,
54 Format
, Pool
, pSharedHandle
);
56 user_assert(!(Usage
& D3DUSAGE_AUTOGENMIPMAP
) ||
57 (Pool
!= D3DPOOL_SYSTEMMEM
&& Levels
<= 1), D3DERR_INVALIDCALL
);
59 user_assert(!pSharedHandle
, D3DERR_INVALIDCALL
); /* TODO */
61 if (Usage
& D3DUSAGE_AUTOGENMIPMAP
)
64 pf
= d3d9_to_pipe_format_checked(screen
, Format
, PIPE_TEXTURE_CUBE
, 0,
65 PIPE_BIND_SAMPLER_VIEW
, FALSE
);
66 if (pf
== PIPE_FORMAT_NONE
)
67 return D3DERR_INVALIDCALL
;
69 /* We support ATI1 and ATI2 hacks only for 2D textures */
70 if (Format
== D3DFMT_ATI1
|| Format
== D3DFMT_ATI2
)
71 return D3DERR_INVALIDCALL
;
73 info
->screen
= pParams
->device
->screen
;
74 info
->target
= PIPE_TEXTURE_CUBE
;
76 info
->width0
= EdgeLength
;
77 info
->height0
= EdgeLength
;
80 info
->last_level
= Levels
- 1;
82 info
->last_level
= util_logbase2(EdgeLength
);
85 info
->bind
= PIPE_BIND_SAMPLER_VIEW
;
86 info
->usage
= PIPE_USAGE_DEFAULT
;
89 if (Usage
& D3DUSAGE_RENDERTARGET
)
90 info
->bind
|= PIPE_BIND_RENDER_TARGET
;
91 if (Usage
& D3DUSAGE_DEPTHSTENCIL
)
92 info
->bind
|= PIPE_BIND_DEPTH_STENCIL
;
94 if (Usage
& D3DUSAGE_DYNAMIC
) {
95 info
->usage
= PIPE_USAGE_DYNAMIC
;
97 PIPE_BIND_TRANSFER_READ
|
98 PIPE_BIND_TRANSFER_WRITE
;
100 if (Usage
& D3DUSAGE_SOFTWAREPROCESSING
)
101 DBG("Application asked for Software Vertex Processing, "
102 "but this is unimplemented\n");
104 if (Pool
!= D3DPOOL_DEFAULT
) {
105 level_offsets
= alloca(sizeof(unsigned) * (info
->last_level
+ 1));
106 face_size
= nine_format_get_size_and_offsets(pf
, level_offsets
,
107 EdgeLength
, EdgeLength
,
109 This
->managed_buffer
= MALLOC(6 * face_size
);
110 if (!This
->managed_buffer
)
111 return E_OUTOFMEMORY
;
114 This
->surfaces
= CALLOC(6 * (info
->last_level
+ 1), sizeof(*This
->surfaces
));
116 return E_OUTOFMEMORY
;
118 hr
= NineBaseTexture9_ctor(&This
->base
, pParams
, NULL
, D3DRTYPE_CUBETEXTURE
,
119 Format
, Pool
, Usage
);
122 This
->base
.pstype
= 2;
124 /* Create all the surfaces right away.
125 * They manage backing storage, and transfers (LockRect) are deferred
128 sfdesc
.Format
= Format
;
129 sfdesc
.Type
= D3DRTYPE_SURFACE
;
130 sfdesc
.Usage
= Usage
;
132 sfdesc
.MultiSampleType
= D3DMULTISAMPLE_NONE
;
133 sfdesc
.MultiSampleQuality
= 0;
134 /* We allocate the memory for the surfaces as continous blocks.
135 * This is the expected behaviour, however we haven't tested for
136 * cube textures in which order the faces/levels should be in memory
138 for (f
= 0; f
< 6; f
++) {
139 offset
= f
* face_size
;
140 for (l
= 0; l
<= info
->last_level
; l
++) {
141 sfdesc
.Width
= sfdesc
.Height
= u_minify(EdgeLength
, l
);
142 p
= This
->managed_buffer
? This
->managed_buffer
+ offset
+
143 level_offsets
[l
] : NULL
;
145 hr
= NineSurface9_new(This
->base
.base
.base
.device
, NineUnknown(This
),
146 This
->base
.base
.resource
, p
, D3DRTYPE_CUBETEXTURE
,
147 l
, f
, &sfdesc
, &This
->surfaces
[f
+ 6 * l
]);
153 for (i
= 0; i
< 6; ++i
) /* width = 0 means empty, depth stays 1 */
154 This
->dirty_rect
[i
].depth
= 1;
160 NineCubeTexture9_dtor( struct NineCubeTexture9
*This
)
164 DBG("This=%p\n", This
);
166 if (This
->surfaces
) {
167 for (i
= 0; i
< This
->base
.base
.info
.last_level
* 6; ++i
)
168 NineUnknown_Destroy(&This
->surfaces
[i
]->base
.base
);
169 FREE(This
->surfaces
);
172 if (This
->managed_buffer
)
173 FREE(This
->managed_buffer
);
175 NineBaseTexture9_dtor(&This
->base
);
179 NineCubeTexture9_GetLevelDesc( struct NineCubeTexture9
*This
,
181 D3DSURFACE_DESC
*pDesc
)
183 DBG("This=%p Level=%u pDesc=%p\n", This
, Level
, pDesc
);
185 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
186 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
189 *pDesc
= This
->surfaces
[Level
* 6]->desc
;
195 NineCubeTexture9_GetCubeMapSurface( struct NineCubeTexture9
*This
,
196 D3DCUBEMAP_FACES FaceType
,
198 IDirect3DSurface9
**ppCubeMapSurface
)
200 const unsigned s
= Level
* 6 + FaceType
;
202 DBG("This=%p FaceType=%d Level=%u ppCubeMapSurface=%p\n",
203 This
, FaceType
, Level
, ppCubeMapSurface
);
205 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
206 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
208 user_assert(FaceType
< 6, D3DERR_INVALIDCALL
);
210 NineUnknown_AddRef(NineUnknown(This
->surfaces
[s
]));
211 *ppCubeMapSurface
= (IDirect3DSurface9
*)This
->surfaces
[s
];
217 NineCubeTexture9_LockRect( struct NineCubeTexture9
*This
,
218 D3DCUBEMAP_FACES FaceType
,
220 D3DLOCKED_RECT
*pLockedRect
,
224 const unsigned s
= Level
* 6 + FaceType
;
226 DBG("This=%p FaceType=%d Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
227 This
, FaceType
, Level
, pLockedRect
, pRect
, Flags
);
229 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
230 user_assert(Level
== 0 || !(This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
),
232 user_assert(FaceType
< 6, D3DERR_INVALIDCALL
);
234 return NineSurface9_LockRect(This
->surfaces
[s
], pLockedRect
, pRect
, Flags
);
238 NineCubeTexture9_UnlockRect( struct NineCubeTexture9
*This
,
239 D3DCUBEMAP_FACES FaceType
,
242 const unsigned s
= Level
* 6 + FaceType
;
244 DBG("This=%p FaceType=%d Level=%u\n", This
, FaceType
, Level
);
246 user_assert(Level
<= This
->base
.base
.info
.last_level
, D3DERR_INVALIDCALL
);
247 user_assert(FaceType
< 6, D3DERR_INVALIDCALL
);
249 return NineSurface9_UnlockRect(This
->surfaces
[s
]);
253 NineCubeTexture9_AddDirtyRect( struct NineCubeTexture9
*This
,
254 D3DCUBEMAP_FACES FaceType
,
255 const RECT
*pDirtyRect
)
257 DBG("This=%p FaceType=%d pDirtyRect=%p\n", This
, FaceType
, pDirtyRect
);
259 user_assert(FaceType
< 6, D3DERR_INVALIDCALL
);
261 if (This
->base
.base
.pool
!= D3DPOOL_MANAGED
) {
262 if (This
->base
.base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
263 This
->base
.dirty_mip
= TRUE
;
266 This
->base
.managed
.dirty
= TRUE
;
268 BASETEX_REGISTER_UPDATE(&This
->base
);
271 u_box_origin_2d(This
->base
.base
.info
.width0
,
272 This
->base
.base
.info
.height0
,
273 &This
->dirty_rect
[FaceType
]);
276 rect_to_pipe_box_clamp(&box
, pDirtyRect
);
277 u_box_union_2d(&This
->dirty_rect
[FaceType
], &This
->dirty_rect
[FaceType
],
283 IDirect3DCubeTexture9Vtbl NineCubeTexture9_vtable
= {
284 (void *)NineUnknown_QueryInterface
,
285 (void *)NineUnknown_AddRef
,
286 (void *)NineUnknown_Release
,
287 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
288 (void *)NineResource9_SetPrivateData
,
289 (void *)NineResource9_GetPrivateData
,
290 (void *)NineResource9_FreePrivateData
,
291 (void *)NineResource9_SetPriority
,
292 (void *)NineResource9_GetPriority
,
293 (void *)NineBaseTexture9_PreLoad
,
294 (void *)NineResource9_GetType
,
295 (void *)NineBaseTexture9_SetLOD
,
296 (void *)NineBaseTexture9_GetLOD
,
297 (void *)NineBaseTexture9_GetLevelCount
,
298 (void *)NineBaseTexture9_SetAutoGenFilterType
,
299 (void *)NineBaseTexture9_GetAutoGenFilterType
,
300 (void *)NineBaseTexture9_GenerateMipSubLevels
,
301 (void *)NineCubeTexture9_GetLevelDesc
,
302 (void *)NineCubeTexture9_GetCubeMapSurface
,
303 (void *)NineCubeTexture9_LockRect
,
304 (void *)NineCubeTexture9_UnlockRect
,
305 (void *)NineCubeTexture9_AddDirtyRect
308 static const GUID
*NineCubeTexture9_IIDs
[] = {
309 &IID_IDirect3DCubeTexture9
,
310 &IID_IDirect3DBaseTexture9
,
311 &IID_IDirect3DResource9
,
317 NineCubeTexture9_new( struct NineDevice9
*pDevice
,
318 UINT EdgeLength
, UINT Levels
,
322 struct NineCubeTexture9
**ppOut
,
323 HANDLE
*pSharedHandle
)
325 NINE_DEVICE_CHILD_NEW(CubeTexture9
, ppOut
, pDevice
,
327 Usage
, Format
, Pool
, pSharedHandle
);