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