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
, DWORD surface_flags
,
32 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
33 const struct wined3d_resource_ops
*resource_ops
)
35 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
38 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
39 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
40 "surface_flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
41 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
42 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
43 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
44 surface_flags
, device
, parent
, parent_ops
, resource_ops
);
46 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BLOCKS_NO_VERIFY
)) == WINED3DFMT_FLAG_BLOCKS
)
48 UINT width_mask
= format
->block_width
- 1;
49 UINT height_mask
= format
->block_height
- 1;
50 if (desc
->width
& width_mask
|| desc
->height
& height_mask
)
51 return WINED3DERR_INVALIDCALL
;
54 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
55 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
56 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
58 static unsigned int once
;
60 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
61 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
62 && !(format
->flags
& WINED3DFMT_FLAG_TEXTURE
) && !once
++)
63 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
65 WARN("Failed to initialize resource, returning %#x\n", hr
);
68 wined3d_resource_update_draw_binding(&texture
->resource
);
70 texture
->texture_ops
= texture_ops
;
71 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
72 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
73 if (!texture
->sub_resources
)
75 ERR("Failed to allocate sub-resource array.\n");
76 resource_cleanup(&texture
->resource
);
80 texture
->layer_count
= layer_count
;
81 texture
->level_count
= level_count
;
82 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
84 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
85 if (surface_flags
& WINED3D_SURFACE_PIN_SYSMEM
)
86 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
88 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
90 texture
->min_mip_lookup
= minMipLookup
;
91 texture
->mag_lookup
= magLookup
;
95 texture
->min_mip_lookup
= minMipLookup_noFilter
;
96 texture
->mag_lookup
= magLookup_noFilter
;
102 /* A GL context is provided by the caller */
103 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
105 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
109 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
111 struct wined3d_device
*device
= texture
->resource
.device
;
112 struct wined3d_context
*context
= NULL
;
114 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
116 context
= context_acquire(device
, NULL
);
119 if (texture
->texture_rgb
.name
)
120 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
122 if (texture
->texture_srgb
.name
)
123 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
125 if (context
) context_release(context
);
127 wined3d_texture_set_dirty(texture
);
129 resource_unload(&texture
->resource
);
132 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
134 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
137 TRACE("texture %p.\n", texture
);
139 for (i
= 0; i
< sub_count
; ++i
)
141 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
144 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
147 wined3d_texture_unload_gl_texture(texture
);
148 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
149 resource_cleanup(&texture
->resource
);
152 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
154 texture
->swapchain
= swapchain
;
155 wined3d_resource_update_draw_binding(&texture
->resource
);
158 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
160 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
163 /* Context activation is done by the caller. */
164 void wined3d_texture_bind(struct wined3d_texture
*texture
,
165 struct wined3d_context
*context
, BOOL srgb
)
167 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
168 struct gl_texture
*gl_tex
;
171 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
173 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
176 /* sRGB mode cache for preload() calls outside drawprim. */
178 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
180 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
182 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
183 target
= texture
->target
;
187 context_bind_texture(context
, target
, gl_tex
->name
);
191 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
192 checkGLcall("glGenTextures");
193 TRACE("Generated texture %d.\n", gl_tex
->name
);
197 ERR("Failed to generate a texture name.\n");
201 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
203 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
205 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
208 /* Initialise the state of the texture object to the OpenGL defaults, not
209 * the wined3d defaults. */
210 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_WRAP
;
211 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_WRAP
;
212 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3D_TADDRESS_WRAP
;
213 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
214 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_LINEAR
;
215 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
216 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
217 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
218 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
219 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
220 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
222 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
223 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
224 wined3d_texture_set_dirty(texture
);
226 context_bind_texture(context
, target
, gl_tex
->name
);
228 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
230 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
231 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
234 /* For a new texture we have to set the texture levels after binding the
235 * texture. Beware that texture rectangles do not support mipmapping, but
236 * set the maxmiplevel if we're relying on the partial
237 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
238 * (I.e., do not care about cond_np2 here, just look for
239 * GL_TEXTURE_RECTANGLE_ARB.) */
240 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
242 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
243 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
244 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
247 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
249 /* Cubemaps are always set to clamp, regardless of the sampler state. */
250 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
251 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
252 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
255 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
257 /* Conditinal non power of two textures use a different clamping
258 * default. If we're using the GL_WINE_normalized_texrect partial
259 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
260 * has the address mode set to repeat - something that prevents us
261 * from hitting the accelerated codepath. Thus manually set the GL
262 * state. The same applies to filtering. Even if the texture has only
263 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
264 * fallback on macos. */
265 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
266 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
267 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
268 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
269 checkGLcall("glTexParameteri");
270 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_CLAMP
;
271 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_CLAMP
;
272 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_POINT
;
273 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
;
274 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_NONE
;
278 /* Context activation is done by the caller. */
279 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
280 struct wined3d_context
*context
, BOOL srgb
)
282 DWORD active_sampler
;
284 /* We don't need a specific texture unit, but after binding the texture
285 * the current unit is dirty. Read the unit back instead of switching to
286 * 0, this avoids messing around with the state manager's GL states. The
287 * current texture unit should always be a valid one.
289 * To be more specific, this is tricky because we can implicitly be
290 * called from sampler() in state.c. This means we can't touch anything
291 * other than whatever happens to be the currently active texture, or we
292 * would risk marking already applied sampler states dirty again. */
293 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
294 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
295 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
297 wined3d_texture_bind(texture
, context
, srgb
);
300 /* Context activation is done by the caller. */
301 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
302 enum wined3d_texture_address d3d_wrap
, GLenum param
, BOOL cond_np2
)
306 if (d3d_wrap
< WINED3D_TADDRESS_WRAP
|| d3d_wrap
> WINED3D_TADDRESS_MIRROR_ONCE
)
308 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap
);
312 /* Cubemaps are always set to clamp, regardless of the sampler state. */
313 if (target
== GL_TEXTURE_CUBE_MAP_ARB
314 || (cond_np2
&& d3d_wrap
== WINED3D_TADDRESS_WRAP
))
315 gl_wrap
= GL_CLAMP_TO_EDGE
;
317 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3D_TADDRESS_WRAP
];
319 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
320 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, param
, gl_wrap
);
321 checkGLcall("glTexParameteri(target, param, gl_wrap)");
324 /* Context activation is done by the caller (state handler). */
325 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
326 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
327 const struct wined3d_gl_info
*gl_info
)
329 BOOL cond_np2
= texture
->flags
& WINED3D_TEXTURE_COND_NP2
;
330 GLenum target
= texture
->target
;
331 struct gl_texture
*gl_tex
;
335 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
337 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
339 /* This function relies on the correct texture being bound and loaded. */
341 if (sampler_states
[WINED3D_SAMP_ADDRESS_U
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
343 state
= sampler_states
[WINED3D_SAMP_ADDRESS_U
];
344 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
345 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
348 if (sampler_states
[WINED3D_SAMP_ADDRESS_V
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
350 state
= sampler_states
[WINED3D_SAMP_ADDRESS_V
];
351 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
352 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
355 if (sampler_states
[WINED3D_SAMP_ADDRESS_W
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
357 state
= sampler_states
[WINED3D_SAMP_ADDRESS_W
];
358 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
359 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
362 if (sampler_states
[WINED3D_SAMP_BORDER_COLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
366 state
= sampler_states
[WINED3D_SAMP_BORDER_COLOR
];
367 D3DCOLORTOGLFLOAT4(state
, col
);
368 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
369 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
370 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
371 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
374 if (sampler_states
[WINED3D_SAMP_MAG_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
378 state
= sampler_states
[WINED3D_SAMP_MAG_FILTER
];
379 if (state
> WINED3D_TEXF_ANISOTROPIC
)
380 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
382 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
383 min(max(state
, WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
));
384 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
385 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
387 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
390 if ((sampler_states
[WINED3D_SAMP_MIN_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
391 || sampler_states
[WINED3D_SAMP_MIP_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
392 || sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
396 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3D_SAMP_MIP_FILTER
];
397 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3D_SAMP_MIN_FILTER
];
398 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
];
400 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3D_TEXF_ANISOTROPIC
401 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3D_TEXF_ANISOTROPIC
)
403 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
404 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
405 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
407 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
408 min(max(sampler_states
[WINED3D_SAMP_MIN_FILTER
], WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
),
409 min(max(sampler_states
[WINED3D_SAMP_MIP_FILTER
], WINED3D_TEXF_NONE
), WINED3D_TEXF_LINEAR
));
411 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
412 sampler_states
[WINED3D_SAMP_MIN_FILTER
],
413 sampler_states
[WINED3D_SAMP_MIP_FILTER
], gl_value
);
414 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
415 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
419 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3D_TEXF_NONE
)
420 gl_value
= texture
->lod
;
421 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
422 gl_value
= texture
->level_count
- 1;
423 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
424 /* texture->lod is already clamped in the setter. */
425 gl_value
= texture
->lod
;
427 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
429 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
430 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
431 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
432 * corresponds to GL_TEXTURE_BASE_LEVEL. */
433 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
437 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3D_TEXF_ANISOTROPIC
438 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3D_TEXF_ANISOTROPIC
439 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3D_TEXF_ANISOTROPIC
)
443 aniso
= sampler_states
[WINED3D_SAMP_MAX_ANISOTROPY
];
445 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
447 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
449 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
450 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
454 WARN("Anisotropic filtering not supported.\n");
456 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
459 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
460 if (sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
462 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
463 sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
464 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
465 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
];
468 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
469 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
471 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
473 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
474 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
475 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
476 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
480 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
481 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
482 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
487 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
491 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
493 if (texture
->swapchain
)
494 return wined3d_swapchain_incref(texture
->swapchain
);
496 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
497 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
502 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
506 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
508 if (texture
->swapchain
)
509 return wined3d_swapchain_decref(texture
->swapchain
);
511 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
512 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
516 wined3d_texture_cleanup(texture
);
517 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
518 HeapFree(GetProcessHeap(), 0, texture
);
524 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
526 TRACE("texture %p.\n", texture
);
528 return &texture
->resource
;
531 /* Context activation is done by the caller */
532 void wined3d_texture_load(struct wined3d_texture
*texture
,
533 struct wined3d_context
*context
, BOOL srgb
)
535 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
536 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
540 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
542 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
546 flag
= WINED3D_TEXTURE_SRGB_VALID
;
548 flag
= WINED3D_TEXTURE_RGB_VALID
;
550 if (texture
->flags
& flag
)
552 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
556 /* Reload the surfaces if the texture is marked dirty. */
557 for (i
= 0; i
< sub_count
; ++i
)
559 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
], context
, srgb
);
561 texture
->flags
|= flag
;
564 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
566 struct wined3d_context
*context
;
567 context
= context_acquire(texture
->resource
.device
, NULL
);
568 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
569 context_release(context
);
572 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
574 TRACE("texture %p.\n", texture
);
576 return texture
->resource
.parent
;
579 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
581 DWORD old
= texture
->lod
;
583 TRACE("texture %p, lod %u.\n", texture
, lod
);
585 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
586 * textures. The call always returns 0, and GetLOD always returns 0. */
587 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
589 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
593 if (lod
>= texture
->level_count
)
594 lod
= texture
->level_count
- 1;
596 if (texture
->lod
!= lod
)
600 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
601 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
602 if (texture
->resource
.bind_count
)
603 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
609 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
611 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
616 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
618 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
620 return texture
->level_count
;
623 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
624 enum wined3d_texture_filter_type filter_type
)
626 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
628 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
630 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
631 return WINED3DERR_INVALIDCALL
;
634 texture
->filter_type
= filter_type
;
639 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
641 TRACE("texture %p.\n", texture
);
643 return texture
->filter_type
;
646 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
647 DWORD flags
, const struct wined3d_color_key
*color_key
)
649 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
651 if (flags
& WINEDDCKEY_COLORSPACE
)
653 FIXME("Unhandled flags %#x.\n", flags
);
654 return WINED3DERR_INVALIDCALL
;
659 switch (flags
& ~WINEDDCKEY_COLORSPACE
)
661 case WINEDDCKEY_DESTBLT
:
662 texture
->dst_blt_color_key
= *color_key
;
663 texture
->color_key_flags
|= WINEDDSD_CKDESTBLT
;
666 case WINEDDCKEY_DESTOVERLAY
:
667 texture
->dst_overlay_color_key
= *color_key
;
668 texture
->color_key_flags
|= WINEDDSD_CKDESTOVERLAY
;
671 case WINEDDCKEY_SRCOVERLAY
:
672 texture
->src_overlay_color_key
= *color_key
;
673 texture
->color_key_flags
|= WINEDDSD_CKSRCOVERLAY
;
676 case WINEDDCKEY_SRCBLT
:
677 texture
->src_blt_color_key
= *color_key
;
678 texture
->color_key_flags
|= WINEDDSD_CKSRCBLT
;
684 switch (flags
& ~WINEDDCKEY_COLORSPACE
)
686 case WINEDDCKEY_DESTBLT
:
687 texture
->color_key_flags
&= ~WINEDDSD_CKDESTBLT
;
690 case WINEDDCKEY_DESTOVERLAY
:
691 texture
->color_key_flags
&= ~WINEDDSD_CKDESTOVERLAY
;
694 case WINEDDCKEY_SRCOVERLAY
:
695 texture
->color_key_flags
&= ~WINEDDSD_CKSRCOVERLAY
;
698 case WINEDDCKEY_SRCBLT
:
699 texture
->color_key_flags
&= ~WINEDDSD_CKSRCBLT
;
707 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
708 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
709 UINT multisample_quality
, void *mem
, UINT pitch
)
711 struct wined3d_device
*device
= texture
->resource
.device
;
712 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
713 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
714 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
715 struct wined3d_surface
*surface
;
717 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
718 "mem %p, pitch %u.\n",
719 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_type
, mem
, pitch
);
722 return WINED3DERR_INVALIDCALL
;
724 if (texture
->level_count
* texture
->layer_count
> 1)
726 WARN("Texture has multiple sub-resources, not supported.\n");
727 return WINED3DERR_INVALIDCALL
;
730 if (texture
->resource
.type
== WINED3D_RTYPE_VOLUME_TEXTURE
)
732 WARN("Not supported on volume textures.\n");
733 return WINED3DERR_INVALIDCALL
;
736 surface
= surface_from_resource(texture
->sub_resources
[0]);
737 if (surface
->resource
.map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
739 WARN("Surface is mapped or the DC is in use.\n");
740 return WINED3DERR_INVALIDCALL
;
743 if (device
->d3d_initialized
)
744 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
746 texture
->resource
.format
= format
;
747 texture
->resource
.multisample_type
= multisample_type
;
748 texture
->resource
.multisample_quality
= multisample_quality
;
749 texture
->resource
.width
= width
;
750 texture
->resource
.height
= height
;
752 return wined3d_surface_update_desc(surface
, gl_info
, mem
, pitch
);
755 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
757 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
759 if (texture
->flags
& alloc_flag
)
762 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
763 texture
->flags
|= alloc_flag
;
766 void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
768 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
771 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
| WINED3D_TEXTURE_CONVERTED
);
772 for (i
= 0; i
< sub_count
; ++i
)
774 texture
->texture_ops
->texture_sub_resource_invalidate_location(texture
->sub_resources
[i
],
775 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
779 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
781 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
782 FIXME("texture %p stub!\n", texture
);
785 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
786 UINT sub_resource_idx
)
788 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
790 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
792 if (sub_resource_idx
>= sub_count
)
794 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
798 return texture
->sub_resources
[sub_resource_idx
];
801 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
802 UINT layer
, const struct wined3d_box
*dirty_region
)
804 struct wined3d_resource
*sub_resource
;
806 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
808 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
810 WARN("Failed to get sub-resource.\n");
811 return WINED3DERR_INVALIDCALL
;
814 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
819 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
820 struct wined3d_context
*context
, BOOL srgb
)
822 surface_load(surface_from_resource(sub_resource
), srgb
);
825 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
826 const struct wined3d_box
*dirty_region
)
828 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
830 surface_prepare_map_memory(surface
);
831 surface_load_location(surface
, surface
->resource
.map_binding
);
832 surface_invalidate_location(surface
, ~surface
->resource
.map_binding
);
835 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
837 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
839 wined3d_surface_destroy(surface
);
842 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
844 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
846 surface_invalidate_location(surface
, location
);
849 /* Context activation is done by the caller. */
850 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
852 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
853 const struct wined3d_format
*format
= texture
->resource
.format
;
854 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
855 const struct wined3d_color_key_conversion
*conversion
;
859 TRACE("texture %p, format %s.\n", texture
, debug_d3dformat(format
->id
));
863 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
865 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
867 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
868 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
869 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
872 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
875 internal
= format
->glGammaInternal
;
876 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
877 && wined3d_resource_is_offscreen(&texture
->resource
))
878 internal
= format
->rtInternal
;
880 internal
= format
->glInternal
;
883 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
885 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
887 for (i
= 0; i
< sub_count
; ++i
)
889 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
890 GLsizei height
= surface
->pow2Height
;
891 GLsizei width
= surface
->pow2Width
;
892 const BYTE
*mem
= NULL
;
894 if (format
->flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
896 height
*= format
->height_scale
.numerator
;
897 height
/= format
->height_scale
.denominator
;
900 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
901 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
903 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
905 if (surface
->flags
& (SFLAG_NONPOW2
| SFLAG_DIBSECTION
)
906 || texture
->flags
& WINED3D_TEXTURE_CONVERTED
907 || !surface
->resource
.heap_memory
)
909 /* In some cases we want to disable client storage.
910 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
911 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
912 * WINED3D_TEXTURE_CONVERTED: The conversion destination memory is freed after loading the surface
913 * heap_memory == NULL: Not defined in the extension. Seems to disable client storage effectively
915 surface
->flags
&= ~SFLAG_CLIENT
;
919 surface
->flags
|= SFLAG_CLIENT
;
920 mem
= surface
->resource
.heap_memory
;
922 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
923 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
927 if (format
->flags
& WINED3DFMT_FLAG_COMPRESSED
&& mem
)
929 GL_EXTCALL(glCompressedTexImage2DARB(surface
->texture_target
, surface
->texture_level
,
930 internal
, width
, height
, 0, surface
->resource
.size
, mem
));
931 checkGLcall("glCompressedTexImage2DARB");
935 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
936 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, mem
);
937 checkGLcall("glTexImage2D");
942 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
943 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
948 static const struct wined3d_texture_ops texture2d_ops
=
950 texture2d_sub_resource_load
,
951 texture2d_sub_resource_add_dirty_region
,
952 texture2d_sub_resource_cleanup
,
953 texture2d_sub_resource_invalidate_location
,
954 texture2d_prepare_texture
,
957 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
959 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
962 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
964 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
967 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
969 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
970 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
973 TRACE("texture %p.\n", texture
);
975 for (i
= 0; i
< sub_count
; ++i
)
977 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
979 sub_resource
->resource_ops
->resource_unload(sub_resource
);
982 wined3d_texture_force_reload(texture
);
983 wined3d_texture_unload_gl_texture(texture
);
986 static const struct wined3d_resource_ops texture_resource_ops
=
988 texture_resource_incref
,
989 texture_resource_decref
,
990 wined3d_texture_unload
,
993 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
994 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
995 const struct wined3d_parent_ops
*parent_ops
)
997 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
998 struct wined3d_resource_desc surface_desc
;
1002 /* TODO: It should only be possible to create textures for formats
1003 * that are reported as supported. */
1004 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1006 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1007 return WINED3DERR_INVALIDCALL
;
1010 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
1012 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
1013 return WINED3DERR_INVALIDCALL
;
1016 /* Calculate levels for mip mapping */
1017 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1019 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1021 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1022 return WINED3DERR_INVALIDCALL
;
1027 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1028 return WINED3DERR_INVALIDCALL
;
1035 levels
= wined3d_log2i(desc
->width
) + 1;
1036 TRACE("Calculated levels = %u.\n", levels
);
1039 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1041 UINT pow2_edge_length
= 1;
1042 while (pow2_edge_length
< desc
->width
)
1043 pow2_edge_length
<<= 1;
1045 if (desc
->width
!= pow2_edge_length
)
1047 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1049 /* SCRATCH textures cannot be used for texturing */
1050 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
1054 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
1055 return WINED3DERR_INVALIDCALL
;
1060 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
, desc
,
1061 surface_flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1063 WARN("Failed to initialize texture, returning %#x\n", hr
);
1067 texture
->pow2_matrix
[0] = 1.0f
;
1068 texture
->pow2_matrix
[5] = 1.0f
;
1069 texture
->pow2_matrix
[10] = 1.0f
;
1070 texture
->pow2_matrix
[15] = 1.0f
;
1071 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1073 /* Generate all the surfaces. */
1074 surface_desc
= *desc
;
1075 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1076 for (i
= 0; i
< texture
->level_count
; ++i
)
1078 /* Create the 6 faces. */
1079 for (j
= 0; j
< 6; ++j
)
1081 static const GLenum cube_targets
[6] =
1083 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1084 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1085 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1086 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1087 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1088 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1090 UINT idx
= j
* texture
->level_count
+ i
;
1091 struct wined3d_surface
*surface
;
1093 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1094 cube_targets
[j
], i
, j
, surface_flags
, &surface
)))
1096 WARN("Failed to create surface, hr %#x.\n", hr
);
1097 wined3d_texture_cleanup(texture
);
1101 texture
->sub_resources
[idx
] = &surface
->resource
;
1102 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1104 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1105 surface_desc
.height
= surface_desc
.width
;
1111 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1112 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
1113 const struct wined3d_parent_ops
*parent_ops
)
1115 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1116 struct wined3d_resource_desc surface_desc
;
1117 UINT pow2_width
, pow2_height
;
1121 /* TODO: It should only be possible to create textures for formats
1122 * that are reported as supported. */
1123 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1125 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1126 return WINED3DERR_INVALIDCALL
;
1129 /* Non-power2 support. */
1130 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1132 pow2_width
= desc
->width
;
1133 pow2_height
= desc
->height
;
1137 /* Find the nearest pow2 match. */
1138 pow2_width
= pow2_height
= 1;
1139 while (pow2_width
< desc
->width
)
1141 while (pow2_height
< desc
->height
)
1144 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
1146 /* levels == 0 returns an error as well */
1149 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1151 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1155 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1156 return WINED3DERR_INVALIDCALL
;
1162 /* Calculate levels for mip mapping. */
1163 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1165 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1167 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1168 return WINED3DERR_INVALIDCALL
;
1173 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
1174 return WINED3DERR_INVALIDCALL
;
1181 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
1182 TRACE("Calculated levels = %u.\n", levels
);
1185 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
, desc
,
1186 surface_flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1188 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1192 /* Precalculated scaling for 'faked' non power of two texture coords. */
1193 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
1194 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1196 texture
->pow2_matrix
[0] = 1.0f
;
1197 texture
->pow2_matrix
[5] = 1.0f
;
1198 texture
->pow2_matrix
[10] = 1.0f
;
1199 texture
->pow2_matrix
[15] = 1.0f
;
1200 texture
->target
= GL_TEXTURE_2D
;
1201 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1202 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1204 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
1205 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1207 texture
->pow2_matrix
[0] = (float)desc
->width
;
1208 texture
->pow2_matrix
[5] = (float)desc
->height
;
1209 texture
->pow2_matrix
[10] = 1.0f
;
1210 texture
->pow2_matrix
[15] = 1.0f
;
1211 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1212 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1213 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1215 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
1216 texture
->min_mip_lookup
= minMipLookup_noMip
;
1218 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1222 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
1224 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1225 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1226 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1230 texture
->pow2_matrix
[0] = 1.0f
;
1231 texture
->pow2_matrix
[5] = 1.0f
;
1234 texture
->pow2_matrix
[10] = 1.0f
;
1235 texture
->pow2_matrix
[15] = 1.0f
;
1236 texture
->target
= GL_TEXTURE_2D
;
1238 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1240 /* Generate all the surfaces. */
1241 surface_desc
= *desc
;
1242 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1243 for (i
= 0; i
< texture
->level_count
; ++i
)
1245 struct wined3d_surface
*surface
;
1247 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1248 texture
->target
, i
, 0, surface_flags
, &surface
)))
1250 WARN("Failed to create surface, hr %#x.\n", hr
);
1251 wined3d_texture_cleanup(texture
);
1255 texture
->sub_resources
[i
] = &surface
->resource
;
1256 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1257 /* Calculate the next mipmap level. */
1258 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1259 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1265 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1266 struct wined3d_context
*context
, BOOL srgb
)
1268 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1271 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1272 const struct wined3d_box
*dirty_region
)
1274 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1277 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1279 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1281 wined3d_volume_destroy(volume
);
1284 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1286 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1288 wined3d_volume_invalidate_location(volume
, location
);
1291 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1293 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1294 const struct wined3d_format
*format
= texture
->resource
.format
;
1295 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1298 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1300 for (i
= 0; i
< sub_count
; ++i
)
1302 struct wined3d_volume
*volume
= volume_from_resource(texture
->sub_resources
[i
]);
1305 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
] && !format
->convert
1306 && volume_prepare_system_memory(volume
))
1308 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume
);
1309 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1310 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1311 mem
= volume
->resource
.heap_memory
;
1312 volume
->flags
|= WINED3D_VFLAG_CLIENT_STORAGE
;
1315 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
,
1316 srgb
? format
->glGammaInternal
: format
->glInternal
,
1317 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
1318 0, format
->glFormat
, format
->glType
, mem
));
1319 checkGLcall("glTexImage3D");
1323 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
1324 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1329 static const struct wined3d_texture_ops texture3d_ops
=
1331 texture3d_sub_resource_load
,
1332 texture3d_sub_resource_add_dirty_region
,
1333 texture3d_sub_resource_cleanup
,
1334 texture3d_sub_resource_invalidate_location
,
1335 texture3d_prepare_texture
,
1338 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1339 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1341 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1342 struct wined3d_resource_desc volume_desc
;
1346 /* TODO: It should only be possible to create textures for formats
1347 * that are reported as supported. */
1348 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1350 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1351 return WINED3DERR_INVALIDCALL
;
1354 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1356 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1357 return WINED3DERR_INVALIDCALL
;
1360 /* Calculate levels for mip mapping. */
1361 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1363 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1365 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1366 return WINED3DERR_INVALIDCALL
;
1371 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1372 return WINED3DERR_INVALIDCALL
;
1379 levels
= wined3d_log2i(max(max(desc
->width
, desc
->height
), desc
->depth
)) + 1;
1380 TRACE("Calculated levels = %u.\n", levels
);
1383 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1385 UINT pow2_w
, pow2_h
, pow2_d
;
1387 while (pow2_w
< desc
->width
)
1390 while (pow2_h
< desc
->height
)
1393 while (pow2_d
< desc
->depth
)
1396 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1398 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1400 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1404 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1405 desc
->width
, desc
->height
, desc
->depth
);
1406 return WINED3DERR_INVALIDCALL
;
1411 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
1412 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
1414 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1418 texture
->pow2_matrix
[0] = 1.0f
;
1419 texture
->pow2_matrix
[5] = 1.0f
;
1420 texture
->pow2_matrix
[10] = 1.0f
;
1421 texture
->pow2_matrix
[15] = 1.0f
;
1422 texture
->target
= GL_TEXTURE_3D
;
1424 /* Generate all the surfaces. */
1425 volume_desc
= *desc
;
1426 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1427 for (i
= 0; i
< texture
->level_count
; ++i
)
1429 struct wined3d_volume
*volume
;
1431 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1433 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1434 wined3d_texture_cleanup(texture
);
1438 texture
->sub_resources
[i
] = &volume
->resource
;
1440 /* Calculate the next mipmap level. */
1441 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1442 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1443 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1449 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1450 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1451 struct wined3d_texture
**texture
)
1453 struct wined3d_texture
*object
;
1456 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1457 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1459 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1460 return E_OUTOFMEMORY
;
1462 switch (desc
->resource_type
)
1464 case WINED3D_RTYPE_TEXTURE
:
1465 hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1468 case WINED3D_RTYPE_VOLUME_TEXTURE
:
1469 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1472 case WINED3D_RTYPE_CUBE_TEXTURE
:
1473 hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1477 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1478 hr
= WINED3DERR_INVALIDCALL
;
1484 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1485 HeapFree(GetProcessHeap(), 0, object
);
1489 TRACE("Created texture %p.\n", object
);