Example #1
0
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;
}
Example #2
0
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;

    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,
                                                         pDesc->Pool == D3DPOOL_SCRATCH);

    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;

    /* 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_malloc(
            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);
    }

    /* Ram buffer with no parent. Has to allocate the resource itself */
    if (!pResource && !pContainer) {
        assert(!user_buffer);
        This->data = align_malloc(
            nine_format_get_level_alloc_size(This->base.info.format,
                                             pDesc->Width,
                                             pDesc->Height,
                                             0), 32);
        if (!This->data)
            return E_OUTOFMEMORY;
    }

    assert(pDesc->Pool != D3DPOOL_SYSTEMMEM || !pResource);

    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;

    /* 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);
    }

    NineSurface9_Dump(This);

    return D3D_OK;
}
Example #3
0
static INLINE HRESULT
present( struct NineSwapChain9 *This,
         const RECT *pSourceRect,
         const RECT *pDestRect,
         HWND hDestWindowOverride,
         const RGNDATA *pDirtyRegion,
         DWORD dwFlags )
{
    struct NineDevice9 *device = This->base.device;
    struct pipe_resource *resource;
    HRESULT hr;
    RGNDATA *rgndata;
    RECT rect;
    struct pipe_blit_info blit;

    /* get a real backbuffer handle from the windowing system */
    hr = This->actx->resource_from_present(This->actx, This->screen,
                                           This->present, hDestWindowOverride,
                                           pDestRect, &rect, &rgndata,
                                           &resource);
    if (FAILED(hr)) {
        return hr;
    } else if (hr == D3DOK_WINDOW_OCCLUDED) {
        /* if we present, nothing will show, so don't present */
        return D3D_OK;
    }

    DBG(">>>\npresent: This=%p pSourceRect=%p pDestRect=%p "
        "pDirtyRegion=%p rgndata=%p\n",
        This, pSourceRect, pDestRect, pDirtyRegion, rgndata);
    if (pSourceRect)
        DBG("pSourceRect = (%u..%u)x(%u..%u)\n",
            pSourceRect->left, pSourceRect->right,
            pSourceRect->top, pSourceRect->bottom);
    if (pDestRect)
        DBG("pDestRect = (%u..%u)x(%u..%u)\n",
            pDestRect->left, pDestRect->right,
            pDestRect->top, pDestRect->bottom);

    if (rgndata) {
        /* TODO */
        blit.dst.resource = NULL;
    } else {
        struct pipe_surface *suf = NineSurface9_GetSurface(This->buffers[0], 0);
        blit.dst.resource = resource;
        blit.dst.level = 0;
        blit.dst.format = resource->format;
        blit.dst.box.z = 0;
        blit.dst.box.depth = 1;
        if (pDestRect) {
            rect_to_pipe_box_xy_only(&blit.dst.box, pDestRect);
            blit.dst.box.x += rect.left;
            blit.dst.box.y += rect.top;
            if (u_box_clip_2d(&blit.dst.box, &blit.dst.box,
                              rect.right, rect.bottom) > 0) {
                DBG("Dest region clipped.\n");
                return D3D_OK;
            }
        } else {
            rect_to_pipe_box_xy_only(&blit.dst.box, &rect);
        }

        blit.src.resource = suf->texture;
        blit.src.level = This->buffers[0]->level;
        blit.src.format = blit.src.resource->format;
        blit.src.box.z = 0;
        blit.src.box.depth = 1;
        if (pSourceRect) {
            rect_to_pipe_box_xy_only(&blit.src.box, pSourceRect);
            u_box_clip_2d(&blit.src.box, &blit.src.box,
                          suf->width,
                          suf->height);
        } else {
            blit.src.box.x = 0;
            blit.src.box.y = 0;
            blit.src.box.width = suf->width;
            blit.src.box.height = suf->height;
        }

        blit.mask = PIPE_MASK_RGBA;
        blit.filter = PIPE_TEX_FILTER_NEAREST;
        blit.scissor_enable = FALSE;
        blit.alpha_blend = FALSE;

        /* blit (and possibly stretch/convert) pixels from This->buffers[0] to
         * emusurf using u_blit. Windows appears to use NEAREST */
        DBG("Blitting (%u..%u)x(%u..%u) to (%u..%u)x(%u..%u).\n",
            blit.src.box.x, blit.src.box.x + blit.src.box.width,
            blit.src.box.y, blit.src.box.y + blit.src.box.height,
            blit.dst.box.x, blit.dst.box.x + blit.dst.box.width,
            blit.dst.box.y, blit.dst.box.y + blit.dst.box.height);

        This->pipe->blit(This->pipe, &blit);
    }

    if (device->cursor.software && device->cursor.visible && device->cursor.w &&
        blit.dst.resource) {
        blit.src.resource = device->cursor.image;
        blit.src.level = 0;
        blit.src.format = device->cursor.image->format;
        blit.src.box.x = 0;
        blit.src.box.y = 0;
        blit.src.box.width = device->cursor.w;
        blit.src.box.height = device->cursor.h;

        ID3DPresent_GetCursorPos(This->present, &device->cursor.pos);

        /* NOTE: blit messes up when box.x + box.width < 0, fix driver */
        blit.dst.box.x = MAX2(device->cursor.pos.x, 0) - device->cursor.hotspot.x;
        blit.dst.box.y = MAX2(device->cursor.pos.y, 0) - device->cursor.hotspot.y;
        blit.dst.box.width = blit.src.box.width;
        blit.dst.box.height = blit.src.box.height;

        DBG("Blitting cursor(%ux%u) to (%i,%i).\n",
            blit.src.box.width, blit.src.box.height,
            blit.dst.box.x, blit.dst.box.y);

        blit.alpha_blend = TRUE;
        This->pipe->blit(This->pipe, &blit);
    }

    if (device->hud && resource) {
        hud_draw(device->hud, resource); /* XXX: no offset */
        /* HUD doesn't clobber stipple */
        NineDevice9_RestoreNonCSOState(device, ~0x2);
    }

    This->pipe->flush(This->pipe, NULL, PIPE_FLUSH_END_OF_FRAME);

    /* really present the frame */
    hr = ID3DPresent_Present(This->present, dwFlags);
    pipe_resource_reference(&resource, NULL);
    if (FAILED(hr)) { return hr; }

    return D3D_OK;
}