Home | History | Annotate | Download | only in nine
      1 /*
      2  * Copyright 2011 Joakim Sindholt <opensource (at) 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 "c99_alloca.h"
     24 
     25 #include "device9.h"
     26 #include "surface9.h"
     27 #include "texture9.h"
     28 #include "nine_helpers.h"
     29 #include "nine_pipe.h"
     30 #include "nine_dump.h"
     31 
     32 #include "pipe/p_state.h"
     33 #include "pipe/p_context.h"
     34 #include "pipe/p_screen.h"
     35 #include "util/u_inlines.h"
     36 #include "util/u_resource.h"
     37 
     38 #define DBG_CHANNEL DBG_TEXTURE
     39 
     40 static HRESULT
     41 NineTexture9_ctor( struct NineTexture9 *This,
     42                    struct NineUnknownParams *pParams,
     43                    UINT Width, UINT Height, UINT Levels,
     44                    DWORD Usage,
     45                    D3DFORMAT Format,
     46                    D3DPOOL Pool,
     47                    HANDLE *pSharedHandle )
     48 {
     49     struct pipe_screen *screen = pParams->device->screen;
     50     struct pipe_resource *info = &This->base.base.info;
     51     enum pipe_format pf;
     52     unsigned *level_offsets;
     53     unsigned l;
     54     D3DSURFACE_DESC sfdesc;
     55     HRESULT hr;
     56     void *user_buffer = NULL, *user_buffer_for_level;
     57 
     58     DBG("(%p) Width=%u Height=%u Levels=%u Usage=%s Format=%s Pool=%s "
     59         "pSharedHandle=%p\n", This, Width, Height, Levels,
     60         nine_D3DUSAGE_to_str(Usage),
     61         d3dformat_to_string(Format), nine_D3DPOOL_to_str(Pool), pSharedHandle);
     62 
     63     user_assert(Width && Height, D3DERR_INVALIDCALL);
     64 
     65     /* pSharedHandle: can be non-null for ex only.
     66      * D3DPOOL_SYSTEMMEM: Levels must be 1
     67      * D3DPOOL_DEFAULT: no restriction for Levels
     68      * Other Pools are forbidden. */
     69     user_assert(!pSharedHandle || pParams->device->ex, D3DERR_INVALIDCALL);
     70     user_assert(!pSharedHandle ||
     71                 (Pool == D3DPOOL_SYSTEMMEM && Levels == 1) ||
     72                 Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
     73 
     74     user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP) ||
     75                 (Pool != D3DPOOL_SYSTEMMEM && Pool != D3DPOOL_SCRATCH && Levels <= 1),
     76                 D3DERR_INVALIDCALL);
     77 
     78     /* TODO: implement pSharedHandle for D3DPOOL_DEFAULT (cross process
     79      * buffer sharing).
     80      *
     81      * Gem names may have fit but they're depreciated and won't work on render-nodes.
     82      * One solution is to use shm buffers. We would use a /dev/shm file, fill the first
     83      * values to tell it is a nine buffer, the size, which function created it, etc,
     84      * and then it would contain the data. The handle would be a number, corresponding to
     85      * the file to read (/dev/shm/nine-share-4 for example would be 4).
     86      *
     87      * Wine just ignores the argument, which works only if the app creates the handle
     88      * and won't use it. Instead of failing, we support that situation by putting an
     89      * invalid handle, that we would fail to import. Please note that we don't advertise
     90      * the flag indicating the support for that feature, but apps seem to not care.
     91      */
     92 
     93     if (pSharedHandle && Pool == D3DPOOL_DEFAULT) {
     94         if (!*pSharedHandle) {
     95             DBG("Creating Texture with invalid handle. Importing will fail\n.");
     96             *pSharedHandle = (HANDLE)1; /* Wine would keep it NULL */
     97             pSharedHandle = NULL;
     98         } else {
     99             ERR("Application tries to use cross-process sharing feature. Nine "
    100                 "doesn't support it");
    101             return D3DERR_INVALIDCALL;
    102         }
    103     }
    104 
    105     if (Usage & D3DUSAGE_AUTOGENMIPMAP)
    106         Levels = 0;
    107 
    108     pf = d3d9_to_pipe_format_checked(screen, Format, PIPE_TEXTURE_2D, 0,
    109                                      PIPE_BIND_SAMPLER_VIEW, FALSE,
    110                                      Pool == D3DPOOL_SCRATCH);
    111 
    112     if (Format != D3DFMT_NULL && pf == PIPE_FORMAT_NONE)
    113         return D3DERR_INVALIDCALL;
    114 
    115     if (compressed_format(Format)) {
    116         const unsigned w = util_format_get_blockwidth(pf);
    117         const unsigned h = util_format_get_blockheight(pf);
    118 
    119         user_assert(!(Width % w) && !(Height % h), D3DERR_INVALIDCALL);
    120     }
    121 
    122     info->screen = screen;
    123     info->target = PIPE_TEXTURE_2D;
    124     info->format = pf;
    125     info->width0 = Width;
    126     info->height0 = Height;
    127     info->depth0 = 1;
    128     if (Levels)
    129         info->last_level = Levels - 1;
    130     else
    131         info->last_level = util_logbase2(MAX2(Width, Height));
    132     info->array_size = 1;
    133     info->nr_samples = 0;
    134     info->bind = PIPE_BIND_SAMPLER_VIEW;
    135     info->usage = PIPE_USAGE_DEFAULT;
    136     info->flags = 0;
    137 
    138     if (Usage & D3DUSAGE_RENDERTARGET)
    139         info->bind |= PIPE_BIND_RENDER_TARGET;
    140     if (Usage & D3DUSAGE_DEPTHSTENCIL)
    141         info->bind |= PIPE_BIND_DEPTH_STENCIL;
    142 
    143     if (Usage & D3DUSAGE_DYNAMIC) {
    144         info->usage = PIPE_USAGE_DYNAMIC;
    145     }
    146 
    147     if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
    148         DBG("Application asked for Software Vertex Processing, "
    149             "but this is unimplemented\n");
    150 
    151     if (pSharedHandle && *pSharedHandle) { /* Pool == D3DPOOL_SYSTEMMEM */
    152         user_buffer = (void *)*pSharedHandle;
    153         level_offsets = alloca(sizeof(unsigned) * (info->last_level + 1));
    154         (void) nine_format_get_size_and_offsets(pf, level_offsets,
    155                                                 Width, Height,
    156                                                 info->last_level);
    157     } else if (Pool != D3DPOOL_DEFAULT) {
    158         /* TODO: For D3DUSAGE_AUTOGENMIPMAP, it is likely we only have to
    159          * allocate only for the first level, since it is the only lockable
    160          * level. Check apps don't crash if we allocate smaller buffer (some
    161          * apps access sublevels of texture even if they locked only first
    162          * level) */
    163         level_offsets = alloca(sizeof(unsigned) * (info->last_level + 1));
    164         user_buffer = align_calloc(
    165             nine_format_get_size_and_offsets(pf, level_offsets,
    166                                              Width, Height,
    167                                              info->last_level), 32);
    168         This->managed_buffer = user_buffer;
    169         if (!This->managed_buffer)
    170             return E_OUTOFMEMORY;
    171     }
    172 
    173     This->surfaces = CALLOC(info->last_level + 1, sizeof(*This->surfaces));
    174     if (!This->surfaces)
    175         return E_OUTOFMEMORY;
    176 
    177     hr = NineBaseTexture9_ctor(&This->base, pParams, NULL, D3DRTYPE_TEXTURE, Format, Pool, Usage);
    178     if (FAILED(hr))
    179         return hr;
    180     This->base.pstype = (Height == 1) ? 1 : 0;
    181 
    182     /* Create all the surfaces right away.
    183      * They manage backing storage, and transfers (LockRect) are deferred
    184      * to them.
    185      */
    186     sfdesc.Format = Format;
    187     sfdesc.Type = D3DRTYPE_SURFACE;
    188     sfdesc.Usage = Usage;
    189     sfdesc.Pool = Pool;
    190     sfdesc.MultiSampleType = D3DMULTISAMPLE_NONE;
    191     sfdesc.MultiSampleQuality = 0;
    192 
    193     for (l = 0; l <= info->last_level; ++l) {
    194         sfdesc.Width = u_minify(Width, l);
    195         sfdesc.Height = u_minify(Height, l);
    196         /* Some apps expect the memory to be allocated in
    197          * continous blocks */
    198         user_buffer_for_level = user_buffer ? user_buffer +
    199             level_offsets[l] : NULL;
    200 
    201         hr = NineSurface9_new(This->base.base.base.device, NineUnknown(This),
    202                               This->base.base.resource, user_buffer_for_level,
    203                               D3DRTYPE_TEXTURE, l, 0,
    204                               &sfdesc, &This->surfaces[l]);
    205         if (FAILED(hr))
    206             return hr;
    207     }
    208 
    209     /* Textures start initially dirty */
    210     This->dirty_rect.width = Width;
    211     This->dirty_rect.height = Height;
    212     This->dirty_rect.depth = 1; /* widht == 0 means empty, depth stays 1 */
    213 
    214     if (pSharedHandle && !*pSharedHandle) {/* Pool == D3DPOOL_SYSTEMMEM */
    215         *pSharedHandle = This->surfaces[0]->data;
    216     }
    217 
    218     return D3D_OK;
    219 }
    220 
    221 static void
    222 NineTexture9_dtor( struct NineTexture9 *This )
    223 {
    224     unsigned l;
    225 
    226     DBG("This=%p\n", This);
    227 
    228     if (This->surfaces) {
    229         /* The surfaces should have 0 references and be unbound now. */
    230         for (l = 0; l <= This->base.base.info.last_level; ++l)
    231             if (This->surfaces[l])
    232                 NineUnknown_Destroy(&This->surfaces[l]->base.base);
    233         FREE(This->surfaces);
    234     }
    235 
    236     if (This->managed_buffer)
    237         align_free(This->managed_buffer);
    238 
    239     NineBaseTexture9_dtor(&This->base);
    240 }
    241 
    242 HRESULT NINE_WINAPI
    243 NineTexture9_GetLevelDesc( struct NineTexture9 *This,
    244                            UINT Level,
    245                            D3DSURFACE_DESC *pDesc )
    246 {
    247     DBG("This=%p Level=%d pDesc=%p\n", This, Level, pDesc);
    248 
    249     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    250     user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
    251                 D3DERR_INVALIDCALL);
    252 
    253     *pDesc = This->surfaces[Level]->desc;
    254 
    255     return D3D_OK;
    256 }
    257 
    258 HRESULT NINE_WINAPI
    259 NineTexture9_GetSurfaceLevel( struct NineTexture9 *This,
    260                               UINT Level,
    261                               IDirect3DSurface9 **ppSurfaceLevel )
    262 {
    263     DBG("This=%p Level=%d ppSurfaceLevel=%p\n", This, Level, ppSurfaceLevel);
    264 
    265     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    266     user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
    267                 D3DERR_INVALIDCALL);
    268 
    269     NineUnknown_AddRef(NineUnknown(This->surfaces[Level]));
    270     *ppSurfaceLevel = (IDirect3DSurface9 *)This->surfaces[Level];
    271 
    272     return D3D_OK;
    273 }
    274 
    275 HRESULT NINE_WINAPI
    276 NineTexture9_LockRect( struct NineTexture9 *This,
    277                        UINT Level,
    278                        D3DLOCKED_RECT *pLockedRect,
    279                        const RECT *pRect,
    280                        DWORD Flags )
    281 {
    282     DBG("This=%p Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
    283         This, Level, pLockedRect, pRect, Flags);
    284 
    285     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    286     user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
    287                 D3DERR_INVALIDCALL);
    288 
    289     return NineSurface9_LockRect(This->surfaces[Level], pLockedRect,
    290                                  pRect, Flags);
    291 }
    292 
    293 HRESULT NINE_WINAPI
    294 NineTexture9_UnlockRect( struct NineTexture9 *This,
    295                          UINT Level )
    296 {
    297     DBG("This=%p Level=%u\n", This, Level);
    298 
    299     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    300 
    301     return NineSurface9_UnlockRect(This->surfaces[Level]);
    302 }
    303 
    304 HRESULT NINE_WINAPI
    305 NineTexture9_AddDirtyRect( struct NineTexture9 *This,
    306                            const RECT *pDirtyRect )
    307 {
    308     DBG("This=%p pDirtyRect=%p[(%u,%u)-(%u,%u)]\n", This, pDirtyRect,
    309         pDirtyRect ? pDirtyRect->left : 0, pDirtyRect ? pDirtyRect->top : 0,
    310         pDirtyRect ? pDirtyRect->right : 0, pDirtyRect ? pDirtyRect->bottom : 0);
    311 
    312     /* Tracking dirty regions on DEFAULT resources is pointless,
    313      * because we always write to the final storage. Just marked it dirty in
    314      * case we need to generate mip maps.
    315      */
    316     if (This->base.base.pool == D3DPOOL_DEFAULT) {
    317         if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP) {
    318             This->base.dirty_mip = TRUE;
    319             BASETEX_REGISTER_UPDATE(&This->base);
    320         }
    321         return D3D_OK;
    322     }
    323 
    324     if (This->base.base.pool == D3DPOOL_MANAGED) {
    325         This->base.managed.dirty = TRUE;
    326         BASETEX_REGISTER_UPDATE(&This->base);
    327     }
    328 
    329     if (!pDirtyRect) {
    330         u_box_origin_2d(This->base.base.info.width0,
    331                         This->base.base.info.height0, &This->dirty_rect);
    332     } else {
    333         if (This->dirty_rect.width == 0) {
    334             rect_to_pipe_box_clamp(&This->dirty_rect, pDirtyRect);
    335         } else {
    336             struct pipe_box box;
    337             rect_to_pipe_box_clamp(&box, pDirtyRect);
    338             u_box_union_2d(&This->dirty_rect, &This->dirty_rect, &box);
    339         }
    340         (void) u_box_clip_2d(&This->dirty_rect, &This->dirty_rect,
    341                              This->base.base.info.width0,
    342                              This->base.base.info.height0);
    343     }
    344     return D3D_OK;
    345 }
    346 
    347 IDirect3DTexture9Vtbl NineTexture9_vtable = {
    348     (void *)NineUnknown_QueryInterface,
    349     (void *)NineUnknown_AddRef,
    350     (void *)NineUnknown_Release,
    351     (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
    352     (void *)NineUnknown_SetPrivateData,
    353     (void *)NineUnknown_GetPrivateData,
    354     (void *)NineUnknown_FreePrivateData,
    355     (void *)NineResource9_SetPriority,
    356     (void *)NineResource9_GetPriority,
    357     (void *)NineBaseTexture9_PreLoad,
    358     (void *)NineResource9_GetType,
    359     (void *)NineBaseTexture9_SetLOD,
    360     (void *)NineBaseTexture9_GetLOD,
    361     (void *)NineBaseTexture9_GetLevelCount,
    362     (void *)NineBaseTexture9_SetAutoGenFilterType,
    363     (void *)NineBaseTexture9_GetAutoGenFilterType,
    364     (void *)NineBaseTexture9_GenerateMipSubLevels,
    365     (void *)NineTexture9_GetLevelDesc,
    366     (void *)NineTexture9_GetSurfaceLevel,
    367     (void *)NineTexture9_LockRect,
    368     (void *)NineTexture9_UnlockRect,
    369     (void *)NineTexture9_AddDirtyRect
    370 };
    371 
    372 static const GUID *NineTexture9_IIDs[] = {
    373     &IID_IDirect3DTexture9,
    374     &IID_IDirect3DBaseTexture9,
    375     &IID_IDirect3DResource9,
    376     &IID_IUnknown,
    377     NULL
    378 };
    379 
    380 HRESULT
    381 NineTexture9_new( struct NineDevice9 *pDevice,
    382                   UINT Width, UINT Height, UINT Levels,
    383                   DWORD Usage,
    384                   D3DFORMAT Format,
    385                   D3DPOOL Pool,
    386                   struct NineTexture9 **ppOut,
    387                   HANDLE *pSharedHandle )
    388 {
    389     NINE_DEVICE_CHILD_NEW(Texture9, ppOut, pDevice,
    390                           Width, Height, Levels,
    391                           Usage, Format, Pool, pSharedHandle);
    392 }
    393