view.c 45.6 KB
Newer Older
1
/*
2
 * Copyright 2009, 2011 Henri Verbeet for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

#include "config.h"
#include "wine/port.h"

#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d);

27 28
#define WINED3D_VIEW_CUBE_ARRAY (WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY)

29 30 31 32 33 34
static BOOL is_stencil_view_format(const struct wined3d_format *format)
{
    return format->id == WINED3DFMT_X24_TYPELESS_G8_UINT
            || 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_gl *texture_gl)
37 38 39 40 41 42
{
    static const struct
    {
        GLenum texture_target;
        unsigned int view_flags;
        GLenum view_target;
43
        enum wined3d_gl_extension extension;
44 45 46
    }
    view_types[] =
    {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        {GL_TEXTURE_CUBE_MAP,  0, GL_TEXTURE_CUBE_MAP},
        {GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_RECTANGLE},
        {GL_TEXTURE_3D,        0, GL_TEXTURE_3D},

        {GL_TEXTURE_2D,       0,                          GL_TEXTURE_2D},
        {GL_TEXTURE_2D,       WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_ARRAY},
        {GL_TEXTURE_2D_ARRAY, 0,                          GL_TEXTURE_2D},
        {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_ARRAY},
        {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_TEXTURE_CUBE,  GL_TEXTURE_CUBE_MAP},
        {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_CUBE_ARRAY,    GL_TEXTURE_CUBE_MAP_ARRAY, ARB_TEXTURE_CUBE_MAP_ARRAY},

        {GL_TEXTURE_2D_MULTISAMPLE,       0,                          GL_TEXTURE_2D_MULTISAMPLE},
        {GL_TEXTURE_2D_MULTISAMPLE,       WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_MULTISAMPLE_ARRAY},
        {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0,                          GL_TEXTURE_2D_MULTISAMPLE},
        {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_MULTISAMPLE_ARRAY},
62 63 64 65 66

        {GL_TEXTURE_1D,       0,                          GL_TEXTURE_1D},
        {GL_TEXTURE_1D,       WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_1D_ARRAY},
        {GL_TEXTURE_1D_ARRAY, 0,                          GL_TEXTURE_1D},
        {GL_TEXTURE_1D_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_1D_ARRAY},
67
    };
68 69
    unsigned int flags = desc->flags & (WINED3D_VIEW_BUFFER_RAW | WINED3D_VIEW_BUFFER_APPEND
            | WINED3D_VIEW_BUFFER_COUNTER | WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY);
70 71 72 73
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(view_types); ++i)
    {
74
        if (view_types[i].texture_target != texture_gl->target || view_types[i].view_flags != flags)
75 76
            continue;
        if (gl_info->supported[view_types[i].extension])
77
            return view_types[i].view_target;
78 79

        FIXME("Extension %#x not supported.\n", view_types[i].extension);
80 81
    }

82
    FIXME("Unhandled view flags %#x for texture target %#x.\n", flags, texture_gl->target);
83
    return texture_gl->target;
84 85
}

86
static const struct wined3d_format *validate_resource_view(const struct wined3d_view_desc *desc,
87
        struct wined3d_resource *resource, BOOL mip_slice, BOOL allow_srgb_toggle)
88
{
89
    const struct wined3d_adapter *adapter = resource->device->adapter;
90 91
    const struct wined3d_format *format;

92
    format = wined3d_get_format(adapter, desc->format_id, resource->bind_flags);
93 94 95 96 97 98 99 100
    if (resource->type == WINED3D_RTYPE_BUFFER && (desc->flags & WINED3D_VIEW_BUFFER_RAW))
    {
        if (format->id != WINED3DFMT_R32_TYPELESS)
        {
            WARN("Invalid format %s for raw buffer view.\n", debug_d3dformat(format->id));
            return NULL;
        }

101
        format = wined3d_get_format(adapter, WINED3DFMT_R32_UINT, resource->bind_flags);
102 103 104 105 106 107 108 109 110 111 112 113 114
    }

    if (wined3d_format_is_typeless(format))
    {
        WARN("Trying to create view for typeless format %s.\n", debug_d3dformat(format->id));
        return NULL;
    }

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        struct wined3d_buffer *buffer = buffer_from_resource(resource);
        unsigned int buffer_size, element_size;

115
        if (buffer->structure_byte_stride)
116 117 118 119 120 121 122
        {
            if (desc->format_id != WINED3DFMT_UNKNOWN)
            {
                WARN("Invalid format %s for structured buffer view.\n", debug_d3dformat(desc->format_id));
                return NULL;
            }

123
            format = wined3d_get_format(adapter, WINED3DFMT_R32_UINT, resource->bind_flags);
124
            element_size = buffer->structure_byte_stride;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        }
        else
        {
            element_size = format->byte_count;
        }

        if (!element_size)
            return NULL;

        buffer_size = buffer->resource.size / element_size;
        if (desc->u.buffer.start_idx >= buffer_size
                || desc->u.buffer.count > buffer_size - desc->u.buffer.start_idx)
            return NULL;
    }
    else
    {
        struct wined3d_texture *texture = texture_from_resource(resource);
        unsigned int depth_or_layer_count;

144
        if (resource->format->id != format->id && !wined3d_format_is_typeless(resource->format)
145
                && (!allow_srgb_toggle || !wined3d_formats_are_srgb_variants(resource->format->id, format->id)))
146 147 148 149 150 151
        {
            WARN("Trying to create incompatible view for non typeless format %s.\n",
                    debug_d3dformat(format->id));
            return NULL;
        }

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        if (mip_slice && resource->type == WINED3D_RTYPE_TEXTURE_3D)
            depth_or_layer_count = wined3d_texture_get_level_depth(texture, desc->u.texture.level_idx);
        else
            depth_or_layer_count = texture->layer_count;

        if (!desc->u.texture.level_count
                || (mip_slice && desc->u.texture.level_count != 1)
                || desc->u.texture.level_idx >= texture->level_count
                || desc->u.texture.level_count > texture->level_count - desc->u.texture.level_idx
                || !desc->u.texture.layer_count
                || desc->u.texture.layer_idx >= depth_or_layer_count
                || desc->u.texture.layer_count > depth_or_layer_count - desc->u.texture.layer_idx)
            return NULL;
    }

    return format;
}

170
static void create_texture_view(struct wined3d_gl_view *view, GLenum view_target,
171
        const struct wined3d_view_desc *desc, struct wined3d_texture_gl *texture_gl,
172 173
        const struct wined3d_format *view_format)
{
174
    const struct wined3d_format_gl *view_format_gl;
175
    unsigned int level_idx, layer_idx, layer_count;
176
    const struct wined3d_gl_info *gl_info;
177
    struct wined3d_context_gl *context_gl;
178
    struct wined3d_context *context;
179
    GLuint texture_name;
180

181
    view_format_gl = wined3d_format_gl(view_format);
182 183
    view->target = view_target;

184
    context = context_acquire(texture_gl->t.resource.device, NULL, 0);
185
    context_gl = wined3d_context_gl(context);
186
    gl_info = context_gl->gl_info;
187 188 189 190 191 192 193 194

    if (!gl_info->supported[ARB_TEXTURE_VIEW])
    {
        context_release(context);
        FIXME("OpenGL implementation does not support texture views.\n");
        return;
    }

195
    wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
196
    texture_name = wined3d_texture_gl_get_texture_name(texture_gl, context, FALSE);
197

198
    level_idx = desc->u.texture.level_idx;
199 200
    layer_idx = desc->u.texture.layer_idx;
    layer_count = desc->u.texture.layer_count;
201
    if (view_target == GL_TEXTURE_3D)
202
    {
203
        if (layer_idx || layer_count != wined3d_texture_get_level_depth(&texture_gl->t, level_idx))
204
            FIXME("Depth slice (%u-%u) not supported.\n", layer_idx, layer_count);
205 206 207 208
        layer_idx = 0;
        layer_count = 1;
    }

209
    gl_info->gl_ops.gl.p_glGenTextures(1, &view->name);
210
    GL_EXTCALL(glTextureView(view->name, view->target, texture_name, view_format_gl->internal,
211 212
            level_idx, desc->u.texture.level_count, layer_idx, layer_count));
    checkGLcall("create texture view");
213

214
    if (is_stencil_view_format(view_format))
215 216 217 218 219 220 221 222 223 224
    {
        static const GLint swizzle[] = {GL_ZERO, GL_RED, GL_ZERO, GL_ZERO};

        if (!gl_info->supported[ARB_STENCIL_TEXTURING])
        {
            context_release(context);
            FIXME("OpenGL implementation does not support stencil texturing.\n");
            return;
        }

225
        wined3d_context_gl_bind_texture(context_gl, view->target, view->name);
226 227
        gl_info->gl_ops.gl.p_glTexParameteriv(view->target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
        gl_info->gl_ops.gl.p_glTexParameteri(view->target, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
228
        checkGLcall("initialize stencil view");
229

230 231
        context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
        context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
232
    }
233
    else if (!is_identity_fixup(view_format->color_fixup) && can_use_texture_swizzle(context->d3d_info, view_format))
234 235 236
    {
        GLint swizzle[4];

237
        wined3d_context_gl_bind_texture(context_gl, view->target, view->name);
238 239 240
        wined3d_gl_texture_swizzle_from_color_fixup(swizzle, view_format->color_fixup);
        gl_info->gl_ops.gl.p_glTexParameteriv(view->target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
        checkGLcall("set format swizzle");
241 242 243

        context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
        context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
244
    }
245 246 247 248

    context_release(context);
}

249
static void create_buffer_texture(struct wined3d_gl_view *view, struct wined3d_context *context,
250 251
        struct wined3d_buffer *buffer, const struct wined3d_format *view_format,
        unsigned int offset, unsigned int size)
252
{
253
    struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
254
    const struct wined3d_gl_info *gl_info = context_gl->gl_info;
255
    const struct wined3d_format_gl *view_format_gl;
256 257 258 259 260 261 262

    if (!gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
    {
        FIXME("OpenGL implementation does not support buffer textures.\n");
        return;
    }

263 264 265 266 267 268 269
    if ((offset & (gl_info->limits.texture_buffer_offset_alignment - 1)))
    {
        FIXME("Buffer offset %u is not %u byte aligned.\n",
                offset, gl_info->limits.texture_buffer_offset_alignment);
        return;
    }

270
    view_format_gl = wined3d_format_gl(view_format);
271 272 273 274 275
    wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);

    view->target = GL_TEXTURE_BUFFER;
    gl_info->gl_ops.gl.p_glGenTextures(1, &view->name);

276
    wined3d_context_gl_bind_texture(context_gl, GL_TEXTURE_BUFFER, view->name);
277 278
    if (gl_info->supported[ARB_TEXTURE_BUFFER_RANGE])
    {
279
        GL_EXTCALL(glTexBufferRange(GL_TEXTURE_BUFFER, view_format_gl->internal,
280
                buffer->buffer_object, offset, size));
281 282 283
    }
    else
    {
284
        if (offset || size != buffer->resource.size)
285
            FIXME("OpenGL implementation does not support ARB_texture_buffer_range.\n");
286
        GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, view_format_gl->internal, buffer->buffer_object));
287
    }
288 289
    checkGLcall("Create buffer texture");

290 291
    context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
    context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
292 293
}

294 295 296
static void get_buffer_view_range(const struct wined3d_buffer *buffer,
        const struct wined3d_view_desc *desc, const struct wined3d_format *view_format,
        unsigned int *offset, unsigned int *size)
297 298 299
{
    if (desc->format_id == WINED3DFMT_UNKNOWN)
    {
300 301
        *offset = desc->u.buffer.start_idx * buffer->structure_byte_stride;
        *size = desc->u.buffer.count * buffer->structure_byte_stride;
302 303 304
    }
    else
    {
305 306
        *offset = desc->u.buffer.start_idx * view_format->byte_count;
        *size = desc->u.buffer.count * view_format->byte_count;
307
    }
308
}
309

310 311 312 313 314 315 316
static void create_buffer_view(struct wined3d_gl_view *view, struct wined3d_context *context,
        const struct wined3d_view_desc *desc, struct wined3d_buffer *buffer,
        const struct wined3d_format *view_format)
{
    unsigned int offset, size;

    get_buffer_view_range(buffer, desc, view_format, &offset, &size);
317
    create_buffer_texture(view, context, buffer, view_format, offset, size);
318 319
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
static void wined3d_view_invalidate_location(struct wined3d_resource *resource,
        const struct wined3d_view_desc *desc, DWORD location)
{
    unsigned int i, sub_resource_idx, layer_count;
    struct wined3d_texture *texture;

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        wined3d_buffer_invalidate_location(buffer_from_resource(resource), location);
        return;
    }

    texture = texture_from_resource(resource);

    sub_resource_idx = desc->u.texture.layer_idx * texture->level_count + desc->u.texture.level_idx;
    layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? desc->u.texture.layer_count : 1;
    for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
        wined3d_texture_invalidate_location(texture, sub_resource_idx, location);
}

340
ULONG CDECL wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view *view)
341
{
342
    ULONG refcount = InterlockedIncrement(&view->refcount);
343

344
    TRACE("%p increasing refcount to %u.\n", view, refcount);
345 346 347 348

    return refcount;
}

349
void wined3d_rendertarget_view_cleanup(struct wined3d_rendertarget_view *view)
350
{
351 352 353 354
    /* Call wined3d_object_destroyed() before releasing the resource,
     * since releasing the resource may end up destroying the parent. */
    view->parent_ops->wined3d_object_destroyed(view->parent);
    wined3d_resource_decref(view->resource);
355 356
}

357
ULONG CDECL wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view *view)
358
{
359
    ULONG refcount = InterlockedDecrement(&view->refcount);
360

361
    TRACE("%p decreasing refcount to %u.\n", view, refcount);
362 363

    if (!refcount)
364
        view->resource->device->adapter->adapter_ops->adapter_destroy_rendertarget_view(view);
365 366 367 368

    return refcount;
}

369
void * CDECL wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view *view)
370
{
371
    TRACE("view %p.\n", view);
372

373
    return view->parent;
374 375
}

376 377
void * CDECL wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view *view)
{
378
    struct wined3d_texture *texture;
379 380 381 382 383 384

    TRACE("view %p.\n", view);

    if (view->resource->type == WINED3D_RTYPE_BUFFER)
        return wined3d_buffer_get_parent(buffer_from_resource(view->resource));

385
    texture = texture_from_resource(view->resource);
386

387
    return texture->sub_resources[view->sub_resource_idx].parent;
388 389 390 391 392 393 394 395 396
}

void CDECL wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view *view, void *parent)
{
    TRACE("view %p, parent %p.\n", view, parent);

    view->parent = parent;
}

397
struct wined3d_resource * CDECL wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view *view)
398
{
399
    TRACE("view %p.\n", view);
400

401
    return view->resource;
402 403
}

404 405
void wined3d_rendertarget_view_get_drawable_size(const struct wined3d_rendertarget_view *view,
        const struct wined3d_context *context, unsigned int *width, unsigned int *height)
406
{
407 408 409 410
    const struct wined3d_texture *texture;

    if (view->resource->type != WINED3D_RTYPE_TEXTURE_2D)
    {
411 412
        *width = view->width;
        *height = view->height;
413 414 415 416 417
        return;
    }

    texture = texture_from_resource(view->resource);
    if (texture->swapchain)
418 419 420 421
    {
        /* The drawable size of an onscreen drawable is the surface size.
         * (Actually: The window size, but the surface is created in window
         * size.) */
422 423
        *width = texture->resource.width;
        *height = texture->resource.height;
424 425 426
    }
    else if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
    {
427
        const struct wined3d_swapchain_desc *desc = &context->swapchain->state.desc;
428 429 430 431

        /* The drawable size of a backbuffer / aux buffer offscreen target is
         * the size of the current context's drawable, which is the size of
         * the back buffer of the swapchain the active context belongs to. */
432 433
        *width = desc->backbuffer_width;
        *height = desc->backbuffer_height;
434 435 436
    }
    else
    {
437
        unsigned int level_idx = view->sub_resource_idx % texture->level_count;
438 439 440

        /* The drawable size of an FBO target is the OpenGL texture size,
         * which is the power of two size. */
441 442
        *width = wined3d_texture_get_level_pow2_width(texture, level_idx);
        *height = wined3d_texture_get_level_pow2_height(texture, level_idx);
443 444 445
    }
}

446 447 448 449
void wined3d_rendertarget_view_prepare_location(struct wined3d_rendertarget_view *view,
        struct wined3d_context *context, DWORD location)
{
    struct wined3d_resource *resource = view->resource;
450
    unsigned int i, sub_resource_idx, layer_count;
451 452 453 454 455 456 457 458 459 460
    struct wined3d_texture *texture;

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
        return;
    }

    texture = texture_from_resource(resource);
    sub_resource_idx = view->sub_resource_idx;
461 462
    layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
    for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
463 464 465
        wined3d_texture_prepare_location(texture, sub_resource_idx, context, location);
}

466 467 468 469
void wined3d_rendertarget_view_load_location(struct wined3d_rendertarget_view *view,
        struct wined3d_context *context, DWORD location)
{
    struct wined3d_resource *resource = view->resource;
470
    unsigned int i, sub_resource_idx, layer_count;
471 472 473 474 475 476 477 478 479 480
    struct wined3d_texture *texture;

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        wined3d_buffer_load_location(buffer_from_resource(resource), context, location);
        return;
    }

    texture = texture_from_resource(resource);
    sub_resource_idx = view->sub_resource_idx;
481 482
    layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
    for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
483 484 485
        wined3d_texture_load_location(texture, sub_resource_idx, context, location);
}

486 487 488
void wined3d_rendertarget_view_validate_location(struct wined3d_rendertarget_view *view, DWORD location)
{
    struct wined3d_resource *resource = view->resource;
489
    unsigned int i, sub_resource_idx, layer_count;
490 491 492 493 494 495 496 497 498 499
    struct wined3d_texture *texture;

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
        return;
    }

    texture = texture_from_resource(resource);
    sub_resource_idx = view->sub_resource_idx;
500 501
    layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
    for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
502 503 504
        wined3d_texture_validate_location(texture, sub_resource_idx, location);
}

505 506 507 508 509
void wined3d_rendertarget_view_invalidate_location(struct wined3d_rendertarget_view *view, DWORD location)
{
    wined3d_view_invalidate_location(view->resource, &view->desc, location);
}

510
static void wined3d_render_target_view_gl_cs_init(void *object)
511
{
512 513 514
    struct wined3d_rendertarget_view_gl *view_gl = object;
    struct wined3d_resource *resource = view_gl->v.resource;
    const struct wined3d_view_desc *desc = &view_gl->v.desc;
515 516 517 518 519 520 521

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
    }
    else
    {
522
        struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
523 524 525
        unsigned int depth_or_layer_count;

        if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
526
            depth_or_layer_count = wined3d_texture_get_level_depth(&texture_gl->t, desc->u.texture.level_idx);
527
        else
528
            depth_or_layer_count = texture_gl->t.layer_count;
529

530 531
        if (resource->format->id != view_gl->v.format->id
                || (view_gl->v.layer_count != 1 && view_gl->v.layer_count != depth_or_layer_count))
532
        {
533 534 535
            GLenum resource_class, view_class;

            resource_class = wined3d_format_gl(resource->format)->view_class;
536
            view_class = wined3d_format_gl(view_gl->v.format)->view_class;
537
            if (resource_class != view_class)
538 539
            {
                FIXME("Render target view not supported, resource format %s, view format %s.\n",
540
                        debug_d3dformat(resource->format->id), debug_d3dformat(view_gl->v.format->id));
541 542
                return;
            }
543
            if (texture_gl->t.swapchain && texture_gl->t.swapchain->state.desc.backbuffer_count > 1)
544 545 546 547 548
            {
                FIXME("Swapchain views not supported.\n");
                return;
            }

549
            create_texture_view(&view_gl->gl_view, texture_gl->target, desc, texture_gl, view_gl->v.format);
550 551 552 553
        }
    }
}

554
static HRESULT wined3d_rendertarget_view_init(struct wined3d_rendertarget_view *view,
555
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
556
        void *parent, const struct wined3d_parent_ops *parent_ops)
557
{
558 559
    BOOL allow_srgb_toggle = FALSE;

560 561
    view->refcount = 1;
    view->parent = parent;
562
    view->parent_ops = parent_ops;
563

564 565 566 567 568 569 570 571
    if (resource->type != WINED3D_RTYPE_BUFFER)
    {
        struct wined3d_texture *texture = texture_from_resource(resource);

        if (texture->swapchain)
            allow_srgb_toggle = TRUE;
    }
    if (!(view->format = validate_resource_view(desc, resource, TRUE, allow_srgb_toggle)))
572
        return E_INVALIDARG;
573
    view->format_flags = view->format->flags[resource->gl_type];
574
    view->desc = *desc;
575

576 577 578
    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        view->sub_resource_idx = 0;
579
        view->layer_count = 1;
580 581 582 583 584
        view->width = desc->u.buffer.count;
        view->height = 1;
    }
    else
    {
585
        struct wined3d_texture *texture = texture_from_resource(resource);
586 587 588 589

        view->sub_resource_idx = desc->u.texture.level_idx;
        if (resource->type != WINED3D_RTYPE_TEXTURE_3D)
            view->sub_resource_idx += desc->u.texture.layer_idx * texture->level_count;
590
        view->layer_count = desc->u.texture.layer_count;
591 592
        view->width = wined3d_texture_get_level_width(texture, desc->u.texture.level_idx);
        view->height = wined3d_texture_get_level_height(texture, desc->u.texture.level_idx);
593
    }
594

595 596 597
    wined3d_resource_incref(view->resource = resource);

    return WINED3D_OK;
598
}
599

600 601 602
HRESULT wined3d_rendertarget_view_no3d_init(struct wined3d_rendertarget_view *view_no3d,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
603
{
604 605
    TRACE("view_no3d %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_no3d, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
606

607 608
    return wined3d_rendertarget_view_init(view_no3d, desc, resource, parent, parent_ops);
}
609

610 611 612 613 614
HRESULT wined3d_rendertarget_view_gl_init(struct wined3d_rendertarget_view_gl *view_gl,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
{
    HRESULT hr;
615

616 617
    TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_gl, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
618

619 620
    if (FAILED(hr = wined3d_rendertarget_view_init(&view_gl->v, desc, resource, parent, parent_ops)))
        return hr;
621

622
    wined3d_cs_init_object(resource->device->cs, wined3d_render_target_view_gl_cs_init, view_gl);
623

624
    return hr;
625
}
626

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
HRESULT wined3d_rendertarget_view_vk_init(struct wined3d_rendertarget_view_vk *view_vk,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
{
    TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_vk, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);

    return wined3d_rendertarget_view_init(&view_vk->v, desc, resource, parent, parent_ops);
}

HRESULT CDECL wined3d_rendertarget_view_create(const struct wined3d_view_desc *desc,
        struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
        struct wined3d_rendertarget_view **view)
{
    const struct wined3d_adapter_ops *adapter_ops;

    TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
            wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops, view);

    adapter_ops = resource->device->adapter->adapter_ops;
    return adapter_ops->adapter_create_rendertarget_view(desc, resource, parent, parent_ops, view);
}

650 651 652 653
HRESULT CDECL wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture *texture,
        unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops,
        struct wined3d_rendertarget_view **view)
{
654
    struct wined3d_view_desc desc;
655 656 657 658

    TRACE("texture %p, sub_resource_idx %u, parent %p, parent_ops %p, view %p.\n",
            texture, sub_resource_idx, parent, parent_ops, view);

659
    desc.format_id = texture->resource.format->id;
660
    desc.flags = 0;
661
    desc.u.texture.level_idx = sub_resource_idx % texture->level_count;
662
    desc.u.texture.level_count = 1;
663 664
    desc.u.texture.layer_idx = sub_resource_idx / texture->level_count;
    desc.u.texture.layer_count = 1;
665

666
    return wined3d_rendertarget_view_create(&desc, &texture->resource, parent, parent_ops, view);
667 668
}

669 670 671 672 673 674 675 676 677
ULONG CDECL wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view *view)
{
    ULONG refcount = InterlockedIncrement(&view->refcount);

    TRACE("%p increasing refcount to %u.\n", view, refcount);

    return refcount;
}

678
void wined3d_shader_resource_view_cleanup(struct wined3d_shader_resource_view *view)
679
{
680 681 682 683
    /* Call wined3d_object_destroyed() before releasing the resource,
     * since releasing the resource may end up destroying the parent. */
    view->parent_ops->wined3d_object_destroyed(view->parent);
    wined3d_resource_decref(view->resource);
684 685
}

686 687 688 689 690 691 692
ULONG CDECL wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view *view)
{
    ULONG refcount = InterlockedDecrement(&view->refcount);

    TRACE("%p decreasing refcount to %u.\n", view, refcount);

    if (!refcount)
693
        view->resource->device->adapter->adapter_ops->adapter_destroy_shader_resource_view(view);
694 695 696 697

    return refcount;
}

698 699 700 701 702 703 704
void * CDECL wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view *view)
{
    TRACE("view %p.\n", view);

    return view->parent;
}

705
static void wined3d_shader_resource_view_gl_cs_init(void *object)
706
{
707 708
    struct wined3d_shader_resource_view_gl *view_gl = object;
    struct wined3d_resource *resource = view_gl->v.resource;
709 710 711 712 713
    const struct wined3d_format *view_format;
    const struct wined3d_gl_info *gl_info;
    const struct wined3d_view_desc *desc;
    GLenum view_target;

714
    view_format = view_gl->v.format;
715
    gl_info = &resource->device->adapter->gl_info;
716
    desc = &view_gl->v.desc;
717 718 719 720

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        struct wined3d_buffer *buffer = buffer_from_resource(resource);
721
        struct wined3d_context *context;
722

723
        context = context_acquire(resource->device, NULL, 0);
724
        create_buffer_view(&view_gl->gl_view, context, desc, buffer, view_format);
725
        context_release(context);
726 727 728
    }
    else
    {
729
        struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
730
        GLenum resource_class, view_class;
731

732 733
        resource_class = wined3d_format_gl(resource->format)->view_class;
        view_class = wined3d_format_gl(view_format)->view_class;
734
        view_target = get_texture_view_target(gl_info, desc, texture_gl);
735

736
        if (resource->format->id == view_format->id && texture_gl->target == view_target
737 738
                && !desc->u.texture.level_idx && desc->u.texture.level_count == texture_gl->t.level_count
                && !desc->u.texture.layer_idx && desc->u.texture.layer_count == texture_gl->t.layer_count
739 740 741 742
                && !is_stencil_view_format(view_format))
        {
            TRACE("Creating identity shader resource view.\n");
        }
743
        else if (texture_gl->t.swapchain && texture_gl->t.swapchain->state.desc.backbuffer_count > 1)
744 745 746 747
        {
            FIXME("Swapchain shader resource views not supported.\n");
        }
        else if (resource->format->typeless_id == view_format->typeless_id
748
                && resource_class == view_class)
749
        {
750
            create_texture_view(&view_gl->gl_view, view_target, desc, texture_gl, view_format);
751
        }
752 753
        else if (wined3d_format_is_depth_view(resource->format->id, view_format->id))
        {
754
            create_texture_view(&view_gl->gl_view, view_target, desc, texture_gl, resource->format);
755
        }
756 757 758 759 760 761 762 763
        else
        {
            FIXME("Shader resource view not supported, resource format %s, view format %s.\n",
                    debug_d3dformat(resource->format->id), debug_d3dformat(view_format->id));
        }
    }
}

764
static HRESULT wined3d_shader_resource_view_init(struct wined3d_shader_resource_view *view,
765
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
766 767
        void *parent, const struct wined3d_parent_ops *parent_ops)
{
768 769 770
    view->refcount = 1;
    view->parent = parent;
    view->parent_ops = parent_ops;
771

772 773
    if (!(resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
        return E_INVALIDARG;
774
    if (!(view->format = validate_resource_view(desc, resource, FALSE, FALSE)))
775
        return E_INVALIDARG;
776 777
    view->desc = *desc;

778 779
    wined3d_resource_incref(view->resource = resource);

780 781 782
    return WINED3D_OK;
}

783 784 785
HRESULT wined3d_shader_resource_view_gl_init(struct wined3d_shader_resource_view_gl *view_gl,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
786
{
787
    HRESULT hr;
788

789 790
    TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_gl, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
791

792
    if (FAILED(hr = wined3d_shader_resource_view_init(&view_gl->v, desc, resource, parent, parent_ops)))
793
        return hr;
794

795
    wined3d_cs_init_object(resource->device->cs, wined3d_shader_resource_view_gl_cs_init, view_gl);
796

797 798
    return hr;
}
799

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
HRESULT wined3d_shader_resource_view_vk_init(struct wined3d_shader_resource_view_vk *view_vk,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
{
    TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_vk, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);

    return wined3d_shader_resource_view_init(&view_vk->v, desc, resource, parent, parent_ops);
}

HRESULT CDECL wined3d_shader_resource_view_create(const struct wined3d_view_desc *desc,
        struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
        struct wined3d_shader_resource_view **view)
{
    const struct wined3d_adapter_ops *adapter_ops;

    TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
            wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops, view);

    adapter_ops = resource->device->adapter->adapter_ops;
    return adapter_ops->adapter_create_shader_resource_view(desc, resource, parent, parent_ops, view);
821
}
822

823
void wined3d_shader_resource_view_gl_bind(struct wined3d_shader_resource_view_gl *view_gl,
824
        unsigned int unit, struct wined3d_sampler_gl *sampler_gl, struct wined3d_context_gl *context_gl)
825
{
826
    const struct wined3d_gl_info *gl_info = context_gl->gl_info;
827
    struct wined3d_texture_gl *texture_gl;
828

829
    wined3d_context_gl_active_texture(context_gl, gl_info, unit);
830

831
    if (view_gl->gl_view.name)
832
    {
833
        wined3d_context_gl_bind_texture(context_gl, view_gl->gl_view.target, view_gl->gl_view.name);
834
        wined3d_sampler_gl_bind(sampler_gl, unit, NULL, context_gl);
835 836 837
        return;
    }

838
    if (view_gl->v.resource->type == WINED3D_RTYPE_BUFFER)
839 840 841 842 843
    {
        FIXME("Buffer shader resources not supported.\n");
        return;
    }

844
    texture_gl = wined3d_texture_gl(wined3d_texture_from_resource(view_gl->v.resource));
845
    wined3d_texture_gl_bind(texture_gl, context_gl, FALSE);
846
    wined3d_sampler_gl_bind(sampler_gl, unit, texture_gl, context_gl);
847
}
848

849
/* Context activation is done by the caller. */
850
static void shader_resource_view_gl_bind_and_dirtify(struct wined3d_shader_resource_view_gl *view_gl,
851
        struct wined3d_context_gl *context_gl)
852
{
853
    if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
854
    {
855
        unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
856
        if (active_sampler != WINED3D_UNMAPPED_STAGE)
857
            context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
858 859 860
    }
    /* FIXME: Ideally we'd only do this when touching a binding that's used by
     * a shader. */
861 862
    context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
    context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
863

864
    wined3d_context_gl_bind_texture(context_gl, view_gl->gl_view.target, view_gl->gl_view.name);
865 866 867 868
}

void shader_resource_view_generate_mipmaps(struct wined3d_shader_resource_view *view)
{
869
    struct wined3d_shader_resource_view_gl *view_gl = wined3d_shader_resource_view_gl(view);
870 871
    unsigned int i, j, layer_count, level_count, base_level, max_level;
    const struct wined3d_gl_info *gl_info;
872
    struct wined3d_texture_gl *texture_gl;
873
    struct wined3d_context_gl *context_gl;
874 875 876 877 878 879 880
    struct wined3d_context *context;
    struct gl_texture *gl_tex;
    DWORD location;
    BOOL srgb;

    TRACE("view %p.\n", view);

881
    context = context_acquire(view_gl->v.resource->device, NULL, 0);
882
    context_gl = wined3d_context_gl(context);
883
    gl_info = context_gl->gl_info;
884 885 886
    layer_count = view_gl->v.desc.u.texture.layer_count;
    level_count = view_gl->v.desc.u.texture.level_count;
    base_level = view_gl->v.desc.u.texture.level_idx;
887 888
    max_level = base_level + level_count - 1;

889
    texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
890
    srgb = !!(texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
891 892
    location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
    for (i = 0; i < layer_count; ++i)
893
        wined3d_texture_load_location(&texture_gl->t, i * level_count + base_level, context, location);
894

895
    if (view_gl->gl_view.name)
896
    {
897
        shader_resource_view_gl_bind_and_dirtify(view_gl, context_gl);
898
    }
899
    else
900
    {
901
        wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
902 903
        gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_BASE_LEVEL, base_level);
        gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_MAX_LEVEL, max_level);
904 905
    }

906
    if (gl_info->supported[ARB_SAMPLER_OBJECTS])
907
        GL_EXTCALL(glBindSampler(context_gl->active_texture, 0));
908
    gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
909 910
    if (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
    {
911 912
        gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target,
                GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
913 914 915
        gl_tex->sampler_desc.srgb_decode = FALSE;
    }

916
    gl_info->fbo_ops.glGenerateMipmap(texture_gl->target);
917 918 919 920 921 922
    checkGLcall("glGenerateMipMap()");

    for (i = 0; i < layer_count; ++i)
    {
        for (j = base_level + 1; j <= max_level; ++j)
        {
923 924
            wined3d_texture_validate_location(&texture_gl->t, i * level_count + j, location);
            wined3d_texture_invalidate_location(&texture_gl->t, i * level_count + j, ~location);
925 926 927
        }
    }

928
    if (!view_gl->gl_view.name)
929 930
    {
        gl_tex->base_level = base_level;
931
        gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target,
932
                GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
933
    }
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

    context_release(context);
}

void CDECL wined3d_shader_resource_view_generate_mipmaps(struct wined3d_shader_resource_view *view)
{
    struct wined3d_texture *texture;

    TRACE("view %p.\n", view);

    if (view->resource->type == WINED3D_RTYPE_BUFFER)
    {
        WARN("Called on buffer resource %p.\n", view->resource);
        return;
    }

    texture = texture_from_resource(view->resource);
    if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
    {
        WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
        return;
    }

    wined3d_cs_emit_generate_mipmaps(view->resource->device->cs, view);
}

960 961 962 963 964 965 966 967 968
ULONG CDECL wined3d_unordered_access_view_incref(struct wined3d_unordered_access_view *view)
{
    ULONG refcount = InterlockedIncrement(&view->refcount);

    TRACE("%p increasing refcount to %u.\n", view, refcount);

    return refcount;
}

969
void wined3d_unordered_access_view_cleanup(struct wined3d_unordered_access_view *view)
970
{
971 972 973 974
    /* Call wined3d_object_destroyed() before releasing the resource,
     * since releasing the resource may end up destroying the parent. */
    view->parent_ops->wined3d_object_destroyed(view->parent);
    wined3d_resource_decref(view->resource);
975 976 977 978 979 980 981 982 983
}

ULONG CDECL wined3d_unordered_access_view_decref(struct wined3d_unordered_access_view *view)
{
    ULONG refcount = InterlockedDecrement(&view->refcount);

    TRACE("%p decreasing refcount to %u.\n", view, refcount);

    if (!refcount)
984
        view->resource->device->adapter->adapter_ops->adapter_destroy_unordered_access_view(view);
985 986 987 988 989 990 991 992 993 994 995

    return refcount;
}

void * CDECL wined3d_unordered_access_view_get_parent(const struct wined3d_unordered_access_view *view)
{
    TRACE("view %p.\n", view);

    return view->parent;
}

996 997 998
void wined3d_unordered_access_view_invalidate_location(struct wined3d_unordered_access_view *view,
        DWORD location)
{
999
    wined3d_view_invalidate_location(view->resource, &view->desc, location);
1000 1001
}

1002 1003
void wined3d_unordered_access_view_gl_clear_uint(struct wined3d_unordered_access_view_gl *view_gl,
        const struct wined3d_uvec4 *clear_value, struct wined3d_context_gl *context_gl)
1004
{
1005
    const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1006
    const struct wined3d_format_gl *format;
1007
    struct wined3d_buffer_gl *buffer_gl;
1008 1009 1010
    struct wined3d_resource *resource;
    unsigned int offset, size;

1011
    resource = view_gl->v.resource;
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    if (resource->type != WINED3D_RTYPE_BUFFER)
    {
        FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
        return;
    }

    if (!gl_info->supported[ARB_CLEAR_BUFFER_OBJECT])
    {
        FIXME("OpenGL implementation does not support ARB_clear_buffer_object.\n");
        return;
    }

1024
    format = wined3d_format_gl(view_gl->v.format);
1025 1026 1027
    if (format->f.id != WINED3DFMT_R32_UINT && format->f.id != WINED3DFMT_R32_SINT
            && format->f.id != WINED3DFMT_R32G32B32A32_UINT
            && format->f.id != WINED3DFMT_R32G32B32A32_SINT)
1028
    {
1029
        FIXME("Not implemented for format %s.\n", debug_d3dformat(format->f.id));
1030 1031 1032
        return;
    }

1033
    buffer_gl = wined3d_buffer_gl(buffer_from_resource(resource));
1034 1035
    wined3d_buffer_load_location(&buffer_gl->b, &context_gl->c, WINED3D_LOCATION_BUFFER);
    wined3d_unordered_access_view_invalidate_location(&view_gl->v, ~WINED3D_LOCATION_BUFFER);
1036

1037
    get_buffer_view_range(&buffer_gl->b, &view_gl->v.desc, &format->f, &offset, &size);
1038
    wined3d_context_gl_bind_bo(context_gl, buffer_gl->buffer_type_hint, buffer_gl->b.buffer_object);
1039
    GL_EXTCALL(glClearBufferSubData(buffer_gl->buffer_type_hint, format->internal,
1040
            offset, size, format->format, format->type, clear_value));
1041
    checkGLcall("clear unordered access view");
1042 1043
}

1044 1045 1046
void wined3d_unordered_access_view_set_counter(struct wined3d_unordered_access_view *view,
        unsigned int value)
{
1047
    struct wined3d_unordered_access_view_gl *view_gl = wined3d_unordered_access_view_gl(view);
1048 1049 1050
    const struct wined3d_gl_info *gl_info;
    struct wined3d_context *context;

1051
    if (!view_gl->counter_bo)
1052 1053
        return;

1054
    context = context_acquire(view_gl->v.resource->device, NULL, 0);
1055
    gl_info = wined3d_context_gl(context)->gl_info;
1056
    GL_EXTCALL(glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, view_gl->counter_bo));
1057 1058 1059 1060 1061
    GL_EXTCALL(glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(value), &value));
    checkGLcall("set atomic counter");
    context_release(context);
}

1062 1063 1064
void wined3d_unordered_access_view_copy_counter(struct wined3d_unordered_access_view *view,
        struct wined3d_buffer *buffer, unsigned int offset, struct wined3d_context *context)
{
1065
    struct wined3d_unordered_access_view_gl *view_gl = wined3d_unordered_access_view_gl(view);
1066
    struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1067 1068 1069
    struct wined3d_bo_address dst, src;
    DWORD dst_location;

1070
    if (!view_gl->counter_bo)
1071 1072 1073 1074 1075
        return;

    dst_location = wined3d_buffer_get_memory(buffer, &dst, buffer->locations);
    dst.addr += offset;

1076
    src.buffer_object = view_gl->counter_bo;
1077 1078
    src.addr = NULL;

1079
    wined3d_context_gl_copy_bo_address(context_gl, &dst, wined3d_buffer_gl(buffer)->buffer_type_hint,
1080 1081 1082 1083 1084
            &src, GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint));

    wined3d_buffer_invalidate_location(buffer, ~dst_location);
}

1085
static void wined3d_unordered_access_view_gl_cs_init(void *object)
1086
{
1087 1088 1089
    struct wined3d_unordered_access_view_gl *view_gl = object;
    struct wined3d_resource *resource = view_gl->v.resource;
    struct wined3d_view_desc *desc = &view_gl->v.desc;
1090 1091 1092 1093 1094 1095 1096
    const struct wined3d_gl_info *gl_info;

    gl_info = &resource->device->adapter->gl_info;

    if (resource->type == WINED3D_RTYPE_BUFFER)
    {
        struct wined3d_buffer *buffer = buffer_from_resource(resource);
1097
        struct wined3d_context *context;
1098

1099
        context = context_acquire(resource->device, NULL, 0);
1100
        gl_info = wined3d_context_gl(context)->gl_info;
1101
        create_buffer_view(&view_gl->gl_view, context, desc, buffer, view_gl->v.format);
1102
        if (desc->flags & (WINED3D_VIEW_BUFFER_COUNTER | WINED3D_VIEW_BUFFER_APPEND))
1103 1104
        {
            static const GLuint initial_value = 0;
1105 1106
            GL_EXTCALL(glGenBuffers(1, &view_gl->counter_bo));
            GL_EXTCALL(glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, view_gl->counter_bo));
1107 1108 1109 1110 1111
            GL_EXTCALL(glBufferData(GL_ATOMIC_COUNTER_BUFFER,
                    sizeof(initial_value), &initial_value, GL_STATIC_DRAW));
            checkGLcall("create atomic counter buffer");
        }
        context_release(context);
1112 1113 1114
    }
    else
    {
1115
        struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
1116 1117 1118
        unsigned int depth_or_layer_count;

        if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
1119
            depth_or_layer_count = wined3d_texture_get_level_depth(&texture_gl->t, desc->u.texture.level_idx);
1120
        else
1121
            depth_or_layer_count = texture_gl->t.layer_count;
1122 1123 1124

        if (desc->u.texture.layer_idx || desc->u.texture.layer_count != depth_or_layer_count)
        {
1125 1126
            create_texture_view(&view_gl->gl_view, get_texture_view_target(gl_info, desc, texture_gl),
                    desc, texture_gl, view_gl->v.format);
1127 1128 1129 1130
        }
    }
}

1131
static HRESULT wined3d_unordered_access_view_init(struct wined3d_unordered_access_view *view,
1132
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1133 1134 1135 1136 1137 1138
        void *parent, const struct wined3d_parent_ops *parent_ops)
{
    view->refcount = 1;
    view->parent = parent;
    view->parent_ops = parent_ops;

1139 1140
    if (!(resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
        return E_INVALIDARG;
1141
    if (!(view->format = validate_resource_view(desc, resource, TRUE, FALSE)))
1142
        return E_INVALIDARG;
1143
    view->desc = *desc;
1144 1145 1146 1147 1148 1149

    wined3d_resource_incref(view->resource = resource);

    return WINED3D_OK;
}

1150 1151 1152
HRESULT wined3d_unordered_access_view_gl_init(struct wined3d_unordered_access_view_gl *view_gl,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
1153 1154 1155
{
    HRESULT hr;

1156 1157
    TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_gl, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
1158

1159
    if (FAILED(hr = wined3d_unordered_access_view_init(&view_gl->v, desc, resource, parent, parent_ops)))
1160 1161
        return hr;

1162
    wined3d_cs_init_object(resource->device->cs, wined3d_unordered_access_view_gl_cs_init, view_gl);
1163

1164 1165
    return hr;
}
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
HRESULT wined3d_unordered_access_view_vk_init(struct wined3d_unordered_access_view_vk *view_vk,
        const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
        void *parent, const struct wined3d_parent_ops *parent_ops)
{
    TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
            view_vk, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);

    return wined3d_unordered_access_view_init(&view_vk->v, desc, resource, parent, parent_ops);
}

HRESULT CDECL wined3d_unordered_access_view_create(const struct wined3d_view_desc *desc,
        struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
        struct wined3d_unordered_access_view **view)
{
    const struct wined3d_adapter_ops *adapter_ops;

    TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
            wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops, view);

    adapter_ops = resource->device->adapter->adapter_ops;
    return adapter_ops->adapter_create_unordered_access_view(desc, resource, parent, parent_ops, view);
1188
}