1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci/**
25bf215546Sopenharmony_ci * \file barrier.c
26bf215546Sopenharmony_ci * Implementation of various pipeline barrier entry points.
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci * \author Marek Olšák <maraeo@gmail.com>
29bf215546Sopenharmony_ci */
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "context.h"
32bf215546Sopenharmony_ci#include "api_exec_decl.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "pipe/p_context.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic void
38bf215546Sopenharmony_cimemory_barrier(struct gl_context *ctx, GLbitfield barriers)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   struct pipe_context *pipe = ctx->pipe;
41bf215546Sopenharmony_ci   unsigned flags = 0;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   if (barriers & GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT)
44bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_VERTEX_BUFFER;
45bf215546Sopenharmony_ci   if (barriers & GL_ELEMENT_ARRAY_BARRIER_BIT)
46bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_INDEX_BUFFER;
47bf215546Sopenharmony_ci   if (barriers & GL_UNIFORM_BARRIER_BIT)
48bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_CONSTANT_BUFFER;
49bf215546Sopenharmony_ci   if (barriers & GL_TEXTURE_FETCH_BARRIER_BIT)
50bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_TEXTURE;
51bf215546Sopenharmony_ci   if (barriers & GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)
52bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_IMAGE;
53bf215546Sopenharmony_ci   if (barriers & GL_COMMAND_BARRIER_BIT)
54bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_INDIRECT_BUFFER;
55bf215546Sopenharmony_ci   if (barriers & GL_PIXEL_BUFFER_BARRIER_BIT) {
56bf215546Sopenharmony_ci      /* The PBO may be
57bf215546Sopenharmony_ci       *  (1) bound as a texture for PBO uploads, or
58bf215546Sopenharmony_ci       *  (2) accessed by the CPU via transfer ops.
59bf215546Sopenharmony_ci       * For case (2), we assume automatic flushing by the driver.
60bf215546Sopenharmony_ci       */
61bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_TEXTURE;
62bf215546Sopenharmony_ci   }
63bf215546Sopenharmony_ci   if (barriers & GL_TEXTURE_UPDATE_BARRIER_BIT) {
64bf215546Sopenharmony_ci      /* GL_TEXTURE_UPDATE_BARRIER_BIT:
65bf215546Sopenharmony_ci       * Texture updates translate to:
66bf215546Sopenharmony_ci       *  (1) texture transfers to/from the CPU,
67bf215546Sopenharmony_ci       *  (2) texture as blit destination, or
68bf215546Sopenharmony_ci       *  (3) texture as framebuffer.
69bf215546Sopenharmony_ci       * Some drivers may handle these automatically, and can ignore the bit.
70bf215546Sopenharmony_ci       */
71bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_UPDATE_TEXTURE;
72bf215546Sopenharmony_ci   }
73bf215546Sopenharmony_ci   if (barriers & GL_BUFFER_UPDATE_BARRIER_BIT) {
74bf215546Sopenharmony_ci      /* GL_BUFFER_UPDATE_BARRIER_BIT:
75bf215546Sopenharmony_ci       * Buffer updates translate to
76bf215546Sopenharmony_ci       *  (1) buffer transfers to/from the CPU,
77bf215546Sopenharmony_ci       *  (2) resource copies and clears.
78bf215546Sopenharmony_ci       * Some drivers may handle these automatically, and can ignore the bit.
79bf215546Sopenharmony_ci       */
80bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_UPDATE_BUFFER;
81bf215546Sopenharmony_ci   }
82bf215546Sopenharmony_ci   if (barriers & GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT)
83bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_MAPPED_BUFFER;
84bf215546Sopenharmony_ci   if (barriers & GL_QUERY_BUFFER_BARRIER_BIT)
85bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_QUERY_BUFFER;
86bf215546Sopenharmony_ci   if (barriers & GL_FRAMEBUFFER_BARRIER_BIT)
87bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_FRAMEBUFFER;
88bf215546Sopenharmony_ci   if (barriers & GL_TRANSFORM_FEEDBACK_BARRIER_BIT)
89bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_STREAMOUT_BUFFER;
90bf215546Sopenharmony_ci   if (barriers & GL_ATOMIC_COUNTER_BARRIER_BIT)
91bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_SHADER_BUFFER;
92bf215546Sopenharmony_ci   if (barriers & GL_SHADER_STORAGE_BARRIER_BIT)
93bf215546Sopenharmony_ci      flags |= PIPE_BARRIER_SHADER_BUFFER;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   if (flags && pipe->memory_barrier)
96bf215546Sopenharmony_ci      pipe->memory_barrier(pipe, flags);
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_civoid GLAPIENTRY
100bf215546Sopenharmony_ci_mesa_TextureBarrierNV(void)
101bf215546Sopenharmony_ci{
102bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   if (!ctx->Extensions.NV_texture_barrier) {
105bf215546Sopenharmony_ci      _mesa_error(ctx, GL_INVALID_OPERATION,
106bf215546Sopenharmony_ci                  "glTextureBarrier(not supported)");
107bf215546Sopenharmony_ci      return;
108bf215546Sopenharmony_ci   }
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   ctx->pipe->texture_barrier(ctx->pipe, PIPE_TEXTURE_BARRIER_SAMPLER);
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_civoid GLAPIENTRY
114bf215546Sopenharmony_ci_mesa_MemoryBarrier(GLbitfield barriers)
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   memory_barrier(ctx, barriers);
119bf215546Sopenharmony_ci}
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_cistatic ALWAYS_INLINE void
122bf215546Sopenharmony_cimemory_barrier_by_region(struct gl_context *ctx, GLbitfield barriers,
123bf215546Sopenharmony_ci                         bool no_error)
124bf215546Sopenharmony_ci{
125bf215546Sopenharmony_ci   GLbitfield all_allowed_bits = GL_ATOMIC_COUNTER_BARRIER_BIT |
126bf215546Sopenharmony_ci                                 GL_FRAMEBUFFER_BARRIER_BIT |
127bf215546Sopenharmony_ci                                 GL_SHADER_IMAGE_ACCESS_BARRIER_BIT |
128bf215546Sopenharmony_ci                                 GL_SHADER_STORAGE_BARRIER_BIT |
129bf215546Sopenharmony_ci                                 GL_TEXTURE_FETCH_BARRIER_BIT |
130bf215546Sopenharmony_ci                                 GL_UNIFORM_BARRIER_BIT;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   /* From section 7.11.2 of the OpenGL ES 3.1 specification:
133bf215546Sopenharmony_ci    *
134bf215546Sopenharmony_ci    *    "When barriers is ALL_BARRIER_BITS, shader memory accesses will be
135bf215546Sopenharmony_ci    *     synchronized relative to all these barrier bits, but not to other
136bf215546Sopenharmony_ci    *     barrier bits specific to MemoryBarrier."
137bf215546Sopenharmony_ci    *
138bf215546Sopenharmony_ci    * That is, if barriers is the special value GL_ALL_BARRIER_BITS, then all
139bf215546Sopenharmony_ci    * barriers allowed by glMemoryBarrierByRegion should be activated."
140bf215546Sopenharmony_ci    */
141bf215546Sopenharmony_ci   if (barriers == GL_ALL_BARRIER_BITS) {
142bf215546Sopenharmony_ci      memory_barrier(ctx, all_allowed_bits);
143bf215546Sopenharmony_ci      return;
144bf215546Sopenharmony_ci   }
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   /* From section 7.11.2 of the OpenGL ES 3.1 specification:
147bf215546Sopenharmony_ci    *
148bf215546Sopenharmony_ci    *    "An INVALID_VALUE error is generated if barriers is not the special
149bf215546Sopenharmony_ci    *     value ALL_BARRIER_BITS, and has any bits set other than those
150bf215546Sopenharmony_ci    *     described above."
151bf215546Sopenharmony_ci    */
152bf215546Sopenharmony_ci   if (!no_error && (barriers & ~all_allowed_bits) != 0) {
153bf215546Sopenharmony_ci      _mesa_error(ctx, GL_INVALID_VALUE,
154bf215546Sopenharmony_ci                  "glMemoryBarrierByRegion(unsupported barrier bit");
155bf215546Sopenharmony_ci   }
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   memory_barrier(ctx, barriers);
158bf215546Sopenharmony_ci}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_civoid GLAPIENTRY
161bf215546Sopenharmony_ci_mesa_MemoryBarrierByRegion_no_error(GLbitfield barriers)
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
164bf215546Sopenharmony_ci   memory_barrier_by_region(ctx, barriers, true);
165bf215546Sopenharmony_ci}
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_civoid GLAPIENTRY
168bf215546Sopenharmony_ci_mesa_MemoryBarrierByRegion(GLbitfield barriers)
169bf215546Sopenharmony_ci{
170bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
171bf215546Sopenharmony_ci   memory_barrier_by_region(ctx, barriers, false);
172bf215546Sopenharmony_ci}
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_civoid GLAPIENTRY
175bf215546Sopenharmony_ci_mesa_BlendBarrier(void)
176bf215546Sopenharmony_ci{
177bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   if (!ctx->Extensions.KHR_blend_equation_advanced) {
180bf215546Sopenharmony_ci      _mesa_error(ctx, GL_INVALID_OPERATION,
181bf215546Sopenharmony_ci                  "glBlendBarrier(not supported)");
182bf215546Sopenharmony_ci      return;
183bf215546Sopenharmony_ci   }
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   ctx->pipe->texture_barrier(ctx->pipe, PIPE_TEXTURE_BARRIER_FRAMEBUFFER);
186bf215546Sopenharmony_ci}
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_civoid GLAPIENTRY
189bf215546Sopenharmony_ci_mesa_FramebufferFetchBarrierEXT(void)
190bf215546Sopenharmony_ci{
191bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   if (!ctx->Extensions.EXT_shader_framebuffer_fetch_non_coherent) {
194bf215546Sopenharmony_ci      _mesa_error(ctx, GL_INVALID_OPERATION,
195bf215546Sopenharmony_ci                  "glFramebufferFetchBarrierEXT(not supported)");
196bf215546Sopenharmony_ci      return;
197bf215546Sopenharmony_ci   }
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   ctx->pipe->texture_barrier(ctx->pipe, PIPE_TEXTURE_BARRIER_FRAMEBUFFER);
200bf215546Sopenharmony_ci}
201