1 /*
2 * Copyright 2011 Joakim Sindholt <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "c99_alloca.h"
7
8 #include "device9.h"
9 #include "cubetexture9.h"
10 #include "nine_memory_helper.h"
11 #include "nine_helpers.h"
12 #include "nine_pipe.h"
13
14 #define DBG_CHANNEL DBG_CUBETEXTURE
15
16
17 static HRESULT
NineCubeTexture9_ctor(struct NineCubeTexture9 * This,struct NineUnknownParams * pParams,UINT EdgeLength,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,HANDLE * pSharedHandle)18 NineCubeTexture9_ctor( struct NineCubeTexture9 *This,
19 struct NineUnknownParams *pParams,
20 UINT EdgeLength, UINT Levels,
21 DWORD Usage,
22 D3DFORMAT Format,
23 D3DPOOL Pool,
24 HANDLE *pSharedHandle )
25 {
26 struct pipe_resource *info = &This->base.base.info;
27 struct pipe_screen *screen = pParams->device->screen;
28 enum pipe_format pf;
29 unsigned i, l, f, offset, face_size = 0;
30 unsigned *level_offsets = NULL;
31 D3DSURFACE_DESC sfdesc;
32 struct nine_allocation *p;
33 HRESULT hr;
34
35 DBG("This=%p pParams=%p EdgeLength=%u Levels=%u Usage=%d "
36 "Format=%d Pool=%d pSharedHandle=%p\n",
37 This, pParams, EdgeLength, Levels, Usage,
38 Format, Pool, pSharedHandle);
39
40 This->base.base.base.device = pParams->device; /* Early fill this field in case of failure */
41
42 user_assert(EdgeLength, D3DERR_INVALIDCALL);
43
44 /* user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL); */
45 user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */
46
47 user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP) ||
48 (Pool != D3DPOOL_SYSTEMMEM && Levels <= 1), D3DERR_INVALIDCALL);
49
50 if (Usage & D3DUSAGE_AUTOGENMIPMAP)
51 Levels = 0;
52
53 pf = d3d9_to_pipe_format_checked(screen, Format, PIPE_TEXTURE_CUBE, 0,
54 PIPE_BIND_SAMPLER_VIEW, false,
55 Pool == D3DPOOL_SCRATCH);
56
57 if (pf == PIPE_FORMAT_NONE)
58 return D3DERR_INVALIDCALL;
59
60 if (compressed_format(Format)) {
61 const unsigned w = util_format_get_blockwidth(pf);
62 const unsigned h = util_format_get_blockheight(pf);
63
64 user_assert(!(EdgeLength % w) && !(EdgeLength % h), D3DERR_INVALIDCALL);
65 }
66
67 info->screen = pParams->device->screen;
68 info->target = PIPE_TEXTURE_CUBE;
69 info->format = pf;
70 info->width0 = EdgeLength;
71 info->height0 = EdgeLength;
72 info->depth0 = 1;
73 if (Levels)
74 info->last_level = Levels - 1;
75 else
76 info->last_level = util_logbase2(EdgeLength);
77 info->array_size = 6;
78 info->nr_samples = 0;
79 info->nr_storage_samples = 0;
80 info->bind = PIPE_BIND_SAMPLER_VIEW;
81 info->usage = PIPE_USAGE_DEFAULT;
82 info->flags = 0;
83
84 if (Usage & D3DUSAGE_RENDERTARGET)
85 info->bind |= PIPE_BIND_RENDER_TARGET;
86 if (Usage & D3DUSAGE_DEPTHSTENCIL)
87 info->bind |= PIPE_BIND_DEPTH_STENCIL;
88
89 if (Usage & D3DUSAGE_DYNAMIC) {
90 info->usage = PIPE_USAGE_DYNAMIC;
91 }
92 if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
93 DBG("Application asked for Software Vertex Processing, "
94 "but this is unimplemented\n");
95
96 hr = NineBaseTexture9_ctor(&This->base, pParams, NULL, D3DRTYPE_CUBETEXTURE,
97 Format, Pool, Usage);
98 if (FAILED(hr))
99 return hr;
100 This->base.pstype = 2;
101
102 if (Pool != D3DPOOL_DEFAULT) {
103 level_offsets = alloca(sizeof(unsigned) * This->base.level_count);
104 face_size = nine_format_get_size_and_offsets(pf, level_offsets,
105 EdgeLength, EdgeLength,
106 This->base.level_count-1);
107 This->managed_buffer = nine_allocate(pParams->device->allocator, 6 * face_size);
108 if (!This->managed_buffer)
109 return E_OUTOFMEMORY;
110 }
111
112 This->surfaces = CALLOC(6 * This->base.level_count, sizeof(*This->surfaces));
113 if (!This->surfaces)
114 return E_OUTOFMEMORY;
115
116 /* Create all the surfaces right away.
117 * They manage backing storage, and transfers (LockRect) are deferred
118 * to them.
119 */
120 sfdesc.Format = Format;
121 sfdesc.Type = D3DRTYPE_SURFACE;
122 sfdesc.Usage = Usage;
123 sfdesc.Pool = Pool;
124 sfdesc.MultiSampleType = D3DMULTISAMPLE_NONE;
125 sfdesc.MultiSampleQuality = 0;
126 /* We allocate the memory for the surfaces as continuous blocks.
127 * This is the expected behaviour, however we haven't tested for
128 * cube textures in which order the faces/levels should be in memory
129 */
130 for (f = 0; f < 6; f++) {
131 offset = f * face_size;
132 for (l = 0; l < This->base.level_count; l++) {
133 sfdesc.Width = sfdesc.Height = u_minify(EdgeLength, l);
134 p = This->managed_buffer ?
135 nine_suballocate(pParams->device->allocator, This->managed_buffer, offset + level_offsets[l]) : NULL;
136
137 hr = NineSurface9_new(This->base.base.base.device, NineUnknown(This),
138 This->base.base.resource, p, D3DRTYPE_CUBETEXTURE,
139 l, f, &sfdesc, &This->surfaces[f + 6 * l]);
140 if (FAILED(hr))
141 return hr;
142 }
143 }
144
145 for (i = 0; i < 6; ++i) {
146 /* Textures start initially dirty */
147 This->dirty_rect[i].width = EdgeLength;
148 This->dirty_rect[i].height = EdgeLength;
149 This->dirty_rect[i].depth = 1;
150 }
151
152 return D3D_OK;
153 }
154
155 static void
NineCubeTexture9_dtor(struct NineCubeTexture9 * This)156 NineCubeTexture9_dtor( struct NineCubeTexture9 *This )
157 {
158 unsigned i;
159 bool is_worker = nine_context_is_worker(This->base.base.base.device);
160
161 DBG("This=%p\n", This);
162
163 if (This->surfaces) {
164 for (i = 0; i < This->base.level_count * 6; ++i)
165 if (This->surfaces[i])
166 NineUnknown_Destroy(&This->surfaces[i]->base.base);
167 FREE(This->surfaces);
168 }
169
170 if (This->managed_buffer) {
171 if (is_worker)
172 nine_free_worker(This->base.base.base.device->allocator, This->managed_buffer);
173 else
174 nine_free(This->base.base.base.device->allocator, This->managed_buffer);
175 }
176
177 NineBaseTexture9_dtor(&This->base);
178 }
179
180 HRESULT NINE_WINAPI
NineCubeTexture9_GetLevelDesc(struct NineCubeTexture9 * This,UINT Level,D3DSURFACE_DESC * pDesc)181 NineCubeTexture9_GetLevelDesc( struct NineCubeTexture9 *This,
182 UINT Level,
183 D3DSURFACE_DESC *pDesc )
184 {
185 DBG("This=%p Level=%u pDesc=%p\n", This, Level, pDesc);
186
187 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
188
189 *pDesc = This->surfaces[Level * 6]->desc;
190
191 return D3D_OK;
192 }
193
194 HRESULT NINE_WINAPI
NineCubeTexture9_GetCubeMapSurface(struct NineCubeTexture9 * This,D3DCUBEMAP_FACES FaceType,UINT Level,IDirect3DSurface9 ** ppCubeMapSurface)195 NineCubeTexture9_GetCubeMapSurface( struct NineCubeTexture9 *This,
196 D3DCUBEMAP_FACES FaceType,
197 UINT Level,
198 IDirect3DSurface9 **ppCubeMapSurface )
199 {
200 const unsigned s = Level * 6 + FaceType;
201
202 DBG("This=%p FaceType=%d Level=%u ppCubeMapSurface=%p\n",
203 This, FaceType, Level, ppCubeMapSurface);
204
205 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
206 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
207
208 NineUnknown_AddRef(NineUnknown(This->surfaces[s]));
209 *ppCubeMapSurface = (IDirect3DSurface9 *)This->surfaces[s];
210
211 return D3D_OK;
212 }
213
214 HRESULT NINE_WINAPI
NineCubeTexture9_LockRect(struct NineCubeTexture9 * This,D3DCUBEMAP_FACES FaceType,UINT Level,D3DLOCKED_RECT * pLockedRect,const RECT * pRect,DWORD Flags)215 NineCubeTexture9_LockRect( struct NineCubeTexture9 *This,
216 D3DCUBEMAP_FACES FaceType,
217 UINT Level,
218 D3DLOCKED_RECT *pLockedRect,
219 const RECT *pRect,
220 DWORD Flags )
221 {
222 const unsigned s = Level * 6 + FaceType;
223
224 DBG("This=%p FaceType=%d Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
225 This, FaceType, Level, pLockedRect, pRect, Flags);
226
227 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
228 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
229
230 return NineSurface9_LockRect(This->surfaces[s], pLockedRect, pRect, Flags);
231 }
232
233 HRESULT NINE_WINAPI
NineCubeTexture9_UnlockRect(struct NineCubeTexture9 * This,D3DCUBEMAP_FACES FaceType,UINT Level)234 NineCubeTexture9_UnlockRect( struct NineCubeTexture9 *This,
235 D3DCUBEMAP_FACES FaceType,
236 UINT Level )
237 {
238 const unsigned s = Level * 6 + FaceType;
239
240 DBG("This=%p FaceType=%d Level=%u\n", This, FaceType, Level);
241
242 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
243 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
244
245 return NineSurface9_UnlockRect(This->surfaces[s]);
246 }
247
248 HRESULT NINE_WINAPI
NineCubeTexture9_AddDirtyRect(struct NineCubeTexture9 * This,D3DCUBEMAP_FACES FaceType,const RECT * pDirtyRect)249 NineCubeTexture9_AddDirtyRect( struct NineCubeTexture9 *This,
250 D3DCUBEMAP_FACES FaceType,
251 const RECT *pDirtyRect )
252 {
253 DBG("This=%p FaceType=%d pDirtyRect=%p\n", This, FaceType, pDirtyRect);
254
255 user_assert(FaceType < 6, D3DERR_INVALIDCALL);
256
257 if (This->base.base.pool != D3DPOOL_MANAGED) {
258 if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP) {
259 This->base.dirty_mip = true;
260 BASETEX_REGISTER_UPDATE(&This->base);
261 }
262 return D3D_OK;
263 }
264
265 if (This->base.base.pool == D3DPOOL_MANAGED) {
266 This->base.managed.dirty = true;
267 BASETEX_REGISTER_UPDATE(&This->base);
268 }
269
270 if (!pDirtyRect) {
271 u_box_origin_2d(This->base.base.info.width0,
272 This->base.base.info.height0,
273 &This->dirty_rect[FaceType]);
274 } else {
275 if (This->dirty_rect[FaceType].width == 0) {
276 rect_to_pipe_box_clamp(&This->dirty_rect[FaceType], pDirtyRect);
277 } else {
278 struct pipe_box box;
279 rect_to_pipe_box_clamp(&box, pDirtyRect);
280 u_box_union_2d(&This->dirty_rect[FaceType], &This->dirty_rect[FaceType],
281 &box);
282 }
283 (void) u_box_clip_2d(&This->dirty_rect[FaceType],
284 &This->dirty_rect[FaceType],
285 This->base.base.info.width0,
286 This->base.base.info.height0);
287 }
288 return D3D_OK;
289 }
290
291 IDirect3DCubeTexture9Vtbl NineCubeTexture9_vtable = {
292 (void *)NineUnknown_QueryInterface,
293 (void *)NineUnknown_AddRef,
294 (void *)NineUnknown_Release,
295 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
296 (void *)NineUnknown_SetPrivateData,
297 (void *)NineUnknown_GetPrivateData,
298 (void *)NineUnknown_FreePrivateData,
299 (void *)NineResource9_SetPriority,
300 (void *)NineResource9_GetPriority,
301 (void *)NineBaseTexture9_PreLoad,
302 (void *)NineResource9_GetType,
303 (void *)NineBaseTexture9_SetLOD,
304 (void *)NineBaseTexture9_GetLOD,
305 (void *)NineBaseTexture9_GetLevelCount,
306 (void *)NineBaseTexture9_SetAutoGenFilterType,
307 (void *)NineBaseTexture9_GetAutoGenFilterType,
308 (void *)NineBaseTexture9_GenerateMipSubLevels,
309 (void *)NineCubeTexture9_GetLevelDesc,
310 (void *)NineCubeTexture9_GetCubeMapSurface,
311 (void *)NineCubeTexture9_LockRect,
312 (void *)NineCubeTexture9_UnlockRect,
313 (void *)NineCubeTexture9_AddDirtyRect
314 };
315
316 static const GUID *NineCubeTexture9_IIDs[] = {
317 &IID_IDirect3DCubeTexture9,
318 &IID_IDirect3DBaseTexture9,
319 &IID_IDirect3DResource9,
320 &IID_IUnknown,
321 NULL
322 };
323
324 HRESULT
NineCubeTexture9_new(struct NineDevice9 * pDevice,UINT EdgeLength,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,struct NineCubeTexture9 ** ppOut,HANDLE * pSharedHandle)325 NineCubeTexture9_new( struct NineDevice9 *pDevice,
326 UINT EdgeLength, UINT Levels,
327 DWORD Usage,
328 D3DFORMAT Format,
329 D3DPOOL Pool,
330 struct NineCubeTexture9 **ppOut,
331 HANDLE *pSharedHandle )
332 {
333 NINE_DEVICE_CHILD_NEW(CubeTexture9, ppOut, pDevice,
334 EdgeLength, Levels,
335 Usage, Format, Pool, pSharedHandle);
336 }
337