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