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