st/nine: Refactor how user constbufs sizes are calculated
[mesa.git] / src / gallium / state_trackers / nine / volume9.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 "volume9.h"
25 #include "basetexture9.h" /* for marking dirty */
26 #include "nine_helpers.h"
27 #include "nine_pipe.h"
28 #include "nine_dump.h"
29
30 #include "util/u_hash_table.h"
31 #include "util/u_format.h"
32 #include "util/u_surface.h"
33 #include "nine_pdata.h"
34
35 #define DBG_CHANNEL DBG_VOLUME
36
37
38 static HRESULT
39 NineVolume9_AllocateData( struct NineVolume9 *This )
40 {
41 unsigned size = This->layer_stride * This->desc.Depth;
42
43 DBG("(%p(This=%p),level=%u) Allocating 0x%x bytes of system memory.\n",
44 This->base.container, This, This->level, size);
45
46 This->data = (uint8_t *)MALLOC(size);
47 if (!This->data)
48 return E_OUTOFMEMORY;
49 return D3D_OK;
50 }
51
52 static HRESULT
53 NineVolume9_ctor( struct NineVolume9 *This,
54 struct NineUnknownParams *pParams,
55 struct NineUnknown *pContainer,
56 struct pipe_resource *pResource,
57 unsigned Level,
58 D3DVOLUME_DESC *pDesc )
59 {
60 HRESULT hr;
61
62 assert(pContainer); /* stand-alone volumes can't be created */
63
64 DBG("This=%p pContainer=%p pDevice=%p pResource=%p Level=%u pDesc=%p\n",
65 This, pContainer, pParams->device, pResource, Level, pDesc);
66
67 /* Mark this as a special surface held by another internal resource. */
68 pParams->container = pContainer;
69
70 user_assert(!(pDesc->Usage & D3DUSAGE_DYNAMIC) ||
71 (pDesc->Pool != D3DPOOL_MANAGED), D3DERR_INVALIDCALL);
72
73 assert(pResource || pDesc->Pool != D3DPOOL_DEFAULT);
74
75 hr = NineUnknown_ctor(&This->base, pParams);
76 if (FAILED(hr))
77 return hr;
78
79 This->pdata = util_hash_table_create(ht_guid_hash, ht_guid_compare);
80 if (!This->pdata)
81 return E_OUTOFMEMORY;
82
83 pipe_resource_reference(&This->resource, pResource);
84
85 This->pipe = pParams->device->pipe;
86 This->transfer = NULL;
87 This->lock_count = 0;
88
89 This->level = Level;
90 This->level_actual = Level;
91 This->desc = *pDesc;
92
93 This->info.screen = pParams->device->screen;
94 This->info.target = PIPE_TEXTURE_3D;
95 This->info.format = d3d9_to_pipe_format(pDesc->Format);
96 This->info.width0 = pDesc->Width;
97 This->info.height0 = pDesc->Height;
98 This->info.depth0 = pDesc->Depth;
99 This->info.last_level = 0;
100 This->info.array_size = 1;
101 This->info.nr_samples = 0;
102 This->info.usage = PIPE_USAGE_DEFAULT;
103 This->info.bind = PIPE_BIND_SAMPLER_VIEW;
104 This->info.flags = 0;
105
106 This->stride = util_format_get_stride(This->info.format, pDesc->Width);
107 This->stride = align(This->stride, 4);
108 This->layer_stride = util_format_get_2d_size(This->info.format,
109 This->stride, pDesc->Height);
110
111 if (pDesc->Pool == D3DPOOL_SYSTEMMEM)
112 This->info.usage = PIPE_USAGE_STAGING;
113
114 if (!This->resource) {
115 hr = NineVolume9_AllocateData(This);
116 if (FAILED(hr))
117 return hr;
118 }
119 return D3D_OK;
120 }
121
122 static void
123 NineVolume9_dtor( struct NineVolume9 *This )
124 {
125 DBG("This=%p\n", This);
126
127 if (This->transfer)
128 NineVolume9_UnlockBox(This);
129
130 pipe_resource_reference(&This->resource, NULL);
131
132 NineUnknown_dtor(&This->base);
133 }
134
135 HRESULT WINAPI
136 NineVolume9_GetContainer( struct NineVolume9 *This,
137 REFIID riid,
138 void **ppContainer )
139 {
140 if (!NineUnknown(This)->container)
141 return E_NOINTERFACE;
142 return NineUnknown_QueryInterface(NineUnknown(This)->container, riid, ppContainer);
143 }
144
145 static INLINE void
146 NineVolume9_MarkContainerDirty( struct NineVolume9 *This )
147 {
148 struct NineBaseTexture9 *tex;
149 #ifdef DEBUG
150 /* This is always contained by a NineVolumeTexture9. */
151 GUID id = IID_IDirect3DVolumeTexture9;
152 REFIID ref = &id;
153 assert(NineUnknown_QueryInterface(This->base.container, ref, (void **)&tex)
154 == S_OK);
155 assert(NineUnknown_Release(NineUnknown(tex)) != 0);
156 #endif
157
158 tex = NineBaseTexture9(This->base.container);
159 assert(tex);
160 if (This->desc.Pool == D3DPOOL_MANAGED)
161 tex->dirty = TRUE;
162 else
163 if (This->desc.Usage & D3DUSAGE_AUTOGENMIPMAP)
164 tex->dirty_mip = TRUE;
165
166 BASETEX_REGISTER_UPDATE(tex);
167 }
168
169 HRESULT WINAPI
170 NineVolume9_GetDesc( struct NineVolume9 *This,
171 D3DVOLUME_DESC *pDesc )
172 {
173 user_assert(pDesc != NULL, E_POINTER);
174 *pDesc = This->desc;
175 return D3D_OK;
176 }
177
178 static INLINE boolean
179 NineVolume9_IsDirty(struct NineVolume9 *This)
180 {
181 return This->dirty_box[0].width != 0;
182 }
183
184 INLINE void
185 NineVolume9_AddDirtyRegion( struct NineVolume9 *This,
186 const struct pipe_box *box )
187 {
188 struct pipe_box cover_a, cover_b;
189 float vol[2];
190
191 if (!box) {
192 u_box_3d(0, 0, 0, This->desc.Width, This->desc.Height,
193 This->desc.Depth, &This->dirty_box[0]);
194 memset(&This->dirty_box[1], 0, sizeof(This->dirty_box[1]));
195 return;
196 }
197 if (!This->dirty_box[0].width) {
198 This->dirty_box[0] = *box;
199 return;
200 }
201
202 u_box_union_3d(&cover_a, &This->dirty_box[0], box);
203 vol[0] = u_box_volume_3d(&cover_a);
204
205 if (This->dirty_box[1].width == 0) {
206 vol[1] = u_box_volume_3d(&This->dirty_box[0]);
207 if (vol[0] > (vol[1] * 1.5f))
208 This->dirty_box[1] = *box;
209 else
210 This->dirty_box[0] = cover_a;
211 } else {
212 u_box_union_3d(&cover_b, &This->dirty_box[1], box);
213 vol[1] = u_box_volume_3d(&cover_b);
214
215 if (vol[0] > vol[1])
216 This->dirty_box[1] = cover_b;
217 else
218 This->dirty_box[0] = cover_a;
219 }
220 }
221
222 static INLINE uint8_t *
223 NineVolume9_GetSystemMemPointer(struct NineVolume9 *This, int x, int y, int z)
224 {
225 unsigned x_offset = util_format_get_stride(This->info.format, x);
226
227 y = util_format_get_nblocksy(This->info.format, y);
228
229 assert(This->data);
230 return This->data + (z * This->layer_stride + y * This->stride + x_offset);
231 }
232
233 HRESULT WINAPI
234 NineVolume9_LockBox( struct NineVolume9 *This,
235 D3DLOCKED_BOX *pLockedVolume,
236 const D3DBOX *pBox,
237 DWORD Flags )
238 {
239 struct pipe_resource *resource = This->resource;
240 struct pipe_box box;
241 unsigned usage;
242
243 DBG("This=%p(%p) pLockedVolume=%p pBox=%p[%u..%u,%u..%u,%u..%u] Flags=%s\n",
244 This, This->base.container, pLockedVolume, pBox,
245 pBox ? pBox->Left : 0, pBox ? pBox->Right : 0,
246 pBox ? pBox->Top : 0, pBox ? pBox->Bottom : 0,
247 pBox ? pBox->Front : 0, pBox ? pBox->Back : 0,
248 nine_D3DLOCK_to_str(Flags));
249
250 user_assert(This->desc.Pool != D3DPOOL_DEFAULT ||
251 (This->desc.Usage & D3DUSAGE_DYNAMIC), D3DERR_INVALIDCALL);
252
253 user_assert(!((Flags & D3DLOCK_DISCARD) && (Flags & D3DLOCK_READONLY)),
254 D3DERR_INVALIDCALL);
255
256 user_assert(This->lock_count == 0, D3DERR_INVALIDCALL);
257 user_assert(pLockedVolume, E_POINTER);
258
259 if (pBox && This->desc.Pool == D3DPOOL_DEFAULT &&
260 util_format_is_compressed(This->info.format)) {
261 const unsigned w = util_format_get_blockwidth(This->info.format);
262 const unsigned h = util_format_get_blockheight(This->info.format);
263 user_assert(!(pBox->Left % w) && !(pBox->Right % w) &&
264 !(pBox->Top % h) && !(pBox->Bottom % h),
265 D3DERR_INVALIDCALL);
266 }
267
268 if (Flags & D3DLOCK_DISCARD) {
269 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE;
270 } else {
271 usage = (Flags & D3DLOCK_READONLY) ?
272 PIPE_TRANSFER_READ : PIPE_TRANSFER_READ_WRITE;
273 }
274 if (Flags & D3DLOCK_DONOTWAIT)
275 usage |= PIPE_TRANSFER_DONTBLOCK;
276
277 if (pBox) {
278 d3dbox_to_pipe_box(&box, pBox);
279 if (u_box_clip_2d(&box, &box, This->desc.Width, This->desc.Height) < 0) {
280 DBG("Locked volume intersection empty.\n");
281 return D3DERR_INVALIDCALL;
282 }
283 } else {
284 u_box_3d(0, 0, 0, This->desc.Width, This->desc.Height, This->desc.Depth,
285 &box);
286 }
287
288 if (This->data) {
289 pLockedVolume->RowPitch = This->stride;
290 pLockedVolume->SlicePitch = This->layer_stride;
291 pLockedVolume->pBits =
292 NineVolume9_GetSystemMemPointer(This, box.x, box.y, box.z);
293 } else {
294 pLockedVolume->pBits =
295 This->pipe->transfer_map(This->pipe, resource, This->level, usage,
296 &box, &This->transfer);
297 if (!This->transfer) {
298 if (Flags & D3DLOCK_DONOTWAIT)
299 return D3DERR_WASSTILLDRAWING;
300 return D3DERR_DRIVERINTERNALERROR;
301 }
302 pLockedVolume->RowPitch = This->transfer->stride;
303 pLockedVolume->SlicePitch = This->transfer->layer_stride;
304 }
305
306 if (!(Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY))) {
307 NineVolume9_MarkContainerDirty(This);
308 if (This->desc.Pool == D3DPOOL_MANAGED)
309 NineVolume9_AddDirtyRegion(This, &box);
310 }
311
312 ++This->lock_count;
313 return D3D_OK;
314 }
315
316 HRESULT WINAPI
317 NineVolume9_UnlockBox( struct NineVolume9 *This )
318 {
319 DBG("This=%p lock_count=%u\n", This, This->lock_count);
320 user_assert(This->lock_count, D3DERR_INVALIDCALL);
321 if (This->transfer) {
322 This->pipe->transfer_unmap(This->pipe, This->transfer);
323 This->transfer = NULL;
324 }
325 --This->lock_count;
326 return D3D_OK;
327 }
328
329
330 HRESULT
331 NineVolume9_CopyVolume( struct NineVolume9 *This,
332 struct NineVolume9 *From,
333 unsigned dstx, unsigned dsty, unsigned dstz,
334 struct pipe_box *pSrcBox )
335 {
336 struct pipe_context *pipe = This->pipe;
337 struct pipe_resource *r_dst = This->resource;
338 struct pipe_resource *r_src = From->resource;
339 struct pipe_transfer *transfer;
340 struct pipe_box src_box;
341 struct pipe_box dst_box;
342 uint8_t *p_dst;
343 const uint8_t *p_src;
344
345 DBG("This=%p From=%p dstx=%u dsty=%u dstz=%u pSrcBox=%p\n",
346 This, From, dstx, dsty, dstz, pSrcBox);
347
348 user_assert(This->desc.Format == From->desc.Format, D3DERR_INVALIDCALL);
349
350 dst_box.x = dstx;
351 dst_box.y = dsty;
352 dst_box.z = dstz;
353
354 if (pSrcBox) {
355 /* make sure it doesn't range outside the source volume */
356 user_assert(pSrcBox->x >= 0 &&
357 (pSrcBox->width - pSrcBox->x) <= From->desc.Width &&
358 pSrcBox->y >= 0 &&
359 (pSrcBox->height - pSrcBox->y) <= From->desc.Height &&
360 pSrcBox->z >= 0 &&
361 (pSrcBox->depth - pSrcBox->z) <= From->desc.Depth,
362 D3DERR_INVALIDCALL);
363 src_box = *pSrcBox;
364 } else {
365 src_box.x = 0;
366 src_box.y = 0;
367 src_box.z = 0;
368 src_box.width = From->desc.Width;
369 src_box.height = From->desc.Height;
370 src_box.depth = From->desc.Depth;
371 }
372 /* limits */
373 dst_box.width = This->desc.Width - dst_box.x;
374 dst_box.height = This->desc.Height - dst_box.y;
375 dst_box.depth = This->desc.Depth - dst_box.z;
376
377 user_assert(src_box.width <= dst_box.width &&
378 src_box.height <= dst_box.height &&
379 src_box.depth <= dst_box.depth, D3DERR_INVALIDCALL);
380
381 dst_box.width = src_box.width;
382 dst_box.height = src_box.height;
383 dst_box.depth = src_box.depth;
384
385 /* Don't copy to device memory of managed resources.
386 * We don't want to download it back again later.
387 */
388 if (This->desc.Pool == D3DPOOL_MANAGED)
389 r_dst = NULL;
390
391 /* Don't copy from stale device memory of managed resources.
392 * Also, don't copy between system and device if we don't have to.
393 */
394 if (From->desc.Pool == D3DPOOL_MANAGED) {
395 if (!r_dst || NineVolume9_IsDirty(From))
396 r_src = NULL;
397 }
398
399 if (r_dst && r_src) {
400 pipe->resource_copy_region(pipe,
401 r_dst, This->level,
402 dst_box.x, dst_box.y, dst_box.z,
403 r_src, From->level,
404 &src_box);
405 } else
406 if (r_dst) {
407 p_src = NineVolume9_GetSystemMemPointer(From,
408 src_box.x, src_box.y, src_box.z);
409
410 pipe->transfer_inline_write(pipe, r_dst, This->level,
411 0, /* WRITE|DISCARD are implicit */
412 &dst_box, p_src,
413 From->stride, From->layer_stride);
414 } else
415 if (r_src) {
416 p_dst = NineVolume9_GetSystemMemPointer(This, 0, 0, 0);
417 p_src = pipe->transfer_map(pipe, r_src, From->level,
418 PIPE_TRANSFER_READ,
419 &src_box, &transfer);
420 if (!p_src)
421 return D3DERR_DRIVERINTERNALERROR;
422
423 util_copy_box(p_dst, This->info.format,
424 This->stride, This->layer_stride,
425 dst_box.x, dst_box.y, dst_box.z,
426 dst_box.width, dst_box.height, dst_box.depth,
427 p_src,
428 transfer->stride, transfer->layer_stride,
429 src_box.x, src_box.y, src_box.z);
430
431 pipe->transfer_unmap(pipe, transfer);
432 } else {
433 p_dst = NineVolume9_GetSystemMemPointer(This, 0, 0, 0);
434 p_src = NineVolume9_GetSystemMemPointer(From, 0, 0, 0);
435
436 util_copy_box(p_dst, This->info.format,
437 This->stride, This->layer_stride,
438 dst_box.x, dst_box.y, dst_box.z,
439 dst_box.width, dst_box.height, dst_box.depth,
440 p_src,
441 From->stride, From->layer_stride,
442 src_box.x, src_box.y, src_box.z);
443 }
444
445 if (This->desc.Pool == D3DPOOL_DEFAULT ||
446 This->desc.Pool == D3DPOOL_MANAGED)
447 NineVolume9_MarkContainerDirty(This);
448 if (!r_dst && This->resource)
449 NineVolume9_AddDirtyRegion(This, &dst_box);
450
451 return D3D_OK;
452 }
453
454 HRESULT
455 NineVolume9_UploadSelf( struct NineVolume9 *This )
456 {
457 struct pipe_context *pipe = This->pipe;
458 struct pipe_resource *res = This->resource;
459 uint8_t *ptr;
460 unsigned i;
461
462 DBG("This=%p dirty=%i data=%p res=%p\n", This, NineVolume9_IsDirty(This),
463 This->data, res);
464
465 assert(This->desc.Pool == D3DPOOL_MANAGED);
466
467 if (!NineVolume9_IsDirty(This))
468 return D3D_OK;
469 assert(res);
470
471 for (i = 0; i < Elements(This->dirty_box); ++i) {
472 const struct pipe_box *box = &This->dirty_box[i];
473 if (box->width == 0)
474 break;
475 ptr = NineVolume9_GetSystemMemPointer(This, box->x, box->y, box->z);
476
477 pipe->transfer_inline_write(pipe, res, This->level,
478 0,
479 box, ptr, This->stride, This->layer_stride);
480 }
481 NineVolume9_ClearDirtyRegion(This);
482
483 return D3D_OK;
484 }
485
486
487 IDirect3DVolume9Vtbl NineVolume9_vtable = {
488 (void *)NineUnknown_QueryInterface,
489 (void *)NineUnknown_AddRef,
490 (void *)NineUnknown_Release,
491 (void *)NineUnknown_GetDevice, /* actually part of Volume9 iface */
492 (void *)NineVolume9_SetPrivateData,
493 (void *)NineVolume9_GetPrivateData,
494 (void *)NineVolume9_FreePrivateData,
495 (void *)NineVolume9_GetContainer,
496 (void *)NineVolume9_GetDesc,
497 (void *)NineVolume9_LockBox,
498 (void *)NineVolume9_UnlockBox
499 };
500
501 static const GUID *NineVolume9_IIDs[] = {
502 &IID_IDirect3DVolume9,
503 &IID_IUnknown,
504 NULL
505 };
506
507 HRESULT
508 NineVolume9_new( struct NineDevice9 *pDevice,
509 struct NineUnknown *pContainer,
510 struct pipe_resource *pResource,
511 unsigned Level,
512 D3DVOLUME_DESC *pDesc,
513 struct NineVolume9 **ppOut )
514 {
515 NINE_DEVICE_CHILD_NEW(Volume9, ppOut, pDevice, /* args */
516 pContainer, pResource, Level, pDesc);
517 }
518
519
520 /*** The boring stuff. TODO: Unify with Resource. ***/
521
522 HRESULT WINAPI
523 NineVolume9_SetPrivateData( struct NineVolume9 *This,
524 REFGUID refguid,
525 const void *pData,
526 DWORD SizeOfData,
527 DWORD Flags )
528 {
529 enum pipe_error err;
530 struct pheader *header;
531 const void *user_data = pData;
532
533 DBG("This=%p refguid=%p pData=%p SizeOfData=%d Flags=%d\n",
534 This, refguid, pData, SizeOfData, Flags);
535
536 if (Flags & D3DSPD_IUNKNOWN)
537 user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
538
539 /* data consists of a header and the actual data. avoiding 2 mallocs */
540 header = CALLOC_VARIANT_LENGTH_STRUCT(pheader, SizeOfData-1);
541 if (!header) { return E_OUTOFMEMORY; }
542 header->unknown = (Flags & D3DSPD_IUNKNOWN) ? TRUE : FALSE;
543
544 /* if the refguid already exists, delete it */
545 NineVolume9_FreePrivateData(This, refguid);
546
547 /* IUnknown special case */
548 if (header->unknown) {
549 /* here the pointer doesn't point to the data we want, so point at the
550 * pointer making what we eventually copy is the pointer itself */
551 user_data = &pData;
552 }
553
554 header->size = SizeOfData;
555 memcpy(header->data, user_data, header->size);
556
557 err = util_hash_table_set(This->pdata, refguid, header);
558 if (err == PIPE_OK) {
559 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header->data); }
560 return D3D_OK;
561 }
562
563 FREE(header);
564 if (err == PIPE_ERROR_OUT_OF_MEMORY) { return E_OUTOFMEMORY; }
565
566 return D3DERR_DRIVERINTERNALERROR;
567 }
568
569 HRESULT WINAPI
570 NineVolume9_GetPrivateData( struct NineVolume9 *This,
571 REFGUID refguid,
572 void *pData,
573 DWORD *pSizeOfData )
574 {
575 struct pheader *header;
576
577 user_assert(pSizeOfData, E_POINTER);
578
579 header = util_hash_table_get(This->pdata, refguid);
580 if (!header) { return D3DERR_NOTFOUND; }
581
582 if (!pData) {
583 *pSizeOfData = header->size;
584 return D3D_OK;
585 }
586 if (*pSizeOfData < header->size) {
587 return D3DERR_MOREDATA;
588 }
589
590 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header->data); }
591 memcpy(pData, header->data, header->size);
592
593 return D3D_OK;
594 }
595
596 HRESULT WINAPI
597 NineVolume9_FreePrivateData( struct NineVolume9 *This,
598 REFGUID refguid )
599 {
600 struct pheader *header;
601
602 DBG("This=%p refguid=%p\n", This, refguid);
603
604 header = util_hash_table_get(This->pdata, refguid);
605 if (!header) { return D3DERR_NOTFOUND; }
606
607 ht_guid_delete(NULL, header, NULL);
608 util_hash_table_remove(This->pdata, refguid);
609
610 return D3D_OK;
611 }
612