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