2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture
);
29 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
30 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, struct wined3d_device
*device
,
31 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_resource_ops
*resource_ops
)
33 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
36 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
37 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
38 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
39 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
40 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
41 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
42 device
, parent
, parent_ops
, resource_ops
);
44 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BLOCKS_NO_VERIFY
)) == WINED3DFMT_FLAG_BLOCKS
)
46 UINT width_mask
= format
->block_width
- 1;
47 UINT height_mask
= format
->block_height
- 1;
48 if (desc
->width
& width_mask
|| desc
->height
& height_mask
)
49 return WINED3DERR_INVALIDCALL
;
52 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
53 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
54 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
56 WARN("Failed to initialize resource, returning %#x\n", hr
);
60 texture
->texture_ops
= texture_ops
;
61 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
62 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
63 if (!texture
->sub_resources
)
65 ERR("Failed to allocate sub-resource array.\n");
66 resource_cleanup(&texture
->resource
);
70 texture
->layer_count
= layer_count
;
71 texture
->level_count
= level_count
;
72 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
74 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
;
76 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
78 texture
->min_mip_lookup
= minMipLookup
;
79 texture
->mag_lookup
= magLookup
;
83 texture
->min_mip_lookup
= minMipLookup_noFilter
;
84 texture
->mag_lookup
= magLookup_noFilter
;
90 /* A GL context is provided by the caller */
91 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
93 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
97 static void wined3d_texture_unload(struct wined3d_texture
*texture
)
99 struct wined3d_device
*device
= texture
->resource
.device
;
100 struct wined3d_context
*context
= NULL
;
102 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
104 context
= context_acquire(device
, NULL
);
107 if (texture
->texture_rgb
.name
)
108 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
110 if (texture
->texture_srgb
.name
)
111 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
113 if (context
) context_release(context
);
115 wined3d_texture_set_dirty(texture
);
117 resource_unload(&texture
->resource
);
120 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
122 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
125 TRACE("texture %p.\n", texture
);
127 for (i
= 0; i
< sub_count
; ++i
)
129 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
132 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
135 wined3d_texture_unload(texture
);
136 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
137 resource_cleanup(&texture
->resource
);
140 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
142 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
145 /* Context activation is done by the caller. */
146 static HRESULT
wined3d_texture_bind(struct wined3d_texture
*texture
,
147 struct wined3d_context
*context
, BOOL srgb
, BOOL
*set_surface_desc
)
149 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
150 struct gl_texture
*gl_tex
;
151 BOOL new_texture
= FALSE
;
152 HRESULT hr
= WINED3D_OK
;
155 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture
, context
, srgb
, set_surface_desc
);
157 /* sRGB mode cache for preload() calls outside drawprim. */
159 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
161 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
163 gl_tex
= wined3d_texture_get_gl_texture(texture
, context
->gl_info
, srgb
);
164 target
= texture
->target
;
166 /* Generate a texture name if we don't already have one. */
169 *set_surface_desc
= TRUE
;
170 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
171 checkGLcall("glGenTextures");
172 TRACE("Generated texture %d.\n", gl_tex
->name
);
173 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
175 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
177 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
179 /* Initialise the state of the texture object to the OpenGL defaults,
180 * not the D3D defaults. */
181 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_WRAP
;
182 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_WRAP
;
183 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3D_TADDRESS_WRAP
;
184 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
185 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_LINEAR
;
186 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
187 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
188 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
189 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
190 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
191 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
193 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
194 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
195 wined3d_texture_set_dirty(texture
);
198 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
200 /* This means double binding the texture at creation, but keeps
201 * the code simpler all in all, and the run-time path free from
202 * additional checks. */
203 context_bind_texture(context
, target
, gl_tex
->name
);
204 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
205 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
210 *set_surface_desc
= FALSE
;
215 context_bind_texture(context
, target
, gl_tex
->name
);
218 /* For a new texture we have to set the texture levels after
219 * binding the texture. Beware that texture rectangles do not
220 * support mipmapping, but set the maxmiplevel if we're relying
221 * on the partial GL_ARB_texture_non_power_of_two emulation with
222 * texture rectangles. (I.e., do not care about cond_np2 here,
223 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
224 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
226 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
227 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
228 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
230 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
232 /* Cubemaps are always set to clamp, regardless of the sampler state. */
233 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
234 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
235 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
241 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
242 hr
= WINED3DERR_INVALIDCALL
;
248 /* Context activation is done by the caller. */
249 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
250 enum wined3d_texture_address d3d_wrap
, GLenum param
, BOOL cond_np2
)
254 if (d3d_wrap
< WINED3D_TADDRESS_WRAP
|| d3d_wrap
> WINED3D_TADDRESS_MIRROR_ONCE
)
256 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap
);
260 /* Cubemaps are always set to clamp, regardless of the sampler state. */
261 if (target
== GL_TEXTURE_CUBE_MAP_ARB
262 || (cond_np2
&& d3d_wrap
== WINED3D_TADDRESS_WRAP
))
263 gl_wrap
= GL_CLAMP_TO_EDGE
;
265 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3D_TADDRESS_WRAP
];
267 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
268 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, param
, gl_wrap
);
269 checkGLcall("glTexParameteri(target, param, gl_wrap)");
272 /* Context activation is done by the caller (state handler). */
273 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
274 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
275 const struct wined3d_gl_info
*gl_info
)
277 BOOL cond_np2
= texture
->flags
& WINED3D_TEXTURE_COND_NP2
;
278 GLenum target
= texture
->target
;
279 struct gl_texture
*gl_tex
;
283 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
285 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
,
286 texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
288 /* This function relies on the correct texture being bound and loaded. */
290 if (sampler_states
[WINED3D_SAMP_ADDRESS_U
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
292 state
= sampler_states
[WINED3D_SAMP_ADDRESS_U
];
293 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
294 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
297 if (sampler_states
[WINED3D_SAMP_ADDRESS_V
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
299 state
= sampler_states
[WINED3D_SAMP_ADDRESS_V
];
300 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
301 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
304 if (sampler_states
[WINED3D_SAMP_ADDRESS_W
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
306 state
= sampler_states
[WINED3D_SAMP_ADDRESS_W
];
307 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
308 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
311 if (sampler_states
[WINED3D_SAMP_BORDER_COLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
315 state
= sampler_states
[WINED3D_SAMP_BORDER_COLOR
];
316 D3DCOLORTOGLFLOAT4(state
, col
);
317 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
318 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
319 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
320 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
323 if (sampler_states
[WINED3D_SAMP_MAG_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
327 state
= sampler_states
[WINED3D_SAMP_MAG_FILTER
];
328 if (state
> WINED3D_TEXF_ANISOTROPIC
)
329 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
331 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
332 min(max(state
, WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
));
333 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
334 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
336 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
339 if ((sampler_states
[WINED3D_SAMP_MIN_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
340 || sampler_states
[WINED3D_SAMP_MIP_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
341 || sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
345 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3D_SAMP_MIP_FILTER
];
346 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3D_SAMP_MIN_FILTER
];
347 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
];
349 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3D_TEXF_ANISOTROPIC
350 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3D_TEXF_ANISOTROPIC
)
352 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
353 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
354 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
356 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
357 min(max(sampler_states
[WINED3D_SAMP_MIN_FILTER
], WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
),
358 min(max(sampler_states
[WINED3D_SAMP_MIP_FILTER
], WINED3D_TEXF_NONE
), WINED3D_TEXF_LINEAR
));
360 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
361 sampler_states
[WINED3D_SAMP_MIN_FILTER
],
362 sampler_states
[WINED3D_SAMP_MIP_FILTER
], gl_value
);
363 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
364 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
368 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3D_TEXF_NONE
)
369 gl_value
= texture
->lod
;
370 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
371 gl_value
= texture
->level_count
- 1;
372 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
373 /* texture->lod is already clamped in the setter. */
374 gl_value
= texture
->lod
;
376 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
378 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
379 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
380 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
381 * corresponds to GL_TEXTURE_BASE_LEVEL. */
382 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
386 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3D_TEXF_ANISOTROPIC
387 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3D_TEXF_ANISOTROPIC
388 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3D_TEXF_ANISOTROPIC
)
392 aniso
= sampler_states
[WINED3D_SAMP_MAX_ANISOTROPY
];
394 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
396 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
398 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
399 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
403 WARN("Anisotropic filtering not supported.\n");
405 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
408 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
409 if (sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
411 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
412 sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
413 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
414 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
];
417 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
418 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
420 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
422 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
423 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
424 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
425 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
429 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
430 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
431 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
436 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
438 ULONG refcount
= InterlockedIncrement(&texture
->resource
.ref
);
440 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
445 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
447 ULONG refcount
= InterlockedDecrement(&texture
->resource
.ref
);
449 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
453 wined3d_texture_cleanup(texture
);
454 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
455 HeapFree(GetProcessHeap(), 0, texture
);
461 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
463 TRACE("texture %p.\n", texture
);
465 return &texture
->resource
;
468 DWORD CDECL
wined3d_texture_set_priority(struct wined3d_texture
*texture
, DWORD priority
)
470 return resource_set_priority(&texture
->resource
, priority
);
473 DWORD CDECL
wined3d_texture_get_priority(const struct wined3d_texture
*texture
)
475 return resource_get_priority(&texture
->resource
);
478 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
480 struct wined3d_context
*context
;
481 context
= context_acquire(texture
->resource
.device
, NULL
);
482 texture
->texture_ops
->texture_preload(texture
, context
, SRGB_ANY
);
483 context_release(context
);
486 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
488 TRACE("texture %p.\n", texture
);
490 return texture
->resource
.parent
;
493 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
495 DWORD old
= texture
->lod
;
497 TRACE("texture %p, lod %u.\n", texture
, lod
);
499 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
500 * textures. The call always returns 0, and GetLOD always returns 0. */
501 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
503 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
507 if (lod
>= texture
->level_count
)
508 lod
= texture
->level_count
- 1;
510 if (texture
->lod
!= lod
)
514 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
515 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
516 if (texture
->resource
.bind_count
)
517 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
523 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
525 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
530 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
532 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
534 return texture
->level_count
;
537 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
538 enum wined3d_texture_filter_type filter_type
)
540 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
542 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
544 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
545 return WINED3DERR_INVALIDCALL
;
548 texture
->filter_type
= filter_type
;
553 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
555 TRACE("texture %p.\n", texture
);
557 return texture
->filter_type
;
560 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
562 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
563 FIXME("texture %p stub!\n", texture
);
566 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
567 UINT sub_resource_idx
)
569 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
571 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
573 if (sub_resource_idx
>= sub_count
)
575 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
579 return texture
->sub_resources
[sub_resource_idx
];
582 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
583 UINT layer
, const struct wined3d_box
*dirty_region
)
585 struct wined3d_resource
*sub_resource
;
587 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
589 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
591 WARN("Failed to get sub-resource.\n");
592 return WINED3DERR_INVALIDCALL
;
595 wined3d_texture_set_dirty(texture
);
596 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
601 /* Context activation is done by the caller. */
602 static HRESULT
texture2d_bind(struct wined3d_texture
*texture
,
603 struct wined3d_context
*context
, BOOL srgb
)
605 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
606 BOOL set_gl_texture_desc
;
609 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
611 hr
= wined3d_texture_bind(texture
, context
, srgb
, &set_gl_texture_desc
);
612 if (set_gl_texture_desc
&& SUCCEEDED(hr
))
614 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
615 BOOL srgb_tex
= !context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
]
616 && (texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
617 struct gl_texture
*gl_tex
;
620 gl_tex
= wined3d_texture_get_gl_texture(texture
, context
->gl_info
, srgb_tex
);
622 for (i
= 0; i
< sub_count
; ++i
)
624 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
625 surface_set_texture_name(surface
, gl_tex
->name
, srgb_tex
);
628 /* Conditinal non power of two textures use a different clamping
629 * default. If we're using the GL_WINE_normalized_texrect partial
630 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
631 * has the address mode set to repeat - something that prevents us
632 * from hitting the accelerated codepath. Thus manually set the GL
633 * state. The same applies to filtering. Even if the texture has only
634 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
635 * fallback on macos. */
636 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
638 GLenum target
= texture
->target
;
640 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
641 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
642 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
643 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
644 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
645 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
646 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
647 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
648 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_CLAMP
;
649 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_CLAMP
;
650 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_POINT
;
651 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
;
652 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_NONE
;
659 static BOOL
texture_srgb_mode(const struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
661 const struct wined3d_gl_info
*gl_info
= &texture
->resource
.device
->adapter
->gl_info
;
671 return !gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
]
672 && texture
->flags
& WINED3D_TEXTURE_IS_SRGB
;
676 /* Context activation is done by the caller */
677 static void texture2d_preload(struct wined3d_texture
*texture
,
678 struct wined3d_context
*context
, enum WINED3DSRGB srgb
)
680 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
681 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
686 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
688 srgb_mode
= texture_srgb_mode(texture
, srgb
);
689 if (srgb_mode
&& !gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
690 flag
= WINED3D_TEXTURE_SRGB_VALID
;
692 flag
= WINED3D_TEXTURE_RGB_VALID
;
694 if (texture
->flags
& flag
)
696 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
700 /* Reload the surfaces if the texture is marked dirty. */
701 for (i
= 0; i
< sub_count
; ++i
)
703 surface_load(surface_from_resource(texture
->sub_resources
[i
]), srgb_mode
);
705 texture
->flags
|= flag
;
708 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
709 const struct wined3d_box
*dirty_region
)
711 surface_add_dirty_rect(surface_from_resource(sub_resource
), dirty_region
);
714 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
716 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
718 /* Clean out the texture name we gave to the surface so that the
719 * surface doesn't try and release it. */
720 surface_set_texture_name(surface
, 0, TRUE
);
721 surface_set_texture_name(surface
, 0, FALSE
);
722 surface_set_texture_target(surface
, 0, 0);
723 surface_set_container(surface
, NULL
);
724 wined3d_surface_decref(surface
);
727 static void texture2d_unload(struct wined3d_resource
*resource
)
729 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
730 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
733 TRACE("texture %p.\n", texture
);
735 for (i
= 0; i
< sub_count
; ++i
)
737 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
738 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
740 sub_resource
->resource_ops
->resource_unload(sub_resource
);
741 surface_set_texture_name(surface
, 0, FALSE
); /* Delete RGB name */
742 surface_set_texture_name(surface
, 0, TRUE
); /* Delete sRGB name */
745 wined3d_texture_unload(texture
);
748 static const struct wined3d_texture_ops texture2d_ops
=
752 texture2d_sub_resource_add_dirty_region
,
753 texture2d_sub_resource_cleanup
,
756 static const struct wined3d_resource_ops texture2d_resource_ops
=
761 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
762 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
763 const struct wined3d_parent_ops
*parent_ops
)
765 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
766 struct wined3d_resource_desc surface_desc
;
770 /* TODO: It should only be possible to create textures for formats
771 * that are reported as supported. */
772 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
774 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
775 return WINED3DERR_INVALIDCALL
;
778 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
780 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
781 return WINED3DERR_INVALIDCALL
;
784 /* Calculate levels for mip mapping */
785 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
787 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
789 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
790 return WINED3DERR_INVALIDCALL
;
795 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
796 return WINED3DERR_INVALIDCALL
;
803 levels
= wined3d_log2i(desc
->width
) + 1;
804 TRACE("Calculated levels = %u.\n", levels
);
807 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
809 UINT pow2_edge_length
= 1;
810 while (pow2_edge_length
< desc
->width
)
811 pow2_edge_length
<<= 1;
813 if (desc
->width
!= pow2_edge_length
)
815 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
817 /* SCRATCH textures cannot be used for texturing */
818 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
822 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
823 return WINED3DERR_INVALIDCALL
;
828 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
829 desc
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
831 WARN("Failed to initialize texture, returning %#x\n", hr
);
835 texture
->pow2_matrix
[0] = 1.0f
;
836 texture
->pow2_matrix
[5] = 1.0f
;
837 texture
->pow2_matrix
[10] = 1.0f
;
838 texture
->pow2_matrix
[15] = 1.0f
;
839 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
841 /* Generate all the surfaces. */
842 surface_desc
= *desc
;
843 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
844 for (i
= 0; i
< texture
->level_count
; ++i
)
846 /* Create the 6 faces. */
847 for (j
= 0; j
< 6; ++j
)
849 static const GLenum cube_targets
[6] =
851 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
852 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
853 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
854 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
855 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
856 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
858 UINT idx
= j
* texture
->level_count
+ i
;
859 struct wined3d_surface
*surface
;
861 if (FAILED(hr
= device
->device_parent
->ops
->create_texture_surface(device
->device_parent
,
862 parent
, &surface_desc
, idx
, surface_flags
, &surface
)))
864 FIXME("(%p) Failed to create surface, hr %#x.\n", texture
, hr
);
865 wined3d_texture_cleanup(texture
);
869 surface_set_container(surface
, texture
);
870 surface_set_texture_target(surface
, cube_targets
[j
], i
);
871 texture
->sub_resources
[idx
] = &surface
->resource
;
872 TRACE("Created surface level %u @ %p.\n", i
, surface
);
874 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
875 surface_desc
.height
= surface_desc
.width
;
881 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
882 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
883 const struct wined3d_parent_ops
*parent_ops
)
885 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
886 struct wined3d_resource_desc surface_desc
;
887 UINT pow2_width
, pow2_height
;
891 /* TODO: It should only be possible to create textures for formats
892 * that are reported as supported. */
893 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
895 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
896 return WINED3DERR_INVALIDCALL
;
899 /* Non-power2 support. */
900 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
902 pow2_width
= desc
->width
;
903 pow2_height
= desc
->height
;
907 /* Find the nearest pow2 match. */
908 pow2_width
= pow2_height
= 1;
909 while (pow2_width
< desc
->width
)
911 while (pow2_height
< desc
->height
)
914 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
916 /* levels == 0 returns an error as well */
919 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
921 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
925 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
926 return WINED3DERR_INVALIDCALL
;
932 /* Calculate levels for mip mapping. */
933 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
935 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
937 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
938 return WINED3DERR_INVALIDCALL
;
943 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
944 return WINED3DERR_INVALIDCALL
;
951 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
952 TRACE("Calculated levels = %u.\n", levels
);
955 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
956 desc
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
958 WARN("Failed to initialize texture, returning %#x.\n", hr
);
962 /* Precalculated scaling for 'faked' non power of two texture coords. */
963 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
964 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
966 texture
->pow2_matrix
[0] = 1.0f
;
967 texture
->pow2_matrix
[5] = 1.0f
;
968 texture
->pow2_matrix
[10] = 1.0f
;
969 texture
->pow2_matrix
[15] = 1.0f
;
970 texture
->target
= GL_TEXTURE_2D
;
971 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
972 texture
->min_mip_lookup
= minMipLookup_noFilter
;
974 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
975 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
977 texture
->pow2_matrix
[0] = (float)desc
->width
;
978 texture
->pow2_matrix
[5] = (float)desc
->height
;
979 texture
->pow2_matrix
[10] = 1.0f
;
980 texture
->pow2_matrix
[15] = 1.0f
;
981 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
982 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
983 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
985 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
986 texture
->min_mip_lookup
= minMipLookup_noMip
;
988 texture
->min_mip_lookup
= minMipLookup_noFilter
;
992 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
994 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
995 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
996 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1000 texture
->pow2_matrix
[0] = 1.0f
;
1001 texture
->pow2_matrix
[5] = 1.0f
;
1004 texture
->pow2_matrix
[10] = 1.0f
;
1005 texture
->pow2_matrix
[15] = 1.0f
;
1006 texture
->target
= GL_TEXTURE_2D
;
1008 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1010 /* Generate all the surfaces. */
1011 surface_desc
= *desc
;
1012 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1013 for (i
= 0; i
< texture
->level_count
; ++i
)
1015 struct wined3d_surface
*surface
;
1017 /* Use the callback to create the texture surface. */
1018 if (FAILED(hr
= device
->device_parent
->ops
->create_texture_surface(device
->device_parent
,
1019 parent
, &surface_desc
, i
, surface_flags
, &surface
)))
1021 FIXME("Failed to create surface %p, hr %#x\n", texture
, hr
);
1022 wined3d_texture_cleanup(texture
);
1026 surface_set_container(surface
, texture
);
1027 surface_set_texture_target(surface
, texture
->target
, i
);
1028 texture
->sub_resources
[i
] = &surface
->resource
;
1029 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1030 /* Calculate the next mipmap level. */
1031 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1032 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1038 /* Context activation is done by the caller. */
1039 static HRESULT
texture3d_bind(struct wined3d_texture
*texture
,
1040 struct wined3d_context
*context
, BOOL srgb
)
1044 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1046 return wined3d_texture_bind(texture
, context
, srgb
, &dummy
);
1049 /* Context activation is done by the caller. */
1050 static void texture3d_preload(struct wined3d_texture
*texture
,
1051 struct wined3d_context
*context
, enum WINED3DSRGB srgb
)
1053 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1054 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1059 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
1061 srgb_mode
= texture_srgb_mode(texture
, srgb
);
1062 if (srgb_mode
&& !gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
1063 flag
= WINED3D_TEXTURE_SRGB_VALID
;
1065 flag
= WINED3D_TEXTURE_RGB_VALID
;
1067 if (texture
->flags
& flag
)
1069 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1073 /* Reload the surfaces if the texture is marked dirty. */
1074 for (i
= 0; i
< sub_count
; ++i
)
1076 wined3d_volume_load(volume_from_resource(texture
->sub_resources
[i
]), context
, srgb_mode
);
1078 texture
->flags
|= flag
;
1081 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1082 const struct wined3d_box
*dirty_region
)
1086 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1088 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1090 /* Cleanup the container. */
1091 volume_set_container(volume
, NULL
);
1092 wined3d_volume_decref(volume
);
1095 static void texture3d_unload(struct wined3d_resource
*resource
)
1097 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1100 TRACE("texture %p.\n", texture
);
1102 for (i
= 0; i
< texture
->level_count
; ++i
)
1104 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
1105 sub_resource
->resource_ops
->resource_unload(sub_resource
);
1108 wined3d_texture_unload(texture
);
1111 static const struct wined3d_texture_ops texture3d_ops
=
1115 texture3d_sub_resource_add_dirty_region
,
1116 texture3d_sub_resource_cleanup
,
1119 static const struct wined3d_resource_ops texture3d_resource_ops
=
1124 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1125 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1127 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1128 UINT tmp_w
, tmp_h
, tmp_d
;
1132 /* TODO: It should only be possible to create textures for formats
1133 * that are reported as supported. */
1134 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1136 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1137 return WINED3DERR_INVALIDCALL
;
1140 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1142 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1143 return WINED3DERR_INVALIDCALL
;
1146 /* Calculate levels for mip mapping. */
1147 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1149 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1151 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1152 return WINED3DERR_INVALIDCALL
;
1157 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1158 return WINED3DERR_INVALIDCALL
;
1165 levels
= wined3d_log2i(max(max(desc
->width
, desc
->height
), desc
->depth
)) + 1;
1166 TRACE("Calculated levels = %u.\n", levels
);
1169 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1171 UINT pow2_w
, pow2_h
, pow2_d
;
1173 while (pow2_w
< desc
->width
)
1176 while (pow2_h
< desc
->height
)
1179 while (pow2_d
< desc
->depth
)
1182 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1184 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1186 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1190 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1191 desc
->width
, desc
->height
, desc
->depth
);
1192 return WINED3DERR_INVALIDCALL
;
1197 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1198 desc
, device
, parent
, parent_ops
, &texture3d_resource_ops
)))
1200 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1204 texture
->pow2_matrix
[0] = 1.0f
;
1205 texture
->pow2_matrix
[5] = 1.0f
;
1206 texture
->pow2_matrix
[10] = 1.0f
;
1207 texture
->pow2_matrix
[15] = 1.0f
;
1208 texture
->target
= GL_TEXTURE_3D
;
1210 /* Generate all the surfaces. */
1211 tmp_w
= desc
->width
;
1212 tmp_h
= desc
->height
;
1213 tmp_d
= desc
->depth
;
1215 for (i
= 0; i
< texture
->level_count
; ++i
)
1217 struct wined3d_volume
*volume
;
1219 /* Create the volume. */
1220 hr
= device
->device_parent
->ops
->create_volume(device
->device_parent
, parent
,
1221 tmp_w
, tmp_h
, tmp_d
, i
, desc
->format
, desc
->pool
, desc
->usage
, &volume
);
1224 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1225 wined3d_texture_cleanup(texture
);
1229 /* Set its container to this texture. */
1230 volume_set_container(volume
, texture
);
1231 texture
->sub_resources
[i
] = &volume
->resource
;
1233 /* Calculate the next mipmap level. */
1234 tmp_w
= max(1, tmp_w
>> 1);
1235 tmp_h
= max(1, tmp_h
>> 1);
1236 tmp_d
= max(1, tmp_d
>> 1);
1242 HRESULT CDECL
wined3d_texture_create_2d(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1243 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1244 struct wined3d_texture
**texture
)
1246 struct wined3d_texture
*object
;
1249 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1250 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1252 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1256 return WINED3DERR_OUTOFVIDEOMEMORY
;
1259 if (FAILED(hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
)))
1261 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1262 HeapFree(GetProcessHeap(), 0, object
);
1267 TRACE("Created texture %p.\n", object
);
1273 HRESULT CDECL
wined3d_texture_create_3d(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1274 UINT level_count
, void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1276 struct wined3d_texture
*object
;
1279 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1280 device
, desc
, level_count
, parent
, parent_ops
, texture
);
1282 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1286 return WINED3DERR_OUTOFVIDEOMEMORY
;
1289 if (FAILED(hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
)))
1291 WARN("Failed to initialize volumetexture, returning %#x\n", hr
);
1292 HeapFree(GetProcessHeap(), 0, object
);
1297 TRACE("Created texture %p.\n", object
);
1303 HRESULT CDECL
wined3d_texture_create_cube(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1304 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1305 struct wined3d_texture
**texture
)
1307 struct wined3d_texture
*object
;
1310 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1311 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1313 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1317 return WINED3DERR_OUTOFVIDEOMEMORY
;
1320 if (FAILED(hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
)))
1322 WARN("Failed to initialize cubetexture, returning %#x\n", hr
);
1323 HeapFree(GetProcessHeap(), 0, object
);
1328 TRACE("Created texture %p.\n", object
);