usp10: Avoid leaking "open_stack" and "stack_index" in case of 0 "pair_count" in...
[wine.git] / dlls / wined3d / view.c
blob05167f4be9c3657173e51852a36b5922e62292fa
1 /*
2 * Copyright 2009, 2011 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define WINED3D_VIEW_CUBE_ARRAY (WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY)
29 static BOOL is_stencil_view_format(const struct wined3d_format *format)
31 return format->id == WINED3DFMT_X24_TYPELESS_G8_UINT
32 || format->id == WINED3DFMT_X32_TYPELESS_G8X24_UINT;
35 static GLenum get_texture_view_target(const struct wined3d_gl_info *gl_info,
36 const struct wined3d_view_desc *desc, const struct wined3d_texture *texture)
38 static const struct
40 GLenum texture_target;
41 unsigned int view_flags;
42 GLenum view_target;
43 enum wined3d_gl_extension extension;
45 view_types[] =
47 {GL_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_CUBE_MAP},
48 {GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_RECTANGLE},
49 {GL_TEXTURE_3D, 0, GL_TEXTURE_3D},
51 {GL_TEXTURE_2D, 0, GL_TEXTURE_2D},
52 {GL_TEXTURE_2D, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_ARRAY},
53 {GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_2D},
54 {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_ARRAY},
55 {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_TEXTURE_CUBE, GL_TEXTURE_CUBE_MAP},
56 {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_CUBE_ARRAY, GL_TEXTURE_CUBE_MAP_ARRAY, ARB_TEXTURE_CUBE_MAP_ARRAY},
58 {GL_TEXTURE_2D_MULTISAMPLE, 0, GL_TEXTURE_2D_MULTISAMPLE},
59 {GL_TEXTURE_2D_MULTISAMPLE, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_MULTISAMPLE_ARRAY},
60 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0, GL_TEXTURE_2D_MULTISAMPLE},
61 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_MULTISAMPLE_ARRAY},
63 unsigned int i;
65 for (i = 0; i < ARRAY_SIZE(view_types); ++i)
67 if (view_types[i].texture_target != texture->target || view_types[i].view_flags != desc->flags)
68 continue;
69 if (gl_info->supported[view_types[i].extension])
70 return view_types[i].view_target;
72 FIXME("Extension %#x not supported.\n", view_types[i].extension);
75 FIXME("Unhandled view flags %#x for texture target %#x.\n", desc->flags, texture->target);
76 return texture->target;
79 static const struct wined3d_format *validate_resource_view(const struct wined3d_view_desc *desc,
80 struct wined3d_resource *resource, BOOL mip_slice, BOOL allow_srgb_toggle)
82 const struct wined3d_gl_info *gl_info = &resource->device->adapter->gl_info;
83 const struct wined3d_format *format;
85 format = wined3d_get_format(gl_info, desc->format_id, resource->usage);
86 if (resource->type == WINED3D_RTYPE_BUFFER && (desc->flags & WINED3D_VIEW_BUFFER_RAW))
88 if (format->id != WINED3DFMT_R32_TYPELESS)
90 WARN("Invalid format %s for raw buffer view.\n", debug_d3dformat(format->id));
91 return NULL;
94 format = wined3d_get_format(gl_info, WINED3DFMT_R32_UINT, resource->usage);
97 if (wined3d_format_is_typeless(format))
99 WARN("Trying to create view for typeless format %s.\n", debug_d3dformat(format->id));
100 return NULL;
103 if (resource->type == WINED3D_RTYPE_BUFFER)
105 struct wined3d_buffer *buffer = buffer_from_resource(resource);
106 unsigned int buffer_size, element_size;
108 if (buffer->desc.structure_byte_stride)
110 if (desc->format_id != WINED3DFMT_UNKNOWN)
112 WARN("Invalid format %s for structured buffer view.\n", debug_d3dformat(desc->format_id));
113 return NULL;
116 format = wined3d_get_format(gl_info, WINED3DFMT_R32_UINT, resource->usage);
117 element_size = buffer->desc.structure_byte_stride;
119 else
121 element_size = format->byte_count;
124 if (!element_size)
125 return NULL;
127 buffer_size = buffer->resource.size / element_size;
128 if (desc->u.buffer.start_idx >= buffer_size
129 || desc->u.buffer.count > buffer_size - desc->u.buffer.start_idx)
130 return NULL;
132 else
134 struct wined3d_texture *texture = texture_from_resource(resource);
135 unsigned int depth_or_layer_count;
137 if (resource->format->id != format->id && !wined3d_format_is_typeless(resource->format)
138 && (!allow_srgb_toggle || !wined3d_formats_are_srgb_variants(resource->format->id, format->id)))
140 WARN("Trying to create incompatible view for non typeless format %s.\n",
141 debug_d3dformat(format->id));
142 return NULL;
145 if (mip_slice && resource->type == WINED3D_RTYPE_TEXTURE_3D)
146 depth_or_layer_count = wined3d_texture_get_level_depth(texture, desc->u.texture.level_idx);
147 else
148 depth_or_layer_count = texture->layer_count;
150 if (!desc->u.texture.level_count
151 || (mip_slice && desc->u.texture.level_count != 1)
152 || desc->u.texture.level_idx >= texture->level_count
153 || desc->u.texture.level_count > texture->level_count - desc->u.texture.level_idx
154 || !desc->u.texture.layer_count
155 || desc->u.texture.layer_idx >= depth_or_layer_count
156 || desc->u.texture.layer_count > depth_or_layer_count - desc->u.texture.layer_idx)
157 return NULL;
160 return format;
163 static void create_texture_view(struct wined3d_gl_view *view, GLenum view_target,
164 const struct wined3d_view_desc *desc, struct wined3d_texture *texture,
165 const struct wined3d_format *view_format)
167 const struct wined3d_gl_info *gl_info;
168 unsigned int layer_idx, layer_count;
169 struct wined3d_context *context;
170 GLuint texture_name;
172 view->target = view_target;
174 context = context_acquire(texture->resource.device, NULL, 0);
175 gl_info = context->gl_info;
177 if (!gl_info->supported[ARB_TEXTURE_VIEW])
179 context_release(context);
180 FIXME("OpenGL implementation does not support texture views.\n");
181 return;
184 wined3d_texture_prepare_texture(texture, context, FALSE);
185 texture_name = wined3d_texture_get_texture_name(texture, context, FALSE);
187 layer_idx = desc->u.texture.layer_idx;
188 layer_count = desc->u.texture.layer_count;
189 if (view_target == GL_TEXTURE_3D && (layer_idx || layer_count != 1))
191 FIXME("Depth slice (%u-%u) not supported.\n", layer_idx, layer_count);
192 layer_idx = 0;
193 layer_count = 1;
196 gl_info->gl_ops.gl.p_glGenTextures(1, &view->name);
197 GL_EXTCALL(glTextureView(view->name, view->target, texture_name, view_format->glInternal,
198 desc->u.texture.level_idx, desc->u.texture.level_count,
199 layer_idx, layer_count));
200 checkGLcall("Create texture view");
202 if (is_stencil_view_format(view_format))
204 static const GLint swizzle[] = {GL_ZERO, GL_RED, GL_ZERO, GL_ZERO};
206 if (!gl_info->supported[ARB_STENCIL_TEXTURING])
208 context_release(context);
209 FIXME("OpenGL implementation does not support stencil texturing.\n");
210 return;
213 context_bind_texture(context, view->target, view->name);
214 gl_info->gl_ops.gl.p_glTexParameteriv(view->target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
215 gl_info->gl_ops.gl.p_glTexParameteri(view->target, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
216 checkGLcall("Initialize stencil view");
218 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
219 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
222 context_release(context);
225 static void create_buffer_texture(struct wined3d_gl_view *view, struct wined3d_context *context,
226 struct wined3d_buffer *buffer, const struct wined3d_format *view_format,
227 unsigned int offset, unsigned int size)
229 const struct wined3d_gl_info *gl_info = context->gl_info;
231 if (!gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
233 FIXME("OpenGL implementation does not support buffer textures.\n");
234 return;
237 if ((offset & (gl_info->limits.texture_buffer_offset_alignment - 1)))
239 FIXME("Buffer offset %u is not %u byte aligned.\n",
240 offset, gl_info->limits.texture_buffer_offset_alignment);
241 return;
244 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
246 view->target = GL_TEXTURE_BUFFER;
247 gl_info->gl_ops.gl.p_glGenTextures(1, &view->name);
249 context_bind_texture(context, GL_TEXTURE_BUFFER, view->name);
250 if (gl_info->supported[ARB_TEXTURE_BUFFER_RANGE])
252 GL_EXTCALL(glTexBufferRange(GL_TEXTURE_BUFFER, view_format->glInternal,
253 buffer->buffer_object, offset, size));
255 else
257 if (offset || size != buffer->resource.size)
258 FIXME("OpenGL implementation does not support ARB_texture_buffer_range.\n");
259 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, view_format->glInternal, buffer->buffer_object));
261 checkGLcall("Create buffer texture");
263 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
264 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
267 static void get_buffer_view_range(const struct wined3d_buffer *buffer,
268 const struct wined3d_view_desc *desc, const struct wined3d_format *view_format,
269 unsigned int *offset, unsigned int *size)
271 if (desc->format_id == WINED3DFMT_UNKNOWN)
273 *offset = desc->u.buffer.start_idx * buffer->desc.structure_byte_stride;
274 *size = desc->u.buffer.count * buffer->desc.structure_byte_stride;
276 else
278 *offset = desc->u.buffer.start_idx * view_format->byte_count;
279 *size = desc->u.buffer.count * view_format->byte_count;
283 static void create_buffer_view(struct wined3d_gl_view *view, struct wined3d_context *context,
284 const struct wined3d_view_desc *desc, struct wined3d_buffer *buffer,
285 const struct wined3d_format *view_format)
287 unsigned int offset, size;
289 get_buffer_view_range(buffer, desc, view_format, &offset, &size);
290 create_buffer_texture(view, context, buffer, view_format, offset, size);
293 static void wined3d_view_invalidate_location(struct wined3d_resource *resource,
294 const struct wined3d_view_desc *desc, DWORD location)
296 unsigned int i, sub_resource_idx, layer_count;
297 struct wined3d_texture *texture;
299 if (resource->type == WINED3D_RTYPE_BUFFER)
301 wined3d_buffer_invalidate_location(buffer_from_resource(resource), location);
302 return;
305 texture = texture_from_resource(resource);
307 sub_resource_idx = desc->u.texture.layer_idx * texture->level_count + desc->u.texture.level_idx;
308 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? desc->u.texture.layer_count : 1;
309 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
310 wined3d_texture_invalidate_location(texture, sub_resource_idx, location);
313 ULONG CDECL wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view *view)
315 ULONG refcount = InterlockedIncrement(&view->refcount);
317 TRACE("%p increasing refcount to %u.\n", view, refcount);
319 return refcount;
322 static void wined3d_rendertarget_view_destroy_object(void *object)
324 struct wined3d_rendertarget_view *view = object;
325 struct wined3d_device *device = view->resource->device;
327 if (view->gl_view.name)
329 const struct wined3d_gl_info *gl_info;
330 struct wined3d_context *context;
332 context = context_acquire(device, NULL, 0);
333 gl_info = context->gl_info;
334 context_gl_resource_released(device, view->gl_view.name, FALSE);
335 gl_info->gl_ops.gl.p_glDeleteTextures(1, &view->gl_view.name);
336 checkGLcall("glDeleteTextures");
337 context_release(context);
340 heap_free(view);
343 ULONG CDECL wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view *view)
345 ULONG refcount = InterlockedDecrement(&view->refcount);
347 TRACE("%p decreasing refcount to %u.\n", view, refcount);
349 if (!refcount)
351 struct wined3d_resource *resource = view->resource;
352 struct wined3d_device *device = resource->device;
354 /* Call wined3d_object_destroyed() before releasing the resource,
355 * since releasing the resource may end up destroying the parent. */
356 view->parent_ops->wined3d_object_destroyed(view->parent);
357 wined3d_cs_destroy_object(device->cs, wined3d_rendertarget_view_destroy_object, view);
358 wined3d_resource_decref(resource);
361 return refcount;
364 void * CDECL wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view *view)
366 TRACE("view %p.\n", view);
368 return view->parent;
371 void * CDECL wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view *view)
373 struct wined3d_texture *texture;
375 TRACE("view %p.\n", view);
377 if (view->resource->type == WINED3D_RTYPE_BUFFER)
378 return wined3d_buffer_get_parent(buffer_from_resource(view->resource));
380 texture = texture_from_resource(view->resource);
382 return texture->sub_resources[view->sub_resource_idx].parent;
385 void CDECL wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view *view, void *parent)
387 TRACE("view %p, parent %p.\n", view, parent);
389 view->parent = parent;
392 struct wined3d_resource * CDECL wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view *view)
394 TRACE("view %p.\n", view);
396 return view->resource;
399 void wined3d_rendertarget_view_get_drawable_size(const struct wined3d_rendertarget_view *view,
400 const struct wined3d_context *context, unsigned int *width, unsigned int *height)
402 const struct wined3d_texture *texture;
404 if (view->resource->type != WINED3D_RTYPE_TEXTURE_2D)
406 *width = view->width;
407 *height = view->height;
408 return;
411 texture = texture_from_resource(view->resource);
412 if (texture->swapchain)
414 /* The drawable size of an onscreen drawable is the surface size.
415 * (Actually: The window size, but the surface is created in window
416 * size.) */
417 *width = texture->resource.width;
418 *height = texture->resource.height;
420 else if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
422 const struct wined3d_swapchain *swapchain = context->swapchain;
424 /* The drawable size of a backbuffer / aux buffer offscreen target is
425 * the size of the current context's drawable, which is the size of
426 * the back buffer of the swapchain the active context belongs to. */
427 *width = swapchain->desc.backbuffer_width;
428 *height = swapchain->desc.backbuffer_height;
430 else
432 unsigned int level_idx = view->sub_resource_idx % texture->level_count;
434 /* The drawable size of an FBO target is the OpenGL texture size,
435 * which is the power of two size. */
436 *width = wined3d_texture_get_level_pow2_width(texture, level_idx);
437 *height = wined3d_texture_get_level_pow2_height(texture, level_idx);
441 void wined3d_rendertarget_view_prepare_location(struct wined3d_rendertarget_view *view,
442 struct wined3d_context *context, DWORD location)
444 struct wined3d_resource *resource = view->resource;
445 unsigned int i, sub_resource_idx, layer_count;
446 struct wined3d_texture *texture;
448 if (resource->type == WINED3D_RTYPE_BUFFER)
450 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
451 return;
454 texture = texture_from_resource(resource);
455 sub_resource_idx = view->sub_resource_idx;
456 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
457 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
458 wined3d_texture_prepare_location(texture, sub_resource_idx, context, location);
461 void wined3d_rendertarget_view_load_location(struct wined3d_rendertarget_view *view,
462 struct wined3d_context *context, DWORD location)
464 struct wined3d_resource *resource = view->resource;
465 unsigned int i, sub_resource_idx, layer_count;
466 struct wined3d_texture *texture;
468 if (resource->type == WINED3D_RTYPE_BUFFER)
470 wined3d_buffer_load_location(buffer_from_resource(resource), context, location);
471 return;
474 texture = texture_from_resource(resource);
475 sub_resource_idx = view->sub_resource_idx;
476 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
477 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
478 wined3d_texture_load_location(texture, sub_resource_idx, context, location);
481 void wined3d_rendertarget_view_validate_location(struct wined3d_rendertarget_view *view, DWORD location)
483 struct wined3d_resource *resource = view->resource;
484 unsigned int i, sub_resource_idx, layer_count;
485 struct wined3d_texture *texture;
487 if (resource->type == WINED3D_RTYPE_BUFFER)
489 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
490 return;
493 texture = texture_from_resource(resource);
494 sub_resource_idx = view->sub_resource_idx;
495 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
496 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
497 wined3d_texture_validate_location(texture, sub_resource_idx, location);
500 void wined3d_rendertarget_view_invalidate_location(struct wined3d_rendertarget_view *view, DWORD location)
502 wined3d_view_invalidate_location(view->resource, &view->desc, location);
505 static void wined3d_render_target_view_cs_init(void *object)
507 struct wined3d_rendertarget_view *view = object;
508 struct wined3d_resource *resource = view->resource;
509 const struct wined3d_view_desc *desc = &view->desc;
511 if (resource->type == WINED3D_RTYPE_BUFFER)
513 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
515 else
517 struct wined3d_texture *texture = texture_from_resource(resource);
518 unsigned int depth_or_layer_count;
520 if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
521 depth_or_layer_count = wined3d_texture_get_level_depth(texture, desc->u.texture.level_idx);
522 else
523 depth_or_layer_count = texture->layer_count;
525 if (resource->format->id != view->format->id
526 || (view->layer_count != 1 && view->layer_count != depth_or_layer_count))
528 if (resource->format->gl_view_class != view->format->gl_view_class)
530 FIXME("Render target view not supported, resource format %s, view format %s.\n",
531 debug_d3dformat(resource->format->id), debug_d3dformat(view->format->id));
532 return;
534 if (texture->swapchain && texture->swapchain->desc.backbuffer_count > 1)
536 FIXME("Swapchain views not supported.\n");
537 return;
540 create_texture_view(&view->gl_view, texture->target, desc, texture, view->format);
545 static HRESULT wined3d_rendertarget_view_init(struct wined3d_rendertarget_view *view,
546 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
547 void *parent, const struct wined3d_parent_ops *parent_ops)
549 BOOL allow_srgb_toggle = FALSE;
551 view->refcount = 1;
552 view->parent = parent;
553 view->parent_ops = parent_ops;
555 if (resource->type != WINED3D_RTYPE_BUFFER)
557 struct wined3d_texture *texture = texture_from_resource(resource);
559 if (texture->swapchain)
560 allow_srgb_toggle = TRUE;
562 if (!(view->format = validate_resource_view(desc, resource, TRUE, allow_srgb_toggle)))
563 return E_INVALIDARG;
564 view->format_flags = view->format->flags[resource->gl_type];
565 view->desc = *desc;
567 if (resource->type == WINED3D_RTYPE_BUFFER)
569 view->sub_resource_idx = 0;
570 view->layer_count = 1;
571 view->width = desc->u.buffer.count;
572 view->height = 1;
574 else
576 struct wined3d_texture *texture = texture_from_resource(resource);
578 view->sub_resource_idx = desc->u.texture.level_idx;
579 if (resource->type != WINED3D_RTYPE_TEXTURE_3D)
580 view->sub_resource_idx += desc->u.texture.layer_idx * texture->level_count;
581 view->layer_count = desc->u.texture.layer_count;
582 view->width = wined3d_texture_get_level_width(texture, desc->u.texture.level_idx);
583 view->height = wined3d_texture_get_level_height(texture, desc->u.texture.level_idx);
586 wined3d_resource_incref(view->resource = resource);
588 wined3d_cs_init_object(resource->device->cs, wined3d_render_target_view_cs_init, view);
590 return WINED3D_OK;
593 HRESULT CDECL wined3d_rendertarget_view_create(const struct wined3d_view_desc *desc,
594 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
595 struct wined3d_rendertarget_view **view)
597 struct wined3d_rendertarget_view *object;
598 HRESULT hr;
600 TRACE("desc %p, resource %p, parent %p, parent_ops %p, view %p.\n",
601 desc, resource, parent, parent_ops, view);
603 if (!(object = heap_alloc_zero(sizeof(*object))))
604 return E_OUTOFMEMORY;
606 if (FAILED(hr = wined3d_rendertarget_view_init(object, desc, resource, parent, parent_ops)))
608 heap_free(object);
609 WARN("Failed to initialise view, hr %#x.\n", hr);
610 return hr;
613 TRACE("Created render target view %p.\n", object);
614 *view = object;
616 return hr;
619 HRESULT CDECL wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture *texture,
620 unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops,
621 struct wined3d_rendertarget_view **view)
623 struct wined3d_view_desc desc;
625 TRACE("texture %p, sub_resource_idx %u, parent %p, parent_ops %p, view %p.\n",
626 texture, sub_resource_idx, parent, parent_ops, view);
628 desc.format_id = texture->resource.format->id;
629 desc.flags = 0;
630 desc.u.texture.level_idx = sub_resource_idx % texture->level_count;
631 desc.u.texture.level_count = 1;
632 desc.u.texture.layer_idx = sub_resource_idx / texture->level_count;
633 desc.u.texture.layer_count = 1;
635 return wined3d_rendertarget_view_create(&desc, &texture->resource, parent, parent_ops, view);
638 ULONG CDECL wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view *view)
640 ULONG refcount = InterlockedIncrement(&view->refcount);
642 TRACE("%p increasing refcount to %u.\n", view, refcount);
644 return refcount;
647 static void wined3d_shader_resource_view_destroy_object(void *object)
649 struct wined3d_shader_resource_view *view = object;
651 if (view->gl_view.name)
653 const struct wined3d_gl_info *gl_info;
654 struct wined3d_context *context;
656 context = context_acquire(view->resource->device, NULL, 0);
657 gl_info = context->gl_info;
658 gl_info->gl_ops.gl.p_glDeleteTextures(1, &view->gl_view.name);
659 checkGLcall("glDeleteTextures");
660 context_release(context);
663 heap_free(view);
666 ULONG CDECL wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view *view)
668 ULONG refcount = InterlockedDecrement(&view->refcount);
670 TRACE("%p decreasing refcount to %u.\n", view, refcount);
672 if (!refcount)
674 struct wined3d_resource *resource = view->resource;
675 struct wined3d_device *device = resource->device;
677 /* Call wined3d_object_destroyed() before releasing the resource,
678 * since releasing the resource may end up destroying the parent. */
679 view->parent_ops->wined3d_object_destroyed(view->parent);
680 wined3d_cs_destroy_object(device->cs, wined3d_shader_resource_view_destroy_object, view);
681 wined3d_resource_decref(resource);
684 return refcount;
687 void * CDECL wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view *view)
689 TRACE("view %p.\n", view);
691 return view->parent;
694 static void wined3d_shader_resource_view_cs_init(void *object)
696 struct wined3d_shader_resource_view *view = object;
697 struct wined3d_resource *resource = view->resource;
698 const struct wined3d_format *view_format;
699 const struct wined3d_gl_info *gl_info;
700 const struct wined3d_view_desc *desc;
701 GLenum view_target;
703 view_format = view->format;
704 gl_info = &resource->device->adapter->gl_info;
705 desc = &view->desc;
707 if (resource->type == WINED3D_RTYPE_BUFFER)
709 struct wined3d_buffer *buffer = buffer_from_resource(resource);
710 struct wined3d_context *context;
712 context = context_acquire(resource->device, NULL, 0);
713 create_buffer_view(&view->gl_view, context, desc, buffer, view_format);
714 context_release(context);
716 else
718 struct wined3d_texture *texture = texture_from_resource(resource);
720 view_target = get_texture_view_target(gl_info, desc, texture);
722 if (resource->format->id == view_format->id && texture->target == view_target
723 && !desc->u.texture.level_idx && desc->u.texture.level_count == texture->level_count
724 && !desc->u.texture.layer_idx && desc->u.texture.layer_count == texture->layer_count
725 && !is_stencil_view_format(view_format))
727 TRACE("Creating identity shader resource view.\n");
729 else if (texture->swapchain && texture->swapchain->desc.backbuffer_count > 1)
731 FIXME("Swapchain shader resource views not supported.\n");
733 else if (resource->format->typeless_id == view_format->typeless_id
734 && resource->format->gl_view_class == view_format->gl_view_class)
736 create_texture_view(&view->gl_view, view_target, desc, texture, view_format);
738 else if (wined3d_format_is_depth_view(resource->format->id, view_format->id))
740 create_texture_view(&view->gl_view, view_target, desc, texture, resource->format);
742 else
744 FIXME("Shader resource view not supported, resource format %s, view format %s.\n",
745 debug_d3dformat(resource->format->id), debug_d3dformat(view_format->id));
750 static HRESULT wined3d_shader_resource_view_init(struct wined3d_shader_resource_view *view,
751 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
752 void *parent, const struct wined3d_parent_ops *parent_ops)
754 view->refcount = 1;
755 view->parent = parent;
756 view->parent_ops = parent_ops;
758 if (!(view->format = validate_resource_view(desc, resource, FALSE, FALSE)))
759 return E_INVALIDARG;
760 view->desc = *desc;
762 wined3d_resource_incref(view->resource = resource);
764 wined3d_cs_init_object(resource->device->cs, wined3d_shader_resource_view_cs_init, view);
766 return WINED3D_OK;
769 HRESULT CDECL wined3d_shader_resource_view_create(const struct wined3d_view_desc *desc,
770 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
771 struct wined3d_shader_resource_view **view)
773 struct wined3d_shader_resource_view *object;
774 HRESULT hr;
776 TRACE("desc %p, resource %p, parent %p, parent_ops %p, view %p.\n",
777 desc, resource, parent, parent_ops, view);
779 if (!(object = heap_alloc_zero(sizeof(*object))))
780 return E_OUTOFMEMORY;
782 if (FAILED(hr = wined3d_shader_resource_view_init(object, desc, resource, parent, parent_ops)))
784 heap_free(object);
785 WARN("Failed to initialise view, hr %#x.\n", hr);
786 return hr;
789 TRACE("Created shader resource view %p.\n", object);
790 *view = object;
792 return WINED3D_OK;
795 void wined3d_shader_resource_view_bind(struct wined3d_shader_resource_view *view,
796 unsigned int unit, struct wined3d_sampler *sampler, struct wined3d_context *context)
798 const struct wined3d_gl_info *gl_info = context->gl_info;
799 struct wined3d_texture *texture;
801 context_active_texture(context, gl_info, unit);
803 if (view->gl_view.name)
805 context_bind_texture(context, view->gl_view.target, view->gl_view.name);
806 wined3d_sampler_bind(sampler, unit, NULL, context);
807 return;
810 if (view->resource->type == WINED3D_RTYPE_BUFFER)
812 FIXME("Buffer shader resources not supported.\n");
813 return;
816 texture = wined3d_texture_from_resource(view->resource);
817 wined3d_texture_bind(texture, context, FALSE);
818 wined3d_sampler_bind(sampler, unit, texture, context);
821 /* Context activation is done by the caller. */
822 static void shader_resource_view_bind_and_dirtify(struct wined3d_shader_resource_view *view,
823 struct wined3d_context *context)
825 if (context->active_texture < ARRAY_SIZE(context->rev_tex_unit_map))
827 DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
828 if (active_sampler != WINED3D_UNMAPPED_STAGE)
829 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
831 /* FIXME: Ideally we'd only do this when touching a binding that's used by
832 * a shader. */
833 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
834 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
836 context_bind_texture(context, view->gl_view.target, view->gl_view.name);
839 void shader_resource_view_generate_mipmaps(struct wined3d_shader_resource_view *view)
841 struct wined3d_texture *texture = texture_from_resource(view->resource);
842 unsigned int i, j, layer_count, level_count, base_level, max_level;
843 const struct wined3d_gl_info *gl_info;
844 struct wined3d_context *context;
845 struct gl_texture *gl_tex;
846 DWORD location;
847 BOOL srgb;
849 TRACE("view %p.\n", view);
851 context = context_acquire(view->resource->device, NULL, 0);
852 gl_info = context->gl_info;
853 layer_count = view->desc.u.texture.layer_count;
854 level_count = view->desc.u.texture.level_count;
855 base_level = view->desc.u.texture.level_idx;
856 max_level = base_level + level_count - 1;
858 srgb = !!(texture->flags & WINED3D_TEXTURE_IS_SRGB);
859 location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
860 for (i = 0; i < layer_count; ++i)
861 wined3d_texture_load_location(texture, i * level_count + base_level, context, location);
863 if (view->gl_view.name)
865 shader_resource_view_bind_and_dirtify(view, context);
867 else
869 wined3d_texture_bind_and_dirtify(texture, context, srgb);
870 gl_info->gl_ops.gl.p_glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, base_level);
871 gl_info->gl_ops.gl.p_glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, max_level);
874 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
875 GL_EXTCALL(glBindSampler(context->active_texture, 0));
876 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
877 if (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
879 gl_info->gl_ops.gl.p_glTexParameteri(texture->target, GL_TEXTURE_SRGB_DECODE_EXT,
880 GL_SKIP_DECODE_EXT);
881 gl_tex->sampler_desc.srgb_decode = FALSE;
884 gl_info->fbo_ops.glGenerateMipmap(texture->target);
885 checkGLcall("glGenerateMipMap()");
887 for (i = 0; i < layer_count; ++i)
889 for (j = base_level + 1; j <= max_level; ++j)
891 wined3d_texture_validate_location(texture, i * level_count + j, location);
892 wined3d_texture_invalidate_location(texture, i * level_count + j, ~location);
896 if (!view->gl_view.name)
898 gl_tex->base_level = base_level;
899 gl_info->gl_ops.gl.p_glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
902 context_release(context);
905 void CDECL wined3d_shader_resource_view_generate_mipmaps(struct wined3d_shader_resource_view *view)
907 struct wined3d_texture *texture;
909 TRACE("view %p.\n", view);
911 if (view->resource->type == WINED3D_RTYPE_BUFFER)
913 WARN("Called on buffer resource %p.\n", view->resource);
914 return;
917 texture = texture_from_resource(view->resource);
918 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
920 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
921 return;
924 wined3d_cs_emit_generate_mipmaps(view->resource->device->cs, view);
927 ULONG CDECL wined3d_unordered_access_view_incref(struct wined3d_unordered_access_view *view)
929 ULONG refcount = InterlockedIncrement(&view->refcount);
931 TRACE("%p increasing refcount to %u.\n", view, refcount);
933 return refcount;
936 static void wined3d_unordered_access_view_destroy_object(void *object)
938 struct wined3d_unordered_access_view *view = object;
940 if (view->gl_view.name || view->counter_bo)
942 const struct wined3d_gl_info *gl_info;
943 struct wined3d_context *context;
945 context = context_acquire(view->resource->device, NULL, 0);
946 gl_info = context->gl_info;
947 if (view->gl_view.name)
948 gl_info->gl_ops.gl.p_glDeleteTextures(1, &view->gl_view.name);
949 if (view->counter_bo)
950 GL_EXTCALL(glDeleteBuffers(1, &view->counter_bo));
951 checkGLcall("delete resources");
952 context_release(context);
955 heap_free(view);
958 ULONG CDECL wined3d_unordered_access_view_decref(struct wined3d_unordered_access_view *view)
960 ULONG refcount = InterlockedDecrement(&view->refcount);
962 TRACE("%p decreasing refcount to %u.\n", view, refcount);
964 if (!refcount)
966 struct wined3d_resource *resource = view->resource;
967 struct wined3d_device *device = resource->device;
969 /* Call wined3d_object_destroyed() before releasing the resource,
970 * since releasing the resource may end up destroying the parent. */
971 view->parent_ops->wined3d_object_destroyed(view->parent);
972 wined3d_cs_destroy_object(device->cs, wined3d_unordered_access_view_destroy_object, view);
973 wined3d_resource_decref(resource);
976 return refcount;
979 void * CDECL wined3d_unordered_access_view_get_parent(const struct wined3d_unordered_access_view *view)
981 TRACE("view %p.\n", view);
983 return view->parent;
986 void wined3d_unordered_access_view_invalidate_location(struct wined3d_unordered_access_view *view,
987 DWORD location)
989 wined3d_view_invalidate_location(view->resource, &view->desc, location);
992 void wined3d_unordered_access_view_clear_uint(struct wined3d_unordered_access_view *view,
993 const struct wined3d_uvec4 *clear_value, struct wined3d_context *context)
995 const struct wined3d_gl_info *gl_info = context->gl_info;
996 const struct wined3d_format *format;
997 struct wined3d_resource *resource;
998 struct wined3d_buffer *buffer;
999 unsigned int offset, size;
1001 resource = view->resource;
1002 if (resource->type != WINED3D_RTYPE_BUFFER)
1004 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
1005 return;
1008 if (!gl_info->supported[ARB_CLEAR_BUFFER_OBJECT])
1010 FIXME("OpenGL implementation does not support ARB_clear_buffer_object.\n");
1011 return;
1014 format = view->format;
1015 if (format->id != WINED3DFMT_R32_UINT && format->id != WINED3DFMT_R32_SINT
1016 && format->id != WINED3DFMT_R32G32B32A32_UINT
1017 && format->id != WINED3DFMT_R32G32B32A32_SINT)
1019 FIXME("Not implemented for format %s.\n", debug_d3dformat(format->id));
1020 return;
1023 buffer = buffer_from_resource(resource);
1024 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
1025 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
1027 get_buffer_view_range(buffer, &view->desc, format, &offset, &size);
1028 context_bind_bo(context, buffer->buffer_type_hint, buffer->buffer_object);
1029 GL_EXTCALL(glClearBufferSubData(buffer->buffer_type_hint, format->glInternal,
1030 offset, size, format->glFormat, format->glType, clear_value));
1031 checkGLcall("clear unordered access view");
1034 void wined3d_unordered_access_view_set_counter(struct wined3d_unordered_access_view *view,
1035 unsigned int value)
1037 const struct wined3d_gl_info *gl_info;
1038 struct wined3d_context *context;
1040 if (!view->counter_bo)
1041 return;
1043 context = context_acquire(view->resource->device, NULL, 0);
1044 gl_info = context->gl_info;
1045 GL_EXTCALL(glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, view->counter_bo));
1046 GL_EXTCALL(glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(value), &value));
1047 checkGLcall("set atomic counter");
1048 context_release(context);
1051 void wined3d_unordered_access_view_copy_counter(struct wined3d_unordered_access_view *view,
1052 struct wined3d_buffer *buffer, unsigned int offset, struct wined3d_context *context)
1054 struct wined3d_bo_address dst, src;
1055 DWORD dst_location;
1057 if (!view->counter_bo)
1058 return;
1060 dst_location = wined3d_buffer_get_memory(buffer, &dst, buffer->locations);
1061 dst.addr += offset;
1063 src.buffer_object = view->counter_bo;
1064 src.addr = NULL;
1066 context_copy_bo_address(context, &dst, buffer->buffer_type_hint,
1067 &src, GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint));
1069 wined3d_buffer_invalidate_location(buffer, ~dst_location);
1072 static void wined3d_unordered_access_view_cs_init(void *object)
1074 struct wined3d_unordered_access_view *view = object;
1075 struct wined3d_resource *resource = view->resource;
1076 struct wined3d_view_desc *desc = &view->desc;
1077 const struct wined3d_gl_info *gl_info;
1079 gl_info = &resource->device->adapter->gl_info;
1081 if (resource->type == WINED3D_RTYPE_BUFFER)
1083 struct wined3d_buffer *buffer = buffer_from_resource(resource);
1084 struct wined3d_context *context;
1086 context = context_acquire(resource->device, NULL, 0);
1087 gl_info = context->gl_info;
1088 create_buffer_view(&view->gl_view, context, desc, buffer, view->format);
1089 if (desc->flags & (WINED3D_VIEW_BUFFER_COUNTER | WINED3D_VIEW_BUFFER_APPEND))
1091 static const GLuint initial_value = 0;
1092 GL_EXTCALL(glGenBuffers(1, &view->counter_bo));
1093 GL_EXTCALL(glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, view->counter_bo));
1094 GL_EXTCALL(glBufferData(GL_ATOMIC_COUNTER_BUFFER,
1095 sizeof(initial_value), &initial_value, GL_STATIC_DRAW));
1096 checkGLcall("create atomic counter buffer");
1098 context_release(context);
1100 else
1102 struct wined3d_texture *texture = texture_from_resource(resource);
1103 unsigned int depth_or_layer_count;
1105 if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
1106 depth_or_layer_count = wined3d_texture_get_level_depth(texture, desc->u.texture.level_idx);
1107 else
1108 depth_or_layer_count = texture->layer_count;
1110 if (desc->u.texture.layer_idx || desc->u.texture.layer_count != depth_or_layer_count)
1112 create_texture_view(&view->gl_view, get_texture_view_target(gl_info, desc, texture),
1113 desc, texture, view->format);
1118 static HRESULT wined3d_unordered_access_view_init(struct wined3d_unordered_access_view *view,
1119 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1120 void *parent, const struct wined3d_parent_ops *parent_ops)
1122 view->refcount = 1;
1123 view->parent = parent;
1124 view->parent_ops = parent_ops;
1126 if (!(view->format = validate_resource_view(desc, resource, TRUE, FALSE)))
1127 return E_INVALIDARG;
1128 view->desc = *desc;
1130 wined3d_resource_incref(view->resource = resource);
1132 wined3d_cs_init_object(resource->device->cs, wined3d_unordered_access_view_cs_init, view);
1134 return WINED3D_OK;
1137 HRESULT CDECL wined3d_unordered_access_view_create(const struct wined3d_view_desc *desc,
1138 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
1139 struct wined3d_unordered_access_view **view)
1141 struct wined3d_unordered_access_view *object;
1142 HRESULT hr;
1144 TRACE("desc %p, resource %p, parent %p, parent_ops %p, view %p.\n",
1145 desc, resource, parent, parent_ops, view);
1147 if (!(object = heap_alloc_zero(sizeof(*object))))
1148 return E_OUTOFMEMORY;
1150 if (FAILED(hr = wined3d_unordered_access_view_init(object, desc, resource, parent, parent_ops)))
1152 heap_free(object);
1153 WARN("Failed to initialise view, hr %#x.\n", hr);
1154 return hr;
1157 TRACE("Created unordered access view %p.\n", object);
1158 *view = object;
1160 return WINED3D_OK;