HRESULT WINAPI NineSurface9_LockRect( struct NineSurface9 *This, D3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD Flags ) { struct pipe_resource *resource = This->base.resource; struct pipe_box box; unsigned usage; DBG("This=%p pLockedRect=%p pRect=%p[%u..%u,%u..%u] Flags=%s\n", This, pLockedRect, pRect, pRect ? pRect->left : 0, pRect ? pRect->right : 0, pRect ? pRect->top : 0, pRect ? pRect->bottom : 0, nine_D3DLOCK_to_str(Flags)); NineSurface9_Dump(This); #ifdef NINE_STRICT user_assert(This->base.pool != D3DPOOL_DEFAULT || (resource && (resource->flags & NINE_RESOURCE_FLAG_LOCKABLE)), D3DERR_INVALIDCALL); #endif user_assert(!(Flags & ~(D3DLOCK_DISCARD | D3DLOCK_DONOTWAIT | D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK | /* ignored */ D3DLOCK_READONLY)), D3DERR_INVALIDCALL); user_assert(!((Flags & D3DLOCK_DISCARD) && (Flags & D3DLOCK_READONLY)), D3DERR_INVALIDCALL); /* check if it's already locked */ user_assert(This->lock_count == 0, D3DERR_INVALIDCALL); user_assert(pLockedRect, E_POINTER); user_assert(This->desc.MultiSampleType == D3DMULTISAMPLE_NONE, D3DERR_INVALIDCALL); if (pRect && This->base.pool == D3DPOOL_DEFAULT && util_format_is_compressed(This->base.info.format)) { const unsigned w = util_format_get_blockwidth(This->base.info.format); const unsigned h = util_format_get_blockheight(This->base.info.format); user_assert(!(pRect->left % w) && !(pRect->right % w) && !(pRect->top % h) && !(pRect->bottom % h), D3DERR_INVALIDCALL); } if (Flags & D3DLOCK_DISCARD) { usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE; } else { usage = (Flags & D3DLOCK_READONLY) ? PIPE_TRANSFER_READ : PIPE_TRANSFER_READ_WRITE; } if (Flags & D3DLOCK_DONOTWAIT) usage |= PIPE_TRANSFER_DONTBLOCK; if (pRect) { rect_to_pipe_box(&box, pRect); if (u_box_clip_2d(&box, &box, This->desc.Width, This->desc.Height) < 0) { DBG("pRect clipped by Width=%u Height=%u\n", This->desc.Width, This->desc.Height); return D3DERR_INVALIDCALL; } } else { u_box_origin_2d(This->desc.Width, This->desc.Height, &box); } user_warn(This->desc.Format == D3DFMT_NULL); if (This->data) { DBG("returning system memory\n"); /* ATI1 and ATI2 need special handling, because of d3d9 bug. * We must advertise to the application as if it is uncompressed * and bpp 8, and the app has a workaround to work with the fact * that it is actually compressed. */ if (is_ATI1_ATI2(This->base.info.format)) { pLockedRect->Pitch = This->desc.Height; pLockedRect->pBits = This->data + box.y * This->desc.Height + box.x; } else { pLockedRect->Pitch = This->stride; pLockedRect->pBits = NineSurface9_GetSystemMemPointer(This, box.x, box.y); } } else { DBG("mapping pipe_resource %p (level=%u usage=%x)\n", resource, This->level, usage); pLockedRect->pBits = This->pipe->transfer_map(This->pipe, resource, This->level, usage, &box, &This->transfer); if (!This->transfer) { DBG("transfer_map failed\n"); if (Flags & D3DLOCK_DONOTWAIT) return D3DERR_WASSTILLDRAWING; return D3DERR_INVALIDCALL; } pLockedRect->Pitch = This->transfer->stride; } if (!(Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY))) { NineSurface9_MarkContainerDirty(This); NineSurface9_AddDirtyRect(This, &box); } ++This->lock_count; return D3D_OK; }
HRESULT NineSurface9_ctor( struct NineSurface9 *This, struct NineUnknownParams *pParams, struct NineUnknown *pContainer, struct pipe_resource *pResource, void *user_buffer, uint8_t TextureType, unsigned Level, unsigned Layer, D3DSURFACE_DESC *pDesc ) { HRESULT hr; DBG("This=%p pDevice=%p pResource=%p Level=%u Layer=%u pDesc=%p\n", This, pParams->device, pResource, Level, Layer, pDesc); /* Mark this as a special surface held by another internal resource. */ pParams->container = pContainer; user_assert(!(pDesc->Usage & D3DUSAGE_DYNAMIC) || (pDesc->Pool != D3DPOOL_MANAGED), D3DERR_INVALIDCALL); assert(pResource || (user_buffer && pDesc->Pool != D3DPOOL_DEFAULT) || (!pContainer && pDesc->Pool != D3DPOOL_DEFAULT) || pDesc->Format == D3DFMT_NULL); assert(!pResource || !user_buffer); assert(!user_buffer || pDesc->Pool != D3DPOOL_DEFAULT); /* The only way we can have !pContainer is being created * from create_zs_or_rt_surface with params 0 0 0 */ assert(pContainer || (Level == 0 && Layer == 0 && TextureType == 0)); This->data = (uint8_t *)user_buffer; This->base.info.screen = pParams->device->screen; This->base.info.target = PIPE_TEXTURE_2D; This->base.info.width0 = pDesc->Width; This->base.info.height0 = pDesc->Height; This->base.info.depth0 = 1; This->base.info.last_level = 0; This->base.info.array_size = 1; This->base.info.nr_samples = pDesc->MultiSampleType; This->base.info.usage = PIPE_USAGE_DEFAULT; This->base.info.bind = PIPE_BIND_SAMPLER_VIEW; This->base.info.flags = 0; This->base.info.format = d3d9_to_pipe_format_checked(This->base.info.screen, pDesc->Format, This->base.info.target, This->base.info.nr_samples, This->base.info.bind, FALSE); if (pDesc->Usage & D3DUSAGE_RENDERTARGET) This->base.info.bind |= PIPE_BIND_RENDER_TARGET; if (pDesc->Usage & D3DUSAGE_DEPTHSTENCIL) This->base.info.bind |= PIPE_BIND_DEPTH_STENCIL; /* Ram buffer with no parent. Has to allocate the resource itself */ if (!pResource && !pContainer) { assert(!user_buffer); This->data = MALLOC( nine_format_get_level_alloc_size(This->base.info.format, pDesc->Width, pDesc->Height, 0)); if (!This->data) return E_OUTOFMEMORY; } if (pDesc->Pool == D3DPOOL_SYSTEMMEM) { This->base.info.usage = PIPE_USAGE_STAGING; assert(!pResource); } else { if (pResource && (pDesc->Usage & D3DUSAGE_DYNAMIC)) pResource->flags |= NINE_RESOURCE_FLAG_LOCKABLE; } hr = NineResource9_ctor(&This->base, pParams, pResource, FALSE, D3DRTYPE_SURFACE, pDesc->Pool, pDesc->Usage); if (FAILED(hr)) return hr; This->pipe = This->base.base.device->pipe; This->transfer = NULL; This->texture = TextureType; This->level = Level; This->level_actual = Level; This->layer = Layer; This->desc = *pDesc; This->stride = nine_format_get_stride(This->base.info.format, pDesc->Width); if (pResource && NineSurface9_IsOffscreenPlain(This)) pResource->flags |= NINE_RESOURCE_FLAG_LOCKABLE; NineSurface9_Dump(This); return D3D_OK; }
HRESULT NineSurface9_ctor( struct NineSurface9 *This, struct NineUnknownParams *pParams, struct NineUnknown *pContainer, struct pipe_resource *pResource, void *user_buffer, uint8_t TextureType, unsigned Level, unsigned Layer, D3DSURFACE_DESC *pDesc ) { HRESULT hr; union pipe_color_union rgba = {0}; struct pipe_surface *surf; struct pipe_context *pipe = pParams->device->pipe; bool allocate = !pContainer && pDesc->Format != D3DFMT_NULL; D3DMULTISAMPLE_TYPE multisample_type; DBG("This=%p pDevice=%p pResource=%p Level=%u Layer=%u pDesc=%p\n", This, pParams->device, pResource, Level, Layer, pDesc); /* Mark this as a special surface held by another internal resource. */ pParams->container = pContainer; /* Make sure there's a Desc */ assert(pDesc); /* D3DUSAGE_DYNAMIC isn't allowed on managed buffers */ user_assert(!(pDesc->Usage & D3DUSAGE_DYNAMIC) || (pDesc->Pool != D3DPOOL_MANAGED), D3DERR_INVALIDCALL); assert(allocate || pResource || user_buffer || pDesc->Format == D3DFMT_NULL); assert(!allocate || (!pResource && !user_buffer)); assert(!pResource || !user_buffer); assert(!user_buffer || pDesc->Pool != D3DPOOL_DEFAULT); assert(!pResource || pDesc->Pool == D3DPOOL_DEFAULT); /* Allocation only from create_zs_or_rt_surface with params 0 0 0 */ assert(!allocate || (Level == 0 && Layer == 0 && TextureType == 0)); This->data = (uint8_t *)user_buffer; multisample_type = pDesc->MultiSampleType; /* Map MultiSampleQuality to MultiSampleType */ hr = d3dmultisample_type_check(pParams->device->screen, pDesc->Format, &multisample_type, pDesc->MultiSampleQuality, NULL); if (FAILED(hr)) { return hr; } /* TODO: this is (except width and height) duplicate from * container info (in the pContainer case). Some refactoring is * needed to avoid duplication */ This->base.info.screen = pParams->device->screen; This->base.info.target = PIPE_TEXTURE_2D; This->base.info.width0 = pDesc->Width; This->base.info.height0 = pDesc->Height; This->base.info.depth0 = 1; This->base.info.last_level = 0; This->base.info.array_size = 1; This->base.info.nr_samples = multisample_type; This->base.info.usage = PIPE_USAGE_DEFAULT; This->base.info.bind = PIPE_BIND_SAMPLER_VIEW; /* StretchRect */ if (pDesc->Usage & D3DUSAGE_RENDERTARGET) { This->base.info.bind |= PIPE_BIND_RENDER_TARGET; } else if (pDesc->Usage & D3DUSAGE_DEPTHSTENCIL) { This->base.info.bind = d3d9_get_pipe_depth_format_bindings(pDesc->Format); if (TextureType) This->base.info.bind |= PIPE_BIND_SAMPLER_VIEW; } This->base.info.flags = 0; This->base.info.format = d3d9_to_pipe_format_checked(This->base.info.screen, pDesc->Format, This->base.info.target, This->base.info.nr_samples, This->base.info.bind, FALSE, pDesc->Pool == D3DPOOL_SCRATCH); if (This->base.info.format == PIPE_FORMAT_NONE && pDesc->Format != D3DFMT_NULL) return D3DERR_INVALIDCALL; if (allocate && compressed_format(pDesc->Format)) { const unsigned w = util_format_get_blockwidth(This->base.info.format); const unsigned h = util_format_get_blockheight(This->base.info.format); /* Note: In the !allocate case, the test could fail (lower levels of a texture) */ user_assert(!(pDesc->Width % w) && !(pDesc->Height % h), D3DERR_INVALIDCALL); } /* Get true format */ This->format_conversion = d3d9_to_pipe_format_checked(This->base.info.screen, pDesc->Format, This->base.info.target, This->base.info.nr_samples, This->base.info.bind, FALSE, TRUE); if (This->base.info.format != This->format_conversion) { This->data_conversion = align_calloc( nine_format_get_level_alloc_size(This->format_conversion, pDesc->Width, pDesc->Height, 0), 32); if (!This->data_conversion) return E_OUTOFMEMORY; This->stride_conversion = nine_format_get_stride(This->format_conversion, pDesc->Width); } if ((allocate && pDesc->Pool != D3DPOOL_DEFAULT) || pDesc->Format == D3DFMT_NULL) { /* Ram buffer with no parent. Has to allocate the resource itself */ assert(!user_buffer); This->data = align_calloc( nine_format_get_level_alloc_size(This->base.info.format, pDesc->Width, pDesc->Height, 0), 32); if (!This->data) return E_OUTOFMEMORY; } hr = NineResource9_ctor(&This->base, pParams, pResource, allocate && (pDesc->Pool == D3DPOOL_DEFAULT), D3DRTYPE_SURFACE, pDesc->Pool, pDesc->Usage); if (FAILED(hr)) return hr; This->pipe = This->base.base.device->pipe; This->transfer = NULL; This->texture = TextureType; This->level = Level; This->level_actual = Level; This->layer = Layer; This->desc = *pDesc; This->stride = nine_format_get_stride(This->base.info.format, pDesc->Width); if (This->base.resource && (pDesc->Usage & D3DUSAGE_DYNAMIC)) This->base.resource->flags |= NINE_RESOURCE_FLAG_LOCKABLE; /* TODO: investigate what else exactly needs to be cleared */ if (This->base.resource && (pDesc->Usage & D3DUSAGE_RENDERTARGET)) { surf = NineSurface9_GetSurface(This, 0); pipe->clear_render_target(pipe, surf, &rgba, 0, 0, pDesc->Width, pDesc->Height, false); } NineSurface9_Dump(This); return D3D_OK; }