st/nine: Clean pSharedHandle Texture ctors checks
[mesa.git] / src / gallium / state_trackers / nine / cubetexture9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "c99_alloca.h"
24
25 #include "device9.h"
26 #include "cubetexture9.h"
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
29
30 #define DBG_CHANNEL DBG_CUBETEXTURE
31
32
33 static HRESULT
34 NineCubeTexture9_ctor( struct NineCubeTexture9 *This,
35 struct NineUnknownParams *pParams,
36 UINT EdgeLength, UINT Levels,
37 DWORD Usage,
38 D3DFORMAT Format,
39 D3DPOOL Pool,
40 HANDLE *pSharedHandle )
41 {
42 struct pipe_resource *info = &This->base.base.info;
43 struct pipe_screen *screen = pParams->device->screen;
44 enum pipe_format pf;
45 unsigned i, l, f, offset, face_size = 0;
46 unsigned *level_offsets = NULL;
47 D3DSURFACE_DESC sfdesc;
48 void *p;
49 HRESULT hr;
50
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);
55
56 user_assert(EdgeLength, D3DERR_INVALIDCALL);
57
58 /* user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL); */
59 user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */
60
61 user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP) ||
62 (Pool != D3DPOOL_SYSTEMMEM && Levels <= 1), D3DERR_INVALIDCALL);
63
64 if (Usage & D3DUSAGE_AUTOGENMIPMAP)
65 Levels = 0;
66
67 pf = d3d9_to_pipe_format_checked(screen, Format, PIPE_TEXTURE_CUBE, 0,
68 PIPE_BIND_SAMPLER_VIEW, FALSE);
69 if (pf == PIPE_FORMAT_NONE)
70 return D3DERR_INVALIDCALL;
71
72 /* We support ATI1 and ATI2 hacks only for 2D textures */
73 if (Format == D3DFMT_ATI1 || Format == D3DFMT_ATI2)
74 return D3DERR_INVALIDCALL;
75
76 if (compressed_format(Format)) {
77 const unsigned w = util_format_get_blockwidth(pf);
78 const unsigned h = util_format_get_blockheight(pf);
79
80 user_assert(!(EdgeLength % w) && !(EdgeLength % h), D3DERR_INVALIDCALL);
81 }
82
83 info->screen = pParams->device->screen;
84 info->target = PIPE_TEXTURE_CUBE;
85 info->format = pf;
86 info->width0 = EdgeLength;
87 info->height0 = EdgeLength;
88 info->depth0 = 1;
89 if (Levels)
90 info->last_level = Levels - 1;
91 else
92 info->last_level = util_logbase2(EdgeLength);
93 info->array_size = 6;
94 info->nr_samples = 0;
95 info->bind = PIPE_BIND_SAMPLER_VIEW;
96 info->usage = PIPE_USAGE_DEFAULT;
97 info->flags = 0;
98
99 if (Usage & D3DUSAGE_RENDERTARGET)
100 info->bind |= PIPE_BIND_RENDER_TARGET;
101 if (Usage & D3DUSAGE_DEPTHSTENCIL)
102 info->bind |= PIPE_BIND_DEPTH_STENCIL;
103
104 if (Usage & D3DUSAGE_DYNAMIC) {
105 info->usage = PIPE_USAGE_DYNAMIC;
106 info->bind |=
107 PIPE_BIND_TRANSFER_READ |
108 PIPE_BIND_TRANSFER_WRITE;
109 }
110 if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
111 DBG("Application asked for Software Vertex Processing, "
112 "but this is unimplemented\n");
113
114 if (Pool != D3DPOOL_DEFAULT) {
115 level_offsets = alloca(sizeof(unsigned) * (info->last_level + 1));
116 face_size = nine_format_get_size_and_offsets(pf, level_offsets,
117 EdgeLength, EdgeLength,
118 info->last_level);
119 This->managed_buffer = align_malloc(6 * face_size, 32);
120 if (!This->managed_buffer)
121 return E_OUTOFMEMORY;
122 }
123
124 This->surfaces = CALLOC(6 * (info->last_level + 1), sizeof(*This->surfaces));
125 if (!This->surfaces)
126 return E_OUTOFMEMORY;
127
128 hr = NineBaseTexture9_ctor(&This->base, pParams, NULL, D3DRTYPE_CUBETEXTURE,
129 Format, Pool, Usage);
130 if (FAILED(hr))
131 return hr;
132 This->base.pstype = 2;
133
134 /* Create all the surfaces right away.
135 * They manage backing storage, and transfers (LockRect) are deferred
136 * to them.
137 */
138 sfdesc.Format = Format;
139 sfdesc.Type = D3DRTYPE_SURFACE;
140 sfdesc.Usage = Usage;
141 sfdesc.Pool = Pool;
142 sfdesc.MultiSampleType = D3DMULTISAMPLE_NONE;
143 sfdesc.MultiSampleQuality = 0;
144 /* We allocate the memory for the surfaces as continous blocks.
145 * This is the expected behaviour, however we haven't tested for
146 * cube textures in which order the faces/levels should be in memory
147 */
148 for (f = 0; f < 6; f++) {
149 offset = f * face_size;
150 for (l = 0; l <= info->last_level; l++) {
151 sfdesc.Width = sfdesc.Height = u_minify(EdgeLength, l);
152 p = This->managed_buffer ? This->managed_buffer + offset +
153 level_offsets[l] : NULL;
154
155 hr = NineSurface9_new(This->base.base.base.device, NineUnknown(This),
156 This->base.base.resource, p, D3DRTYPE_CUBETEXTURE,
157 l, f, &sfdesc, &This->surfaces[f + 6 * l]);
158 if (FAILED(hr))
159 return hr;
160 }
161 }
162
163 for (i = 0; i < 6; ++i) {
164 /* Textures start initially dirty */
165 This->dirty_rect[i].width = EdgeLength;
166 This->dirty_rect[i].height = EdgeLength;
167 This->dirty_rect[i].depth = 1;
168 }
169
170 return D3D_OK;
171 }
172
173 static void
174 NineCubeTexture9_dtor( struct NineCubeTexture9 *This )
175 {
176 unsigned i;
177
178 DBG("This=%p\n", This);
179
180 if (This->surfaces) {
181 for (i = 0; i < This->base.base.info.last_level * 6; ++i)
182 NineUnknown_Destroy(&This->surfaces[i]->base.base);
183 FREE(This->surfaces);
184 }
185
186 if (This->managed_buffer)
187 align_free(This->managed_buffer);
188
189 NineBaseTexture9_dtor(&This->base);
190 }
191
192 HRESULT WINAPI
193 NineCubeTexture9_GetLevelDesc( struct NineCubeTexture9 *This,
194 UINT Level,
195 D3DSURFACE_DESC *pDesc )
196 {
197 DBG("This=%p Level=%u pDesc=%p\n", This, Level, pDesc);
198
199 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
200 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
201 D3DERR_INVALIDCALL);
202
203 *pDesc = This->surfaces[Level * 6]->desc;
204
205 return D3D_OK;
206 }
207
208 HRESULT WINAPI
209 NineCubeTexture9_GetCubeMapSurface( struct NineCubeTexture9 *This,
210 D3DCUBEMAP_FACES FaceType,
211 UINT Level,
212 IDirect3DSurface9 **ppCubeMapSurface )
213 {
214 const unsigned s = Level * 6 + FaceType;
215
216 DBG("This=%p FaceType=%d Level=%u ppCubeMapSurface=%p\n",
217 This, FaceType, Level, ppCubeMapSurface);
218
219 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
220 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
221 D3DERR_INVALIDCALL);
222 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
223
224 NineUnknown_AddRef(NineUnknown(This->surfaces[s]));
225 *ppCubeMapSurface = (IDirect3DSurface9 *)This->surfaces[s];
226
227 return D3D_OK;
228 }
229
230 HRESULT WINAPI
231 NineCubeTexture9_LockRect( struct NineCubeTexture9 *This,
232 D3DCUBEMAP_FACES FaceType,
233 UINT Level,
234 D3DLOCKED_RECT *pLockedRect,
235 const RECT *pRect,
236 DWORD Flags )
237 {
238 const unsigned s = Level * 6 + FaceType;
239
240 DBG("This=%p FaceType=%d Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
241 This, FaceType, Level, pLockedRect, pRect, Flags);
242
243 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
244 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
245 D3DERR_INVALIDCALL);
246 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
247
248 return NineSurface9_LockRect(This->surfaces[s], pLockedRect, pRect, Flags);
249 }
250
251 HRESULT WINAPI
252 NineCubeTexture9_UnlockRect( struct NineCubeTexture9 *This,
253 D3DCUBEMAP_FACES FaceType,
254 UINT Level )
255 {
256 const unsigned s = Level * 6 + FaceType;
257
258 DBG("This=%p FaceType=%d Level=%u\n", This, FaceType, Level);
259
260 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
261 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
262
263 return NineSurface9_UnlockRect(This->surfaces[s]);
264 }
265
266 HRESULT WINAPI
267 NineCubeTexture9_AddDirtyRect( struct NineCubeTexture9 *This,
268 D3DCUBEMAP_FACES FaceType,
269 const RECT *pDirtyRect )
270 {
271 DBG("This=%p FaceType=%d pDirtyRect=%p\n", This, FaceType, pDirtyRect);
272
273 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
274
275 if (This->base.base.pool != D3DPOOL_MANAGED) {
276 if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP) {
277 This->base.dirty_mip = TRUE;
278 BASETEX_REGISTER_UPDATE(&This->base);
279 }
280 return D3D_OK;
281 }
282
283 if (This->base.base.pool == D3DPOOL_MANAGED) {
284 This->base.managed.dirty = TRUE;
285 BASETEX_REGISTER_UPDATE(&This->base);
286 }
287
288 if (!pDirtyRect) {
289 u_box_origin_2d(This->base.base.info.width0,
290 This->base.base.info.height0,
291 &This->dirty_rect[FaceType]);
292 } else {
293 struct pipe_box box;
294 rect_to_pipe_box_clamp(&box, pDirtyRect);
295 u_box_union_2d(&This->dirty_rect[FaceType], &This->dirty_rect[FaceType],
296 &box);
297 (void) u_box_clip_2d(&This->dirty_rect[FaceType],
298 &This->dirty_rect[FaceType],
299 This->base.base.info.width0,
300 This->base.base.info.height0);
301 }
302 return D3D_OK;
303 }
304
305 IDirect3DCubeTexture9Vtbl NineCubeTexture9_vtable = {
306 (void *)NineUnknown_QueryInterface,
307 (void *)NineUnknown_AddRef,
308 (void *)NineUnknown_Release,
309 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
310 (void *)NineResource9_SetPrivateData,
311 (void *)NineResource9_GetPrivateData,
312 (void *)NineResource9_FreePrivateData,
313 (void *)NineResource9_SetPriority,
314 (void *)NineResource9_GetPriority,
315 (void *)NineBaseTexture9_PreLoad,
316 (void *)NineResource9_GetType,
317 (void *)NineBaseTexture9_SetLOD,
318 (void *)NineBaseTexture9_GetLOD,
319 (void *)NineBaseTexture9_GetLevelCount,
320 (void *)NineBaseTexture9_SetAutoGenFilterType,
321 (void *)NineBaseTexture9_GetAutoGenFilterType,
322 (void *)NineBaseTexture9_GenerateMipSubLevels,
323 (void *)NineCubeTexture9_GetLevelDesc,
324 (void *)NineCubeTexture9_GetCubeMapSurface,
325 (void *)NineCubeTexture9_LockRect,
326 (void *)NineCubeTexture9_UnlockRect,
327 (void *)NineCubeTexture9_AddDirtyRect
328 };
329
330 static const GUID *NineCubeTexture9_IIDs[] = {
331 &IID_IDirect3DCubeTexture9,
332 &IID_IDirect3DBaseTexture9,
333 &IID_IDirect3DResource9,
334 &IID_IUnknown,
335 NULL
336 };
337
338 HRESULT
339 NineCubeTexture9_new( struct NineDevice9 *pDevice,
340 UINT EdgeLength, UINT Levels,
341 DWORD Usage,
342 D3DFORMAT Format,
343 D3DPOOL Pool,
344 struct NineCubeTexture9 **ppOut,
345 HANDLE *pSharedHandle )
346 {
347 NINE_DEVICE_CHILD_NEW(CubeTexture9, ppOut, pDevice,
348 EdgeLength, Levels,
349 Usage, Format, Pool, pSharedHandle);
350 }