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