st/nine: Return D3DERR_INVALIDCALL when trying to create a texture of bad format
[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 "device9.h"
24 #include "cubetexture9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27
28 #define DBG_CHANNEL DBG_CUBETEXTURE
29
30
31 static HRESULT
32 NineCubeTexture9_ctor( struct NineCubeTexture9 *This,
33 struct NineUnknownParams *pParams,
34 UINT EdgeLength, UINT Levels,
35 DWORD Usage,
36 D3DFORMAT Format,
37 D3DPOOL Pool,
38 HANDLE *pSharedHandle )
39 {
40 struct pipe_resource *info = &This->base.base.info;
41 struct pipe_screen *screen = pParams->device->screen;
42 enum pipe_format pf;
43 unsigned i;
44 D3DSURFACE_DESC sfdesc;
45 HRESULT hr;
46
47 DBG("This=%p pParams=%p EdgeLength=%u Levels=%u Usage=%d "
48 "Format=%d Pool=%d pSharedHandle=%p\n",
49 This, pParams, EdgeLength, Levels, Usage,
50 Format, Pool, pSharedHandle);
51
52 user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP) ||
53 (Pool != D3DPOOL_SYSTEMMEM && Levels <= 1), D3DERR_INVALIDCALL);
54
55 user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */
56
57 if (Usage & D3DUSAGE_AUTOGENMIPMAP)
58 Levels = 0;
59
60 pf = d3d9_to_pipe_format(Format);
61 if (pf == PIPE_FORMAT_NONE ||
62 !screen->is_format_supported(screen, pf, PIPE_TEXTURE_CUBE, 0, PIPE_BIND_SAMPLER_VIEW)) {
63 return D3DERR_INVALIDCALL;
64 }
65
66 info->screen = pParams->device->screen;
67 info->target = PIPE_TEXTURE_CUBE;
68 info->format = pf;
69 info->width0 = EdgeLength;
70 info->height0 = EdgeLength;
71 info->depth0 = 1;
72 if (Levels)
73 info->last_level = Levels - 1;
74 else
75 info->last_level = util_logbase2(EdgeLength);
76 info->array_size = 6;
77 info->nr_samples = 0;
78 info->bind = PIPE_BIND_SAMPLER_VIEW;
79 info->usage = PIPE_USAGE_DEFAULT;
80 info->flags = 0;
81
82 if (Usage & D3DUSAGE_RENDERTARGET)
83 info->bind |= PIPE_BIND_RENDER_TARGET;
84 if (Usage & D3DUSAGE_DEPTHSTENCIL)
85 info->bind |= PIPE_BIND_DEPTH_STENCIL;
86
87 if (Usage & D3DUSAGE_DYNAMIC) {
88 info->usage = PIPE_USAGE_DYNAMIC;
89 info->bind |=
90 PIPE_BIND_TRANSFER_READ |
91 PIPE_BIND_TRANSFER_WRITE;
92 }
93
94 This->surfaces = CALLOC(6 * (info->last_level + 1), sizeof(*This->surfaces));
95 if (!This->surfaces)
96 return E_OUTOFMEMORY;
97
98 hr = NineBaseTexture9_ctor(&This->base, pParams, NULL, D3DRTYPE_CUBETEXTURE,
99 Format, Pool, Usage);
100 if (FAILED(hr))
101 return hr;
102 This->base.pstype = 2;
103
104 /* Create all the surfaces right away.
105 * They manage backing storage, and transfers (LockRect) are deferred
106 * to them.
107 */
108 sfdesc.Format = Format;
109 sfdesc.Type = D3DRTYPE_SURFACE;
110 sfdesc.Usage = Usage;
111 sfdesc.Pool = Pool;
112 sfdesc.MultiSampleType = D3DMULTISAMPLE_NONE;
113 sfdesc.MultiSampleQuality = 0;
114 for (i = 0; i < (info->last_level + 1) * 6; ++i) {
115 sfdesc.Width = sfdesc.Height = u_minify(EdgeLength, i / 6);
116
117 hr = NineSurface9_new(This->base.base.base.device, NineUnknown(This),
118 This->base.base.resource, NULL, D3DRTYPE_CUBETEXTURE,
119 i / 6, i % 6,
120 &sfdesc, &This->surfaces[i]);
121 if (FAILED(hr))
122 return hr;
123 }
124 for (i = 0; i < 6; ++i) /* width = 0 means empty, depth stays 1 */
125 This->dirty_rect[i].depth = 1;
126
127 return D3D_OK;
128 }
129
130 static void
131 NineCubeTexture9_dtor( struct NineCubeTexture9 *This )
132 {
133 unsigned i;
134
135 DBG("This=%p\n", This);
136
137 if (This->surfaces) {
138 for (i = 0; i < This->base.base.info.last_level * 6; ++i)
139 NineUnknown_Destroy(&This->surfaces[i]->base.base);
140 FREE(This->surfaces);
141 }
142
143 NineBaseTexture9_dtor(&This->base);
144 }
145
146 HRESULT WINAPI
147 NineCubeTexture9_GetLevelDesc( struct NineCubeTexture9 *This,
148 UINT Level,
149 D3DSURFACE_DESC *pDesc )
150 {
151 DBG("This=%p Level=%u pDesc=%p\n", This, Level, pDesc);
152
153 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
154 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
155 D3DERR_INVALIDCALL);
156
157 *pDesc = This->surfaces[Level * 6]->desc;
158
159 return D3D_OK;
160 }
161
162 HRESULT WINAPI
163 NineCubeTexture9_GetCubeMapSurface( struct NineCubeTexture9 *This,
164 D3DCUBEMAP_FACES FaceType,
165 UINT Level,
166 IDirect3DSurface9 **ppCubeMapSurface )
167 {
168 const unsigned s = Level * 6 + FaceType;
169
170 DBG("This=%p FaceType=%d Level=%u ppCubeMapSurface=%p\n",
171 This, FaceType, Level, ppCubeMapSurface);
172
173 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
174 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
175 D3DERR_INVALIDCALL);
176 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
177
178 NineUnknown_AddRef(NineUnknown(This->surfaces[s]));
179 *ppCubeMapSurface = (IDirect3DSurface9 *)This->surfaces[s];
180
181 return D3D_OK;
182 }
183
184 HRESULT WINAPI
185 NineCubeTexture9_LockRect( struct NineCubeTexture9 *This,
186 D3DCUBEMAP_FACES FaceType,
187 UINT Level,
188 D3DLOCKED_RECT *pLockedRect,
189 const RECT *pRect,
190 DWORD Flags )
191 {
192 const unsigned s = Level * 6 + FaceType;
193
194 DBG("This=%p FaceType=%d Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
195 This, FaceType, Level, pLockedRect, pRect, Flags);
196
197 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
198 user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
199 D3DERR_INVALIDCALL);
200 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
201
202 return NineSurface9_LockRect(This->surfaces[s], pLockedRect, pRect, Flags);
203 }
204
205 HRESULT WINAPI
206 NineCubeTexture9_UnlockRect( struct NineCubeTexture9 *This,
207 D3DCUBEMAP_FACES FaceType,
208 UINT Level )
209 {
210 const unsigned s = Level * 6 + FaceType;
211
212 DBG("This=%p FaceType=%d Level=%u\n", This, FaceType, Level);
213
214 user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
215 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
216
217 return NineSurface9_UnlockRect(This->surfaces[s]);
218 }
219
220 HRESULT WINAPI
221 NineCubeTexture9_AddDirtyRect( struct NineCubeTexture9 *This,
222 D3DCUBEMAP_FACES FaceType,
223 const RECT *pDirtyRect )
224 {
225 DBG("This=%p FaceType=%d pDirtyRect=%p\n", This, FaceType, pDirtyRect);
226
227 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
228
229 if (This->base.base.pool != D3DPOOL_MANAGED) {
230 if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP)
231 This->base.dirty_mip = TRUE;
232 return D3D_OK;
233 }
234 This->base.dirty = TRUE;
235
236 BASETEX_REGISTER_UPDATE(&This->base);
237
238 if (!pDirtyRect) {
239 u_box_origin_2d(This->base.base.info.width0,
240 This->base.base.info.height0,
241 &This->dirty_rect[FaceType]);
242 } else {
243 struct pipe_box box;
244 rect_to_pipe_box_clamp(&box, pDirtyRect);
245 u_box_union_2d(&This->dirty_rect[FaceType], &This->dirty_rect[FaceType],
246 &box);
247 }
248 return D3D_OK;
249 }
250
251 IDirect3DCubeTexture9Vtbl NineCubeTexture9_vtable = {
252 (void *)NineUnknown_QueryInterface,
253 (void *)NineUnknown_AddRef,
254 (void *)NineUnknown_Release,
255 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
256 (void *)NineResource9_SetPrivateData,
257 (void *)NineResource9_GetPrivateData,
258 (void *)NineResource9_FreePrivateData,
259 (void *)NineResource9_SetPriority,
260 (void *)NineResource9_GetPriority,
261 (void *)NineBaseTexture9_PreLoad,
262 (void *)NineResource9_GetType,
263 (void *)NineBaseTexture9_SetLOD,
264 (void *)NineBaseTexture9_GetLOD,
265 (void *)NineBaseTexture9_GetLevelCount,
266 (void *)NineBaseTexture9_SetAutoGenFilterType,
267 (void *)NineBaseTexture9_GetAutoGenFilterType,
268 (void *)NineBaseTexture9_GenerateMipSubLevels,
269 (void *)NineCubeTexture9_GetLevelDesc,
270 (void *)NineCubeTexture9_GetCubeMapSurface,
271 (void *)NineCubeTexture9_LockRect,
272 (void *)NineCubeTexture9_UnlockRect,
273 (void *)NineCubeTexture9_AddDirtyRect
274 };
275
276 static const GUID *NineCubeTexture9_IIDs[] = {
277 &IID_IDirect3DCubeTexture9,
278 &IID_IDirect3DBaseTexture9,
279 &IID_IDirect3DResource9,
280 &IID_IUnknown,
281 NULL
282 };
283
284 HRESULT
285 NineCubeTexture9_new( struct NineDevice9 *pDevice,
286 UINT EdgeLength, UINT Levels,
287 DWORD Usage,
288 D3DFORMAT Format,
289 D3DPOOL Pool,
290 struct NineCubeTexture9 **ppOut,
291 HANDLE *pSharedHandle )
292 {
293 NINE_DEVICE_CHILD_NEW(CubeTexture9, ppOut, pDevice,
294 EdgeLength, Levels,
295 Usage, Format, Pool, pSharedHandle);
296 }