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