1 /**************************************************************************
2 *
3 * Copyright 2014 Ilia Mirkin. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27
28 #include "program/prog_parameter.h"
29 #include "program/prog_print.h"
30 #include "compiler/glsl/ir_uniform.h"
31
32 #include "pipe/p_context.h"
33 #include "pipe/p_defines.h"
34 #include "util/u_inlines.h"
35 #include "util/u_surface.h"
36
37 #include "st_debug.h"
38 #include "st_context.h"
39 #include "st_atom.h"
40 #include "st_program.h"
41
42 static void
st_binding_to_sb(struct gl_buffer_binding * binding,struct pipe_shader_buffer * sb,unsigned alignment)43 st_binding_to_sb(struct gl_buffer_binding *binding,
44 struct pipe_shader_buffer *sb,
45 unsigned alignment)
46 {
47 struct gl_buffer_object *st_obj = binding->BufferObject;
48
49 if (st_obj && st_obj->buffer) {
50 unsigned offset = 0;
51 sb->buffer = st_obj->buffer;
52 offset = binding->Offset % alignment;
53 sb->buffer_offset = binding->Offset - offset;
54 sb->buffer_size = st_obj->buffer->width0 - sb->buffer_offset;
55
56 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
57 * Take the minimum just to be sure.
58 */
59 if (!binding->AutomaticSize)
60 sb->buffer_size = MIN2(sb->buffer_size, (unsigned) binding->Size + offset);
61 } else {
62 sb->buffer = NULL;
63 sb->buffer_offset = 0;
64 sb->buffer_size = 0;
65 }
66 }
67
68 static void
st_bind_atomics(struct st_context * st,struct gl_program * prog,gl_shader_stage stage)69 st_bind_atomics(struct st_context *st, struct gl_program *prog,
70 gl_shader_stage stage)
71 {
72 unsigned i;
73 enum pipe_shader_type shader_type = pipe_shader_type_from_mesa(stage);
74
75 if (!prog || !st->pipe->set_shader_buffers || st->has_hw_atomics)
76 return;
77
78 /* For !has_hw_atomics, the atomic counters have been rewritten to be above
79 * the SSBOs used by the program.
80 */
81 unsigned buffer_base = prog->info.num_ssbos;
82 unsigned used_bindings = 0;
83 for (i = 0; i < prog->sh.data->NumAtomicBuffers; i++) {
84 struct gl_active_atomic_buffer *atomic =
85 &prog->sh.data->AtomicBuffers[i];
86 struct pipe_shader_buffer sb;
87
88 st_binding_to_sb(&st->ctx->AtomicBufferBindings[atomic->Binding], &sb,
89 st->ctx->Const.ShaderStorageBufferOffsetAlignment);
90
91 st->pipe->set_shader_buffers(st->pipe, shader_type,
92 buffer_base + atomic->Binding, 1, &sb, 0x1);
93 used_bindings = MAX2(atomic->Binding + 1, used_bindings);
94 }
95 st->last_used_atomic_bindings[shader_type] = used_bindings;
96 }
97
98 void
st_bind_vs_atomics(struct st_context * st)99 st_bind_vs_atomics(struct st_context *st)
100 {
101 struct gl_program *prog =
102 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
103
104 st_bind_atomics(st, prog, MESA_SHADER_VERTEX);
105 }
106
107 void
st_bind_fs_atomics(struct st_context * st)108 st_bind_fs_atomics(struct st_context *st)
109 {
110 struct gl_program *prog =
111 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
112
113 st_bind_atomics(st, prog, MESA_SHADER_FRAGMENT);
114 }
115
116 void
st_bind_gs_atomics(struct st_context * st)117 st_bind_gs_atomics(struct st_context *st)
118 {
119 struct gl_program *prog =
120 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
121
122 st_bind_atomics(st, prog, MESA_SHADER_GEOMETRY);
123 }
124
125 void
st_bind_tcs_atomics(struct st_context * st)126 st_bind_tcs_atomics(struct st_context *st)
127 {
128 struct gl_program *prog =
129 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
130
131 st_bind_atomics(st, prog, MESA_SHADER_TESS_CTRL);
132 }
133
134 void
st_bind_tes_atomics(struct st_context * st)135 st_bind_tes_atomics(struct st_context *st)
136 {
137 struct gl_program *prog =
138 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
139
140 st_bind_atomics(st, prog, MESA_SHADER_TESS_EVAL);
141 }
142
143 void
st_bind_cs_atomics(struct st_context * st)144 st_bind_cs_atomics(struct st_context *st)
145 {
146 if (st->has_hw_atomics) {
147 st_bind_hw_atomic_buffers(st);
148 return;
149 }
150 struct gl_program *prog =
151 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
152
153 st_bind_atomics(st, prog, MESA_SHADER_COMPUTE);
154 }
155
156 void
st_bind_hw_atomic_buffers(struct st_context * st)157 st_bind_hw_atomic_buffers(struct st_context *st)
158 {
159 struct pipe_shader_buffer buffers[PIPE_MAX_HW_ATOMIC_BUFFERS];
160 int i;
161
162 if (!st->has_hw_atomics)
163 return;
164
165 for (i = 0; i < st->ctx->Const.MaxAtomicBufferBindings; i++)
166 st_binding_to_sb(&st->ctx->AtomicBufferBindings[i], &buffers[i], 1);
167
168 st->pipe->set_hw_atomic_buffers(st->pipe, 0, st->ctx->Const.MaxAtomicBufferBindings, buffers);
169 }
170