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 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
705 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
706 UINT multisample_quality
, void *mem
, UINT pitch
)
708 struct wined3d_device
*device
= texture
->resource
.device
;
709 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
710 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
711 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
712 struct wined3d_surface
*surface
;
714 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
715 "mem %p, pitch %u.\n",
716 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_type
, mem
, pitch
);
719 return WINED3DERR_INVALIDCALL
;
721 if (texture
->level_count
* texture
->layer_count
> 1)
723 WARN("Texture has multiple sub-resources, not supported.\n");
724 return WINED3DERR_INVALIDCALL
;
727 if (texture
->resource
.type
== WINED3D_RTYPE_VOLUME_TEXTURE
)
729 WARN("Not supported on volume textures.\n");
730 return WINED3DERR_INVALIDCALL
;
733 surface
= surface_from_resource(texture
->sub_resources
[0]);
734 if (surface
->resource
.map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
736 WARN("Surface is mapped or the DC is in use.\n");
737 return WINED3DERR_INVALIDCALL
;
740 if (device
->d3d_initialized
)
741 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
743 texture
->resource
.format
= format
;
744 texture
->resource
.multisample_type
= multisample_type
;
745 texture
->resource
.multisample_quality
= multisample_quality
;
746 texture
->resource
.width
= width
;
747 texture
->resource
.height
= height
;
749 return wined3d_surface_update_desc(surface
, gl_info
, mem
, pitch
);
752 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
754 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
755 FIXME("texture %p stub!\n", texture
);
758 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
759 UINT sub_resource_idx
)
761 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
763 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
765 if (sub_resource_idx
>= sub_count
)
767 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
771 return texture
->sub_resources
[sub_resource_idx
];
774 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
775 UINT layer
, const struct wined3d_box
*dirty_region
)
777 struct wined3d_resource
*sub_resource
;
779 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
781 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
783 WARN("Failed to get sub-resource.\n");
784 return WINED3DERR_INVALIDCALL
;
787 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
792 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
793 struct wined3d_context
*context
, BOOL srgb
)
795 surface_load(surface_from_resource(sub_resource
), srgb
);
798 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
799 const struct wined3d_box
*dirty_region
)
801 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
803 surface_prepare_map_memory(surface
);
804 surface_load_location(surface
, surface
->resource
.map_binding
);
805 surface_invalidate_location(surface
, ~surface
->resource
.map_binding
);
808 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
810 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
812 wined3d_surface_destroy(surface
);
815 static const struct wined3d_texture_ops texture2d_ops
=
817 texture2d_sub_resource_load
,
818 texture2d_sub_resource_add_dirty_region
,
819 texture2d_sub_resource_cleanup
,
822 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
824 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
827 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
829 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
832 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
834 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
835 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
838 TRACE("texture %p.\n", texture
);
840 for (i
= 0; i
< sub_count
; ++i
)
842 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
844 sub_resource
->resource_ops
->resource_unload(sub_resource
);
847 wined3d_texture_unload_gl_texture(texture
);
850 static const struct wined3d_resource_ops texture_resource_ops
=
852 texture_resource_incref
,
853 texture_resource_decref
,
854 wined3d_texture_unload
,
857 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
858 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
859 const struct wined3d_parent_ops
*parent_ops
)
861 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
862 struct wined3d_resource_desc surface_desc
;
866 /* TODO: It should only be possible to create textures for formats
867 * that are reported as supported. */
868 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
870 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
871 return WINED3DERR_INVALIDCALL
;
874 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
876 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
877 return WINED3DERR_INVALIDCALL
;
880 /* Calculate levels for mip mapping */
881 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
883 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
885 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
886 return WINED3DERR_INVALIDCALL
;
891 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
892 return WINED3DERR_INVALIDCALL
;
899 levels
= wined3d_log2i(desc
->width
) + 1;
900 TRACE("Calculated levels = %u.\n", levels
);
903 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
905 UINT pow2_edge_length
= 1;
906 while (pow2_edge_length
< desc
->width
)
907 pow2_edge_length
<<= 1;
909 if (desc
->width
!= pow2_edge_length
)
911 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
913 /* SCRATCH textures cannot be used for texturing */
914 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
918 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
919 return WINED3DERR_INVALIDCALL
;
924 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
925 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
927 WARN("Failed to initialize texture, returning %#x\n", hr
);
931 texture
->pow2_matrix
[0] = 1.0f
;
932 texture
->pow2_matrix
[5] = 1.0f
;
933 texture
->pow2_matrix
[10] = 1.0f
;
934 texture
->pow2_matrix
[15] = 1.0f
;
935 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
937 /* Generate all the surfaces. */
938 surface_desc
= *desc
;
939 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
940 for (i
= 0; i
< texture
->level_count
; ++i
)
942 /* Create the 6 faces. */
943 for (j
= 0; j
< 6; ++j
)
945 static const GLenum cube_targets
[6] =
947 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
948 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
949 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
950 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
951 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
952 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
954 UINT idx
= j
* texture
->level_count
+ i
;
955 struct wined3d_surface
*surface
;
957 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
958 cube_targets
[j
], i
, j
, surface_flags
, &surface
)))
960 WARN("Failed to create surface, hr %#x.\n", hr
);
961 wined3d_texture_cleanup(texture
);
965 texture
->sub_resources
[idx
] = &surface
->resource
;
966 TRACE("Created surface level %u @ %p.\n", i
, surface
);
968 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
969 surface_desc
.height
= surface_desc
.width
;
975 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
976 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
977 const struct wined3d_parent_ops
*parent_ops
)
979 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
980 struct wined3d_resource_desc surface_desc
;
981 UINT pow2_width
, pow2_height
;
985 /* TODO: It should only be possible to create textures for formats
986 * that are reported as supported. */
987 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
989 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
990 return WINED3DERR_INVALIDCALL
;
993 /* Non-power2 support. */
994 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
996 pow2_width
= desc
->width
;
997 pow2_height
= desc
->height
;
1001 /* Find the nearest pow2 match. */
1002 pow2_width
= pow2_height
= 1;
1003 while (pow2_width
< desc
->width
)
1005 while (pow2_height
< desc
->height
)
1008 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
1010 /* levels == 0 returns an error as well */
1013 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1015 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1019 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1020 return WINED3DERR_INVALIDCALL
;
1026 /* Calculate levels for mip mapping. */
1027 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1029 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1031 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1032 return WINED3DERR_INVALIDCALL
;
1037 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
1038 return WINED3DERR_INVALIDCALL
;
1045 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
1046 TRACE("Calculated levels = %u.\n", levels
);
1049 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
1050 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1052 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1056 /* Precalculated scaling for 'faked' non power of two texture coords. */
1057 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
1058 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1060 texture
->pow2_matrix
[0] = 1.0f
;
1061 texture
->pow2_matrix
[5] = 1.0f
;
1062 texture
->pow2_matrix
[10] = 1.0f
;
1063 texture
->pow2_matrix
[15] = 1.0f
;
1064 texture
->target
= GL_TEXTURE_2D
;
1065 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1066 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1068 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
1069 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1071 texture
->pow2_matrix
[0] = (float)desc
->width
;
1072 texture
->pow2_matrix
[5] = (float)desc
->height
;
1073 texture
->pow2_matrix
[10] = 1.0f
;
1074 texture
->pow2_matrix
[15] = 1.0f
;
1075 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1076 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1077 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1079 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
1080 texture
->min_mip_lookup
= minMipLookup_noMip
;
1082 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1086 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
1088 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1089 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1090 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1094 texture
->pow2_matrix
[0] = 1.0f
;
1095 texture
->pow2_matrix
[5] = 1.0f
;
1098 texture
->pow2_matrix
[10] = 1.0f
;
1099 texture
->pow2_matrix
[15] = 1.0f
;
1100 texture
->target
= GL_TEXTURE_2D
;
1102 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1104 /* Generate all the surfaces. */
1105 surface_desc
= *desc
;
1106 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1107 for (i
= 0; i
< texture
->level_count
; ++i
)
1109 struct wined3d_surface
*surface
;
1111 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1112 texture
->target
, i
, 0, surface_flags
, &surface
)))
1114 WARN("Failed to create surface, hr %#x.\n", hr
);
1115 wined3d_texture_cleanup(texture
);
1119 texture
->sub_resources
[i
] = &surface
->resource
;
1120 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1121 /* Calculate the next mipmap level. */
1122 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1123 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1129 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1130 struct wined3d_context
*context
, BOOL srgb
)
1132 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1135 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1136 const struct wined3d_box
*dirty_region
)
1138 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1141 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1143 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1145 wined3d_volume_destroy(volume
);
1148 static const struct wined3d_texture_ops texture3d_ops
=
1150 texture3d_sub_resource_load
,
1151 texture3d_sub_resource_add_dirty_region
,
1152 texture3d_sub_resource_cleanup
,
1155 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1156 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1158 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1159 struct wined3d_resource_desc volume_desc
;
1163 /* TODO: It should only be possible to create textures for formats
1164 * that are reported as supported. */
1165 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1167 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1168 return WINED3DERR_INVALIDCALL
;
1171 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1173 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1174 return WINED3DERR_INVALIDCALL
;
1177 /* Calculate levels for mip mapping. */
1178 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1180 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1182 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1183 return WINED3DERR_INVALIDCALL
;
1188 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1189 return WINED3DERR_INVALIDCALL
;
1196 levels
= wined3d_log2i(max(max(desc
->width
, desc
->height
), desc
->depth
)) + 1;
1197 TRACE("Calculated levels = %u.\n", levels
);
1200 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1202 UINT pow2_w
, pow2_h
, pow2_d
;
1204 while (pow2_w
< desc
->width
)
1207 while (pow2_h
< desc
->height
)
1210 while (pow2_d
< desc
->depth
)
1213 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1215 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1217 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1221 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1222 desc
->width
, desc
->height
, desc
->depth
);
1223 return WINED3DERR_INVALIDCALL
;
1228 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1229 desc
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1231 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1235 texture
->pow2_matrix
[0] = 1.0f
;
1236 texture
->pow2_matrix
[5] = 1.0f
;
1237 texture
->pow2_matrix
[10] = 1.0f
;
1238 texture
->pow2_matrix
[15] = 1.0f
;
1239 texture
->target
= GL_TEXTURE_3D
;
1241 /* Generate all the surfaces. */
1242 volume_desc
= *desc
;
1243 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1244 for (i
= 0; i
< texture
->level_count
; ++i
)
1246 struct wined3d_volume
*volume
;
1248 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1250 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1251 wined3d_texture_cleanup(texture
);
1255 texture
->sub_resources
[i
] = &volume
->resource
;
1257 /* Calculate the next mipmap level. */
1258 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1259 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1260 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1266 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1267 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1268 struct wined3d_texture
**texture
)
1270 struct wined3d_texture
*object
;
1273 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1274 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1276 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1277 return E_OUTOFMEMORY
;
1279 switch (desc
->resource_type
)
1281 case WINED3D_RTYPE_TEXTURE
:
1282 hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1285 case WINED3D_RTYPE_VOLUME_TEXTURE
:
1286 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1289 case WINED3D_RTYPE_CUBE_TEXTURE
:
1290 hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1294 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1295 hr
= WINED3DERR_INVALIDCALL
;
1301 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1302 HeapFree(GetProcessHeap(), 0, object
);
1306 TRACE("Created texture %p.\n", object
);