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
);
67 wined3d_resource_update_draw_binding(&texture
->resource
);
69 texture
->texture_ops
= texture_ops
;
70 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
71 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
72 if (!texture
->sub_resources
)
74 ERR("Failed to allocate sub-resource array.\n");
75 resource_cleanup(&texture
->resource
);
79 texture
->layer_count
= layer_count
;
80 texture
->level_count
= level_count
;
81 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
83 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
;
85 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
87 texture
->min_mip_lookup
= minMipLookup
;
88 texture
->mag_lookup
= magLookup
;
92 texture
->min_mip_lookup
= minMipLookup_noFilter
;
93 texture
->mag_lookup
= magLookup_noFilter
;
99 /* A GL context is provided by the caller */
100 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
102 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
106 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
108 struct wined3d_device
*device
= texture
->resource
.device
;
109 struct wined3d_context
*context
= NULL
;
111 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
113 context
= context_acquire(device
, NULL
);
116 if (texture
->texture_rgb
.name
)
117 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
119 if (texture
->texture_srgb
.name
)
120 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
122 if (context
) context_release(context
);
124 wined3d_texture_set_dirty(texture
);
126 resource_unload(&texture
->resource
);
129 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
131 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
134 TRACE("texture %p.\n", texture
);
136 for (i
= 0; i
< sub_count
; ++i
)
138 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
141 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
144 wined3d_texture_unload_gl_texture(texture
);
145 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
146 resource_cleanup(&texture
->resource
);
149 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
151 texture
->swapchain
= swapchain
;
152 wined3d_resource_update_draw_binding(&texture
->resource
);
155 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
157 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
160 /* Context activation is done by the caller. */
161 void wined3d_texture_bind(struct wined3d_texture
*texture
,
162 struct wined3d_context
*context
, BOOL srgb
)
164 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
165 struct gl_texture
*gl_tex
;
168 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
170 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
173 /* sRGB mode cache for preload() calls outside drawprim. */
175 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
177 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
179 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
180 target
= texture
->target
;
184 context_bind_texture(context
, target
, gl_tex
->name
);
188 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
189 checkGLcall("glGenTextures");
190 TRACE("Generated texture %d.\n", gl_tex
->name
);
194 ERR("Failed to generate a texture name.\n");
198 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
200 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
202 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
205 /* Initialise the state of the texture object to the OpenGL defaults, not
206 * the wined3d defaults. */
207 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_WRAP
;
208 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_WRAP
;
209 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3D_TADDRESS_WRAP
;
210 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
211 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_LINEAR
;
212 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
213 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
214 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
215 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
216 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
217 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
219 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
220 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
221 wined3d_texture_set_dirty(texture
);
223 context_bind_texture(context
, target
, gl_tex
->name
);
225 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
227 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
228 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
231 /* For a new texture we have to set the texture levels after binding the
232 * texture. Beware that texture rectangles do not support mipmapping, but
233 * set the maxmiplevel if we're relying on the partial
234 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
235 * (I.e., do not care about cond_np2 here, just look for
236 * GL_TEXTURE_RECTANGLE_ARB.) */
237 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
239 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
240 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
241 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
244 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
246 /* Cubemaps are always set to clamp, regardless of the sampler state. */
247 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
248 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
249 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
252 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
254 /* Conditinal non power of two textures use a different clamping
255 * default. If we're using the GL_WINE_normalized_texrect partial
256 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
257 * has the address mode set to repeat - something that prevents us
258 * from hitting the accelerated codepath. Thus manually set the GL
259 * state. The same applies to filtering. Even if the texture has only
260 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
261 * fallback on macos. */
262 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
263 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
264 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
265 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
266 checkGLcall("glTexParameteri");
267 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_CLAMP
;
268 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_CLAMP
;
269 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_POINT
;
270 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
;
271 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_NONE
;
275 /* Context activation is done by the caller. */
276 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
277 struct wined3d_context
*context
, BOOL srgb
)
279 DWORD active_sampler
;
281 /* We don't need a specific texture unit, but after binding the texture
282 * the current unit is dirty. Read the unit back instead of switching to
283 * 0, this avoids messing around with the state manager's GL states. The
284 * current texture unit should always be a valid one.
286 * To be more specific, this is tricky because we can implicitly be
287 * called from sampler() in state.c. This means we can't touch anything
288 * other than whatever happens to be the currently active texture, or we
289 * would risk marking already applied sampler states dirty again. */
290 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
291 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
292 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
294 wined3d_texture_bind(texture
, context
, srgb
);
297 /* Context activation is done by the caller. */
298 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
299 enum wined3d_texture_address d3d_wrap
, GLenum param
, BOOL cond_np2
)
303 if (d3d_wrap
< WINED3D_TADDRESS_WRAP
|| d3d_wrap
> WINED3D_TADDRESS_MIRROR_ONCE
)
305 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap
);
309 /* Cubemaps are always set to clamp, regardless of the sampler state. */
310 if (target
== GL_TEXTURE_CUBE_MAP_ARB
311 || (cond_np2
&& d3d_wrap
== WINED3D_TADDRESS_WRAP
))
312 gl_wrap
= GL_CLAMP_TO_EDGE
;
314 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3D_TADDRESS_WRAP
];
316 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
317 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, param
, gl_wrap
);
318 checkGLcall("glTexParameteri(target, param, gl_wrap)");
321 /* Context activation is done by the caller (state handler). */
322 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
323 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
324 const struct wined3d_gl_info
*gl_info
)
326 BOOL cond_np2
= texture
->flags
& WINED3D_TEXTURE_COND_NP2
;
327 GLenum target
= texture
->target
;
328 struct gl_texture
*gl_tex
;
332 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
334 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
336 /* This function relies on the correct texture being bound and loaded. */
338 if (sampler_states
[WINED3D_SAMP_ADDRESS_U
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
340 state
= sampler_states
[WINED3D_SAMP_ADDRESS_U
];
341 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
342 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
345 if (sampler_states
[WINED3D_SAMP_ADDRESS_V
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
347 state
= sampler_states
[WINED3D_SAMP_ADDRESS_V
];
348 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
349 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
352 if (sampler_states
[WINED3D_SAMP_ADDRESS_W
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
354 state
= sampler_states
[WINED3D_SAMP_ADDRESS_W
];
355 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
356 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
359 if (sampler_states
[WINED3D_SAMP_BORDER_COLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
363 state
= sampler_states
[WINED3D_SAMP_BORDER_COLOR
];
364 D3DCOLORTOGLFLOAT4(state
, col
);
365 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
366 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
367 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
368 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
371 if (sampler_states
[WINED3D_SAMP_MAG_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
375 state
= sampler_states
[WINED3D_SAMP_MAG_FILTER
];
376 if (state
> WINED3D_TEXF_ANISOTROPIC
)
377 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
379 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
380 min(max(state
, WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
));
381 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
382 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
384 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
387 if ((sampler_states
[WINED3D_SAMP_MIN_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
388 || sampler_states
[WINED3D_SAMP_MIP_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
389 || sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
393 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3D_SAMP_MIP_FILTER
];
394 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3D_SAMP_MIN_FILTER
];
395 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
];
397 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3D_TEXF_ANISOTROPIC
398 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3D_TEXF_ANISOTROPIC
)
400 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
401 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
402 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
404 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
405 min(max(sampler_states
[WINED3D_SAMP_MIN_FILTER
], WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
),
406 min(max(sampler_states
[WINED3D_SAMP_MIP_FILTER
], WINED3D_TEXF_NONE
), WINED3D_TEXF_LINEAR
));
408 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
409 sampler_states
[WINED3D_SAMP_MIN_FILTER
],
410 sampler_states
[WINED3D_SAMP_MIP_FILTER
], gl_value
);
411 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
412 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
416 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3D_TEXF_NONE
)
417 gl_value
= texture
->lod
;
418 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
419 gl_value
= texture
->level_count
- 1;
420 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
421 /* texture->lod is already clamped in the setter. */
422 gl_value
= texture
->lod
;
424 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
426 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
427 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
428 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
429 * corresponds to GL_TEXTURE_BASE_LEVEL. */
430 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
434 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3D_TEXF_ANISOTROPIC
435 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3D_TEXF_ANISOTROPIC
436 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3D_TEXF_ANISOTROPIC
)
440 aniso
= sampler_states
[WINED3D_SAMP_MAX_ANISOTROPY
];
442 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
444 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
446 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
447 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
451 WARN("Anisotropic filtering not supported.\n");
453 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
456 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
457 if (sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
459 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
460 sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
461 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
462 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
];
465 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
466 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
468 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
470 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
471 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
472 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
473 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
477 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
478 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
479 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
484 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
488 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
490 if (texture
->swapchain
)
491 return wined3d_swapchain_incref(texture
->swapchain
);
493 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
494 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
499 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
503 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
505 if (texture
->swapchain
)
506 return wined3d_swapchain_decref(texture
->swapchain
);
508 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
509 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
513 wined3d_texture_cleanup(texture
);
514 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
515 HeapFree(GetProcessHeap(), 0, texture
);
521 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
523 TRACE("texture %p.\n", texture
);
525 return &texture
->resource
;
528 /* Context activation is done by the caller */
529 void wined3d_texture_load(struct wined3d_texture
*texture
,
530 struct wined3d_context
*context
, BOOL srgb
)
532 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
533 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
537 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
539 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
543 flag
= WINED3D_TEXTURE_SRGB_VALID
;
545 flag
= WINED3D_TEXTURE_RGB_VALID
;
547 if (texture
->flags
& flag
)
549 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
553 /* Reload the surfaces if the texture is marked dirty. */
554 for (i
= 0; i
< sub_count
; ++i
)
556 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
], context
, srgb
);
558 texture
->flags
|= flag
;
561 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
563 struct wined3d_context
*context
;
564 context
= context_acquire(texture
->resource
.device
, NULL
);
565 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
566 context_release(context
);
569 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
571 TRACE("texture %p.\n", texture
);
573 return texture
->resource
.parent
;
576 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
578 DWORD old
= texture
->lod
;
580 TRACE("texture %p, lod %u.\n", texture
, lod
);
582 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
583 * textures. The call always returns 0, and GetLOD always returns 0. */
584 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
586 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
590 if (lod
>= texture
->level_count
)
591 lod
= texture
->level_count
- 1;
593 if (texture
->lod
!= lod
)
597 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
598 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
599 if (texture
->resource
.bind_count
)
600 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
606 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
608 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
613 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
615 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
617 return texture
->level_count
;
620 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
621 enum wined3d_texture_filter_type filter_type
)
623 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
625 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
627 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
628 return WINED3DERR_INVALIDCALL
;
631 texture
->filter_type
= filter_type
;
636 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
638 TRACE("texture %p.\n", texture
);
640 return texture
->filter_type
;
643 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
644 DWORD flags
, const struct wined3d_color_key
*color_key
)
646 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
648 if (flags
& WINEDDCKEY_COLORSPACE
)
650 FIXME("Unhandled flags %#x.\n", flags
);
651 return WINED3DERR_INVALIDCALL
;
656 switch (flags
& ~WINEDDCKEY_COLORSPACE
)
658 case WINEDDCKEY_DESTBLT
:
659 texture
->dst_blt_color_key
= *color_key
;
660 texture
->color_key_flags
|= WINEDDSD_CKDESTBLT
;
663 case WINEDDCKEY_DESTOVERLAY
:
664 texture
->dst_overlay_color_key
= *color_key
;
665 texture
->color_key_flags
|= WINEDDSD_CKDESTOVERLAY
;
668 case WINEDDCKEY_SRCOVERLAY
:
669 texture
->src_overlay_color_key
= *color_key
;
670 texture
->color_key_flags
|= WINEDDSD_CKSRCOVERLAY
;
673 case WINEDDCKEY_SRCBLT
:
674 texture
->src_blt_color_key
= *color_key
;
675 texture
->color_key_flags
|= WINEDDSD_CKSRCBLT
;
681 switch (flags
& ~WINEDDCKEY_COLORSPACE
)
683 case WINEDDCKEY_DESTBLT
:
684 texture
->color_key_flags
&= ~WINEDDSD_CKDESTBLT
;
687 case WINEDDCKEY_DESTOVERLAY
:
688 texture
->color_key_flags
&= ~WINEDDSD_CKDESTOVERLAY
;
691 case WINEDDCKEY_SRCOVERLAY
:
692 texture
->color_key_flags
&= ~WINEDDSD_CKSRCOVERLAY
;
695 case WINEDDCKEY_SRCBLT
:
696 texture
->color_key_flags
&= ~WINEDDSD_CKSRCBLT
;
704 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
706 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
707 FIXME("texture %p stub!\n", texture
);
710 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
711 UINT sub_resource_idx
)
713 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
715 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
717 if (sub_resource_idx
>= sub_count
)
719 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
723 return texture
->sub_resources
[sub_resource_idx
];
726 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
727 UINT layer
, const struct wined3d_box
*dirty_region
)
729 struct wined3d_resource
*sub_resource
;
731 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
733 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
735 WARN("Failed to get sub-resource.\n");
736 return WINED3DERR_INVALIDCALL
;
739 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
744 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
745 struct wined3d_context
*context
, BOOL srgb
)
747 surface_load(surface_from_resource(sub_resource
), srgb
);
750 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
751 const struct wined3d_box
*dirty_region
)
753 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
755 surface_prepare_map_memory(surface
);
756 surface_load_location(surface
, surface
->resource
.map_binding
);
757 surface_invalidate_location(surface
, ~surface
->resource
.map_binding
);
760 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
762 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
764 wined3d_surface_destroy(surface
);
767 static const struct wined3d_texture_ops texture2d_ops
=
769 texture2d_sub_resource_load
,
770 texture2d_sub_resource_add_dirty_region
,
771 texture2d_sub_resource_cleanup
,
774 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
776 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
779 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
781 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
784 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
786 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
787 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
790 TRACE("texture %p.\n", texture
);
792 for (i
= 0; i
< sub_count
; ++i
)
794 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
796 sub_resource
->resource_ops
->resource_unload(sub_resource
);
799 wined3d_texture_unload_gl_texture(texture
);
802 static const struct wined3d_resource_ops texture_resource_ops
=
804 texture_resource_incref
,
805 texture_resource_decref
,
806 wined3d_texture_unload
,
809 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
810 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
811 const struct wined3d_parent_ops
*parent_ops
)
813 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
814 struct wined3d_resource_desc surface_desc
;
818 /* TODO: It should only be possible to create textures for formats
819 * that are reported as supported. */
820 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
822 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
823 return WINED3DERR_INVALIDCALL
;
826 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
828 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
829 return WINED3DERR_INVALIDCALL
;
832 /* Calculate levels for mip mapping */
833 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
835 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
837 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
838 return WINED3DERR_INVALIDCALL
;
843 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
844 return WINED3DERR_INVALIDCALL
;
851 levels
= wined3d_log2i(desc
->width
) + 1;
852 TRACE("Calculated levels = %u.\n", levels
);
855 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
857 UINT pow2_edge_length
= 1;
858 while (pow2_edge_length
< desc
->width
)
859 pow2_edge_length
<<= 1;
861 if (desc
->width
!= pow2_edge_length
)
863 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
865 /* SCRATCH textures cannot be used for texturing */
866 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
870 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
871 return WINED3DERR_INVALIDCALL
;
876 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
877 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
879 WARN("Failed to initialize texture, returning %#x\n", hr
);
883 texture
->pow2_matrix
[0] = 1.0f
;
884 texture
->pow2_matrix
[5] = 1.0f
;
885 texture
->pow2_matrix
[10] = 1.0f
;
886 texture
->pow2_matrix
[15] = 1.0f
;
887 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
889 /* Generate all the surfaces. */
890 surface_desc
= *desc
;
891 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
892 for (i
= 0; i
< texture
->level_count
; ++i
)
894 /* Create the 6 faces. */
895 for (j
= 0; j
< 6; ++j
)
897 static const GLenum cube_targets
[6] =
899 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
900 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
901 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
902 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
903 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
904 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
906 UINT idx
= j
* texture
->level_count
+ i
;
907 struct wined3d_surface
*surface
;
909 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
910 cube_targets
[j
], i
, surface_flags
, &surface
)))
912 WARN("Failed to create surface, hr %#x.\n", hr
);
913 wined3d_texture_cleanup(texture
);
917 texture
->sub_resources
[idx
] = &surface
->resource
;
918 TRACE("Created surface level %u @ %p.\n", i
, surface
);
920 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
921 surface_desc
.height
= surface_desc
.width
;
927 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
928 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
929 const struct wined3d_parent_ops
*parent_ops
)
931 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
932 struct wined3d_resource_desc surface_desc
;
933 UINT pow2_width
, pow2_height
;
937 /* TODO: It should only be possible to create textures for formats
938 * that are reported as supported. */
939 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
941 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
942 return WINED3DERR_INVALIDCALL
;
945 /* Non-power2 support. */
946 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
948 pow2_width
= desc
->width
;
949 pow2_height
= desc
->height
;
953 /* Find the nearest pow2 match. */
954 pow2_width
= pow2_height
= 1;
955 while (pow2_width
< desc
->width
)
957 while (pow2_height
< desc
->height
)
960 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
962 /* levels == 0 returns an error as well */
965 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
967 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
971 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
972 return WINED3DERR_INVALIDCALL
;
978 /* Calculate levels for mip mapping. */
979 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
981 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
983 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
984 return WINED3DERR_INVALIDCALL
;
989 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
990 return WINED3DERR_INVALIDCALL
;
997 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
998 TRACE("Calculated levels = %u.\n", levels
);
1001 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
1002 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1004 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1008 /* Precalculated scaling for 'faked' non power of two texture coords. */
1009 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
1010 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1012 texture
->pow2_matrix
[0] = 1.0f
;
1013 texture
->pow2_matrix
[5] = 1.0f
;
1014 texture
->pow2_matrix
[10] = 1.0f
;
1015 texture
->pow2_matrix
[15] = 1.0f
;
1016 texture
->target
= GL_TEXTURE_2D
;
1017 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1018 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1020 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
1021 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1023 texture
->pow2_matrix
[0] = (float)desc
->width
;
1024 texture
->pow2_matrix
[5] = (float)desc
->height
;
1025 texture
->pow2_matrix
[10] = 1.0f
;
1026 texture
->pow2_matrix
[15] = 1.0f
;
1027 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1028 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1029 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1031 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
1032 texture
->min_mip_lookup
= minMipLookup_noMip
;
1034 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1038 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
1040 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1041 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1042 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1046 texture
->pow2_matrix
[0] = 1.0f
;
1047 texture
->pow2_matrix
[5] = 1.0f
;
1050 texture
->pow2_matrix
[10] = 1.0f
;
1051 texture
->pow2_matrix
[15] = 1.0f
;
1052 texture
->target
= GL_TEXTURE_2D
;
1054 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1056 /* Generate all the surfaces. */
1057 surface_desc
= *desc
;
1058 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1059 for (i
= 0; i
< texture
->level_count
; ++i
)
1061 struct wined3d_surface
*surface
;
1063 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1064 texture
->target
, i
, surface_flags
, &surface
)))
1066 WARN("Failed to create surface, hr %#x.\n", hr
);
1067 wined3d_texture_cleanup(texture
);
1071 texture
->sub_resources
[i
] = &surface
->resource
;
1072 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1073 /* Calculate the next mipmap level. */
1074 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1075 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1081 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1082 struct wined3d_context
*context
, BOOL srgb
)
1084 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1087 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1088 const struct wined3d_box
*dirty_region
)
1090 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1093 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1095 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1097 wined3d_volume_destroy(volume
);
1100 static const struct wined3d_texture_ops texture3d_ops
=
1102 texture3d_sub_resource_load
,
1103 texture3d_sub_resource_add_dirty_region
,
1104 texture3d_sub_resource_cleanup
,
1107 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1108 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1110 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1111 struct wined3d_resource_desc volume_desc
;
1115 /* TODO: It should only be possible to create textures for formats
1116 * that are reported as supported. */
1117 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1119 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1120 return WINED3DERR_INVALIDCALL
;
1123 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1125 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1126 return WINED3DERR_INVALIDCALL
;
1129 /* Calculate levels for mip mapping. */
1130 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1132 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1134 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1135 return WINED3DERR_INVALIDCALL
;
1140 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1141 return WINED3DERR_INVALIDCALL
;
1148 levels
= wined3d_log2i(max(max(desc
->width
, desc
->height
), desc
->depth
)) + 1;
1149 TRACE("Calculated levels = %u.\n", levels
);
1152 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1154 UINT pow2_w
, pow2_h
, pow2_d
;
1156 while (pow2_w
< desc
->width
)
1159 while (pow2_h
< desc
->height
)
1162 while (pow2_d
< desc
->depth
)
1165 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1167 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1169 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1173 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1174 desc
->width
, desc
->height
, desc
->depth
);
1175 return WINED3DERR_INVALIDCALL
;
1180 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1181 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1183 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1187 texture
->pow2_matrix
[0] = 1.0f
;
1188 texture
->pow2_matrix
[5] = 1.0f
;
1189 texture
->pow2_matrix
[10] = 1.0f
;
1190 texture
->pow2_matrix
[15] = 1.0f
;
1191 texture
->target
= GL_TEXTURE_3D
;
1193 /* Generate all the surfaces. */
1194 volume_desc
= *desc
;
1195 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1196 for (i
= 0; i
< texture
->level_count
; ++i
)
1198 struct wined3d_volume
*volume
;
1200 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1202 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1203 wined3d_texture_cleanup(texture
);
1207 texture
->sub_resources
[i
] = &volume
->resource
;
1209 /* Calculate the next mipmap level. */
1210 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1211 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1212 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1218 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1219 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1220 struct wined3d_texture
**texture
)
1222 struct wined3d_texture
*object
;
1225 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1226 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1228 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1229 return E_OUTOFMEMORY
;
1231 switch (desc
->resource_type
)
1233 case WINED3D_RTYPE_TEXTURE
:
1234 hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1237 case WINED3D_RTYPE_VOLUME_TEXTURE
:
1238 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1241 case WINED3D_RTYPE_CUBE_TEXTURE
:
1242 hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1246 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1247 hr
= WINED3DERR_INVALIDCALL
;
1253 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1254 HeapFree(GetProcessHeap(), 0, object
);
1258 TRACE("Created texture %p.\n", object
);