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
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
)
40 GLenum texture_target
;
41 unsigned int view_flags
;
43 enum wined3d_gl_extension extension
;
47 {GL_TEXTURE_CUBE_MAP
, 0, GL_TEXTURE_CUBE_MAP
},
48 {GL_TEXTURE_RECTANGLE
, 0, GL_TEXTURE_RECTANGLE
},
49 {GL_TEXTURE_2D
, 0, GL_TEXTURE_2D
},
50 {GL_TEXTURE_2D
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_2D_ARRAY
},
51 {GL_TEXTURE_2D_ARRAY
, 0, GL_TEXTURE_2D
},
52 {GL_TEXTURE_2D_ARRAY
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_2D_ARRAY
},
53 {GL_TEXTURE_2D_ARRAY
, WINED3D_VIEW_TEXTURE_CUBE
, GL_TEXTURE_CUBE_MAP
},
54 {GL_TEXTURE_2D_ARRAY
, WINED3D_VIEW_CUBE_ARRAY
, GL_TEXTURE_CUBE_MAP_ARRAY
, ARB_TEXTURE_CUBE_MAP_ARRAY
},
55 {GL_TEXTURE_3D
, 0, GL_TEXTURE_3D
},
59 for (i
= 0; i
< ARRAY_SIZE(view_types
); ++i
)
61 if (view_types
[i
].texture_target
!= texture
->target
|| view_types
[i
].view_flags
!= desc
->flags
)
63 if (gl_info
->supported
[view_types
[i
].extension
])
64 return view_types
[i
].view_target
;
66 FIXME("Extension %#x not supported.\n", view_types
[i
].extension
);
69 FIXME("Unhandled view flags %#x for texture target %#x.\n", desc
->flags
, texture
->target
);
70 return texture
->target
;
73 static const struct wined3d_format
*validate_resource_view(const struct wined3d_view_desc
*desc
,
74 struct wined3d_resource
*resource
, BOOL mip_slice
)
76 const struct wined3d_gl_info
*gl_info
= &resource
->device
->adapter
->gl_info
;
77 const struct wined3d_format
*format
;
79 format
= wined3d_get_format(gl_info
, desc
->format_id
, resource
->usage
);
80 if (resource
->type
== WINED3D_RTYPE_BUFFER
&& (desc
->flags
& WINED3D_VIEW_BUFFER_RAW
))
82 if (format
->id
!= WINED3DFMT_R32_TYPELESS
)
84 WARN("Invalid format %s for raw buffer view.\n", debug_d3dformat(format
->id
));
88 format
= wined3d_get_format(gl_info
, WINED3DFMT_R32_UINT
, resource
->usage
);
91 if (wined3d_format_is_typeless(format
))
93 WARN("Trying to create view for typeless format %s.\n", debug_d3dformat(format
->id
));
97 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
99 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
100 unsigned int buffer_size
, element_size
;
102 if (buffer
->desc
.structure_byte_stride
)
104 if (desc
->format_id
!= WINED3DFMT_UNKNOWN
)
106 WARN("Invalid format %s for structured buffer view.\n", debug_d3dformat(desc
->format_id
));
110 format
= wined3d_get_format(gl_info
, WINED3DFMT_R32_UINT
, resource
->usage
);
111 element_size
= buffer
->desc
.structure_byte_stride
;
115 element_size
= format
->byte_count
;
121 buffer_size
= buffer
->resource
.size
/ element_size
;
122 if (desc
->u
.buffer
.start_idx
>= buffer_size
123 || desc
->u
.buffer
.count
> buffer_size
- desc
->u
.buffer
.start_idx
)
128 struct wined3d_texture
*texture
= texture_from_resource(resource
);
129 unsigned int depth_or_layer_count
;
131 if (resource
->format
->id
!= format
->id
&& !wined3d_format_is_typeless(resource
->format
)
132 && !wined3d_formats_are_srgb_variants(resource
->format
->id
, format
->id
))
134 WARN("Trying to create incompatible view for non typeless format %s.\n",
135 debug_d3dformat(format
->id
));
139 if (mip_slice
&& resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
140 depth_or_layer_count
= wined3d_texture_get_level_depth(texture
, desc
->u
.texture
.level_idx
);
142 depth_or_layer_count
= texture
->layer_count
;
144 if (!desc
->u
.texture
.level_count
145 || (mip_slice
&& desc
->u
.texture
.level_count
!= 1)
146 || desc
->u
.texture
.level_idx
>= texture
->level_count
147 || desc
->u
.texture
.level_count
> texture
->level_count
- desc
->u
.texture
.level_idx
148 || !desc
->u
.texture
.layer_count
149 || desc
->u
.texture
.layer_idx
>= depth_or_layer_count
150 || desc
->u
.texture
.layer_count
> depth_or_layer_count
- desc
->u
.texture
.layer_idx
)
157 static void create_texture_view(struct wined3d_gl_view
*view
, GLenum view_target
,
158 const struct wined3d_view_desc
*desc
, struct wined3d_texture
*texture
,
159 const struct wined3d_format
*view_format
)
161 const struct wined3d_gl_info
*gl_info
;
162 unsigned int layer_idx
, layer_count
;
163 struct wined3d_context
*context
;
166 view
->target
= view_target
;
168 context
= context_acquire(texture
->resource
.device
, NULL
, 0);
169 gl_info
= context
->gl_info
;
171 if (!gl_info
->supported
[ARB_TEXTURE_VIEW
])
173 context_release(context
);
174 FIXME("OpenGL implementation does not support texture views.\n");
178 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
179 texture_name
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
181 layer_idx
= desc
->u
.texture
.layer_idx
;
182 layer_count
= desc
->u
.texture
.layer_count
;
183 if (view_target
== GL_TEXTURE_3D
&& (layer_idx
|| layer_count
!= 1))
185 FIXME("Depth slice (%u-%u) not supported.\n", layer_idx
, layer_count
);
190 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &view
->name
);
191 GL_EXTCALL(glTextureView(view
->name
, view
->target
, texture_name
, view_format
->glInternal
,
192 desc
->u
.texture
.level_idx
, desc
->u
.texture
.level_count
,
193 layer_idx
, layer_count
));
194 checkGLcall("Create texture view");
196 if (is_stencil_view_format(view_format
))
198 static const GLint swizzle
[] = {GL_ZERO
, GL_RED
, GL_ZERO
, GL_ZERO
};
200 if (!gl_info
->supported
[ARB_STENCIL_TEXTURING
])
202 context_release(context
);
203 FIXME("OpenGL implementation does not support stencil texturing.\n");
207 context_bind_texture(context
, view
->target
, view
->name
);
208 gl_info
->gl_ops
.gl
.p_glTexParameteriv(view
->target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzle
);
209 gl_info
->gl_ops
.gl
.p_glTexParameteri(view
->target
, GL_DEPTH_STENCIL_TEXTURE_MODE
, GL_STENCIL_INDEX
);
210 checkGLcall("Initialize stencil view");
212 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
213 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
216 context_release(context
);
219 static void create_buffer_texture(struct wined3d_gl_view
*view
, struct wined3d_context
*context
,
220 struct wined3d_buffer
*buffer
, const struct wined3d_format
*view_format
,
221 unsigned int offset
, unsigned int size
)
223 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
225 if (!gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
227 FIXME("OpenGL implementation does not support buffer textures.\n");
231 if ((offset
& (gl_info
->limits
.texture_buffer_offset_alignment
- 1)))
233 FIXME("Buffer offset %u is not %u byte aligned.\n",
234 offset
, gl_info
->limits
.texture_buffer_offset_alignment
);
238 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
240 view
->target
= GL_TEXTURE_BUFFER
;
241 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &view
->name
);
243 context_bind_texture(context
, GL_TEXTURE_BUFFER
, view
->name
);
244 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_RANGE
])
246 GL_EXTCALL(glTexBufferRange(GL_TEXTURE_BUFFER
, view_format
->glInternal
,
247 buffer
->buffer_object
, offset
, size
));
251 if (offset
|| size
!= buffer
->resource
.size
)
252 FIXME("OpenGL implementation does not support ARB_texture_buffer_range.\n");
253 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER
, view_format
->glInternal
, buffer
->buffer_object
));
255 checkGLcall("Create buffer texture");
257 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
258 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
261 static void get_buffer_view_range(const struct wined3d_buffer
*buffer
,
262 const struct wined3d_view_desc
*desc
, const struct wined3d_format
*view_format
,
263 unsigned int *offset
, unsigned int *size
)
265 if (desc
->format_id
== WINED3DFMT_UNKNOWN
)
267 *offset
= desc
->u
.buffer
.start_idx
* buffer
->desc
.structure_byte_stride
;
268 *size
= desc
->u
.buffer
.count
* buffer
->desc
.structure_byte_stride
;
272 *offset
= desc
->u
.buffer
.start_idx
* view_format
->byte_count
;
273 *size
= desc
->u
.buffer
.count
* view_format
->byte_count
;
277 static void create_buffer_view(struct wined3d_gl_view
*view
, struct wined3d_context
*context
,
278 const struct wined3d_view_desc
*desc
, struct wined3d_buffer
*buffer
,
279 const struct wined3d_format
*view_format
)
281 unsigned int offset
, size
;
283 get_buffer_view_range(buffer
, desc
, view_format
, &offset
, &size
);
284 create_buffer_texture(view
, context
, buffer
, view_format
, offset
, size
);
287 static void wined3d_view_invalidate_location(struct wined3d_resource
*resource
,
288 const struct wined3d_view_desc
*desc
, DWORD location
)
290 unsigned int i
, sub_resource_idx
, layer_count
;
291 struct wined3d_texture
*texture
;
293 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
295 wined3d_buffer_invalidate_location(buffer_from_resource(resource
), location
);
299 texture
= texture_from_resource(resource
);
301 sub_resource_idx
= desc
->u
.texture
.layer_idx
* texture
->level_count
+ desc
->u
.texture
.level_idx
;
302 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? desc
->u
.texture
.layer_count
: 1;
303 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
304 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, location
);
307 ULONG CDECL
wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view
*view
)
309 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
311 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
316 static void wined3d_rendertarget_view_destroy_object(void *object
)
318 struct wined3d_rendertarget_view
*view
= object
;
319 struct wined3d_device
*device
= view
->resource
->device
;
321 if (view
->gl_view
.name
)
323 const struct wined3d_gl_info
*gl_info
;
324 struct wined3d_context
*context
;
326 context
= context_acquire(device
, NULL
, 0);
327 gl_info
= context
->gl_info
;
328 context_gl_resource_released(device
, view
->gl_view
.name
, FALSE
);
329 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &view
->gl_view
.name
);
330 checkGLcall("glDeleteTextures");
331 context_release(context
);
334 HeapFree(GetProcessHeap(), 0, view
);
337 ULONG CDECL
wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view
*view
)
339 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
341 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
345 struct wined3d_resource
*resource
= view
->resource
;
346 struct wined3d_device
*device
= resource
->device
;
348 /* Call wined3d_object_destroyed() before releasing the resource,
349 * since releasing the resource may end up destroying the parent. */
350 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
351 wined3d_cs_destroy_object(device
->cs
, wined3d_rendertarget_view_destroy_object
, view
);
352 wined3d_resource_decref(resource
);
358 void * CDECL
wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view
*view
)
360 TRACE("view %p.\n", view
);
365 void * CDECL
wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view
*view
)
367 struct wined3d_texture
*texture
;
369 TRACE("view %p.\n", view
);
371 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
372 return wined3d_buffer_get_parent(buffer_from_resource(view
->resource
));
374 texture
= texture_from_resource(view
->resource
);
376 return texture
->sub_resources
[view
->sub_resource_idx
].parent
;
379 void CDECL
wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view
*view
, void *parent
)
381 TRACE("view %p, parent %p.\n", view
, parent
);
383 view
->parent
= parent
;
386 struct wined3d_resource
* CDECL
wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view
*view
)
388 TRACE("view %p.\n", view
);
390 return view
->resource
;
393 void wined3d_rendertarget_view_get_drawable_size(const struct wined3d_rendertarget_view
*view
,
394 const struct wined3d_context
*context
, unsigned int *width
, unsigned int *height
)
396 const struct wined3d_texture
*texture
;
398 if (view
->resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
400 *width
= view
->width
;
401 *height
= view
->height
;
405 texture
= texture_from_resource(view
->resource
);
406 if (texture
->swapchain
)
408 /* The drawable size of an onscreen drawable is the surface size.
409 * (Actually: The window size, but the surface is created in window
411 *width
= texture
->resource
.width
;
412 *height
= texture
->resource
.height
;
414 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
416 const struct wined3d_swapchain
*swapchain
= context
->swapchain
;
418 /* The drawable size of a backbuffer / aux buffer offscreen target is
419 * the size of the current context's drawable, which is the size of
420 * the back buffer of the swapchain the active context belongs to. */
421 *width
= swapchain
->desc
.backbuffer_width
;
422 *height
= swapchain
->desc
.backbuffer_height
;
426 unsigned int level_idx
= view
->sub_resource_idx
% texture
->level_count
;
428 /* The drawable size of an FBO target is the OpenGL texture size,
429 * which is the power of two size. */
430 *width
= wined3d_texture_get_level_pow2_width(texture
, level_idx
);
431 *height
= wined3d_texture_get_level_pow2_height(texture
, level_idx
);
435 void wined3d_rendertarget_view_prepare_location(struct wined3d_rendertarget_view
*view
,
436 struct wined3d_context
*context
, DWORD location
)
438 struct wined3d_resource
*resource
= view
->resource
;
439 unsigned int i
, sub_resource_idx
, layer_count
;
440 struct wined3d_texture
*texture
;
442 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
444 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
448 texture
= texture_from_resource(resource
);
449 sub_resource_idx
= view
->sub_resource_idx
;
450 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
451 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
452 wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
);
455 void wined3d_rendertarget_view_load_location(struct wined3d_rendertarget_view
*view
,
456 struct wined3d_context
*context
, DWORD location
)
458 struct wined3d_resource
*resource
= view
->resource
;
459 unsigned int i
, sub_resource_idx
, layer_count
;
460 struct wined3d_texture
*texture
;
462 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
464 wined3d_buffer_load_location(buffer_from_resource(resource
), context
, location
);
468 texture
= texture_from_resource(resource
);
469 sub_resource_idx
= view
->sub_resource_idx
;
470 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
471 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
472 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
475 void wined3d_rendertarget_view_validate_location(struct wined3d_rendertarget_view
*view
, DWORD location
)
477 struct wined3d_resource
*resource
= view
->resource
;
478 unsigned int i
, sub_resource_idx
, layer_count
;
479 struct wined3d_texture
*texture
;
481 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
483 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
487 texture
= texture_from_resource(resource
);
488 sub_resource_idx
= view
->sub_resource_idx
;
489 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
490 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
491 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
494 void wined3d_rendertarget_view_invalidate_location(struct wined3d_rendertarget_view
*view
, DWORD location
)
496 wined3d_view_invalidate_location(view
->resource
, &view
->desc
, location
);
499 static void wined3d_render_target_view_cs_init(void *object
)
501 struct wined3d_rendertarget_view
*view
= object
;
502 struct wined3d_resource
*resource
= view
->resource
;
503 const struct wined3d_view_desc
*desc
= &view
->desc
;
505 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
507 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
511 struct wined3d_texture
*texture
= texture_from_resource(resource
);
512 unsigned int depth_or_layer_count
;
514 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
515 depth_or_layer_count
= wined3d_texture_get_level_depth(texture
, desc
->u
.texture
.level_idx
);
517 depth_or_layer_count
= texture
->layer_count
;
519 if (resource
->format
->id
!= view
->format
->id
520 || (view
->layer_count
!= 1 && view
->layer_count
!= depth_or_layer_count
))
522 if (resource
->format
->gl_view_class
!= view
->format
->gl_view_class
)
524 FIXME("Render target view not supported, resource format %s, view format %s.\n",
525 debug_d3dformat(resource
->format
->id
), debug_d3dformat(view
->format
->id
));
528 if (texture
->swapchain
&& texture
->swapchain
->desc
.backbuffer_count
> 1)
530 FIXME("Swapchain views not supported.\n");
534 create_texture_view(&view
->gl_view
, texture
->target
, desc
, texture
, view
->format
);
539 static HRESULT
wined3d_rendertarget_view_init(struct wined3d_rendertarget_view
*view
,
540 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
541 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
544 view
->parent
= parent
;
545 view
->parent_ops
= parent_ops
;
547 if (!(view
->format
= validate_resource_view(desc
, resource
, TRUE
)))
549 view
->format_flags
= view
->format
->flags
[resource
->gl_type
];
552 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
554 view
->sub_resource_idx
= 0;
555 view
->layer_count
= 1;
556 view
->width
= desc
->u
.buffer
.count
;
561 struct wined3d_texture
*texture
= texture_from_resource(resource
);
563 view
->sub_resource_idx
= desc
->u
.texture
.level_idx
;
564 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
)
565 view
->sub_resource_idx
+= desc
->u
.texture
.layer_idx
* texture
->level_count
;
566 view
->layer_count
= desc
->u
.texture
.layer_count
;
567 view
->width
= wined3d_texture_get_level_width(texture
, desc
->u
.texture
.level_idx
);
568 view
->height
= wined3d_texture_get_level_height(texture
, desc
->u
.texture
.level_idx
);
571 wined3d_resource_incref(view
->resource
= resource
);
573 wined3d_cs_init_object(resource
->device
->cs
, wined3d_render_target_view_cs_init
, view
);
578 HRESULT CDECL
wined3d_rendertarget_view_create(const struct wined3d_view_desc
*desc
,
579 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
580 struct wined3d_rendertarget_view
**view
)
582 struct wined3d_rendertarget_view
*object
;
585 TRACE("desc %p, resource %p, parent %p, parent_ops %p, view %p.\n",
586 desc
, resource
, parent
, parent_ops
, view
);
588 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
589 return E_OUTOFMEMORY
;
591 if (FAILED(hr
= wined3d_rendertarget_view_init(object
, desc
, resource
, parent
, parent_ops
)))
593 HeapFree(GetProcessHeap(), 0, object
);
594 WARN("Failed to initialise view, hr %#x.\n", hr
);
598 TRACE("Created render target view %p.\n", object
);
604 HRESULT CDECL
wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture
*texture
,
605 unsigned int sub_resource_idx
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
606 struct wined3d_rendertarget_view
**view
)
608 struct wined3d_view_desc desc
;
610 TRACE("texture %p, sub_resource_idx %u, parent %p, parent_ops %p, view %p.\n",
611 texture
, sub_resource_idx
, parent
, parent_ops
, view
);
613 desc
.format_id
= texture
->resource
.format
->id
;
615 desc
.u
.texture
.level_idx
= sub_resource_idx
% texture
->level_count
;
616 desc
.u
.texture
.level_count
= 1;
617 desc
.u
.texture
.layer_idx
= sub_resource_idx
/ texture
->level_count
;
618 desc
.u
.texture
.layer_count
= 1;
620 return wined3d_rendertarget_view_create(&desc
, &texture
->resource
, parent
, parent_ops
, view
);
623 ULONG CDECL
wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view
*view
)
625 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
627 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
632 static void wined3d_shader_resource_view_destroy_object(void *object
)
634 struct wined3d_shader_resource_view
*view
= object
;
636 if (view
->gl_view
.name
)
638 const struct wined3d_gl_info
*gl_info
;
639 struct wined3d_context
*context
;
641 context
= context_acquire(view
->resource
->device
, NULL
, 0);
642 gl_info
= context
->gl_info
;
643 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &view
->gl_view
.name
);
644 checkGLcall("glDeleteTextures");
645 context_release(context
);
648 HeapFree(GetProcessHeap(), 0, view
);
651 ULONG CDECL
wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view
*view
)
653 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
655 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
659 struct wined3d_resource
*resource
= view
->resource
;
660 struct wined3d_device
*device
= resource
->device
;
662 /* Call wined3d_object_destroyed() before releasing the resource,
663 * since releasing the resource may end up destroying the parent. */
664 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
665 wined3d_cs_destroy_object(device
->cs
, wined3d_shader_resource_view_destroy_object
, view
);
666 wined3d_resource_decref(resource
);
672 void * CDECL
wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view
*view
)
674 TRACE("view %p.\n", view
);
679 static void wined3d_shader_resource_view_cs_init(void *object
)
681 struct wined3d_shader_resource_view
*view
= object
;
682 struct wined3d_resource
*resource
= view
->resource
;
683 const struct wined3d_format
*view_format
;
684 const struct wined3d_gl_info
*gl_info
;
685 const struct wined3d_view_desc
*desc
;
688 view_format
= view
->format
;
689 gl_info
= &resource
->device
->adapter
->gl_info
;
692 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
694 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
695 struct wined3d_context
*context
;
697 context
= context_acquire(resource
->device
, NULL
, 0);
698 create_buffer_view(&view
->gl_view
, context
, desc
, buffer
, view_format
);
699 context_release(context
);
703 struct wined3d_texture
*texture
= texture_from_resource(resource
);
705 view_target
= get_texture_view_target(gl_info
, desc
, texture
);
707 if (resource
->format
->id
== view_format
->id
&& texture
->target
== view_target
708 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture
->level_count
709 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture
->layer_count
710 && !is_stencil_view_format(view_format
))
712 TRACE("Creating identity shader resource view.\n");
714 else if (texture
->swapchain
&& texture
->swapchain
->desc
.backbuffer_count
> 1)
716 FIXME("Swapchain shader resource views not supported.\n");
718 else if (resource
->format
->typeless_id
== view_format
->typeless_id
719 && resource
->format
->gl_view_class
== view_format
->gl_view_class
)
721 create_texture_view(&view
->gl_view
, view_target
, desc
, texture
, view_format
);
723 else if (wined3d_format_is_depth_view(resource
->format
->id
, view_format
->id
))
725 create_texture_view(&view
->gl_view
, view_target
, desc
, texture
, resource
->format
);
729 FIXME("Shader resource view not supported, resource format %s, view format %s.\n",
730 debug_d3dformat(resource
->format
->id
), debug_d3dformat(view_format
->id
));
735 static HRESULT
wined3d_shader_resource_view_init(struct wined3d_shader_resource_view
*view
,
736 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
737 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
740 view
->parent
= parent
;
741 view
->parent_ops
= parent_ops
;
743 if (!(view
->format
= validate_resource_view(desc
, resource
, FALSE
)))
747 wined3d_resource_incref(view
->resource
= resource
);
749 wined3d_cs_init_object(resource
->device
->cs
, wined3d_shader_resource_view_cs_init
, view
);
754 HRESULT CDECL
wined3d_shader_resource_view_create(const struct wined3d_view_desc
*desc
,
755 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
756 struct wined3d_shader_resource_view
**view
)
758 struct wined3d_shader_resource_view
*object
;
761 TRACE("desc %p, resource %p, parent %p, parent_ops %p, view %p.\n",
762 desc
, resource
, parent
, parent_ops
, view
);
764 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
765 return E_OUTOFMEMORY
;
767 if (FAILED(hr
= wined3d_shader_resource_view_init(object
, desc
, resource
, parent
, parent_ops
)))
769 HeapFree(GetProcessHeap(), 0, object
);
770 WARN("Failed to initialise view, hr %#x.\n", hr
);
774 TRACE("Created shader resource view %p.\n", object
);
780 void wined3d_shader_resource_view_bind(struct wined3d_shader_resource_view
*view
,
781 unsigned int unit
, struct wined3d_sampler
*sampler
, struct wined3d_context
*context
)
783 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
784 struct wined3d_texture
*texture
;
786 context_active_texture(context
, gl_info
, unit
);
788 if (view
->gl_view
.name
)
790 context_bind_texture(context
, view
->gl_view
.target
, view
->gl_view
.name
);
791 wined3d_sampler_bind(sampler
, unit
, NULL
, context
);
795 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
797 FIXME("Buffer shader resources not supported.\n");
801 texture
= wined3d_texture_from_resource(view
->resource
);
802 wined3d_texture_bind(texture
, context
, FALSE
);
803 wined3d_sampler_bind(sampler
, unit
, texture
, context
);
806 /* Context activation is done by the caller. */
807 static void shader_resource_view_bind_and_dirtify(struct wined3d_shader_resource_view
*view
,
808 struct wined3d_context
*context
)
810 if (context
->active_texture
< ARRAY_SIZE(context
->rev_tex_unit_map
))
812 DWORD active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
813 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
814 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
816 /* FIXME: Ideally we'd only do this when touching a binding that's used by
818 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
819 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
821 context_bind_texture(context
, view
->gl_view
.target
, view
->gl_view
.name
);
824 void shader_resource_view_generate_mipmaps(struct wined3d_shader_resource_view
*view
)
826 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
827 unsigned int i
, j
, layer_count
, level_count
, base_level
, max_level
;
828 const struct wined3d_gl_info
*gl_info
;
829 struct wined3d_context
*context
;
830 struct gl_texture
*gl_tex
;
834 TRACE("view %p.\n", view
);
836 context
= context_acquire(view
->resource
->device
, NULL
, 0);
837 gl_info
= context
->gl_info
;
838 layer_count
= view
->desc
.u
.texture
.layer_count
;
839 level_count
= view
->desc
.u
.texture
.level_count
;
840 base_level
= view
->desc
.u
.texture
.level_idx
;
841 max_level
= base_level
+ level_count
- 1;
843 srgb
= !!(texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
844 location
= srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
;
845 for (i
= 0; i
< layer_count
; ++i
)
846 wined3d_texture_load_location(texture
, i
* level_count
+ base_level
, context
, location
);
847 if (view
->gl_view
.name
)
848 shader_resource_view_bind_and_dirtify(view
, context
);
850 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
851 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
852 GL_EXTCALL(glBindSampler(context
->active_texture
, 0));
853 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture
->target
, GL_TEXTURE_BASE_LEVEL
, base_level
);
854 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture
->target
, GL_TEXTURE_MAX_LEVEL
, max_level
);
855 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
856 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
858 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture
->target
, GL_TEXTURE_SRGB_DECODE_EXT
,
860 gl_tex
->sampler_desc
.srgb_decode
= FALSE
;
863 gl_info
->fbo_ops
.glGenerateMipmap(texture
->target
);
864 checkGLcall("glGenerateMipMap()");
866 for (i
= 0; i
< layer_count
; ++i
)
868 for (j
= base_level
+ 1; j
<= max_level
; ++j
)
870 wined3d_texture_validate_location(texture
, i
* level_count
+ j
, location
);
871 wined3d_texture_invalidate_location(texture
, i
* level_count
+ j
, ~location
);
875 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture
->target
, GL_TEXTURE_MAX_LEVEL
, level_count
- 1);
877 texture
->texture_srgb
.base_level
= base_level
;
879 texture
->texture_rgb
.base_level
= base_level
;
881 context_release(context
);
884 void CDECL
wined3d_shader_resource_view_generate_mipmaps(struct wined3d_shader_resource_view
*view
)
886 struct wined3d_texture
*texture
;
888 TRACE("view %p.\n", view
);
890 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
892 WARN("Called on buffer resource %p.\n", view
->resource
);
896 texture
= texture_from_resource(view
->resource
);
897 if (!(texture
->flags
& WINED3D_TEXTURE_GENERATE_MIPMAPS
))
899 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
903 wined3d_cs_emit_generate_mipmaps(view
->resource
->device
->cs
, view
);
906 ULONG CDECL
wined3d_unordered_access_view_incref(struct wined3d_unordered_access_view
*view
)
908 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
910 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
915 static void wined3d_unordered_access_view_destroy_object(void *object
)
917 struct wined3d_unordered_access_view
*view
= object
;
919 if (view
->gl_view
.name
|| view
->counter_bo
)
921 const struct wined3d_gl_info
*gl_info
;
922 struct wined3d_context
*context
;
924 context
= context_acquire(view
->resource
->device
, NULL
, 0);
925 gl_info
= context
->gl_info
;
926 if (view
->gl_view
.name
)
927 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &view
->gl_view
.name
);
928 if (view
->counter_bo
)
929 GL_EXTCALL(glDeleteBuffers(1, &view
->counter_bo
));
930 checkGLcall("delete resources");
931 context_release(context
);
934 HeapFree(GetProcessHeap(), 0, view
);
937 ULONG CDECL
wined3d_unordered_access_view_decref(struct wined3d_unordered_access_view
*view
)
939 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
941 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
945 struct wined3d_resource
*resource
= view
->resource
;
946 struct wined3d_device
*device
= resource
->device
;
948 /* Call wined3d_object_destroyed() before releasing the resource,
949 * since releasing the resource may end up destroying the parent. */
950 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
951 wined3d_cs_destroy_object(device
->cs
, wined3d_unordered_access_view_destroy_object
, view
);
952 wined3d_resource_decref(resource
);
958 void * CDECL
wined3d_unordered_access_view_get_parent(const struct wined3d_unordered_access_view
*view
)
960 TRACE("view %p.\n", view
);
965 void wined3d_unordered_access_view_invalidate_location(struct wined3d_unordered_access_view
*view
,
968 wined3d_view_invalidate_location(view
->resource
, &view
->desc
, location
);
971 void wined3d_unordered_access_view_clear_uint(struct wined3d_unordered_access_view
*view
,
972 const struct wined3d_uvec4
*clear_value
, struct wined3d_context
*context
)
974 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
975 const struct wined3d_format
*format
;
976 struct wined3d_resource
*resource
;
977 struct wined3d_buffer
*buffer
;
978 unsigned int offset
, size
;
980 resource
= view
->resource
;
981 if (resource
->type
!= WINED3D_RTYPE_BUFFER
)
983 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
987 if (!gl_info
->supported
[ARB_CLEAR_BUFFER_OBJECT
])
989 FIXME("OpenGL implementation does not support ARB_clear_buffer_object.\n");
993 format
= view
->format
;
994 if (format
->id
!= WINED3DFMT_R32_UINT
&& format
->id
!= WINED3DFMT_R32_SINT
995 && format
->id
!= WINED3DFMT_R32G32B32A32_UINT
996 && format
->id
!= WINED3DFMT_R32G32B32A32_SINT
)
998 FIXME("Not implemented for format %s.\n", debug_d3dformat(format
->id
));
1002 buffer
= buffer_from_resource(resource
);
1003 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
1004 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
1006 get_buffer_view_range(buffer
, &view
->desc
, format
, &offset
, &size
);
1007 context_bind_bo(context
, buffer
->buffer_type_hint
, buffer
->buffer_object
);
1008 GL_EXTCALL(glClearBufferSubData(buffer
->buffer_type_hint
, format
->glInternal
,
1009 offset
, size
, format
->glFormat
, format
->glType
, clear_value
));
1010 checkGLcall("clear unordered access view");
1013 void wined3d_unordered_access_view_set_counter(struct wined3d_unordered_access_view
*view
,
1016 const struct wined3d_gl_info
*gl_info
;
1017 struct wined3d_context
*context
;
1019 if (!view
->counter_bo
)
1022 context
= context_acquire(view
->resource
->device
, NULL
, 0);
1023 gl_info
= context
->gl_info
;
1024 GL_EXTCALL(glBindBuffer(GL_ATOMIC_COUNTER_BUFFER
, view
->counter_bo
));
1025 GL_EXTCALL(glBufferSubData(GL_ATOMIC_COUNTER_BUFFER
, 0, sizeof(value
), &value
));
1026 checkGLcall("set atomic counter");
1027 context_release(context
);
1030 void wined3d_unordered_access_view_copy_counter(struct wined3d_unordered_access_view
*view
,
1031 struct wined3d_buffer
*buffer
, unsigned int offset
, struct wined3d_context
*context
)
1033 struct wined3d_bo_address dst
, src
;
1036 if (!view
->counter_bo
)
1039 dst_location
= wined3d_buffer_get_memory(buffer
, &dst
, buffer
->locations
);
1042 src
.buffer_object
= view
->counter_bo
;
1045 context_copy_bo_address(context
, &dst
, buffer
->buffer_type_hint
,
1046 &src
, GL_ATOMIC_COUNTER_BUFFER
, sizeof(GLuint
));
1048 wined3d_buffer_invalidate_location(buffer
, ~dst_location
);
1051 static void wined3d_unordered_access_view_cs_init(void *object
)
1053 struct wined3d_unordered_access_view
*view
= object
;
1054 struct wined3d_resource
*resource
= view
->resource
;
1055 struct wined3d_view_desc
*desc
= &view
->desc
;
1056 const struct wined3d_gl_info
*gl_info
;
1058 gl_info
= &resource
->device
->adapter
->gl_info
;
1060 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1062 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1063 struct wined3d_context
*context
;
1065 context
= context_acquire(resource
->device
, NULL
, 0);
1066 gl_info
= context
->gl_info
;
1067 create_buffer_view(&view
->gl_view
, context
, desc
, buffer
, view
->format
);
1068 if (desc
->flags
& (WINED3D_VIEW_BUFFER_COUNTER
| WINED3D_VIEW_BUFFER_APPEND
))
1070 static const GLuint initial_value
= 0;
1071 GL_EXTCALL(glGenBuffers(1, &view
->counter_bo
));
1072 GL_EXTCALL(glBindBuffer(GL_ATOMIC_COUNTER_BUFFER
, view
->counter_bo
));
1073 GL_EXTCALL(glBufferData(GL_ATOMIC_COUNTER_BUFFER
,
1074 sizeof(initial_value
), &initial_value
, GL_STATIC_DRAW
));
1075 checkGLcall("create atomic counter buffer");
1077 context_release(context
);
1081 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1082 unsigned int depth_or_layer_count
;
1084 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
1085 depth_or_layer_count
= wined3d_texture_get_level_depth(texture
, desc
->u
.texture
.level_idx
);
1087 depth_or_layer_count
= texture
->layer_count
;
1089 if (desc
->u
.texture
.layer_idx
|| desc
->u
.texture
.layer_count
!= depth_or_layer_count
)
1091 create_texture_view(&view
->gl_view
, get_texture_view_target(gl_info
, desc
, texture
),
1092 desc
, texture
, view
->format
);
1097 static HRESULT
wined3d_unordered_access_view_init(struct wined3d_unordered_access_view
*view
,
1098 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1099 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1102 view
->parent
= parent
;
1103 view
->parent_ops
= parent_ops
;
1105 if (!(view
->format
= validate_resource_view(desc
, resource
, TRUE
)))
1106 return E_INVALIDARG
;
1109 wined3d_resource_incref(view
->resource
= resource
);
1111 wined3d_cs_init_object(resource
->device
->cs
, wined3d_unordered_access_view_cs_init
, view
);
1116 HRESULT CDECL
wined3d_unordered_access_view_create(const struct wined3d_view_desc
*desc
,
1117 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1118 struct wined3d_unordered_access_view
**view
)
1120 struct wined3d_unordered_access_view
*object
;
1123 TRACE("desc %p, resource %p, parent %p, parent_ops %p, view %p.\n",
1124 desc
, resource
, parent
, parent_ops
, view
);
1126 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1127 return E_OUTOFMEMORY
;
1129 if (FAILED(hr
= wined3d_unordered_access_view_init(object
, desc
, resource
, parent
, parent_ops
)))
1131 HeapFree(GetProcessHeap(), 0, object
);
1132 WARN("Failed to initialise view, hr %#x.\n", hr
);
1136 TRACE("Created unordered access view %p.\n", object
);