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