OSDN Git Service

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