st/nine: Optimize volume upload with conversion
[mesa.git] / src / gallium / state_trackers / nine / surface9.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 "iunknown.h"
24 #include "surface9.h"
25 #include "device9.h"
26
27 /* for marking dirty */
28 #include "basetexture9.h"
29 #include "texture9.h"
30 #include "cubetexture9.h"
31
32 #include "nine_helpers.h"
33 #include "nine_pipe.h"
34 #include "nine_dump.h"
35 #include "nine_state.h"
36
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39 #include "pipe/p_state.h"
40
41 #include "util/u_math.h"
42 #include "util/u_inlines.h"
43 #include "util/u_surface.h"
44
45 #define DBG_CHANNEL DBG_SURFACE
46
47 static void
48 NineSurface9_CreatePipeSurfaces( struct NineSurface9 *This );
49
50 HRESULT
51 NineSurface9_ctor( struct NineSurface9 *This,
52 struct NineUnknownParams *pParams,
53 struct NineUnknown *pContainer,
54 struct pipe_resource *pResource,
55 void *user_buffer,
56 uint8_t TextureType,
57 unsigned Level,
58 unsigned Layer,
59 D3DSURFACE_DESC *pDesc )
60 {
61 HRESULT hr;
62 bool allocate = !pContainer && pDesc->Format != D3DFMT_NULL;
63 D3DMULTISAMPLE_TYPE multisample_type;
64
65 DBG("This=%p pDevice=%p pResource=%p Level=%u Layer=%u pDesc=%p\n",
66 This, pParams->device, pResource, Level, Layer, pDesc);
67
68 /* Mark this as a special surface held by another internal resource. */
69 pParams->container = pContainer;
70 /* Make sure there's a Desc */
71 assert(pDesc);
72
73 assert(allocate || pResource || user_buffer ||
74 pDesc->Format == D3DFMT_NULL);
75 assert(!allocate || (!pResource && !user_buffer));
76 assert(!pResource || !user_buffer);
77 assert(!user_buffer || pDesc->Pool != D3DPOOL_DEFAULT);
78 assert(!pResource || pDesc->Pool == D3DPOOL_DEFAULT);
79 /* Allocation only from create_zs_or_rt_surface with params 0 0 0 */
80 assert(!allocate || (Level == 0 && Layer == 0 && TextureType == 0));
81
82 This->data = (uint8_t *)user_buffer;
83
84 multisample_type = pDesc->MultiSampleType;
85
86 /* Map MultiSampleQuality to MultiSampleType */
87 hr = d3dmultisample_type_check(pParams->device->screen,
88 pDesc->Format,
89 &multisample_type,
90 pDesc->MultiSampleQuality,
91 NULL);
92 if (FAILED(hr)) {
93 return hr;
94 }
95
96 /* TODO: this is (except width and height) duplicate from
97 * container info (in the pContainer case). Some refactoring is
98 * needed to avoid duplication */
99 This->base.info.screen = pParams->device->screen;
100 This->base.info.target = PIPE_TEXTURE_2D;
101 This->base.info.width0 = pDesc->Width;
102 This->base.info.height0 = pDesc->Height;
103 This->base.info.depth0 = 1;
104 This->base.info.last_level = 0;
105 This->base.info.array_size = 1;
106 This->base.info.nr_samples = multisample_type;
107 This->base.info.nr_storage_samples = multisample_type;
108 This->base.info.usage = PIPE_USAGE_DEFAULT;
109 This->base.info.bind = PIPE_BIND_SAMPLER_VIEW; /* StretchRect */
110
111 if (pDesc->Usage & D3DUSAGE_RENDERTARGET) {
112 This->base.info.bind |= PIPE_BIND_RENDER_TARGET;
113 } else if (pDesc->Usage & D3DUSAGE_DEPTHSTENCIL) {
114 if (!depth_stencil_format(pDesc->Format))
115 return D3DERR_INVALIDCALL;
116 This->base.info.bind = d3d9_get_pipe_depth_format_bindings(pDesc->Format);
117 if (TextureType)
118 This->base.info.bind |= PIPE_BIND_SAMPLER_VIEW;
119 }
120
121 This->base.info.flags = 0;
122 This->base.info.format = d3d9_to_pipe_format_checked(This->base.info.screen,
123 pDesc->Format,
124 This->base.info.target,
125 This->base.info.nr_samples,
126 This->base.info.bind,
127 FALSE,
128 pDesc->Pool == D3DPOOL_SCRATCH);
129
130 if (This->base.info.format == PIPE_FORMAT_NONE && pDesc->Format != D3DFMT_NULL)
131 return D3DERR_INVALIDCALL;
132
133 if (allocate && compressed_format(pDesc->Format)) {
134 const unsigned w = util_format_get_blockwidth(This->base.info.format);
135 const unsigned h = util_format_get_blockheight(This->base.info.format);
136
137 /* Note: In the !allocate case, the test could fail (lower levels of a texture) */
138 user_assert(!(pDesc->Width % w) && !(pDesc->Height % h), D3DERR_INVALIDCALL);
139 }
140
141 /* Get true format */
142 This->format_conversion = d3d9_to_pipe_format_checked(This->base.info.screen,
143 pDesc->Format,
144 This->base.info.target,
145 This->base.info.nr_samples,
146 This->base.info.bind,
147 FALSE,
148 TRUE);
149 if (This->base.info.format != This->format_conversion) {
150 This->data_conversion = align_calloc(
151 nine_format_get_level_alloc_size(This->format_conversion,
152 pDesc->Width,
153 pDesc->Height,
154 0), 32);
155 if (!This->data_conversion)
156 return E_OUTOFMEMORY;
157 This->stride_conversion = nine_format_get_stride(This->format_conversion,
158 pDesc->Width);
159 }
160
161 if ((allocate && pDesc->Pool != D3DPOOL_DEFAULT) || pDesc->Format == D3DFMT_NULL) {
162 /* Ram buffer with no parent. Has to allocate the resource itself */
163 assert(!user_buffer);
164 This->data = align_calloc(
165 nine_format_get_level_alloc_size(This->base.info.format,
166 pDesc->Width,
167 pDesc->Height,
168 0), 32);
169 if (!This->data)
170 return E_OUTOFMEMORY;
171 }
172
173 hr = NineResource9_ctor(&This->base, pParams, pResource,
174 allocate && (pDesc->Pool == D3DPOOL_DEFAULT),
175 D3DRTYPE_SURFACE, pDesc->Pool, pDesc->Usage);
176
177 if (FAILED(hr))
178 return hr;
179
180 This->transfer = NULL;
181
182 This->texture = TextureType;
183 This->level = Level;
184 This->level_actual = Level;
185 This->layer = Layer;
186 This->desc = *pDesc;
187
188 This->stride = nine_format_get_stride(This->base.info.format, pDesc->Width);
189
190 if (This->base.resource && (pDesc->Usage & D3DUSAGE_DYNAMIC))
191 This->base.resource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
192
193 if (This->base.resource && (pDesc->Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)))
194 NineSurface9_CreatePipeSurfaces(This);
195
196 /* TODO: investigate what else exactly needs to be cleared */
197 if (This->base.resource && (pDesc->Usage & D3DUSAGE_RENDERTARGET))
198 nine_context_clear_render_target(pParams->device, This, 0, 0, 0, pDesc->Width, pDesc->Height);
199
200 NineSurface9_Dump(This);
201
202 return D3D_OK;
203 }
204
205 void
206 NineSurface9_dtor( struct NineSurface9 *This )
207 {
208 DBG("This=%p\n", This);
209
210 if (This->transfer) {
211 struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.base.device);
212 pipe->transfer_unmap(pipe, This->transfer);
213 This->transfer = NULL;
214 }
215
216 /* Note: Following condition cannot happen currently, since we
217 * refcount the surface in the functions increasing
218 * pending_uploads_counter. */
219 if (p_atomic_read(&This->pending_uploads_counter))
220 nine_csmt_process(This->base.base.device);
221
222 pipe_surface_reference(&This->surface[0], NULL);
223 pipe_surface_reference(&This->surface[1], NULL);
224
225 /* Release system memory when we have to manage it (no parent) */
226 if (!This->base.base.container && This->data)
227 align_free(This->data);
228 if (This->data_conversion)
229 align_free(This->data_conversion);
230 NineResource9_dtor(&This->base);
231 }
232
233 static void
234 NineSurface9_CreatePipeSurfaces( struct NineSurface9 *This )
235 {
236 struct pipe_context *pipe;
237 struct pipe_screen *screen = NineDevice9_GetScreen(This->base.base.device);
238 struct pipe_resource *resource = This->base.resource;
239 struct pipe_surface templ;
240 enum pipe_format srgb_format;
241
242 assert(This->desc.Pool == D3DPOOL_DEFAULT);
243 assert(resource);
244
245 srgb_format = util_format_srgb(resource->format);
246 if (srgb_format == PIPE_FORMAT_NONE ||
247 !screen->is_format_supported(screen, srgb_format,
248 resource->target, 0, 0, resource->bind))
249 srgb_format = resource->format;
250
251 memset(&templ, 0, sizeof(templ));
252 templ.format = resource->format;
253 templ.u.tex.level = This->level;
254 templ.u.tex.first_layer = This->layer;
255 templ.u.tex.last_layer = This->layer;
256
257 pipe = nine_context_get_pipe_acquire(This->base.base.device);
258
259 This->surface[0] = pipe->create_surface(pipe, resource, &templ);
260
261 memset(&templ, 0, sizeof(templ));
262 templ.format = srgb_format;
263 templ.u.tex.level = This->level;
264 templ.u.tex.first_layer = This->layer;
265 templ.u.tex.last_layer = This->layer;
266
267 This->surface[1] = pipe->create_surface(pipe, resource, &templ);
268
269 nine_context_get_pipe_release(This->base.base.device);
270
271 assert(This->surface[0]); /* TODO: Handle failure */
272 assert(This->surface[1]);
273 }
274
275 #if defined(DEBUG) || !defined(NDEBUG)
276 void
277 NineSurface9_Dump( struct NineSurface9 *This )
278 {
279 struct NineBaseTexture9 *tex;
280 GUID id = IID_IDirect3DBaseTexture9;
281 REFIID ref = &id;
282
283 DBG("\nNineSurface9(%p->%p/%p): Pool=%s Type=%s Usage=%s\n"
284 "Dims=%ux%u Format=%s Stride=%u Lockable=%i\n"
285 "Level=%u(%u), Layer=%u\n", This, This->base.resource, This->data,
286 nine_D3DPOOL_to_str(This->desc.Pool),
287 nine_D3DRTYPE_to_str(This->desc.Type),
288 nine_D3DUSAGE_to_str(This->desc.Usage),
289 This->desc.Width, This->desc.Height,
290 d3dformat_to_string(This->desc.Format), This->stride,
291 This->base.resource &&
292 (This->base.resource->flags & NINE_RESOURCE_FLAG_LOCKABLE),
293 This->level, This->level_actual, This->layer);
294
295 if (!This->base.base.container)
296 return;
297 NineUnknown_QueryInterface(This->base.base.container, ref, (void **)&tex);
298 if (tex) {
299 NineBaseTexture9_Dump(tex);
300 NineUnknown_Release(NineUnknown(tex));
301 }
302 }
303 #endif /* DEBUG || !NDEBUG */
304
305 HRESULT NINE_WINAPI
306 NineSurface9_GetContainer( struct NineSurface9 *This,
307 REFIID riid,
308 void **ppContainer )
309 {
310 HRESULT hr;
311 char guid_str[64];
312
313 DBG("This=%p riid=%p id=%s ppContainer=%p\n",
314 This, riid, riid ? GUID_sprintf(guid_str, riid) : "", ppContainer);
315
316 (void)guid_str;
317
318 if (!ppContainer) return E_POINTER;
319
320 /* Return device for OffscreenPlainSurface, DepthStencilSurface and RenderTarget */
321 if (!NineUnknown(This)->container) {
322 *ppContainer = NineUnknown(This)->device;
323 NineUnknown_AddRef(NineUnknown(*ppContainer));
324
325 return D3D_OK;
326 }
327
328 hr = NineUnknown_QueryInterface(NineUnknown(This)->container, riid, ppContainer);
329 if (FAILED(hr))
330 DBG("QueryInterface FAILED!\n");
331 return hr;
332 }
333
334 void
335 NineSurface9_MarkContainerDirty( struct NineSurface9 *This )
336 {
337 if (This->texture) {
338 struct NineBaseTexture9 *tex =
339 NineBaseTexture9(This->base.base.container);
340 assert(tex);
341 assert(This->texture == D3DRTYPE_TEXTURE ||
342 This->texture == D3DRTYPE_CUBETEXTURE);
343 if (This->base.pool == D3DPOOL_MANAGED)
344 tex->managed.dirty = TRUE;
345 else
346 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
347 tex->dirty_mip = TRUE;
348
349 BASETEX_REGISTER_UPDATE(tex);
350 }
351 }
352
353 HRESULT NINE_WINAPI
354 NineSurface9_GetDesc( struct NineSurface9 *This,
355 D3DSURFACE_DESC *pDesc )
356 {
357 user_assert(pDesc != NULL, E_POINTER);
358 *pDesc = This->desc;
359 return D3D_OK;
360 }
361
362 /* Add the dirty rects to the source texture */
363 inline void
364 NineSurface9_AddDirtyRect( struct NineSurface9 *This,
365 const struct pipe_box *box )
366 {
367 RECT dirty_rect;
368
369 DBG("This=%p box=%p\n", This, box);
370
371 assert (This->base.pool != D3DPOOL_MANAGED ||
372 This->texture == D3DRTYPE_CUBETEXTURE ||
373 This->texture == D3DRTYPE_TEXTURE);
374
375 if (This->base.pool == D3DPOOL_DEFAULT)
376 return;
377
378 /* Add a dirty rect to level 0 of the parent texture */
379 dirty_rect.left = box->x << This->level_actual;
380 dirty_rect.right = dirty_rect.left + (box->width << This->level_actual);
381 dirty_rect.top = box->y << This->level_actual;
382 dirty_rect.bottom = dirty_rect.top + (box->height << This->level_actual);
383
384 if (This->texture == D3DRTYPE_TEXTURE) {
385 struct NineTexture9 *tex =
386 NineTexture9(This->base.base.container);
387
388 NineTexture9_AddDirtyRect(tex, &dirty_rect);
389 } else if (This->texture == D3DRTYPE_CUBETEXTURE) {
390 struct NineCubeTexture9 *ctex =
391 NineCubeTexture9(This->base.base.container);
392
393 NineCubeTexture9_AddDirtyRect(ctex, This->layer, &dirty_rect);
394 }
395 }
396
397 static inline uint8_t *
398 NineSurface9_GetSystemMemPointer(struct NineSurface9 *This, int x, int y)
399 {
400 unsigned x_offset = util_format_get_stride(This->base.info.format, x);
401
402 y = util_format_get_nblocksy(This->base.info.format, y);
403
404 assert(This->data);
405 return This->data + (y * This->stride + x_offset);
406 }
407
408 HRESULT NINE_WINAPI
409 NineSurface9_LockRect( struct NineSurface9 *This,
410 D3DLOCKED_RECT *pLockedRect,
411 const RECT *pRect,
412 DWORD Flags )
413 {
414 struct pipe_resource *resource = This->base.resource;
415 struct pipe_context *pipe;
416 struct pipe_box box;
417 unsigned usage;
418
419 DBG("This=%p pLockedRect=%p pRect=%p[%u..%u,%u..%u] Flags=%s\n", This,
420 pLockedRect, pRect,
421 pRect ? pRect->left : 0, pRect ? pRect->right : 0,
422 pRect ? pRect->top : 0, pRect ? pRect->bottom : 0,
423 nine_D3DLOCK_to_str(Flags));
424 NineSurface9_Dump(This);
425
426 /* check if it's already locked */
427 user_assert(This->lock_count == 0, D3DERR_INVALIDCALL);
428
429 /* set pBits to NULL after lock_count check */
430 user_assert(pLockedRect, E_POINTER);
431 pLockedRect->pBits = NULL;
432
433 #ifdef NINE_STRICT
434 user_assert(This->base.pool != D3DPOOL_DEFAULT ||
435 (resource && (resource->flags & NINE_RESOURCE_FLAG_LOCKABLE)),
436 D3DERR_INVALIDCALL);
437 #endif
438 user_assert(!(Flags & ~(D3DLOCK_DISCARD |
439 D3DLOCK_DONOTWAIT |
440 D3DLOCK_NO_DIRTY_UPDATE |
441 D3DLOCK_NOOVERWRITE |
442 D3DLOCK_NOSYSLOCK | /* ignored */
443 D3DLOCK_READONLY)), D3DERR_INVALIDCALL);
444 user_assert(!((Flags & D3DLOCK_DISCARD) && (Flags & D3DLOCK_READONLY)),
445 D3DERR_INVALIDCALL);
446
447 user_assert(This->desc.MultiSampleType == D3DMULTISAMPLE_NONE,
448 D3DERR_INVALIDCALL);
449
450 if (pRect && This->desc.Pool == D3DPOOL_DEFAULT &&
451 util_format_is_compressed(This->base.info.format)) {
452 const unsigned w = util_format_get_blockwidth(This->base.info.format);
453 const unsigned h = util_format_get_blockheight(This->base.info.format);
454 user_assert((pRect->left == 0 && pRect->right == This->desc.Width &&
455 pRect->top == 0 && pRect->bottom == This->desc.Height) ||
456 (!(pRect->left % w) && !(pRect->right % w) &&
457 !(pRect->top % h) && !(pRect->bottom % h)),
458 D3DERR_INVALIDCALL);
459 }
460
461 if (Flags & D3DLOCK_DISCARD) {
462 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE;
463 } else {
464 usage = (Flags & D3DLOCK_READONLY) ?
465 PIPE_TRANSFER_READ : PIPE_TRANSFER_READ_WRITE;
466 }
467 if (Flags & D3DLOCK_DONOTWAIT)
468 usage |= PIPE_TRANSFER_DONTBLOCK;
469
470 if (pRect) {
471 /* Windows XP accepts invalid locking rectangles, Windows 7 rejects
472 * them. Use Windows XP behaviour for now. */
473 rect_to_pipe_box(&box, pRect);
474 } else {
475 u_box_origin_2d(This->desc.Width, This->desc.Height, &box);
476 }
477 box.z = This->layer;
478
479 user_warn(This->desc.Format == D3DFMT_NULL);
480
481 if (p_atomic_read(&This->pending_uploads_counter))
482 nine_csmt_process(This->base.base.device);
483
484 if (This->data_conversion) {
485 /* For now we only have uncompressed formats here */
486 pLockedRect->Pitch = This->stride_conversion;
487 pLockedRect->pBits = This->data_conversion + box.y * This->stride_conversion +
488 util_format_get_stride(This->format_conversion, box.x);
489 } else if (This->data) {
490 DBG("returning system memory\n");
491 /* ATI1 and ATI2 need special handling, because of d3d9 bug.
492 * We must advertise to the application as if it is uncompressed
493 * and bpp 8, and the app has a workaround to work with the fact
494 * that it is actually compressed. */
495 if (is_ATI1_ATI2(This->base.info.format)) {
496 pLockedRect->Pitch = This->desc.Width;
497 pLockedRect->pBits = This->data + box.y * This->desc.Width + box.x;
498 } else {
499 pLockedRect->Pitch = This->stride;
500 pLockedRect->pBits = NineSurface9_GetSystemMemPointer(This,
501 box.x,
502 box.y);
503 }
504 } else {
505 bool no_refs = !p_atomic_read(&This->base.base.bind) &&
506 !(This->base.base.container && p_atomic_read(&This->base.base.container->bind));
507 DBG("mapping pipe_resource %p (level=%u usage=%x)\n",
508 resource, This->level, usage);
509
510 /* if the object is not bound internally, there can't be any pending
511 * operation with the surface in the queue */
512 if (no_refs)
513 pipe = nine_context_get_pipe_acquire(This->base.base.device);
514 else
515 pipe = NineDevice9_GetPipe(This->base.base.device);
516 pLockedRect->pBits = pipe->transfer_map(pipe, resource,
517 This->level, usage, &box,
518 &This->transfer);
519 if (no_refs)
520 nine_context_get_pipe_release(This->base.base.device);
521 if (!This->transfer) {
522 DBG("transfer_map failed\n");
523 if (Flags & D3DLOCK_DONOTWAIT)
524 return D3DERR_WASSTILLDRAWING;
525 return D3DERR_INVALIDCALL;
526 }
527 pLockedRect->Pitch = This->transfer->stride;
528 }
529
530 if (!(Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY))) {
531 NineSurface9_MarkContainerDirty(This);
532 NineSurface9_AddDirtyRect(This, &box);
533 }
534
535 ++This->lock_count;
536 return D3D_OK;
537 }
538
539 HRESULT NINE_WINAPI
540 NineSurface9_UnlockRect( struct NineSurface9 *This )
541 {
542 struct pipe_box dst_box, src_box;
543 struct pipe_context *pipe;
544 DBG("This=%p lock_count=%u\n", This, This->lock_count);
545 user_assert(This->lock_count, D3DERR_INVALIDCALL);
546 if (This->transfer) {
547 pipe = nine_context_get_pipe_acquire(This->base.base.device);
548 pipe->transfer_unmap(pipe, This->transfer);
549 nine_context_get_pipe_release(This->base.base.device);
550 This->transfer = NULL;
551 }
552 --This->lock_count;
553
554 if (This->data_conversion) {
555 if (This->data) {
556 (void) util_format_translate(This->base.info.format,
557 This->data, This->stride,
558 0, 0,
559 This->format_conversion,
560 This->data_conversion,
561 This->stride_conversion,
562 0, 0,
563 This->desc.Width, This->desc.Height);
564 } else {
565 u_box_2d_zslice(0, 0, This->layer,
566 This->desc.Width, This->desc.Height, &dst_box);
567 u_box_2d_zslice(0, 0, 0,
568 This->desc.Width, This->desc.Height, &src_box);
569
570 nine_context_box_upload(This->base.base.device,
571 &This->pending_uploads_counter,
572 (struct NineUnknown *)This,
573 This->base.resource,
574 This->level,
575 &dst_box,
576 This->format_conversion,
577 This->data_conversion,
578 This->stride_conversion,
579 0, /* depth = 1 */
580 &src_box);
581 }
582 }
583 return D3D_OK;
584 }
585
586 HRESULT NINE_WINAPI
587 NineSurface9_GetDC( struct NineSurface9 *This,
588 HDC *phdc )
589 {
590 STUB(D3DERR_INVALIDCALL);
591 }
592
593 HRESULT NINE_WINAPI
594 NineSurface9_ReleaseDC( struct NineSurface9 *This,
595 HDC hdc )
596 {
597 STUB(D3DERR_INVALIDCALL);
598 }
599
600 IDirect3DSurface9Vtbl NineSurface9_vtable = {
601 (void *)NineUnknown_QueryInterface,
602 (void *)NineUnknown_AddRef,
603 (void *)NineUnknown_Release,
604 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
605 (void *)NineUnknown_SetPrivateData,
606 (void *)NineUnknown_GetPrivateData,
607 (void *)NineUnknown_FreePrivateData,
608 (void *)NineResource9_SetPriority,
609 (void *)NineResource9_GetPriority,
610 (void *)NineResource9_PreLoad,
611 (void *)NineResource9_GetType,
612 (void *)NineSurface9_GetContainer,
613 (void *)NineSurface9_GetDesc,
614 (void *)NineSurface9_LockRect,
615 (void *)NineSurface9_UnlockRect,
616 (void *)NineSurface9_GetDC,
617 (void *)NineSurface9_ReleaseDC
618 };
619
620 /* When this function is called, we have already checked
621 * The copy regions fit the surfaces */
622 void
623 NineSurface9_CopyMemToDefault( struct NineSurface9 *This,
624 struct NineSurface9 *From,
625 const POINT *pDestPoint,
626 const RECT *pSourceRect )
627 {
628 struct pipe_resource *r_dst = This->base.resource;
629 struct pipe_box dst_box, src_box;
630 int src_x, src_y, dst_x, dst_y, copy_width, copy_height;
631
632 assert(This->base.pool == D3DPOOL_DEFAULT &&
633 From->base.pool == D3DPOOL_SYSTEMMEM);
634
635 if (pDestPoint) {
636 dst_x = pDestPoint->x;
637 dst_y = pDestPoint->y;
638 } else {
639 dst_x = 0;
640 dst_y = 0;
641 }
642
643 if (pSourceRect) {
644 src_x = pSourceRect->left;
645 src_y = pSourceRect->top;
646 copy_width = pSourceRect->right - pSourceRect->left;
647 copy_height = pSourceRect->bottom - pSourceRect->top;
648 } else {
649 src_x = 0;
650 src_y = 0;
651 copy_width = From->desc.Width;
652 copy_height = From->desc.Height;
653 }
654
655 u_box_2d_zslice(dst_x, dst_y, This->layer,
656 copy_width, copy_height, &dst_box);
657 u_box_2d_zslice(src_x, src_y, 0,
658 copy_width, copy_height, &src_box);
659
660 nine_context_box_upload(This->base.base.device,
661 &From->pending_uploads_counter,
662 (struct NineUnknown *)From,
663 r_dst,
664 This->level,
665 &dst_box,
666 From->base.info.format,
667 From->data, From->stride,
668 0, /* depth = 1 */
669 &src_box);
670 if (From->texture == D3DRTYPE_TEXTURE) {
671 struct NineTexture9 *tex =
672 NineTexture9(From->base.base.container);
673 /* D3DPOOL_SYSTEMMEM with buffer content passed
674 * from the user: execute the upload right now.
675 * It is possible it is enough to delay upload
676 * until the surface refcount is 0, but the
677 * bind refcount may not be 0, and thus the dtor
678 * is not executed (and doesn't trigger the
679 * pending_uploads_counter check). */
680 if (!tex->managed_buffer)
681 nine_csmt_process(This->base.base.device);
682 }
683
684 if (This->data_conversion)
685 (void) util_format_translate(This->format_conversion,
686 This->data_conversion,
687 This->stride_conversion,
688 dst_x, dst_y,
689 From->base.info.format,
690 From->data, From->stride,
691 src_x, src_y,
692 copy_width, copy_height);
693
694 NineSurface9_MarkContainerDirty(This);
695 }
696
697 void
698 NineSurface9_CopyDefaultToMem( struct NineSurface9 *This,
699 struct NineSurface9 *From )
700 {
701 struct pipe_context *pipe;
702 struct pipe_resource *r_src = From->base.resource;
703 struct pipe_transfer *transfer;
704 struct pipe_box src_box;
705 uint8_t *p_dst;
706 const uint8_t *p_src;
707
708 assert(This->base.pool == D3DPOOL_SYSTEMMEM &&
709 From->base.pool == D3DPOOL_DEFAULT);
710
711 assert(This->desc.Width == From->desc.Width);
712 assert(This->desc.Height == From->desc.Height);
713
714 u_box_origin_2d(This->desc.Width, This->desc.Height, &src_box);
715 src_box.z = From->layer;
716
717 if (p_atomic_read(&This->pending_uploads_counter))
718 nine_csmt_process(This->base.base.device);
719
720 pipe = NineDevice9_GetPipe(This->base.base.device);
721 p_src = pipe->transfer_map(pipe, r_src, From->level,
722 PIPE_TRANSFER_READ,
723 &src_box, &transfer);
724 p_dst = NineSurface9_GetSystemMemPointer(This, 0, 0);
725
726 assert (p_src && p_dst);
727
728 util_copy_rect(p_dst, This->base.info.format,
729 This->stride, 0, 0,
730 This->desc.Width, This->desc.Height,
731 p_src,
732 transfer->stride, 0, 0);
733
734 pipe->transfer_unmap(pipe, transfer);
735 }
736
737
738 /* Gladly, rendering to a MANAGED surface is not permitted, so we will
739 * never have to do the reverse, i.e. download the surface.
740 */
741 HRESULT
742 NineSurface9_UploadSelf( struct NineSurface9 *This,
743 const struct pipe_box *damaged )
744 {
745 struct pipe_resource *res = This->base.resource;
746 struct pipe_box box;
747
748 DBG("This=%p damaged=%p\n", This, damaged);
749
750 assert(This->base.pool == D3DPOOL_MANAGED);
751
752 if (damaged) {
753 box = *damaged;
754 box.z = This->layer;
755 box.depth = 1;
756 } else {
757 box.x = 0;
758 box.y = 0;
759 box.z = This->layer;
760 box.width = This->desc.Width;
761 box.height = This->desc.Height;
762 box.depth = 1;
763 }
764
765 nine_context_box_upload(This->base.base.device,
766 &This->pending_uploads_counter,
767 (struct NineUnknown *)This,
768 res,
769 This->level,
770 &box,
771 res->format,
772 This->data, This->stride,
773 0, /* depth = 1 */
774 &box);
775
776 return D3D_OK;
777 }
778
779 /* Currently nine_context uses the NineSurface9
780 * fields when it is render target. Any modification requires
781 * pending commands with the surface to be executed. If the bind
782 * count is 0, there is no pending commands. */
783 #define PROCESS_IF_BOUND(surf) \
784 if (surf->base.base.bind) \
785 nine_csmt_process(surf->base.base.device);
786
787 void
788 NineSurface9_SetResource( struct NineSurface9 *This,
789 struct pipe_resource *resource, unsigned level )
790 {
791 /* No need to call PROCESS_IF_BOUND, because SetResource is used only
792 * for MANAGED textures, and they are not render targets. */
793 assert(This->base.pool == D3DPOOL_MANAGED);
794 This->level = level;
795 pipe_resource_reference(&This->base.resource, resource);
796 }
797
798 void
799 NineSurface9_SetMultiSampleType( struct NineSurface9 *This,
800 D3DMULTISAMPLE_TYPE mst )
801 {
802 PROCESS_IF_BOUND(This);
803 This->desc.MultiSampleType = mst;
804 }
805
806 void
807 NineSurface9_SetResourceResize( struct NineSurface9 *This,
808 struct pipe_resource *resource )
809 {
810 assert(This->level == 0 && This->level_actual == 0);
811 assert(!This->lock_count);
812 assert(This->desc.Pool == D3DPOOL_DEFAULT);
813 assert(!This->texture);
814
815 PROCESS_IF_BOUND(This);
816 pipe_resource_reference(&This->base.resource, resource);
817
818 This->desc.Width = This->base.info.width0 = resource->width0;
819 This->desc.Height = This->base.info.height0 = resource->height0;
820 This->base.info.nr_samples = resource->nr_samples;
821 This->base.info.nr_storage_samples = resource->nr_storage_samples;
822
823 This->stride = nine_format_get_stride(This->base.info.format,
824 This->desc.Width);
825
826 pipe_surface_reference(&This->surface[0], NULL);
827 pipe_surface_reference(&This->surface[1], NULL);
828 if (resource)
829 NineSurface9_CreatePipeSurfaces(This);
830 }
831
832
833 static const GUID *NineSurface9_IIDs[] = {
834 &IID_IDirect3DSurface9,
835 &IID_IDirect3DResource9,
836 &IID_IUnknown,
837 NULL
838 };
839
840 HRESULT
841 NineSurface9_new( struct NineDevice9 *pDevice,
842 struct NineUnknown *pContainer,
843 struct pipe_resource *pResource,
844 void *user_buffer,
845 uint8_t TextureType,
846 unsigned Level,
847 unsigned Layer,
848 D3DSURFACE_DESC *pDesc,
849 struct NineSurface9 **ppOut )
850 {
851 NINE_DEVICE_CHILD_NEW(Surface9, ppOut, pDevice, /* args */
852 pContainer, pResource, user_buffer,
853 TextureType, Level, Layer, pDesc);
854 }