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
);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
30 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
31 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, struct wined3d_device
*device
,
32 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_resource_ops
*resource_ops
)
34 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
37 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
38 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
39 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
40 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
41 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
42 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
43 device
, parent
, parent_ops
, resource_ops
);
45 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BLOCKS_NO_VERIFY
)) == WINED3DFMT_FLAG_BLOCKS
)
47 UINT width_mask
= format
->block_width
- 1;
48 UINT height_mask
= format
->block_height
- 1;
49 if (desc
->width
& width_mask
|| desc
->height
& height_mask
)
50 return WINED3DERR_INVALIDCALL
;
53 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
54 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
55 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
57 static unsigned int once
;
59 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
60 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
61 && !(format
->flags
& WINED3DFMT_FLAG_TEXTURE
) && !once
++)
62 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
64 WARN("Failed to initialize resource, returning %#x\n", hr
);
68 texture
->texture_ops
= texture_ops
;
69 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
70 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
71 if (!texture
->sub_resources
)
73 ERR("Failed to allocate sub-resource array.\n");
74 resource_cleanup(&texture
->resource
);
78 texture
->layer_count
= layer_count
;
79 texture
->level_count
= level_count
;
80 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
82 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
;
84 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
86 texture
->min_mip_lookup
= minMipLookup
;
87 texture
->mag_lookup
= magLookup
;
91 texture
->min_mip_lookup
= minMipLookup_noFilter
;
92 texture
->mag_lookup
= magLookup_noFilter
;
98 /* A GL context is provided by the caller */
99 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
101 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
105 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
107 struct wined3d_device
*device
= texture
->resource
.device
;
108 struct wined3d_context
*context
= NULL
;
110 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
112 context
= context_acquire(device
, NULL
);
115 if (texture
->texture_rgb
.name
)
116 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
118 if (texture
->texture_srgb
.name
)
119 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
121 if (context
) context_release(context
);
123 wined3d_texture_set_dirty(texture
);
125 resource_unload(&texture
->resource
);
128 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
130 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
133 TRACE("texture %p.\n", texture
);
135 for (i
= 0; i
< sub_count
; ++i
)
137 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
140 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
143 wined3d_texture_unload_gl_texture(texture
);
144 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
145 resource_cleanup(&texture
->resource
);
148 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
150 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
153 /* Context activation is done by the caller. */
154 void wined3d_texture_bind(struct wined3d_texture
*texture
,
155 struct wined3d_context
*context
, BOOL srgb
)
157 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
158 struct gl_texture
*gl_tex
;
161 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
163 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
166 /* sRGB mode cache for preload() calls outside drawprim. */
168 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
170 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
172 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
173 target
= texture
->target
;
177 context_bind_texture(context
, target
, gl_tex
->name
);
181 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
182 checkGLcall("glGenTextures");
183 TRACE("Generated texture %d.\n", gl_tex
->name
);
187 ERR("Failed to generate a texture name.\n");
191 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
193 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
195 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
198 /* Initialise the state of the texture object to the OpenGL defaults, not
199 * the wined3d defaults. */
200 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_WRAP
;
201 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_WRAP
;
202 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3D_TADDRESS_WRAP
;
203 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
204 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_LINEAR
;
205 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
206 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
207 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
208 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
209 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
210 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
212 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
213 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
214 wined3d_texture_set_dirty(texture
);
216 context_bind_texture(context
, target
, gl_tex
->name
);
218 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
220 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
221 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
224 /* For a new texture we have to set the texture levels after binding the
225 * texture. Beware that texture rectangles do not support mipmapping, but
226 * set the maxmiplevel if we're relying on the partial
227 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
228 * (I.e., do not care about cond_np2 here, just look for
229 * GL_TEXTURE_RECTANGLE_ARB.) */
230 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
232 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
233 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
234 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
237 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
239 /* Cubemaps are always set to clamp, regardless of the sampler state. */
240 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
241 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
242 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
245 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
247 /* Conditinal non power of two textures use a different clamping
248 * default. If we're using the GL_WINE_normalized_texrect partial
249 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
250 * has the address mode set to repeat - something that prevents us
251 * from hitting the accelerated codepath. Thus manually set the GL
252 * state. The same applies to filtering. Even if the texture has only
253 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
254 * fallback on macos. */
255 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
256 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
257 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
258 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
259 checkGLcall("glTexParameteri");
260 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_CLAMP
;
261 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_CLAMP
;
262 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_POINT
;
263 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
;
264 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_NONE
;
268 /* Context activation is done by the caller. */
269 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
270 struct wined3d_context
*context
, BOOL srgb
)
272 DWORD active_sampler
;
274 /* We don't need a specific texture unit, but after binding the texture
275 * the current unit is dirty. Read the unit back instead of switching to
276 * 0, this avoids messing around with the state manager's GL states. The
277 * current texture unit should always be a valid one.
279 * To be more specific, this is tricky because we can implicitly be
280 * called from sampler() in state.c. This means we can't touch anything
281 * other than whatever happens to be the currently active texture, or we
282 * would risk marking already applied sampler states dirty again. */
283 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
284 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
285 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
287 wined3d_texture_bind(texture
, context
, srgb
);
290 /* Context activation is done by the caller. */
291 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
292 enum wined3d_texture_address d3d_wrap
, GLenum param
, BOOL cond_np2
)
296 if (d3d_wrap
< WINED3D_TADDRESS_WRAP
|| d3d_wrap
> WINED3D_TADDRESS_MIRROR_ONCE
)
298 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap
);
302 /* Cubemaps are always set to clamp, regardless of the sampler state. */
303 if (target
== GL_TEXTURE_CUBE_MAP_ARB
304 || (cond_np2
&& d3d_wrap
== WINED3D_TADDRESS_WRAP
))
305 gl_wrap
= GL_CLAMP_TO_EDGE
;
307 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3D_TADDRESS_WRAP
];
309 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
310 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, param
, gl_wrap
);
311 checkGLcall("glTexParameteri(target, param, gl_wrap)");
314 /* Context activation is done by the caller (state handler). */
315 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
316 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
317 const struct wined3d_gl_info
*gl_info
)
319 BOOL cond_np2
= texture
->flags
& WINED3D_TEXTURE_COND_NP2
;
320 GLenum target
= texture
->target
;
321 struct gl_texture
*gl_tex
;
325 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
327 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
329 /* This function relies on the correct texture being bound and loaded. */
331 if (sampler_states
[WINED3D_SAMP_ADDRESS_U
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
333 state
= sampler_states
[WINED3D_SAMP_ADDRESS_U
];
334 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
335 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
338 if (sampler_states
[WINED3D_SAMP_ADDRESS_V
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
340 state
= sampler_states
[WINED3D_SAMP_ADDRESS_V
];
341 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
342 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
345 if (sampler_states
[WINED3D_SAMP_ADDRESS_W
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
347 state
= sampler_states
[WINED3D_SAMP_ADDRESS_W
];
348 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
349 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
352 if (sampler_states
[WINED3D_SAMP_BORDER_COLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
356 state
= sampler_states
[WINED3D_SAMP_BORDER_COLOR
];
357 D3DCOLORTOGLFLOAT4(state
, col
);
358 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
359 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
360 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
361 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
364 if (sampler_states
[WINED3D_SAMP_MAG_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
368 state
= sampler_states
[WINED3D_SAMP_MAG_FILTER
];
369 if (state
> WINED3D_TEXF_ANISOTROPIC
)
370 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
372 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
373 min(max(state
, WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
));
374 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
375 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
377 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
380 if ((sampler_states
[WINED3D_SAMP_MIN_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
381 || sampler_states
[WINED3D_SAMP_MIP_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
382 || sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
386 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3D_SAMP_MIP_FILTER
];
387 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3D_SAMP_MIN_FILTER
];
388 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
];
390 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3D_TEXF_ANISOTROPIC
391 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3D_TEXF_ANISOTROPIC
)
393 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
394 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
395 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
397 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
398 min(max(sampler_states
[WINED3D_SAMP_MIN_FILTER
], WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
),
399 min(max(sampler_states
[WINED3D_SAMP_MIP_FILTER
], WINED3D_TEXF_NONE
), WINED3D_TEXF_LINEAR
));
401 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
402 sampler_states
[WINED3D_SAMP_MIN_FILTER
],
403 sampler_states
[WINED3D_SAMP_MIP_FILTER
], gl_value
);
404 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
405 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
409 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3D_TEXF_NONE
)
410 gl_value
= texture
->lod
;
411 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
412 gl_value
= texture
->level_count
- 1;
413 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
414 /* texture->lod is already clamped in the setter. */
415 gl_value
= texture
->lod
;
417 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
419 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
420 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
421 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
422 * corresponds to GL_TEXTURE_BASE_LEVEL. */
423 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
427 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3D_TEXF_ANISOTROPIC
428 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3D_TEXF_ANISOTROPIC
429 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3D_TEXF_ANISOTROPIC
)
433 aniso
= sampler_states
[WINED3D_SAMP_MAX_ANISOTROPY
];
435 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
437 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
439 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
440 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
444 WARN("Anisotropic filtering not supported.\n");
446 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
449 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
450 if (sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
452 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
453 sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
454 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
455 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
];
458 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
459 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
461 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
463 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
464 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
465 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
466 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
470 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
471 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
472 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
477 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
479 ULONG refcount
= InterlockedIncrement(&texture
->resource
.ref
);
481 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
486 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
488 ULONG refcount
= InterlockedDecrement(&texture
->resource
.ref
);
490 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
494 wined3d_texture_cleanup(texture
);
495 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
496 HeapFree(GetProcessHeap(), 0, texture
);
502 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
504 TRACE("texture %p.\n", texture
);
506 return &texture
->resource
;
509 /* Context activation is done by the caller */
510 void wined3d_texture_load(struct wined3d_texture
*texture
,
511 struct wined3d_context
*context
, BOOL srgb
)
513 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
514 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
518 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
520 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
524 flag
= WINED3D_TEXTURE_SRGB_VALID
;
526 flag
= WINED3D_TEXTURE_RGB_VALID
;
528 if (texture
->flags
& flag
)
530 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
534 /* Reload the surfaces if the texture is marked dirty. */
535 for (i
= 0; i
< sub_count
; ++i
)
537 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
], context
, srgb
);
539 texture
->flags
|= flag
;
542 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
544 struct wined3d_context
*context
;
545 context
= context_acquire(texture
->resource
.device
, NULL
);
546 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
547 context_release(context
);
550 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
552 TRACE("texture %p.\n", texture
);
554 return texture
->resource
.parent
;
557 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
559 DWORD old
= texture
->lod
;
561 TRACE("texture %p, lod %u.\n", texture
, lod
);
563 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
564 * textures. The call always returns 0, and GetLOD always returns 0. */
565 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
567 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
571 if (lod
>= texture
->level_count
)
572 lod
= texture
->level_count
- 1;
574 if (texture
->lod
!= lod
)
578 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
579 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
580 if (texture
->resource
.bind_count
)
581 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
587 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
589 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
594 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
596 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
598 return texture
->level_count
;
601 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
602 enum wined3d_texture_filter_type filter_type
)
604 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
606 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
608 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
609 return WINED3DERR_INVALIDCALL
;
612 texture
->filter_type
= filter_type
;
617 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
619 TRACE("texture %p.\n", texture
);
621 return texture
->filter_type
;
624 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
625 DWORD flags
, const struct wined3d_color_key
*color_key
)
627 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
629 if (flags
& WINEDDCKEY_COLORSPACE
)
631 FIXME("Unhandled flags %#x.\n", flags
);
632 return WINED3DERR_INVALIDCALL
;
637 switch (flags
& ~WINEDDCKEY_COLORSPACE
)
639 case WINEDDCKEY_DESTBLT
:
640 texture
->dst_blt_color_key
= *color_key
;
641 texture
->color_key_flags
|= WINEDDSD_CKDESTBLT
;
644 case WINEDDCKEY_DESTOVERLAY
:
645 texture
->dst_overlay_color_key
= *color_key
;
646 texture
->color_key_flags
|= WINEDDSD_CKDESTOVERLAY
;
649 case WINEDDCKEY_SRCOVERLAY
:
650 texture
->src_overlay_color_key
= *color_key
;
651 texture
->color_key_flags
|= WINEDDSD_CKSRCOVERLAY
;
654 case WINEDDCKEY_SRCBLT
:
655 texture
->src_blt_color_key
= *color_key
;
656 texture
->color_key_flags
|= WINEDDSD_CKSRCBLT
;
662 switch (flags
& ~WINEDDCKEY_COLORSPACE
)
664 case WINEDDCKEY_DESTBLT
:
665 texture
->color_key_flags
&= ~WINEDDSD_CKDESTBLT
;
668 case WINEDDCKEY_DESTOVERLAY
:
669 texture
->color_key_flags
&= ~WINEDDSD_CKDESTOVERLAY
;
672 case WINEDDCKEY_SRCOVERLAY
:
673 texture
->color_key_flags
&= ~WINEDDSD_CKSRCOVERLAY
;
676 case WINEDDCKEY_SRCBLT
:
677 texture
->color_key_flags
&= ~WINEDDSD_CKSRCBLT
;
685 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
687 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
688 FIXME("texture %p stub!\n", texture
);
691 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
692 UINT sub_resource_idx
)
694 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
696 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
698 if (sub_resource_idx
>= sub_count
)
700 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
704 return texture
->sub_resources
[sub_resource_idx
];
707 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
708 UINT layer
, const struct wined3d_box
*dirty_region
)
710 struct wined3d_resource
*sub_resource
;
712 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
714 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
716 WARN("Failed to get sub-resource.\n");
717 return WINED3DERR_INVALIDCALL
;
720 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
725 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
726 struct wined3d_context
*context
, BOOL srgb
)
728 surface_load(surface_from_resource(sub_resource
), srgb
);
731 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
732 const struct wined3d_box
*dirty_region
)
734 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
736 surface_prepare_map_memory(surface
);
737 surface_load_location(surface
, surface
->map_binding
);
738 surface_invalidate_location(surface
, ~surface
->map_binding
);
741 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
743 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
745 wined3d_surface_destroy(surface
);
748 static const struct wined3d_texture_ops texture2d_ops
=
750 texture2d_sub_resource_load
,
751 texture2d_sub_resource_add_dirty_region
,
752 texture2d_sub_resource_cleanup
,
755 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
757 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
758 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
761 TRACE("texture %p.\n", texture
);
763 for (i
= 0; i
< sub_count
; ++i
)
765 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
767 sub_resource
->resource_ops
->resource_unload(sub_resource
);
770 wined3d_texture_unload_gl_texture(texture
);
773 static const struct wined3d_resource_ops texture_resource_ops
=
775 wined3d_texture_unload
,
778 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
779 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
780 const struct wined3d_parent_ops
*parent_ops
)
782 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
783 struct wined3d_resource_desc surface_desc
;
787 /* TODO: It should only be possible to create textures for formats
788 * that are reported as supported. */
789 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
791 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
792 return WINED3DERR_INVALIDCALL
;
795 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
797 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
798 return WINED3DERR_INVALIDCALL
;
801 /* Calculate levels for mip mapping */
802 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
804 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
806 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
807 return WINED3DERR_INVALIDCALL
;
812 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
813 return WINED3DERR_INVALIDCALL
;
820 levels
= wined3d_log2i(desc
->width
) + 1;
821 TRACE("Calculated levels = %u.\n", levels
);
824 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
826 UINT pow2_edge_length
= 1;
827 while (pow2_edge_length
< desc
->width
)
828 pow2_edge_length
<<= 1;
830 if (desc
->width
!= pow2_edge_length
)
832 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
834 /* SCRATCH textures cannot be used for texturing */
835 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
839 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
840 return WINED3DERR_INVALIDCALL
;
845 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
846 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
848 WARN("Failed to initialize texture, returning %#x\n", hr
);
852 texture
->pow2_matrix
[0] = 1.0f
;
853 texture
->pow2_matrix
[5] = 1.0f
;
854 texture
->pow2_matrix
[10] = 1.0f
;
855 texture
->pow2_matrix
[15] = 1.0f
;
856 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
858 /* Generate all the surfaces. */
859 surface_desc
= *desc
;
860 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
861 for (i
= 0; i
< texture
->level_count
; ++i
)
863 /* Create the 6 faces. */
864 for (j
= 0; j
< 6; ++j
)
866 static const GLenum cube_targets
[6] =
868 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
869 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
870 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
871 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
872 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
873 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
875 UINT idx
= j
* texture
->level_count
+ i
;
876 struct wined3d_surface
*surface
;
878 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
879 cube_targets
[j
], i
, surface_flags
, &surface
)))
881 WARN("Failed to create surface, hr %#x.\n", hr
);
882 wined3d_texture_cleanup(texture
);
886 texture
->sub_resources
[idx
] = &surface
->resource
;
887 TRACE("Created surface level %u @ %p.\n", i
, surface
);
889 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
890 surface_desc
.height
= surface_desc
.width
;
896 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
897 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
898 const struct wined3d_parent_ops
*parent_ops
)
900 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
901 struct wined3d_resource_desc surface_desc
;
902 UINT pow2_width
, pow2_height
;
906 /* TODO: It should only be possible to create textures for formats
907 * that are reported as supported. */
908 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
910 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
911 return WINED3DERR_INVALIDCALL
;
914 /* Non-power2 support. */
915 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
917 pow2_width
= desc
->width
;
918 pow2_height
= desc
->height
;
922 /* Find the nearest pow2 match. */
923 pow2_width
= pow2_height
= 1;
924 while (pow2_width
< desc
->width
)
926 while (pow2_height
< desc
->height
)
929 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
931 /* levels == 0 returns an error as well */
934 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
936 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
940 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
941 return WINED3DERR_INVALIDCALL
;
947 /* Calculate levels for mip mapping. */
948 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
950 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
952 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
953 return WINED3DERR_INVALIDCALL
;
958 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
959 return WINED3DERR_INVALIDCALL
;
966 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
967 TRACE("Calculated levels = %u.\n", levels
);
970 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
971 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
973 WARN("Failed to initialize texture, returning %#x.\n", hr
);
977 /* Precalculated scaling for 'faked' non power of two texture coords. */
978 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
979 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
981 texture
->pow2_matrix
[0] = 1.0f
;
982 texture
->pow2_matrix
[5] = 1.0f
;
983 texture
->pow2_matrix
[10] = 1.0f
;
984 texture
->pow2_matrix
[15] = 1.0f
;
985 texture
->target
= GL_TEXTURE_2D
;
986 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
987 texture
->min_mip_lookup
= minMipLookup_noFilter
;
989 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
990 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
992 texture
->pow2_matrix
[0] = (float)desc
->width
;
993 texture
->pow2_matrix
[5] = (float)desc
->height
;
994 texture
->pow2_matrix
[10] = 1.0f
;
995 texture
->pow2_matrix
[15] = 1.0f
;
996 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
997 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
998 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1000 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
1001 texture
->min_mip_lookup
= minMipLookup_noMip
;
1003 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1007 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
1009 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1010 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1011 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1015 texture
->pow2_matrix
[0] = 1.0f
;
1016 texture
->pow2_matrix
[5] = 1.0f
;
1019 texture
->pow2_matrix
[10] = 1.0f
;
1020 texture
->pow2_matrix
[15] = 1.0f
;
1021 texture
->target
= GL_TEXTURE_2D
;
1023 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1025 /* Generate all the surfaces. */
1026 surface_desc
= *desc
;
1027 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1028 for (i
= 0; i
< texture
->level_count
; ++i
)
1030 struct wined3d_surface
*surface
;
1032 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1033 texture
->target
, i
, surface_flags
, &surface
)))
1035 WARN("Failed to create surface, hr %#x.\n", hr
);
1036 wined3d_texture_cleanup(texture
);
1040 texture
->sub_resources
[i
] = &surface
->resource
;
1041 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1042 /* Calculate the next mipmap level. */
1043 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1044 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1050 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1051 struct wined3d_context
*context
, BOOL srgb
)
1053 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1056 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1057 const struct wined3d_box
*dirty_region
)
1059 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1062 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1064 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1066 wined3d_volume_destroy(volume
);
1069 static const struct wined3d_texture_ops texture3d_ops
=
1071 texture3d_sub_resource_load
,
1072 texture3d_sub_resource_add_dirty_region
,
1073 texture3d_sub_resource_cleanup
,
1076 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1077 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1079 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1080 struct wined3d_resource_desc volume_desc
;
1084 /* TODO: It should only be possible to create textures for formats
1085 * that are reported as supported. */
1086 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1088 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1089 return WINED3DERR_INVALIDCALL
;
1092 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1094 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1095 return WINED3DERR_INVALIDCALL
;
1098 /* Calculate levels for mip mapping. */
1099 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1101 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1103 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1104 return WINED3DERR_INVALIDCALL
;
1109 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1110 return WINED3DERR_INVALIDCALL
;
1117 levels
= wined3d_log2i(max(max(desc
->width
, desc
->height
), desc
->depth
)) + 1;
1118 TRACE("Calculated levels = %u.\n", levels
);
1121 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1123 UINT pow2_w
, pow2_h
, pow2_d
;
1125 while (pow2_w
< desc
->width
)
1128 while (pow2_h
< desc
->height
)
1131 while (pow2_d
< desc
->depth
)
1134 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1136 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1138 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1142 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1143 desc
->width
, desc
->height
, desc
->depth
);
1144 return WINED3DERR_INVALIDCALL
;
1149 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1150 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1152 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1156 texture
->pow2_matrix
[0] = 1.0f
;
1157 texture
->pow2_matrix
[5] = 1.0f
;
1158 texture
->pow2_matrix
[10] = 1.0f
;
1159 texture
->pow2_matrix
[15] = 1.0f
;
1160 texture
->target
= GL_TEXTURE_3D
;
1162 /* Generate all the surfaces. */
1163 volume_desc
= *desc
;
1164 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1165 for (i
= 0; i
< texture
->level_count
; ++i
)
1167 struct wined3d_volume
*volume
;
1169 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1171 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1172 wined3d_texture_cleanup(texture
);
1176 texture
->sub_resources
[i
] = &volume
->resource
;
1178 /* Calculate the next mipmap level. */
1179 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1180 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1181 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1187 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1188 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1189 struct wined3d_texture
**texture
)
1191 struct wined3d_texture
*object
;
1194 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1195 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1197 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1198 return E_OUTOFMEMORY
;
1200 switch (desc
->resource_type
)
1202 case WINED3D_RTYPE_TEXTURE
:
1203 hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1206 case WINED3D_RTYPE_VOLUME_TEXTURE
:
1207 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1210 case WINED3D_RTYPE_CUBE_TEXTURE
:
1211 hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1215 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1216 hr
= WINED3DERR_INVALIDCALL
;
1222 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1223 HeapFree(GetProcessHeap(), 0, object
);
1227 TRACE("Created texture %p.\n", object
);