VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c@ 41109

Last change on this file since 41109 was 41109, checked in by vboxsync, 13 years ago

crOpenGL,wined3d,wddm: second part of fixing resource leakage, basics for cr commands submission from r0 miniport driver (for r0 visible region reporting, WPF 3D rendering fixes w/o Aero, etc.)

  • Property svn:eol-style set to native
File size: 27.7 KB
Line 
1/*
2 * IWineD3DTexture implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25/*
26 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
28 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
29 * a choice of LGPL license versions is made available with the language indicating
30 * that LGPLv2 or any later version may be used, or where a choice of which version
31 * of the LGPL is applied is otherwise unspecified.
32 */
33
34#include "config.h"
35#include "wined3d_private.h"
36
37WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
38
39static void texture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
40{
41 /* Override the IWineD3DResource PreLoad method. */
42 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
43 IWineD3DDeviceImpl *device = This->resource.device;
44 struct wined3d_context *context = NULL;
45 unsigned int i;
46 BOOL srgb_mode;
47 BOOL *dirty;
48
49 TRACE("(%p) : About to load texture.\n", This);
50
51 switch (srgb)
52 {
53 case SRGB_RGB:
54 srgb_mode = FALSE;
55 break;
56
57 case SRGB_BOTH:
58 texture_internal_preload(iface, SRGB_RGB);
59 /* Fallthrough */
60
61 case SRGB_SRGB:
62 srgb_mode = TRUE;
63 break;
64
65 default:
66 srgb_mode = This->baseTexture.is_srgb;
67 break;
68 }
69 dirty = srgb_mode ? &This->baseTexture.texture_srgb.dirty : &This->baseTexture.texture_rgb.dirty;
70
71 if (!device->isInDraw)
72 {
73 /* context_acquire() sets isInDraw to TRUE when loading a pbuffer into a texture,
74 * thus no danger of recursive calls. */
75 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
76 }
77
78 if (This->resource.format_desc->format == WINED3DFMT_P8_UINT
79 || This->resource.format_desc->format == WINED3DFMT_P8_UINT_A8_UNORM)
80 {
81 for (i = 0; i < This->baseTexture.levels; ++i)
82 {
83 if (palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[i]))
84 {
85 TRACE("Reloading surface because the d3d8/9 palette was changed.\n");
86 /* TODO: This is not necessarily needed with hw palettized texture support. */
87 IWineD3DSurface_LoadLocation(This->surfaces[i], SFLAG_INSYSMEM, NULL);
88 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
89 IWineD3DSurface_ModifyLocation(This->surfaces[i], SFLAG_INTEXTURE, FALSE);
90 }
91 }
92 }
93
94 /* If the texture is marked dirty or the srgb sampler setting has changed
95 * since the last load then reload the surfaces. */
96 if (*dirty)
97 {
98 for (i = 0; i < This->baseTexture.levels; ++i)
99 {
100 IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
101 }
102 }
103 else
104 {
105 TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
106 }
107
108 if (context) context_release(context);
109
110 /* No longer dirty. */
111 *dirty = FALSE;
112}
113
114static void texture_cleanup(IWineD3DTextureImpl *This)
115{
116 unsigned int i;
117
118 TRACE("(%p) : Cleaning up\n", This);
119
120 for (i = 0; i < This->baseTexture.levels; ++i)
121 {
122 if (This->surfaces[i])
123 {
124 /* Clean out the texture name we gave to the surface so that the
125 * surface doesn't try and release it */
126 surface_set_texture_name(This->surfaces[i], 0, TRUE);
127 surface_set_texture_name(This->surfaces[i], 0, FALSE);
128 surface_set_texture_target(This->surfaces[i], 0);
129 IWineD3DSurface_SetContainer(This->surfaces[i], 0);
130 IWineD3DSurface_Release(This->surfaces[i]);
131 }
132 }
133
134 TRACE("(%p) : Cleaning up base texture\n", This);
135 basetexture_cleanup((IWineD3DBaseTexture *)This);
136}
137
138/* *******************************************
139 IWineD3DTexture IUnknown parts follow
140 ******************************************* */
141
142static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
143{
144 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
145 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
146 if (IsEqualGUID(riid, &IID_IUnknown)
147 || IsEqualGUID(riid, &IID_IWineD3DBase)
148 || IsEqualGUID(riid, &IID_IWineD3DResource)
149 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
150 || IsEqualGUID(riid, &IID_IWineD3DTexture)){
151 IUnknown_AddRef(iface);
152 *ppobj = This;
153 return WINED3D_OK;
154 }
155 *ppobj = NULL;
156 return E_NOINTERFACE;
157}
158
159static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
160 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
161 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
162 return InterlockedIncrement(&This->resource.ref);
163}
164
165static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
166 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
167 ULONG ref;
168 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
169 ref = InterlockedDecrement(&This->resource.ref);
170 if (!ref)
171 {
172 texture_cleanup(This);
173 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
174 HeapFree(GetProcessHeap(), 0, This);
175 }
176 return ref;
177}
178
179
180/* ****************************************************
181 IWineD3DTexture IWineD3DResource parts follow
182 **************************************************** */
183static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
184 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
185}
186
187static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
188 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
189}
190
191static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
192 return resource_free_private_data((IWineD3DResource *)iface, refguid);
193}
194
195static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
196 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
197}
198
199static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
200 return resource_get_priority((IWineD3DResource *)iface);
201}
202
203static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
204 texture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
205}
206
207static void WINAPI IWineD3DTextureImpl_UnLoad(IWineD3DTexture *iface) {
208 unsigned int i;
209 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
210 TRACE("(%p)\n", This);
211
212 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
213 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
214 * surface is fine
215 */
216 for (i = 0; i < This->baseTexture.levels; i++) {
217 IWineD3DSurface_UnLoad(This->surfaces[i]);
218 surface_set_texture_name(This->surfaces[i], 0, FALSE); /* Delete rgb name */
219 surface_set_texture_name(This->surfaces[i], 0, TRUE); /* delete srgb name */
220 }
221
222 basetexture_unload((IWineD3DBaseTexture *)iface);
223}
224
225static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
226 return resource_get_type((IWineD3DResource *)iface);
227}
228
229static HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
230 return resource_get_parent((IWineD3DResource *)iface, pParent);
231}
232
233#ifdef VBOX_WITH_WDDM
234static HRESULT WINAPI IWineD3DTextureImpl_SetShRcState(IWineD3DTexture *iface, VBOXWINEEX_SHRC_STATE enmState) {
235 IWineD3DTextureImpl *This = (IWineD3DTextureImpl*)iface;
236 struct wined3d_context *context = NULL;
237 HRESULT hr = IWineD3DResourceImpl_SetShRcState((IWineD3DResource*)iface, enmState);
238 unsigned int i;
239
240 if (FAILED(hr))
241 {
242 ERR("IWineD3DResource_SetShRcState failed");
243 return hr;
244 }
245
246 for (i = 0; i < This->baseTexture.levels; ++i)
247 {
248 if (This->surfaces[i])
249 {
250 HRESULT tmpHr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->surfaces[i], enmState);
251 Assert(tmpHr == S_OK);
252 }
253 }
254
255 if (!This->resource.device->isInDraw)
256 {
257 context = context_acquire(This->resource.device, NULL, CTXUSAGE_RESOURCELOAD);
258 if (!context)
259 {
260 ERR("zero context!");
261 return E_FAIL;
262 }
263
264 if (!context->valid)
265 {
266 ERR("context invalid!");
267 context_release(context);
268 return E_FAIL;
269 }
270 }
271
272 device_cleanup_durtify_texture_target(This->resource.device, This->target);
273
274 if (context)
275 context_release(context);
276
277 return WINED3D_OK;
278}
279#endif
280
281/* ******************************************************
282 IWineD3DTexture IWineD3DBaseTexture parts follow
283 ****************************************************** */
284static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
285 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
286}
287
288static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
289 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
290}
291
292static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
293 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
294}
295
296static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
297 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
298}
299
300static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
301 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
302}
303
304static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
305 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
306}
307
308/* Internal function, No d3d mapping */
309static BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
310 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
311}
312
313static BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
314 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
315}
316
317/* Context activation is done by the caller. */
318static HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface, BOOL srgb) {
319 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
320 BOOL set_gl_texture_desc;
321 HRESULT hr;
322
323 TRACE("(%p) : relay to BaseTexture\n", This);
324
325 hr = basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &set_gl_texture_desc);
326 if (set_gl_texture_desc && SUCCEEDED(hr)) {
327 UINT i;
328 struct gl_texture *gl_tex;
329
330 if(This->baseTexture.is_srgb) {
331 gl_tex = &This->baseTexture.texture_srgb;
332 } else {
333 gl_tex = &This->baseTexture.texture_rgb;
334 }
335
336 for (i = 0; i < This->baseTexture.levels; ++i) {
337 surface_set_texture_name(This->surfaces[i], gl_tex->name, This->baseTexture.is_srgb);
338 }
339
340 /* Conditinal non power of two textures use a different clamping default. If we're using the GL_WINE_normalized_texrect
341 * partial driver emulation, we're dealing with a GL_TEXTURE_2D texture which has the address mode set to repeat - something
342 * that prevents us from hitting the accelerated codepath. Thus manually set the GL state. The same applies to filtering.
343 * Even if the texture has only one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW fallback on macos.
344 */
345 if(IWineD3DBaseTexture_IsCondNP2(iface)) {
346#ifdef VBOX_WITH_WDDM
347 if (!VBOXSHRC_IS_SHARED_OPENED(This))
348#endif
349 {
350 ENTER_GL();
351 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
352 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
353 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
354 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
355 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
356 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
357 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
358 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
359 LEAVE_GL();
360 }
361 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_CLAMP;
362 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_CLAMP;
363 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
364 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
365 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
366 }
367 }
368
369 return hr;
370}
371
372static UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
373 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
374 TRACE("(%p)\n", This);
375
376 return This->target;
377}
378
379static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
380 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
381 TRACE("(%p)\n", This);
382
383 return This->cond_np2;
384}
385
386/* *******************************************
387 IWineD3DTexture IWineD3DTexture parts follow
388 ******************************************* */
389static HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
390 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
391
392 if (Level < This->baseTexture.levels) {
393 TRACE("(%p) Level (%d)\n", This, Level);
394 return IWineD3DSurface_GetDesc(This->surfaces[Level], pDesc);
395 }
396 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
397 return WINED3DERR_INVALIDCALL;
398}
399
400static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
401 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
402 HRESULT hr = WINED3DERR_INVALIDCALL;
403
404 if (Level < This->baseTexture.levels) {
405 *ppSurfaceLevel = This->surfaces[Level];
406 IWineD3DSurface_AddRef(This->surfaces[Level]);
407 hr = WINED3D_OK;
408 TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
409 }
410 if (WINED3D_OK != hr) {
411 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
412 *ppSurfaceLevel = NULL; /* Just to be on the safe side.. */
413 }
414 return hr;
415}
416
417static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, WINED3DLOCKED_RECT *pLockedRect,
418 CONST RECT *pRect, DWORD Flags) {
419 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
420 HRESULT hr = WINED3DERR_INVALIDCALL;
421
422 if (Level < This->baseTexture.levels) {
423 hr = IWineD3DSurface_LockRect(This->surfaces[Level], pLockedRect, pRect, Flags);
424 }
425 if (WINED3D_OK == hr) {
426 TRACE("(%p) Level (%d) success\n", This, Level);
427 } else {
428 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
429 }
430
431 return hr;
432}
433
434static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
435 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
436 HRESULT hr = WINED3DERR_INVALIDCALL;
437
438 if (Level < This->baseTexture.levels) {
439 hr = IWineD3DSurface_UnlockRect(This->surfaces[Level]);
440 }
441 if ( WINED3D_OK == hr) {
442 TRACE("(%p) Level (%d) success\n", This, Level);
443 } else {
444 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
445 }
446 return hr;
447}
448
449static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
450 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
451 This->baseTexture.texture_rgb.dirty = TRUE;
452 This->baseTexture.texture_srgb.dirty = TRUE;
453 TRACE("(%p) : dirtyfication of surface Level (0)\n", This);
454 surface_add_dirty_rect(This->surfaces[0], pDirtyRect);
455
456 return WINED3D_OK;
457}
458
459static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
460{
461 /* IUnknown */
462 IWineD3DTextureImpl_QueryInterface,
463 IWineD3DTextureImpl_AddRef,
464 IWineD3DTextureImpl_Release,
465 /* IWineD3DResource */
466 IWineD3DTextureImpl_GetParent,
467 IWineD3DTextureImpl_SetPrivateData,
468 IWineD3DTextureImpl_GetPrivateData,
469 IWineD3DTextureImpl_FreePrivateData,
470 IWineD3DTextureImpl_SetPriority,
471 IWineD3DTextureImpl_GetPriority,
472 IWineD3DTextureImpl_PreLoad,
473 IWineD3DTextureImpl_UnLoad,
474 IWineD3DTextureImpl_GetType,
475#ifdef VBOX_WITH_WDDM
476 IWineD3DTextureImpl_SetShRcState,
477#endif
478 /* IWineD3DBaseTexture */
479 IWineD3DTextureImpl_SetLOD,
480 IWineD3DTextureImpl_GetLOD,
481 IWineD3DTextureImpl_GetLevelCount,
482 IWineD3DTextureImpl_SetAutoGenFilterType,
483 IWineD3DTextureImpl_GetAutoGenFilterType,
484 IWineD3DTextureImpl_GenerateMipSubLevels,
485 IWineD3DTextureImpl_SetDirty,
486 IWineD3DTextureImpl_GetDirty,
487 IWineD3DTextureImpl_BindTexture,
488 IWineD3DTextureImpl_GetTextureDimensions,
489 IWineD3DTextureImpl_IsCondNP2,
490 /* IWineD3DTexture */
491 IWineD3DTextureImpl_GetLevelDesc,
492 IWineD3DTextureImpl_GetSurfaceLevel,
493 IWineD3DTextureImpl_LockRect,
494 IWineD3DTextureImpl_UnlockRect,
495 IWineD3DTextureImpl_AddDirtyRect
496};
497
498void texture_state_init(IWineD3DTexture *iface, struct gl_texture *gl_tex)
499{
500 basetexture_state_init((IWineD3DBaseTexture*)iface, gl_tex);
501 if(IWineD3DBaseTexture_IsCondNP2(iface)) {
502 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_CLAMP;
503 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_CLAMP;
504 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
505 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
506 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
507 }
508}
509
510HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
511 IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool,
512 IUnknown *parent, const struct wined3d_parent_ops *parent_ops
513#ifdef VBOX_WITH_WDDM
514 , HANDLE *shared_handle
515 , void **pavClientMem
516#endif
517 )
518{
519 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
520 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
521 UINT pow2_width, pow2_height;
522 UINT tmp_w, tmp_h;
523 unsigned int i;
524 HRESULT hr;
525
526 /* TODO: It should only be possible to create textures for formats
527 * that are reported as supported. */
528 if (WINED3DFMT_UNKNOWN >= format)
529 {
530 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
531 return WINED3DERR_INVALIDCALL;
532 }
533
534 /* Non-power2 support. */
535 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
536 {
537 pow2_width = width;
538 pow2_height = height;
539 }
540 else
541 {
542 /* Find the nearest pow2 match. */
543 pow2_width = pow2_height = 1;
544 while (pow2_width < width) pow2_width <<= 1;
545 while (pow2_height < height) pow2_height <<= 1;
546
547 if (pow2_width != width || pow2_height != height)
548 {
549 if (levels > 1)
550 {
551 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
552 return WINED3DERR_INVALIDCALL;
553 }
554 levels = 1;
555 }
556 }
557
558 /* Calculate levels for mip mapping. */
559 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
560 {
561 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
562 {
563 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
564 return WINED3DERR_INVALIDCALL;
565 }
566
567 if (levels > 1)
568 {
569 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
570 return WINED3DERR_INVALIDCALL;
571 }
572
573 levels = 1;
574 }
575 else if (!levels)
576 {
577 levels = wined3d_log2i(max(width, height)) + 1;
578 TRACE("Calculated levels = %u.\n", levels);
579 }
580
581 texture->lpVtbl = &IWineD3DTexture_Vtbl;
582
583 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels, WINED3DRTYPE_TEXTURE,
584 device, 0, usage, format_desc, pool, parent, parent_ops
585#ifdef VBOX_WITH_WDDM
586 , shared_handle, pavClientMem
587#endif
588 );
589 if (FAILED(hr))
590 {
591 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
592 return hr;
593 }
594
595 /* Precalculated scaling for 'faked' non power of two texture coords.
596 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
597 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
598 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
599 if (gl_info->supported[WINE_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
600 {
601 texture->baseTexture.pow2Matrix[0] = 1.0f;
602 texture->baseTexture.pow2Matrix[5] = 1.0f;
603 texture->baseTexture.pow2Matrix[10] = 1.0f;
604 texture->baseTexture.pow2Matrix[15] = 1.0f;
605 texture->target = GL_TEXTURE_2D;
606 texture->cond_np2 = TRUE;
607 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
608 }
609 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
610 && !(format_desc->format == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
611 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
612 {
613 if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
614
615 texture->baseTexture.pow2Matrix[0] = (float)width;
616 texture->baseTexture.pow2Matrix[5] = (float)height;
617 texture->baseTexture.pow2Matrix[10] = 1.0f;
618 texture->baseTexture.pow2Matrix[15] = 1.0f;
619 texture->target = GL_TEXTURE_RECTANGLE_ARB;
620 texture->cond_np2 = TRUE;
621
622 if(texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
623 {
624 texture->baseTexture.minMipLookup = minMipLookup_noMip;
625 }
626 else
627 {
628 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
629 }
630 }
631 else
632 {
633 if ((width != pow2_width) || (height != pow2_height))
634 {
635 texture->baseTexture.pow2Matrix_identity = FALSE;
636 texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
637 texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
638 }
639 else
640 {
641 texture->baseTexture.pow2Matrix[0] = 1.0f;
642 texture->baseTexture.pow2Matrix[5] = 1.0f;
643 }
644
645 texture->baseTexture.pow2Matrix[10] = 1.0f;
646 texture->baseTexture.pow2Matrix[15] = 1.0f;
647 texture->target = GL_TEXTURE_2D;
648 texture->cond_np2 = FALSE;
649 }
650 TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
651
652 /* Generate all the surfaces. */
653 tmp_w = width;
654 tmp_h = height;
655 for (i = 0; i < texture->baseTexture.levels; ++i)
656 {
657#ifdef VBOX_WITH_WDDM
658 /* Use the callback to create the texture surface. */
659 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h, format_desc->format,
660 usage, pool, i, WINED3DCUBEMAP_FACE_POSITIVE_X, &texture->surfaces[i]
661 , NULL /* <- we first create a surface in an everage "non-shared" fashion and initialize its share properties later (see below)
662 * this is done this way because the surface does not have its parent (texture) setup properly
663 * thus we can not initialize texture at this stage */
664 , pavClientMem ? pavClientMem[i] : NULL);
665
666#else
667 /* Use the callback to create the texture surface. */
668 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h, format_desc->format,
669 usage, pool, i, WINED3DCUBEMAP_FACE_POSITIVE_X, &texture->surfaces[i]);
670#endif
671
672 if (FAILED(hr))
673 {
674 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
675 texture->surfaces[i] = NULL;
676 texture_cleanup(texture);
677 return hr;
678 }
679
680 IWineD3DSurface_SetContainer(texture->surfaces[i], (IWineD3DBase *)texture);
681 TRACE("Created surface level %u @ %p.\n", i, texture->surfaces[i]);
682 surface_set_texture_target(texture->surfaces[i], texture->target);
683 /* Calculate the next mipmap level. */
684 tmp_w = max(1, tmp_w >> 1);
685 tmp_h = max(1, tmp_h >> 1);
686 }
687 texture->baseTexture.internal_preload = texture_internal_preload;
688
689#ifdef VBOX_WITH_WDDM
690 if (VBOXSHRC_IS_SHARED(texture))
691 {
692 Assert(shared_handle);
693 for (i = 0; i < texture->baseTexture.levels; ++i)
694 {
695 VBOXSHRC_COPY_SHAREDATA((IWineD3DSurfaceImpl*)texture->surfaces[i], texture);
696 }
697#ifdef DEBUG
698 for (i = 0; i < texture->baseTexture.levels; ++i)
699 {
700 Assert(!((IWineD3DSurfaceImpl*)texture->surfaces[i])->texture_name);
701 }
702#endif
703 for (i = 0; i < texture->baseTexture.levels; ++i)
704 {
705 if (!VBOXSHRC_IS_SHARED_OPENED(texture))
706 {
707 IWineD3DSurface_LoadLocation(texture->surfaces[i], SFLAG_INTEXTURE, NULL);
708 Assert(!(*shared_handle));
709 *shared_handle = VBOXSHRC_GET_SHAREHANDLE(texture);
710 }
711 else
712 {
713 surface_setup_location_onopen((IWineD3DSurfaceImpl*)texture->surfaces[i]);
714 Assert(*shared_handle);
715 Assert(*shared_handle == VBOXSHRC_GET_SHAREHANDLE(texture));
716 }
717 }
718#ifdef DEBUG
719 for (i = 0; i < texture->baseTexture.levels; ++i)
720 {
721 Assert((GLuint)(*shared_handle) == ((IWineD3DSurfaceImpl*)texture->surfaces[i])->texture_name);
722 }
723#endif
724
725 if (!VBOXSHRC_IS_SHARED(texture))
726 {
727 struct wined3d_context * context;
728
729 Assert(!device->isInDraw);
730
731 /* flush to ensure the texture is allocated/referenced before it is used/released by another
732 * process opening/creating it */
733 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
734 if (context->valid)
735 {
736 wglFlush();
737 }
738 else
739 {
740 ERR("invalid context!");
741 }
742 context_release(context);
743 }
744 }
745 else
746 {
747 Assert(!shared_handle);
748 }
749#endif
750
751 return WINED3D_OK;
752}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette