• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
NineTexture9_ctor(struct NineTexture9 * This,struct NineUnknownParams * pParams,UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,HANDLE * pSharedHandle)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->nr_storage_samples = 0;
135     info->bind = PIPE_BIND_SAMPLER_VIEW;
136     info->usage = PIPE_USAGE_DEFAULT;
137     info->flags = 0;
138 
139     if (Usage & D3DUSAGE_RENDERTARGET)
140         info->bind |= PIPE_BIND_RENDER_TARGET;
141     if (Usage & D3DUSAGE_DEPTHSTENCIL)
142         info->bind |= PIPE_BIND_DEPTH_STENCIL;
143 
144     if (Usage & D3DUSAGE_DYNAMIC) {
145         info->usage = PIPE_USAGE_DYNAMIC;
146     }
147 
148     if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
149         DBG("Application asked for Software Vertex Processing, "
150             "but this is unimplemented\n");
151 
152     if (pSharedHandle && *pSharedHandle) { /* Pool == D3DPOOL_SYSTEMMEM */
153         user_buffer = (void *)*pSharedHandle;
154         level_offsets = alloca(sizeof(unsigned) * (info->last_level + 1));
155         (void) nine_format_get_size_and_offsets(pf, level_offsets,
156                                                 Width, Height,
157                                                 info->last_level);
158     } else if (Pool != D3DPOOL_DEFAULT) {
159         /* TODO: For D3DUSAGE_AUTOGENMIPMAP, it is likely we only have to
160          * allocate only for the first level, since it is the only lockable
161          * level. Check apps don't crash if we allocate smaller buffer (some
162          * apps access sublevels of texture even if they locked only first
163          * level) */
164         level_offsets = alloca(sizeof(unsigned) * (info->last_level + 1));
165         user_buffer = align_calloc(
166             nine_format_get_size_and_offsets(pf, level_offsets,
167                                              Width, Height,
168                                              info->last_level), 32);
169         This->managed_buffer = user_buffer;
170         if (!This->managed_buffer)
171             return E_OUTOFMEMORY;
172     }
173 
174     This->surfaces = CALLOC(info->last_level + 1, sizeof(*This->surfaces));
175     if (!This->surfaces)
176         return E_OUTOFMEMORY;
177 
178     hr = NineBaseTexture9_ctor(&This->base, pParams, NULL, D3DRTYPE_TEXTURE, Format, Pool, Usage);
179     if (FAILED(hr))
180         return hr;
181     This->base.pstype = (Height == 1) ? 1 : 0;
182 
183     /* Create all the surfaces right away.
184      * They manage backing storage, and transfers (LockRect) are deferred
185      * to them.
186      */
187     sfdesc.Format = Format;
188     sfdesc.Type = D3DRTYPE_SURFACE;
189     sfdesc.Usage = Usage;
190     sfdesc.Pool = Pool;
191     sfdesc.MultiSampleType = D3DMULTISAMPLE_NONE;
192     sfdesc.MultiSampleQuality = 0;
193 
194     for (l = 0; l <= info->last_level; ++l) {
195         sfdesc.Width = u_minify(Width, l);
196         sfdesc.Height = u_minify(Height, l);
197         /* Some apps expect the memory to be allocated in
198          * continous blocks */
199         user_buffer_for_level = user_buffer ? user_buffer +
200             level_offsets[l] : NULL;
201 
202         hr = NineSurface9_new(This->base.base.base.device, NineUnknown(This),
203                               This->base.base.resource, user_buffer_for_level,
204                               D3DRTYPE_TEXTURE, l, 0,
205                               &sfdesc, &This->surfaces[l]);
206         if (FAILED(hr))
207             return hr;
208     }
209 
210     /* Textures start initially dirty */
211     This->dirty_rect.width = Width;
212     This->dirty_rect.height = Height;
213     This->dirty_rect.depth = 1; /* widht == 0 means empty, depth stays 1 */
214 
215     if (pSharedHandle && !*pSharedHandle) {/* Pool == D3DPOOL_SYSTEMMEM */
216         *pSharedHandle = This->surfaces[0]->data;
217     }
218 
219     return D3D_OK;
220 }
221 
222 static void
NineTexture9_dtor(struct NineTexture9 * This)223 NineTexture9_dtor( struct NineTexture9 *This )
224 {
225     unsigned l;
226 
227     DBG("This=%p\n", This);
228 
229     if (This->surfaces) {
230         /* The surfaces should have 0 references and be unbound now. */
231         for (l = 0; l <= This->base.base.info.last_level; ++l)
232             if (This->surfaces[l])
233                 NineUnknown_Destroy(&This->surfaces[l]->base.base);
234         FREE(This->surfaces);
235     }
236 
237     if (This->managed_buffer)
238         align_free(This->managed_buffer);
239 
240     NineBaseTexture9_dtor(&This->base);
241 }
242 
243 HRESULT NINE_WINAPI
NineTexture9_GetLevelDesc(struct NineTexture9 * This,UINT Level,D3DSURFACE_DESC * pDesc)244 NineTexture9_GetLevelDesc( struct NineTexture9 *This,
245                            UINT Level,
246                            D3DSURFACE_DESC *pDesc )
247 {
248     DBG("This=%p Level=%d pDesc=%p\n", This, Level, pDesc);
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     *pDesc = This->surfaces[Level]->desc;
255 
256     return D3D_OK;
257 }
258 
259 HRESULT NINE_WINAPI
NineTexture9_GetSurfaceLevel(struct NineTexture9 * This,UINT Level,IDirect3DSurface9 ** ppSurfaceLevel)260 NineTexture9_GetSurfaceLevel( struct NineTexture9 *This,
261                               UINT Level,
262                               IDirect3DSurface9 **ppSurfaceLevel )
263 {
264     DBG("This=%p Level=%d ppSurfaceLevel=%p\n", This, Level, ppSurfaceLevel);
265 
266     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
267     user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
268                 D3DERR_INVALIDCALL);
269 
270     NineUnknown_AddRef(NineUnknown(This->surfaces[Level]));
271     *ppSurfaceLevel = (IDirect3DSurface9 *)This->surfaces[Level];
272 
273     return D3D_OK;
274 }
275 
276 HRESULT NINE_WINAPI
NineTexture9_LockRect(struct NineTexture9 * This,UINT Level,D3DLOCKED_RECT * pLockedRect,const RECT * pRect,DWORD Flags)277 NineTexture9_LockRect( struct NineTexture9 *This,
278                        UINT Level,
279                        D3DLOCKED_RECT *pLockedRect,
280                        const RECT *pRect,
281                        DWORD Flags )
282 {
283     DBG("This=%p Level=%u pLockedRect=%p pRect=%p Flags=%d\n",
284         This, Level, pLockedRect, pRect, Flags);
285 
286     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
287     user_assert(Level == 0 || !(This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP),
288                 D3DERR_INVALIDCALL);
289 
290     return NineSurface9_LockRect(This->surfaces[Level], pLockedRect,
291                                  pRect, Flags);
292 }
293 
294 HRESULT NINE_WINAPI
NineTexture9_UnlockRect(struct NineTexture9 * This,UINT Level)295 NineTexture9_UnlockRect( struct NineTexture9 *This,
296                          UINT Level )
297 {
298     DBG("This=%p Level=%u\n", This, Level);
299 
300     user_assert(Level <= This->base.base.info.last_level, D3DERR_INVALIDCALL);
301 
302     return NineSurface9_UnlockRect(This->surfaces[Level]);
303 }
304 
305 HRESULT NINE_WINAPI
NineTexture9_AddDirtyRect(struct NineTexture9 * This,const RECT * pDirtyRect)306 NineTexture9_AddDirtyRect( struct NineTexture9 *This,
307                            const RECT *pDirtyRect )
308 {
309     DBG("This=%p pDirtyRect=%p[(%u,%u)-(%u,%u)]\n", This, pDirtyRect,
310         pDirtyRect ? pDirtyRect->left : 0, pDirtyRect ? pDirtyRect->top : 0,
311         pDirtyRect ? pDirtyRect->right : 0, pDirtyRect ? pDirtyRect->bottom : 0);
312 
313     /* Tracking dirty regions on DEFAULT resources is pointless,
314      * because we always write to the final storage. Just marked it dirty in
315      * case we need to generate mip maps.
316      */
317     if (This->base.base.pool == D3DPOOL_DEFAULT) {
318         if (This->base.base.usage & D3DUSAGE_AUTOGENMIPMAP) {
319             This->base.dirty_mip = TRUE;
320             BASETEX_REGISTER_UPDATE(&This->base);
321         }
322         return D3D_OK;
323     }
324 
325     if (This->base.base.pool == D3DPOOL_MANAGED) {
326         This->base.managed.dirty = TRUE;
327         BASETEX_REGISTER_UPDATE(&This->base);
328     }
329 
330     if (!pDirtyRect) {
331         u_box_origin_2d(This->base.base.info.width0,
332                         This->base.base.info.height0, &This->dirty_rect);
333     } else {
334         if (This->dirty_rect.width == 0) {
335             rect_to_pipe_box_clamp(&This->dirty_rect, pDirtyRect);
336         } else {
337             struct pipe_box box;
338             rect_to_pipe_box_clamp(&box, pDirtyRect);
339             u_box_union_2d(&This->dirty_rect, &This->dirty_rect, &box);
340         }
341         (void) u_box_clip_2d(&This->dirty_rect, &This->dirty_rect,
342                              This->base.base.info.width0,
343                              This->base.base.info.height0);
344     }
345     return D3D_OK;
346 }
347 
348 IDirect3DTexture9Vtbl NineTexture9_vtable = {
349     (void *)NineUnknown_QueryInterface,
350     (void *)NineUnknown_AddRef,
351     (void *)NineUnknown_Release,
352     (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
353     (void *)NineUnknown_SetPrivateData,
354     (void *)NineUnknown_GetPrivateData,
355     (void *)NineUnknown_FreePrivateData,
356     (void *)NineResource9_SetPriority,
357     (void *)NineResource9_GetPriority,
358     (void *)NineBaseTexture9_PreLoad,
359     (void *)NineResource9_GetType,
360     (void *)NineBaseTexture9_SetLOD,
361     (void *)NineBaseTexture9_GetLOD,
362     (void *)NineBaseTexture9_GetLevelCount,
363     (void *)NineBaseTexture9_SetAutoGenFilterType,
364     (void *)NineBaseTexture9_GetAutoGenFilterType,
365     (void *)NineBaseTexture9_GenerateMipSubLevels,
366     (void *)NineTexture9_GetLevelDesc,
367     (void *)NineTexture9_GetSurfaceLevel,
368     (void *)NineTexture9_LockRect,
369     (void *)NineTexture9_UnlockRect,
370     (void *)NineTexture9_AddDirtyRect
371 };
372 
373 static const GUID *NineTexture9_IIDs[] = {
374     &IID_IDirect3DTexture9,
375     &IID_IDirect3DBaseTexture9,
376     &IID_IDirect3DResource9,
377     &IID_IUnknown,
378     NULL
379 };
380 
381 HRESULT
NineTexture9_new(struct NineDevice9 * pDevice,UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,struct NineTexture9 ** ppOut,HANDLE * pSharedHandle)382 NineTexture9_new( struct NineDevice9 *pDevice,
383                   UINT Width, UINT Height, UINT Levels,
384                   DWORD Usage,
385                   D3DFORMAT Format,
386                   D3DPOOL Pool,
387                   struct NineTexture9 **ppOut,
388                   HANDLE *pSharedHandle )
389 {
390     NINE_DEVICE_CHILD_NEW(Texture9, ppOut, pDevice,
391                           Width, Height, Levels,
392                           Usage, Format, Pool, pSharedHandle);
393 }
394