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
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture
);
27 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
28 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, struct wined3d_device
*device
,
29 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_resource_ops
*resource_ops
)
31 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
34 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
35 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
36 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
37 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
38 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
39 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
40 device
, parent
, parent_ops
, resource_ops
);
42 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
43 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
44 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
46 WARN("Failed to initialize resource, returning %#x\n", hr
);
50 texture
->texture_ops
= texture_ops
;
51 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
52 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
53 if (!texture
->sub_resources
)
55 ERR("Failed to allocate sub-resource array.\n");
56 resource_cleanup(&texture
->resource
);
60 texture
->layer_count
= layer_count
;
61 texture
->level_count
= level_count
;
62 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
64 texture
->texture_rgb
.dirty
= TRUE
;
65 texture
->texture_srgb
.dirty
= TRUE
;
66 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
;
68 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
70 texture
->min_mip_lookup
= minMipLookup
;
71 texture
->mag_lookup
= magLookup
;
75 texture
->min_mip_lookup
= minMipLookup_noFilter
;
76 texture
->mag_lookup
= magLookup_noFilter
;
82 /* A GL context is provided by the caller */
83 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
85 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
89 static void wined3d_texture_unload(struct wined3d_texture
*texture
)
91 struct wined3d_device
*device
= texture
->resource
.device
;
92 struct wined3d_context
*context
= NULL
;
94 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
96 context
= context_acquire(device
, NULL
);
99 if (texture
->texture_rgb
.name
)
100 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
102 if (texture
->texture_srgb
.name
)
103 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
105 if (context
) context_release(context
);
107 wined3d_texture_set_dirty(texture
, TRUE
);
109 resource_unload(&texture
->resource
);
112 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
114 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
117 TRACE("texture %p.\n", texture
);
119 for (i
= 0; i
< sub_count
; ++i
)
121 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
124 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
127 wined3d_texture_unload(texture
);
128 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
129 resource_cleanup(&texture
->resource
);
132 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
, BOOL dirty
)
134 texture
->texture_rgb
.dirty
= dirty
;
135 texture
->texture_srgb
.dirty
= dirty
;
138 /* Context activation is done by the caller. */
139 static HRESULT
wined3d_texture_bind(struct wined3d_texture
*texture
,
140 struct wined3d_context
*context
, BOOL srgb
, BOOL
*set_surface_desc
)
142 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
143 struct gl_texture
*gl_tex
;
144 BOOL new_texture
= FALSE
;
145 HRESULT hr
= WINED3D_OK
;
148 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture
, context
, srgb
, set_surface_desc
);
150 /* sRGB mode cache for preload() calls outside drawprim. */
152 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
154 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
156 gl_tex
= wined3d_texture_get_gl_texture(texture
, context
->gl_info
, srgb
);
157 target
= texture
->target
;
159 /* Generate a texture name if we don't already have one. */
162 *set_surface_desc
= TRUE
;
163 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
164 checkGLcall("glGenTextures");
165 TRACE("Generated texture %d.\n", gl_tex
->name
);
166 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
168 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
170 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
172 /* Initialise the state of the texture object to the OpenGL defaults,
173 * not the D3D defaults. */
174 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_WRAP
;
175 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_WRAP
;
176 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3D_TADDRESS_WRAP
;
177 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
178 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_LINEAR
;
179 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
180 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
181 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
182 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
183 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
184 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
186 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
187 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
188 wined3d_texture_set_dirty(texture
, TRUE
);
191 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
193 /* This means double binding the texture at creation, but keeps
194 * the code simpler all in all, and the run-time path free from
195 * additional checks. */
196 context_bind_texture(context
, target
, gl_tex
->name
);
197 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
198 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
203 *set_surface_desc
= FALSE
;
208 context_bind_texture(context
, target
, gl_tex
->name
);
211 /* For a new texture we have to set the texture levels after
212 * binding the texture. Beware that texture rectangles do not
213 * support mipmapping, but set the maxmiplevel if we're relying
214 * on the partial GL_ARB_texture_non_power_of_two emulation with
215 * texture rectangles. (I.e., do not care about cond_np2 here,
216 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
217 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
219 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
220 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
221 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
223 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
225 /* Cubemaps are always set to clamp, regardless of the sampler state. */
226 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
227 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
228 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
234 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
235 hr
= WINED3DERR_INVALIDCALL
;
241 /* Context activation is done by the caller. */
242 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
243 enum wined3d_texture_address d3d_wrap
, GLenum param
, BOOL cond_np2
)
247 if (d3d_wrap
< WINED3D_TADDRESS_WRAP
|| d3d_wrap
> WINED3D_TADDRESS_MIRROR_ONCE
)
249 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap
);
253 /* Cubemaps are always set to clamp, regardless of the sampler state. */
254 if (target
== GL_TEXTURE_CUBE_MAP_ARB
255 || (cond_np2
&& d3d_wrap
== WINED3D_TADDRESS_WRAP
))
256 gl_wrap
= GL_CLAMP_TO_EDGE
;
258 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3D_TADDRESS_WRAP
];
260 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
261 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, param
, gl_wrap
);
262 checkGLcall("glTexParameteri(target, param, gl_wrap)");
265 /* Context activation is done by the caller (state handler). */
266 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
267 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
268 const struct wined3d_gl_info
*gl_info
)
270 BOOL cond_np2
= texture
->flags
& WINED3D_TEXTURE_COND_NP2
;
271 GLenum target
= texture
->target
;
272 struct gl_texture
*gl_tex
;
276 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
278 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
,
279 texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
281 /* This function relies on the correct texture being bound and loaded. */
283 if (sampler_states
[WINED3D_SAMP_ADDRESS_U
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
285 state
= sampler_states
[WINED3D_SAMP_ADDRESS_U
];
286 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
287 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
290 if (sampler_states
[WINED3D_SAMP_ADDRESS_V
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
292 state
= sampler_states
[WINED3D_SAMP_ADDRESS_V
];
293 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
294 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
297 if (sampler_states
[WINED3D_SAMP_ADDRESS_W
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
299 state
= sampler_states
[WINED3D_SAMP_ADDRESS_W
];
300 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
301 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
304 if (sampler_states
[WINED3D_SAMP_BORDER_COLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
308 state
= sampler_states
[WINED3D_SAMP_BORDER_COLOR
];
309 D3DCOLORTOGLFLOAT4(state
, col
);
310 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
311 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
312 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
313 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
316 if (sampler_states
[WINED3D_SAMP_MAG_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
320 state
= sampler_states
[WINED3D_SAMP_MAG_FILTER
];
321 if (state
> WINED3D_TEXF_ANISOTROPIC
)
322 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
324 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
325 min(max(state
, WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
));
326 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
327 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
329 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
332 if ((sampler_states
[WINED3D_SAMP_MIN_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
333 || sampler_states
[WINED3D_SAMP_MIP_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
334 || sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
338 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3D_SAMP_MIP_FILTER
];
339 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3D_SAMP_MIN_FILTER
];
340 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
];
342 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3D_TEXF_ANISOTROPIC
343 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3D_TEXF_ANISOTROPIC
)
345 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
346 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
347 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
349 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
350 min(max(sampler_states
[WINED3D_SAMP_MIN_FILTER
], WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
),
351 min(max(sampler_states
[WINED3D_SAMP_MIP_FILTER
], WINED3D_TEXF_NONE
), WINED3D_TEXF_LINEAR
));
353 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
354 sampler_states
[WINED3D_SAMP_MIN_FILTER
],
355 sampler_states
[WINED3D_SAMP_MIP_FILTER
], gl_value
);
356 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
357 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
361 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3D_TEXF_NONE
)
362 gl_value
= texture
->lod
;
363 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
364 gl_value
= texture
->level_count
- 1;
365 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
366 /* texture->lod is already clamped in the setter. */
367 gl_value
= texture
->lod
;
369 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
371 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
372 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
373 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
374 * corresponds to GL_TEXTURE_BASE_LEVEL. */
375 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
379 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3D_TEXF_ANISOTROPIC
380 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3D_TEXF_ANISOTROPIC
381 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3D_TEXF_ANISOTROPIC
)
385 aniso
= sampler_states
[WINED3D_SAMP_MAX_ANISOTROPY
];
387 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
389 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
391 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
392 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
396 WARN("Anisotropic filtering not supported.\n");
398 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
401 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
402 if (sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
404 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
405 sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
406 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
407 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
];
410 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
411 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
413 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
415 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
416 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
417 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
418 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
422 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
423 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
424 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
429 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
431 ULONG refcount
= InterlockedIncrement(&texture
->resource
.ref
);
433 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
438 /* Do not call while under the GL lock. */
439 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
441 ULONG refcount
= InterlockedDecrement(&texture
->resource
.ref
);
443 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
447 wined3d_texture_cleanup(texture
);
448 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
449 HeapFree(GetProcessHeap(), 0, texture
);
455 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
457 TRACE("texture %p.\n", texture
);
459 return &texture
->resource
;
462 DWORD CDECL
wined3d_texture_set_priority(struct wined3d_texture
*texture
, DWORD priority
)
464 return resource_set_priority(&texture
->resource
, priority
);
467 DWORD CDECL
wined3d_texture_get_priority(const struct wined3d_texture
*texture
)
469 return resource_get_priority(&texture
->resource
);
472 /* Do not call while under the GL lock. */
473 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
475 texture
->texture_ops
->texture_preload(texture
, SRGB_ANY
);
478 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
480 TRACE("texture %p.\n", texture
);
482 return texture
->resource
.parent
;
485 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
487 DWORD old
= texture
->lod
;
489 TRACE("texture %p, lod %u.\n", texture
, lod
);
491 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
492 * textures. The call always returns 0, and GetLOD always returns 0. */
493 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
495 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
499 if (lod
>= texture
->level_count
)
500 lod
= texture
->level_count
- 1;
502 if (texture
->lod
!= lod
)
506 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
507 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
508 if (texture
->resource
.bind_count
)
509 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
515 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
517 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
522 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
524 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
526 return texture
->level_count
;
529 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
530 enum wined3d_texture_filter_type filter_type
)
532 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
534 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
536 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
537 return WINED3DERR_INVALIDCALL
;
540 texture
->filter_type
= filter_type
;
545 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
547 TRACE("texture %p.\n", texture
);
549 return texture
->filter_type
;
552 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
554 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
555 FIXME("texture %p stub!\n", texture
);
558 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
559 UINT sub_resource_idx
)
561 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
563 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
565 if (sub_resource_idx
>= sub_count
)
567 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
571 return texture
->sub_resources
[sub_resource_idx
];
574 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
575 UINT layer
, const struct wined3d_box
*dirty_region
)
577 struct wined3d_resource
*sub_resource
;
579 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
581 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
583 WARN("Failed to get sub-resource.\n");
584 return WINED3DERR_INVALIDCALL
;
587 wined3d_texture_set_dirty(texture
, TRUE
);
588 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
593 /* Context activation is done by the caller. */
594 static HRESULT
texture2d_bind(struct wined3d_texture
*texture
,
595 struct wined3d_context
*context
, BOOL srgb
)
597 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
598 BOOL set_gl_texture_desc
;
601 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
603 hr
= wined3d_texture_bind(texture
, context
, srgb
, &set_gl_texture_desc
);
604 if (set_gl_texture_desc
&& SUCCEEDED(hr
))
606 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
607 BOOL srgb_tex
= !context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
]
608 && (texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
609 struct gl_texture
*gl_tex
;
612 gl_tex
= wined3d_texture_get_gl_texture(texture
, context
->gl_info
, srgb_tex
);
614 for (i
= 0; i
< sub_count
; ++i
)
616 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
617 surface_set_texture_name(surface
, gl_tex
->name
, srgb_tex
);
620 /* Conditinal non power of two textures use a different clamping
621 * default. If we're using the GL_WINE_normalized_texrect partial
622 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
623 * has the address mode set to repeat - something that prevents us
624 * from hitting the accelerated codepath. Thus manually set the GL
625 * state. The same applies to filtering. Even if the texture has only
626 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
627 * fallback on macos. */
628 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
630 GLenum target
= texture
->target
;
632 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
633 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
634 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
635 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
636 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
637 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
638 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
639 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
640 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_CLAMP
;
641 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_CLAMP
;
642 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_POINT
;
643 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
;
644 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_NONE
;
651 static BOOL
texture_srgb_mode(const struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
662 return texture
->flags
& WINED3D_TEXTURE_IS_SRGB
;
666 /* Do not call while under the GL lock. */
667 static void texture2d_preload(struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
669 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
670 struct wined3d_device
*device
= texture
->resource
.device
;
671 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
672 struct wined3d_context
*context
= NULL
;
673 struct gl_texture
*gl_tex
;
677 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
679 srgb_mode
= texture_srgb_mode(texture
, srgb
);
680 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, srgb_mode
);
682 if (!device
->isInDraw
)
684 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
685 * when loading offscreen render targets into the texture. */
686 context
= context_acquire(device
, NULL
);
691 /* Reload the surfaces if the texture is marked dirty. */
692 for (i
= 0; i
< sub_count
; ++i
)
694 surface_load(surface_from_resource(texture
->sub_resources
[i
]), srgb_mode
);
699 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
702 /* No longer dirty. */
703 gl_tex
->dirty
= FALSE
;
705 if (context
) context_release(context
);
708 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
709 const struct wined3d_box
*dirty_region
)
711 surface_add_dirty_rect(surface_from_resource(sub_resource
), dirty_region
);
714 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
716 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
718 /* Clean out the texture name we gave to the surface so that the
719 * surface doesn't try and release it. */
720 surface_set_texture_name(surface
, 0, TRUE
);
721 surface_set_texture_name(surface
, 0, FALSE
);
722 surface_set_texture_target(surface
, 0, 0);
723 surface_set_container(surface
, NULL
);
724 wined3d_surface_decref(surface
);
727 /* Do not call while under the GL lock. */
728 static void texture2d_unload(struct wined3d_resource
*resource
)
730 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
731 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
734 TRACE("texture %p.\n", texture
);
736 for (i
= 0; i
< sub_count
; ++i
)
738 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
739 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
741 sub_resource
->resource_ops
->resource_unload(sub_resource
);
742 surface_set_texture_name(surface
, 0, FALSE
); /* Delete RGB name */
743 surface_set_texture_name(surface
, 0, TRUE
); /* Delete sRGB name */
746 wined3d_texture_unload(texture
);
749 static const struct wined3d_texture_ops texture2d_ops
=
753 texture2d_sub_resource_add_dirty_region
,
754 texture2d_sub_resource_cleanup
,
757 static const struct wined3d_resource_ops texture2d_resource_ops
=
762 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
763 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
764 const struct wined3d_parent_ops
*parent_ops
)
766 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
767 struct wined3d_resource_desc surface_desc
;
771 /* TODO: It should only be possible to create textures for formats
772 * that are reported as supported. */
773 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
775 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
776 return WINED3DERR_INVALIDCALL
;
779 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
781 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
782 return WINED3DERR_INVALIDCALL
;
785 /* Calculate levels for mip mapping */
786 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
788 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
790 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
791 return WINED3DERR_INVALIDCALL
;
796 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
797 return WINED3DERR_INVALIDCALL
;
804 levels
= wined3d_log2i(desc
->width
) + 1;
805 TRACE("Calculated levels = %u.\n", levels
);
808 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
810 UINT pow2_edge_length
= 1;
811 while (pow2_edge_length
< desc
->width
)
812 pow2_edge_length
<<= 1;
814 if (desc
->width
!= pow2_edge_length
)
816 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
818 /* SCRATCH textures cannot be used for texturing */
819 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
823 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
824 return WINED3DERR_INVALIDCALL
;
829 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
830 desc
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
832 WARN("Failed to initialize texture, returning %#x\n", hr
);
836 texture
->pow2_matrix
[0] = 1.0f
;
837 texture
->pow2_matrix
[5] = 1.0f
;
838 texture
->pow2_matrix
[10] = 1.0f
;
839 texture
->pow2_matrix
[15] = 1.0f
;
840 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
842 /* Generate all the surfaces. */
843 surface_desc
= *desc
;
844 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
845 for (i
= 0; i
< texture
->level_count
; ++i
)
847 /* Create the 6 faces. */
848 for (j
= 0; j
< 6; ++j
)
850 static const GLenum cube_targets
[6] =
852 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
853 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
854 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
855 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
856 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
857 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
859 UINT idx
= j
* texture
->level_count
+ i
;
860 struct wined3d_surface
*surface
;
862 if (FAILED(hr
= device
->device_parent
->ops
->create_texture_surface(device
->device_parent
,
863 parent
, &surface_desc
, idx
, surface_flags
, &surface
)))
865 FIXME("(%p) Failed to create surface, hr %#x.\n", texture
, hr
);
866 wined3d_texture_cleanup(texture
);
870 surface_set_container(surface
, texture
);
871 surface_set_texture_target(surface
, cube_targets
[j
], i
);
872 texture
->sub_resources
[idx
] = &surface
->resource
;
873 TRACE("Created surface level %u @ %p.\n", i
, surface
);
875 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
876 surface_desc
.height
= surface_desc
.width
;
882 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
883 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
884 const struct wined3d_parent_ops
*parent_ops
)
886 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
887 struct wined3d_resource_desc surface_desc
;
888 UINT pow2_width
, pow2_height
;
892 /* TODO: It should only be possible to create textures for formats
893 * that are reported as supported. */
894 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
896 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
897 return WINED3DERR_INVALIDCALL
;
900 /* Non-power2 support. */
901 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
903 pow2_width
= desc
->width
;
904 pow2_height
= desc
->height
;
908 /* Find the nearest pow2 match. */
909 pow2_width
= pow2_height
= 1;
910 while (pow2_width
< desc
->width
)
912 while (pow2_height
< desc
->height
)
915 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
917 /* levels == 0 returns an error as well */
920 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
922 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
926 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
927 return WINED3DERR_INVALIDCALL
;
933 /* Calculate levels for mip mapping. */
934 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
936 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
938 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
939 return WINED3DERR_INVALIDCALL
;
944 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
945 return WINED3DERR_INVALIDCALL
;
952 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
953 TRACE("Calculated levels = %u.\n", levels
);
956 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
957 desc
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
959 WARN("Failed to initialize texture, returning %#x.\n", hr
);
963 /* Precalculated scaling for 'faked' non power of two texture coords. */
964 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
965 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
967 texture
->pow2_matrix
[0] = 1.0f
;
968 texture
->pow2_matrix
[5] = 1.0f
;
969 texture
->pow2_matrix
[10] = 1.0f
;
970 texture
->pow2_matrix
[15] = 1.0f
;
971 texture
->target
= GL_TEXTURE_2D
;
972 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
973 texture
->min_mip_lookup
= minMipLookup_noFilter
;
975 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
976 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
978 texture
->pow2_matrix
[0] = (float)desc
->width
;
979 texture
->pow2_matrix
[5] = (float)desc
->height
;
980 texture
->pow2_matrix
[10] = 1.0f
;
981 texture
->pow2_matrix
[15] = 1.0f
;
982 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
983 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
984 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
986 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
987 texture
->min_mip_lookup
= minMipLookup_noMip
;
989 texture
->min_mip_lookup
= minMipLookup_noFilter
;
993 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
995 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
996 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
997 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1001 texture
->pow2_matrix
[0] = 1.0f
;
1002 texture
->pow2_matrix
[5] = 1.0f
;
1005 texture
->pow2_matrix
[10] = 1.0f
;
1006 texture
->pow2_matrix
[15] = 1.0f
;
1007 texture
->target
= GL_TEXTURE_2D
;
1009 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1011 /* Generate all the surfaces. */
1012 surface_desc
= *desc
;
1013 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1014 for (i
= 0; i
< texture
->level_count
; ++i
)
1016 struct wined3d_surface
*surface
;
1018 /* Use the callback to create the texture surface. */
1019 if (FAILED(hr
= device
->device_parent
->ops
->create_texture_surface(device
->device_parent
,
1020 parent
, &surface_desc
, i
, surface_flags
, &surface
)))
1022 FIXME("Failed to create surface %p, hr %#x\n", texture
, hr
);
1023 wined3d_texture_cleanup(texture
);
1027 surface_set_container(surface
, texture
);
1028 surface_set_texture_target(surface
, texture
->target
, i
);
1029 texture
->sub_resources
[i
] = &surface
->resource
;
1030 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1031 /* Calculate the next mipmap level. */
1032 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1033 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1039 /* Context activation is done by the caller. */
1040 static HRESULT
texture3d_bind(struct wined3d_texture
*texture
,
1041 struct wined3d_context
*context
, BOOL srgb
)
1045 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1047 return wined3d_texture_bind(texture
, context
, srgb
, &dummy
);
1050 /* Do not call while under the GL lock. */
1051 static void texture3d_preload(struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
1053 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1054 struct wined3d_device
*device
= texture
->resource
.device
;
1055 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1056 struct wined3d_context
*context
= NULL
;
1057 struct gl_texture
*gl_tex
;
1061 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
1063 srgb_mode
= texture_srgb_mode(texture
, srgb
);
1064 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, srgb_mode
);
1068 context
= context_acquire(device
, NULL
);
1070 /* Reload the surfaces if the texture is marked dirty. */
1071 for (i
= 0; i
< sub_count
; ++i
)
1073 wined3d_volume_load(volume_from_resource(texture
->sub_resources
[i
]), context
,
1077 context_release(context
);
1081 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1084 /* No longer dirty. */
1085 gl_tex
->dirty
= FALSE
;
1088 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1089 const struct wined3d_box
*dirty_region
)
1093 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1095 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1097 /* Cleanup the container. */
1098 volume_set_container(volume
, NULL
);
1099 wined3d_volume_decref(volume
);
1102 /* Do not call while under the GL lock. */
1103 static void texture3d_unload(struct wined3d_resource
*resource
)
1105 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1108 TRACE("texture %p.\n", texture
);
1110 for (i
= 0; i
< texture
->level_count
; ++i
)
1112 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
1113 sub_resource
->resource_ops
->resource_unload(sub_resource
);
1116 wined3d_texture_unload(texture
);
1119 static const struct wined3d_texture_ops texture3d_ops
=
1123 texture3d_sub_resource_add_dirty_region
,
1124 texture3d_sub_resource_cleanup
,
1127 static const struct wined3d_resource_ops texture3d_resource_ops
=
1132 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1133 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1135 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1136 UINT tmp_w
, tmp_h
, tmp_d
;
1140 /* TODO: It should only be possible to create textures for formats
1141 * that are reported as supported. */
1142 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1144 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1145 return WINED3DERR_INVALIDCALL
;
1148 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1150 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1151 return WINED3DERR_INVALIDCALL
;
1154 /* Calculate levels for mip mapping. */
1155 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1157 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1159 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1160 return WINED3DERR_INVALIDCALL
;
1165 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1166 return WINED3DERR_INVALIDCALL
;
1173 levels
= wined3d_log2i(max(max(desc
->width
, desc
->height
), desc
->depth
)) + 1;
1174 TRACE("Calculated levels = %u.\n", levels
);
1177 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1179 UINT pow2_w
, pow2_h
, pow2_d
;
1181 while (pow2_w
< desc
->width
)
1184 while (pow2_h
< desc
->height
)
1187 while (pow2_d
< desc
->depth
)
1190 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1192 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1194 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1198 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1199 desc
->width
, desc
->height
, desc
->depth
);
1200 return WINED3DERR_INVALIDCALL
;
1205 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1206 desc
, device
, parent
, parent_ops
, &texture3d_resource_ops
)))
1208 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1212 texture
->pow2_matrix
[0] = 1.0f
;
1213 texture
->pow2_matrix
[5] = 1.0f
;
1214 texture
->pow2_matrix
[10] = 1.0f
;
1215 texture
->pow2_matrix
[15] = 1.0f
;
1216 texture
->target
= GL_TEXTURE_3D
;
1218 /* Generate all the surfaces. */
1219 tmp_w
= desc
->width
;
1220 tmp_h
= desc
->height
;
1221 tmp_d
= desc
->depth
;
1223 for (i
= 0; i
< texture
->level_count
; ++i
)
1225 struct wined3d_volume
*volume
;
1227 /* Create the volume. */
1228 hr
= device
->device_parent
->ops
->create_volume(device
->device_parent
, parent
,
1229 tmp_w
, tmp_h
, tmp_d
, i
, desc
->format
, desc
->pool
, desc
->usage
, &volume
);
1232 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1233 wined3d_texture_cleanup(texture
);
1237 /* Set its container to this texture. */
1238 volume_set_container(volume
, texture
);
1239 texture
->sub_resources
[i
] = &volume
->resource
;
1241 /* Calculate the next mipmap level. */
1242 tmp_w
= max(1, tmp_w
>> 1);
1243 tmp_h
= max(1, tmp_h
>> 1);
1244 tmp_d
= max(1, tmp_d
>> 1);
1250 HRESULT CDECL
wined3d_texture_create_2d(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1251 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1252 struct wined3d_texture
**texture
)
1254 struct wined3d_texture
*object
;
1257 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1258 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1260 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1264 return WINED3DERR_OUTOFVIDEOMEMORY
;
1267 if (FAILED(hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
)))
1269 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1270 HeapFree(GetProcessHeap(), 0, object
);
1275 TRACE("Created texture %p.\n", object
);
1281 HRESULT CDECL
wined3d_texture_create_3d(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1282 UINT level_count
, void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1284 struct wined3d_texture
*object
;
1287 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1288 device
, desc
, level_count
, parent
, parent_ops
, texture
);
1290 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1294 return WINED3DERR_OUTOFVIDEOMEMORY
;
1297 if (FAILED(hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
)))
1299 WARN("Failed to initialize volumetexture, returning %#x\n", hr
);
1300 HeapFree(GetProcessHeap(), 0, object
);
1305 TRACE("Created texture %p.\n", object
);
1311 HRESULT CDECL
wined3d_texture_create_cube(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1312 UINT level_count
, DWORD surface_flags
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1313 struct wined3d_texture
**texture
)
1315 struct wined3d_texture
*object
;
1318 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1319 device
, desc
, level_count
, surface_flags
, parent
, parent_ops
, texture
);
1321 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1325 return WINED3DERR_OUTOFVIDEOMEMORY
;
1328 if (FAILED(hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
)))
1330 WARN("Failed to initialize cubetexture, returning %#x\n", hr
);
1331 HeapFree(GetProcessHeap(), 0, object
);
1336 TRACE("Created texture %p.\n", object
);