aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/state_trackers/nine/cubetexture9.c
blob: 89821682f8f293c710e698ea4f7026bca9e8ad3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "c99_alloca.h"

#include "device9.h"
#include "cubetexture9.h"
#include "nine_helpers.h"
#include "nine_pipe.h"

#define DBG_CHANNEL DBG_CUBETEXTURE


static HRESULT
NineCubeTexture9_ctor( struct NineCubeTexture9 *This,
                       struct NineUnknownParams *pParams,
                       UINT EdgeLength, UINT Levels,
                       DWORD Usage,
                       D3DFORMAT Format,
                       D3DPOOL Pool,
                       HANDLE *pSharedHandle )
{
    struct pipe_resource *info = &This->base.base.info;
    struct pipe_screen *screen = pParams->device->screen;
    enum pipe_format pf;
    unsigned i, l, f, offset, face_size = 0;
    unsigned *level_offsets = NULL;
    D3DSURFACE_DESC sfdesc;
    void *p;
    HRESULT hr;

    DBG("This=%p pParams=%p EdgeLength=%u Levels=%u Usage=%d "
        "Format=%d Pool=%d pSharedHandle=%p\n",
        This, pParams, EdgeLength, Levels, Usage,
        Format, Pool, pSharedHandle);

    user_assert(EdgeLength, D3DERR_INVALIDCALL);

    /* user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL); */
    user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */

    user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP) ||
                (Pool != D3DPOOL_SYSTEMMEM && Levels <= 1), D3DERR_INVALIDCALL);

    if (Usage & D3DUSAGE_AUTOGENMIPMAP)
        Levels = 0;

    pf = d3d9_to_pipe_format_checked(screen, Format, PIPE_TEXTURE_CUBE, 0,
                                     PIPE_BIND_SAMPLER_VIEW, FALSE,
                                     Pool == D3DPOOL_SCRATCH);

    if (pf == PIPE_FORMAT_NONE)
        return D3DERR_INVALIDCALL;

    if (compressed_format(Format)) {
        const unsigned w = util_format_get_blockwidth(pf);
        const unsigned h = util_format_get_blockheight(pf);

        user_assert(!(EdgeLength % w) && !(EdgeLength % h), D3DERR_INVALIDCALL);
    }

    info->screen = pParams->device->screen;
    info->target = PIPE_TEXTURE_CUBE;
    info->format = pf;
    info->width0 = EdgeLength;
    info->height0 = EdgeLength;
    info->depth0 = 1;
    if (Levels)
        info->last_level = Levels - 1;
    else
        info->last_level = util_logbase2(EdgeLength);
    info->array_size = 6;
    info->nr_samples = 0;
    info->nr_storage_samples = 0;
    info->bind = PIPE_BIND_SAMPLER_VIEW;
    info->usage = PIPE_USAGE_DEFAULT;
    info->flags = 0;

    if (Usage & D3DUSAGE_RENDERTARGET)
        info->bind |= PIPE_BIND_RENDER_TARGET;
    if (Usage & D3DUSAGE_DEPTHSTENCIL)
        info->bind |= PIPE_BIND_DEPTH_STENCIL;

    if (Usage & D3DUSAGE_DYNAMIC) {
        info->usage = PIPE_USAGE_DYNAMIC;
    }
    if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
        DBG("Application asked for Software Vertex Processing, "
            "but this is unimplemented\n");

    if (Pool != D3DPOOL_DEFAULT) {
        level_offsets = alloca(sizeof(unsigned) * (info->last_level + 1));
        face_size = nine_format_get_size_and_offsets(pf, level_offsets,
                                                     EdgeLength, EdgeLength,
                                                     info->last_level);
        This->managed_buffer = align_calloc(6 * face_size, 32);
        if (!This->managed_buffer)
            return E_OUTOFMEMORY;
    }

    This->surfaces = CALLOC(6 * (info->last_level + 1), sizeof(*This->surfaces));
    if (!This->surfaces)
        return E_OUTOFMEMORY;

    hr = NineBaseTexture9_ctor(&This->base, pParams, NULL, D3DRTYPE_CUBETEXTURE,
                               Format, Pool, Usage);
    if (FAILED(hr))
        return hr;
    This->base.pstype = 2;

    /* Create all the surfaces right away.
     * They manage backing storage, and transfers (LockRect) are deferred
     * to them.
     */
    sfdesc.Format = Format;
    sfdesc.Type = D3DRTYPE_SURFACE;
    sfdesc.Usage = Usage;
    sfdesc.Pool = Pool;
    sfdesc.MultiSampleType = D3DMULTISAMPLE_NONE;
    sfdesc.MultiSampleQuality = 0;
    /* We allocate the memory for the surfaces as continous blocks.
     * This is the expected behaviour, however we haven't tested for
     * cube textures in which order the faces/levels should be in memory
     */
    for (f = 0; f < 6; f++) {
        offset = f * face_size;
        for (l = 0; l <= info->last_level; l++) {
            sfdesc.Width = sfdesc.Height = u_minify(EdgeLength, l);
            p = This->managed_buffer ? This->managed_buffer + offset +
                    level_offsets[l] : NULL;

            hr = NineSurface9_new(This->base.base.base.device, NineUnknown(This),
                                  This->base.base.resource, p, D3DRTYPE_CUBETEXTURE,
                                  l, f, &sfdesc, &This->surfaces[f + 6 * l]);
            if (FAILED(hr))
                return hr;
        }
    }

    for (i = 0; i < 6; ++i) {
        /* Textures start initially dirty */
        This->dirty_rect[i].width = EdgeLength;
        This->dirty_rect[i].height = EdgeLength;
        This->dirty_rect[i].depth = 1;
    }

    return D3D_OK;
}

static void
NineCubeTexture9_dtor( struct NineCubeTexture9 *This )
{
    unsigned i;

    DBG("This=%p\n", This);

    if (This->surfaces) {
        for (i = 0; i < (This->base.base.info.last_level + 1) * 6; ++i)
            NineUnknown_Destroy(&This->surfaces[i]->base.base);
        FREE(This->surfaces);
    }

    if (This->managed_buffer)
        align_free(This->managed_buffer);

    NineBaseTexture9_dtor(&This->base);
}

HRESULT NINE_WINAPI
NineCubeTexture9_GetLevelDesc( struct NineCubeTexture9 *This,
                               UINT Level,
                               D3DSURFACE_DESC *pDesc )
{
    DBG("This=%p Level=%u pDesc=%p\n", This, Level, pDesc);

    user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
                D3DERR_INVALIDCALL);

    *pDesc = This->surfaces[Level * 6]->desc;

    return D3D_OK;
}

HRESULT NINE_WINAPI
NineCubeTexture9_GetCubeMapSurface( struct NineCubeTexture9 *This,
                                    D3DCUBEMAP_FACES FaceType,
                                    UINT Level,
                                    IDirect3DSurface9 **ppCubeMapSurface )
{
    const unsigned s = Level * 6 + FaceType;

    DBG("This=%p FaceType=%d Level=%u ppCubeMapSurface=%p\n",
        This, FaceType, Level, ppCubeMapSurface);

    user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
                D3DERR_INVALIDCALL);
    user_assert(FaceType < 6, D3DERR_INVALIDCALL);

    NineUnknown_AddRef(NineUnknown(This->surfaces[s]));
    *ppCubeMapSurface = (IDirect3DSurface9 *)This->surfaces[s];

    return D3D_OK;
}

HRESULT NINE_WINAPI
NineCubeTexture9_LockRect( struct NineCubeTexture9 *This,
                           D3DCUBEMAP_FACES FaceType,
                           UINT Level,
                           D3DLOCKED_RECT *pLockedRect,
                           const RECT *pRect,
                           DWORD Flags )
{
    const unsigned s = Level * 6 + FaceType;

    DBG("This=%p FaceType=%d Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
        This, FaceType, Level, pLockedRect, pRect, Flags);

    user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
                D3DERR_INVALIDCALL);
    user_assert(FaceType < 6, D3DERR_INVALIDCALL);

    return NineSurface9_LockRect(This->surfaces[s], pLockedRect, pRect, Flags);
}

HRESULT NINE_WINAPI
NineCubeTexture9_UnlockRect( struct NineCubeTexture9 *This,
                             D3DCUBEMAP_FACES FaceType,
                             UINT Level )
{
    const unsigned s = Level * 6 + FaceType;

    DBG("This=%p FaceType=%d Level=%u\n", This, FaceType, Level);

    user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
    user_assert(FaceType < 6, D3DERR_INVALIDCALL);

    return NineSurface9_UnlockRect(This->surfaces[s]);
}

HRESULT NINE_WINAPI
NineCubeTexture9_AddDirtyRect( struct NineCubeTexture9 *This,
                               D3DCUBEMAP_FACES FaceType,
                               const RECT *pDirtyRect )
{
    DBG("This=%p FaceType=%d pDirtyRect=%p\n", This, FaceType, pDirtyRect);

    user_assert(FaceType < 6, D3DERR_INVALIDCALL);

    if (This->base.base.pool != D3DPOOL_MANAGED) {
        if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP) {
            This->base.dirty_mip = TRUE;
            BASETEX_REGISTER_UPDATE(&This->base);
        }
        return D3D_OK;
    }

    if (This->base.base.pool == D3DPOOL_MANAGED) {
        This->base.managed.dirty = TRUE;
        BASETEX_REGISTER_UPDATE(&This->base);
    }

    if (!pDirtyRect) {
        u_box_origin_2d(This->base.base.info.width0,
                        This->base.base.info.height0,
                        &This->dirty_rect[FaceType]);
    } else {
        if (This->dirty_rect[FaceType].width == 0) {
            rect_to_pipe_box_clamp(&This->dirty_rect[FaceType], pDirtyRect);
        } else {
            struct pipe_box box;
            rect_to_pipe_box_clamp(&box, pDirtyRect);
            u_box_union_2d(&This->dirty_rect[FaceType], &This->dirty_rect[FaceType],
                           &box);
        }
        (void) u_box_clip_2d(&This->dirty_rect[FaceType],
                             &This->dirty_rect[FaceType],
                             This->base.base.info.width0,
                             This->base.base.info.height0);
    }
    return D3D_OK;
}

IDirect3DCubeTexture9Vtbl NineCubeTexture9_vtable = {
    (void *)NineUnknown_QueryInterface,
    (void *)NineUnknown_AddRef,
    (void *)NineUnknown_Release,
    (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
    (void *)NineUnknown_SetPrivateData,
    (void *)NineUnknown_GetPrivateData,
    (void *)NineUnknown_FreePrivateData,
    (void *)NineResource9_SetPriority,
    (void *)NineResource9_GetPriority,
    (void *)NineBaseTexture9_PreLoad,
    (void *)NineResource9_GetType,
    (void *)NineBaseTexture9_SetLOD,
    (void *)NineBaseTexture9_GetLOD,
    (void *)NineBaseTexture9_GetLevelCount,
    (void *)NineBaseTexture9_SetAutoGenFilterType,
    (void *)NineBaseTexture9_GetAutoGenFilterType,
    (void *)NineBaseTexture9_GenerateMipSubLevels,
    (void *)NineCubeTexture9_GetLevelDesc,
    (void *)NineCubeTexture9_GetCubeMapSurface,
    (void *)NineCubeTexture9_LockRect,
    (void *)NineCubeTexture9_UnlockRect,
    (void *)NineCubeTexture9_AddDirtyRect
};

static const GUID *NineCubeTexture9_IIDs[] = {
    &IID_IDirect3DCubeTexture9,
    &IID_IDirect3DBaseTexture9,
    &IID_IDirect3DResource9,
    &IID_IUnknown,
    NULL
};

HRESULT
NineCubeTexture9_new( struct NineDevice9 *pDevice,
                      UINT EdgeLength, UINT Levels,
                      DWORD Usage,
                      D3DFORMAT Format,
                      D3DPOOL Pool,
                      struct NineCubeTexture9 **ppOut,
                      HANDLE *pSharedHandle )
{
    NINE_DEVICE_CHILD_NEW(CubeTexture9, ppOut, pDevice,
                          EdgeLength, Levels,
                          Usage, Format, Pool, pSharedHandle);
}