st/nine: Support internal compressed format for volumes
[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_format.h"
32 #include "util/u_surface.h"
33
34 #define DBG_CHANNEL DBG_VOLUME
35
36
37 static HRESULT
38 NineVolume9_AllocateData( struct NineVolume9 *This )
39 {
40 unsigned size = This->layer_stride * This->desc.Depth;
41
42 DBG("(%p(This=%p),level=%u) Allocating 0x%x bytes of system memory.\n",
43 This->base.container, This, This->level, size);
44
45 This->data = (uint8_t *)align_calloc(size, 32);
46 if (!This->data)
47 return E_OUTOFMEMORY;
48 return D3D_OK;
49 }
50
51 static HRESULT
52 NineVolume9_ctor( struct NineVolume9 *This,
53 struct NineUnknownParams *pParams,
54 struct NineUnknown *pContainer,
55 struct pipe_resource *pResource,
56 unsigned Level,
57 D3DVOLUME_DESC *pDesc )
58 {
59 HRESULT hr;
60
61 assert(pContainer); /* stand-alone volumes can't be created */
62
63 DBG("This=%p pContainer=%p pDevice=%p pResource=%p Level=%u pDesc=%p\n",
64 This, pContainer, pParams->device, pResource, Level, pDesc);
65
66 /* Mark this as a special surface held by another internal resource. */
67 pParams->container = pContainer;
68
69 user_assert(!(pDesc->Usage & D3DUSAGE_DYNAMIC) ||
70 (pDesc->Pool != D3DPOOL_MANAGED), D3DERR_INVALIDCALL);
71
72 assert(pResource || pDesc->Pool != D3DPOOL_DEFAULT);
73
74 hr = NineUnknown_ctor(&This->base, pParams);
75 if (FAILED(hr))
76 return hr;
77
78 pipe_resource_reference(&This->resource, pResource);
79
80 This->transfer = NULL;
81 This->lock_count = 0;
82
83 This->level = Level;
84 This->level_actual = Level;
85 This->desc = *pDesc;
86
87 This->info.screen = pParams->device->screen;
88 This->info.target = PIPE_TEXTURE_3D;
89 This->info.width0 = pDesc->Width;
90 This->info.height0 = pDesc->Height;
91 This->info.depth0 = pDesc->Depth;
92 This->info.last_level = 0;
93 This->info.array_size = 1;
94 This->info.nr_samples = 0;
95 This->info.nr_storage_samples = 0;
96 This->info.usage = PIPE_USAGE_DEFAULT;
97 This->info.bind = PIPE_BIND_SAMPLER_VIEW;
98 This->info.flags = 0;
99 This->info.format = d3d9_to_pipe_format_checked(This->info.screen,
100 pDesc->Format,
101 This->info.target,
102 This->info.nr_samples,
103 This->info.bind, FALSE,
104 pDesc->Pool == D3DPOOL_SCRATCH);
105
106 if (This->info.format == PIPE_FORMAT_NONE)
107 return D3DERR_DRIVERINTERNALERROR;
108
109 This->stride = util_format_get_stride(This->info.format, pDesc->Width);
110 This->stride = align(This->stride, 4);
111 This->layer_stride = util_format_get_2d_size(This->info.format,
112 This->stride, pDesc->Height);
113
114 /* Get true format */
115 This->format_internal = d3d9_to_pipe_format_checked(This->info.screen,
116 pDesc->Format,
117 This->info.target,
118 This->info.nr_samples,
119 This->info.bind, FALSE,
120 TRUE);
121 if (This->info.format != This->format_internal) {
122 This->stride_internal = nine_format_get_stride(This->format_internal,
123 pDesc->Width);
124 This->layer_stride_internal = util_format_get_2d_size(This->format_internal,
125 This->stride_internal,
126 pDesc->Height);
127 This->data_internal = align_calloc(This->layer_stride_internal *
128 This->desc.Depth, 32);
129 if (!This->data_internal)
130 return E_OUTOFMEMORY;
131 }
132
133 if (!This->resource) {
134 hr = NineVolume9_AllocateData(This);
135 if (FAILED(hr))
136 return hr;
137 }
138 return D3D_OK;
139 }
140
141 static void
142 NineVolume9_dtor( struct NineVolume9 *This )
143 {
144 DBG("This=%p\n", This);
145
146 if (This->transfer) {
147 struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.device);
148 pipe->transfer_unmap(pipe, This->transfer);
149 This->transfer = NULL;
150 }
151
152 /* Note: Following condition cannot happen currently, since we
153 * refcount the volume in the functions increasing
154 * pending_uploads_counter. */
155 if (p_atomic_read(&This->pending_uploads_counter))
156 nine_csmt_process(This->base.device);
157
158 if (This->data)
159 align_free(This->data);
160 if (This->data_internal)
161 align_free(This->data_internal);
162
163 pipe_resource_reference(&This->resource, NULL);
164
165 NineUnknown_dtor(&This->base);
166 }
167
168 HRESULT NINE_WINAPI
169 NineVolume9_GetContainer( struct NineVolume9 *This,
170 REFIID riid,
171 void **ppContainer )
172 {
173 char guid_str[64];
174
175 DBG("This=%p riid=%p id=%s ppContainer=%p\n",
176 This, riid, riid ? GUID_sprintf(guid_str, riid) : "", ppContainer);
177
178 (void)guid_str;
179
180 if (!NineUnknown(This)->container)
181 return E_NOINTERFACE;
182 return NineUnknown_QueryInterface(NineUnknown(This)->container, riid, ppContainer);
183 }
184
185 static inline void
186 NineVolume9_MarkContainerDirty( struct NineVolume9 *This )
187 {
188 struct NineBaseTexture9 *tex;
189 #if defined(DEBUG) || !defined(NDEBUG)
190 /* This is always contained by a NineVolumeTexture9. */
191 GUID id = IID_IDirect3DVolumeTexture9;
192 REFIID ref = &id;
193 assert(NineUnknown_QueryInterface(This->base.container, ref, (void **)&tex)
194 == S_OK);
195 assert(NineUnknown_Release(NineUnknown(tex)) != 0);
196 #endif
197
198 tex = NineBaseTexture9(This->base.container);
199 assert(tex);
200 if (This->desc.Pool == D3DPOOL_MANAGED)
201 tex->managed.dirty = TRUE;
202
203 BASETEX_REGISTER_UPDATE(tex);
204 }
205
206 HRESULT NINE_WINAPI
207 NineVolume9_GetDesc( struct NineVolume9 *This,
208 D3DVOLUME_DESC *pDesc )
209 {
210 user_assert(pDesc != NULL, E_POINTER);
211 *pDesc = This->desc;
212 return D3D_OK;
213 }
214
215 inline void
216 NineVolume9_AddDirtyRegion( struct NineVolume9 *This,
217 const struct pipe_box *box )
218 {
219 D3DBOX dirty_region;
220 struct NineVolumeTexture9 *tex = NineVolumeTexture9(This->base.container);
221
222 if (!box) {
223 NineVolumeTexture9_AddDirtyBox(tex, NULL);
224 } else {
225 dirty_region.Left = box->x << This->level_actual;
226 dirty_region.Top = box->y << This->level_actual;
227 dirty_region.Front = box->z << This->level_actual;
228 dirty_region.Right = dirty_region.Left + (box->width << This->level_actual);
229 dirty_region.Bottom = dirty_region.Top + (box->height << This->level_actual);
230 dirty_region.Back = dirty_region.Front + (box->depth << This->level_actual);
231 NineVolumeTexture9_AddDirtyBox(tex, &dirty_region);
232 }
233 }
234
235 static inline unsigned
236 NineVolume9_GetSystemMemOffset(enum pipe_format format, unsigned stride,
237 unsigned layer_stride,
238 int x, int y, int z)
239 {
240 unsigned x_offset = util_format_get_stride(format, x);
241
242 y = util_format_get_nblocksy(format, y);
243
244 return z * layer_stride + y * stride + x_offset;
245 }
246
247 HRESULT NINE_WINAPI
248 NineVolume9_LockBox( struct NineVolume9 *This,
249 D3DLOCKED_BOX *pLockedVolume,
250 const D3DBOX *pBox,
251 DWORD Flags )
252 {
253 struct pipe_context *pipe;
254 struct pipe_resource *resource = This->resource;
255 struct pipe_box box;
256 unsigned usage;
257
258 DBG("This=%p(%p) pLockedVolume=%p pBox=%p[%u..%u,%u..%u,%u..%u] Flags=%s\n",
259 This, This->base.container, pLockedVolume, pBox,
260 pBox ? pBox->Left : 0, pBox ? pBox->Right : 0,
261 pBox ? pBox->Top : 0, pBox ? pBox->Bottom : 0,
262 pBox ? pBox->Front : 0, pBox ? pBox->Back : 0,
263 nine_D3DLOCK_to_str(Flags));
264
265 /* check if it's already locked */
266 user_assert(This->lock_count == 0, D3DERR_INVALIDCALL);
267
268 /* set pBits to NULL after lock_count check */
269 user_assert(pLockedVolume, E_POINTER);
270 pLockedVolume->pBits = NULL;
271
272 user_assert(This->desc.Pool != D3DPOOL_DEFAULT ||
273 (This->desc.Usage & D3DUSAGE_DYNAMIC), D3DERR_INVALIDCALL);
274
275 user_assert(!((Flags & D3DLOCK_DISCARD) && (Flags & D3DLOCK_READONLY)),
276 D3DERR_INVALIDCALL);
277
278 if (pBox && compressed_format (This->desc.Format)) { /* For volume all pools are checked */
279 const unsigned w = util_format_get_blockwidth(This->info.format);
280 const unsigned h = util_format_get_blockheight(This->info.format);
281 user_assert((pBox->Left == 0 && pBox->Right == This->desc.Width &&
282 pBox->Top == 0 && pBox->Bottom == This->desc.Height) ||
283 (!(pBox->Left % w) && !(pBox->Right % w) &&
284 !(pBox->Top % h) && !(pBox->Bottom % h)),
285 D3DERR_INVALIDCALL);
286 }
287
288 if (Flags & D3DLOCK_DISCARD) {
289 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE;
290 } else {
291 usage = (Flags & D3DLOCK_READONLY) ?
292 PIPE_TRANSFER_READ : PIPE_TRANSFER_READ_WRITE;
293 }
294 if (Flags & D3DLOCK_DONOTWAIT)
295 usage |= PIPE_TRANSFER_DONTBLOCK;
296
297 if (pBox) {
298 user_assert(pBox->Right > pBox->Left, D3DERR_INVALIDCALL);
299 user_assert(pBox->Bottom > pBox->Top, D3DERR_INVALIDCALL);
300 user_assert(pBox->Back > pBox->Front, D3DERR_INVALIDCALL);
301 user_assert(pBox->Right <= This->desc.Width, D3DERR_INVALIDCALL);
302 user_assert(pBox->Bottom <= This->desc.Height, D3DERR_INVALIDCALL);
303 user_assert(pBox->Back <= This->desc.Depth, D3DERR_INVALIDCALL);
304
305 d3dbox_to_pipe_box(&box, pBox);
306 if (u_box_clip_2d(&box, &box, This->desc.Width, This->desc.Height) < 0) {
307 DBG("Locked volume intersection empty.\n");
308 return D3DERR_INVALIDCALL;
309 }
310 } else {
311 u_box_3d(0, 0, 0, This->desc.Width, This->desc.Height, This->desc.Depth,
312 &box);
313 }
314
315 if (p_atomic_read(&This->pending_uploads_counter))
316 nine_csmt_process(This->base.device);
317
318 if (This->data_internal || This->data) {
319 enum pipe_format format = This->info.format;
320 unsigned stride = This->stride;
321 unsigned layer_stride = This->layer_stride;
322 uint8_t *data = This->data;
323 if (This->data_internal) {
324 format = This->format_internal;
325 stride = This->stride_internal;
326 layer_stride = This->layer_stride_internal;
327 data = This->data_internal;
328 }
329 pLockedVolume->RowPitch = stride;
330 pLockedVolume->SlicePitch = layer_stride;
331 pLockedVolume->pBits = data +
332 NineVolume9_GetSystemMemOffset(format, stride,
333 layer_stride,
334 box.x, box.y, box.z);
335 } else {
336 bool no_refs = !p_atomic_read(&This->base.bind) &&
337 !p_atomic_read(&This->base.container->bind);
338 if (no_refs)
339 pipe = nine_context_get_pipe_acquire(This->base.device);
340 else
341 pipe = NineDevice9_GetPipe(This->base.device);
342 pLockedVolume->pBits =
343 pipe->transfer_map(pipe, resource, This->level, usage,
344 &box, &This->transfer);
345 if (no_refs)
346 nine_context_get_pipe_release(This->base.device);
347 if (!This->transfer) {
348 if (Flags & D3DLOCK_DONOTWAIT)
349 return D3DERR_WASSTILLDRAWING;
350 return D3DERR_DRIVERINTERNALERROR;
351 }
352 pLockedVolume->RowPitch = This->transfer->stride;
353 pLockedVolume->SlicePitch = This->transfer->layer_stride;
354 }
355
356 if (!(Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY))) {
357 NineVolume9_MarkContainerDirty(This);
358 NineVolume9_AddDirtyRegion(This, &box);
359 }
360
361 ++This->lock_count;
362 return D3D_OK;
363 }
364
365 HRESULT NINE_WINAPI
366 NineVolume9_UnlockBox( struct NineVolume9 *This )
367 {
368 struct pipe_context *pipe;
369
370 DBG("This=%p lock_count=%u\n", This, This->lock_count);
371 user_assert(This->lock_count, D3DERR_INVALIDCALL);
372 if (This->transfer) {
373 pipe = nine_context_get_pipe_acquire(This->base.device);
374 pipe->transfer_unmap(pipe, This->transfer);
375 This->transfer = NULL;
376 nine_context_get_pipe_release(This->base.device);
377 }
378 --This->lock_count;
379
380 if (This->data_internal) {
381 struct pipe_box box;
382
383 u_box_3d(0, 0, 0, This->desc.Width, This->desc.Height, This->desc.Depth,
384 &box);
385
386
387 if (This->data) {
388 (void) util_format_translate_3d(This->info.format,
389 This->data, This->stride,
390 This->layer_stride,
391 0, 0, 0,
392 This->format_internal,
393 This->data_internal,
394 This->stride_internal,
395 This->layer_stride_internal,
396 0, 0, 0,
397 This->desc.Width, This->desc.Height,
398 This->desc.Depth);
399 } else {
400 nine_context_box_upload(This->base.device,
401 &This->pending_uploads_counter,
402 (struct NineUnknown *)This,
403 This->resource,
404 This->level,
405 &box,
406 This->format_internal,
407 This->data_internal,
408 This->stride_internal,
409 This->layer_stride_internal,
410 &box);
411 }
412 }
413
414 return D3D_OK;
415 }
416
417 /* When this function is called, we have already checked
418 * The copy regions fit the volumes */
419 void
420 NineVolume9_CopyMemToDefault( struct NineVolume9 *This,
421 struct NineVolume9 *From,
422 unsigned dstx, unsigned dsty, unsigned dstz,
423 struct pipe_box *pSrcBox )
424 {
425 struct pipe_resource *r_dst = This->resource;
426 struct pipe_box src_box;
427 struct pipe_box dst_box;
428
429 DBG("This=%p From=%p dstx=%u dsty=%u dstz=%u pSrcBox=%p\n",
430 This, From, dstx, dsty, dstz, pSrcBox);
431
432 assert(This->desc.Pool == D3DPOOL_DEFAULT &&
433 From->desc.Pool == D3DPOOL_SYSTEMMEM);
434
435 dst_box.x = dstx;
436 dst_box.y = dsty;
437 dst_box.z = dstz;
438
439 if (pSrcBox) {
440 src_box = *pSrcBox;
441 } else {
442 src_box.x = 0;
443 src_box.y = 0;
444 src_box.z = 0;
445 src_box.width = From->desc.Width;
446 src_box.height = From->desc.Height;
447 src_box.depth = From->desc.Depth;
448 }
449
450 dst_box.width = src_box.width;
451 dst_box.height = src_box.height;
452 dst_box.depth = src_box.depth;
453
454 nine_context_box_upload(This->base.device,
455 &From->pending_uploads_counter,
456 (struct NineUnknown *)From,
457 r_dst,
458 This->level,
459 &dst_box,
460 From->info.format,
461 From->data, From->stride,
462 From->layer_stride,
463 &src_box);
464
465 if (This->data_internal)
466 (void) util_format_translate_3d(This->format_internal,
467 This->data_internal,
468 This->stride_internal,
469 This->layer_stride_internal,
470 dstx, dsty, dstz,
471 From->info.format,
472 From->data, From->stride,
473 From->layer_stride,
474 src_box.x, src_box.y,
475 src_box.z,
476 src_box.width,
477 src_box.height,
478 src_box.depth);
479
480 NineVolume9_MarkContainerDirty(This);
481
482 return;
483 }
484
485 HRESULT
486 NineVolume9_UploadSelf( struct NineVolume9 *This,
487 const struct pipe_box *damaged )
488 {
489 struct pipe_resource *res = This->resource;
490 struct pipe_box box;
491
492 DBG("This=%p damaged=%p data=%p res=%p\n", This, damaged,
493 This->data, res);
494
495 assert(This->desc.Pool == D3DPOOL_MANAGED);
496 assert(res);
497
498 if (damaged) {
499 box = *damaged;
500 } else {
501 box.x = 0;
502 box.y = 0;
503 box.z = 0;
504 box.width = This->desc.Width;
505 box.height = This->desc.Height;
506 box.depth = This->desc.Depth;
507 }
508
509 nine_context_box_upload(This->base.device,
510 &This->pending_uploads_counter,
511 (struct NineUnknown *)This,
512 res,
513 This->level,
514 &box,
515 res->format,
516 This->data, This->stride,
517 This->layer_stride,
518 &box);
519
520 return D3D_OK;
521 }
522
523
524 IDirect3DVolume9Vtbl NineVolume9_vtable = {
525 (void *)NineUnknown_QueryInterface,
526 (void *)NineUnknown_AddRef,
527 (void *)NineUnknown_Release,
528 (void *)NineUnknown_GetDevice, /* actually part of Volume9 iface */
529 (void *)NineUnknown_SetPrivateData,
530 (void *)NineUnknown_GetPrivateData,
531 (void *)NineUnknown_FreePrivateData,
532 (void *)NineVolume9_GetContainer,
533 (void *)NineVolume9_GetDesc,
534 (void *)NineVolume9_LockBox,
535 (void *)NineVolume9_UnlockBox
536 };
537
538 static const GUID *NineVolume9_IIDs[] = {
539 &IID_IDirect3DVolume9,
540 &IID_IUnknown,
541 NULL
542 };
543
544 HRESULT
545 NineVolume9_new( struct NineDevice9 *pDevice,
546 struct NineUnknown *pContainer,
547 struct pipe_resource *pResource,
548 unsigned Level,
549 D3DVOLUME_DESC *pDesc,
550 struct NineVolume9 **ppOut )
551 {
552 NINE_DEVICE_CHILD_NEW(Volume9, ppOut, pDevice, /* args */
553 pContainer, pResource, Level, pDesc);
554 }