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 #include "main/imports.h"
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_cb_bufferobjects.h"
39 #include "st_context.h"
40 #include "st_atom.h"
41 #include "st_program.h"
42
43 static void
st_bind_atomics(struct st_context * st,struct gl_program * prog,enum pipe_shader_type shader_type)44 st_bind_atomics(struct st_context *st, struct gl_program *prog,
45 enum pipe_shader_type shader_type)
46 {
47 unsigned i;
48
49 if (!prog || !st->pipe->set_shader_buffers || st->has_hw_atomics)
50 return;
51
52 for (i = 0; i < prog->sh.data->NumAtomicBuffers; i++) {
53 struct gl_active_atomic_buffer *atomic =
54 &prog->sh.data->AtomicBuffers[i];
55 struct gl_buffer_binding *binding =
56 &st->ctx->AtomicBufferBindings[atomic->Binding];
57 struct st_buffer_object *st_obj =
58 st_buffer_object(binding->BufferObject);
59 struct pipe_shader_buffer sb = { 0 };
60
61 if (st_obj && st_obj->buffer) {
62 sb.buffer = st_obj->buffer;
63 sb.buffer_offset = binding->Offset;
64 sb.buffer_size = st_obj->buffer->width0 - binding->Offset;
65
66 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
67 * Take the minimum just to be sure.
68 */
69 if (!binding->AutomaticSize)
70 sb.buffer_size = MIN2(sb.buffer_size, (unsigned) binding->Size);
71 }
72
73 st->pipe->set_shader_buffers(st->pipe, shader_type,
74 atomic->Binding, 1, &sb);
75 }
76 }
77
78 void
st_bind_vs_atomics(struct st_context * st)79 st_bind_vs_atomics(struct st_context *st)
80 {
81 struct gl_program *prog =
82 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
83
84 st_bind_atomics(st, prog, PIPE_SHADER_VERTEX);
85 }
86
87 void
st_bind_fs_atomics(struct st_context * st)88 st_bind_fs_atomics(struct st_context *st)
89 {
90 struct gl_program *prog =
91 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
92
93 st_bind_atomics(st, prog, PIPE_SHADER_FRAGMENT);
94 }
95
96 void
st_bind_gs_atomics(struct st_context * st)97 st_bind_gs_atomics(struct st_context *st)
98 {
99 struct gl_program *prog =
100 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
101
102 st_bind_atomics(st, prog, PIPE_SHADER_GEOMETRY);
103 }
104
105 void
st_bind_tcs_atomics(struct st_context * st)106 st_bind_tcs_atomics(struct st_context *st)
107 {
108 struct gl_program *prog =
109 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
110
111 st_bind_atomics(st, prog, PIPE_SHADER_TESS_CTRL);
112 }
113
114 void
st_bind_tes_atomics(struct st_context * st)115 st_bind_tes_atomics(struct st_context *st)
116 {
117 struct gl_program *prog =
118 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
119
120 st_bind_atomics(st, prog, PIPE_SHADER_TESS_EVAL);
121 }
122
123 void
st_bind_cs_atomics(struct st_context * st)124 st_bind_cs_atomics(struct st_context *st)
125 {
126 if (st->has_hw_atomics) {
127 st_bind_hw_atomic_buffers(st);
128 return;
129 }
130 struct gl_program *prog =
131 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
132
133 st_bind_atomics(st, prog, PIPE_SHADER_COMPUTE);
134 }
135
136 void
st_bind_hw_atomic_buffers(struct st_context * st)137 st_bind_hw_atomic_buffers(struct st_context *st)
138 {
139 struct pipe_shader_buffer buffers[PIPE_MAX_HW_ATOMIC_BUFFERS];
140 int i;
141
142 if (!st->has_hw_atomics)
143 return;
144
145 for (i = 0; i < st->ctx->Const.MaxAtomicBufferBindings; i++) {
146 struct gl_buffer_binding *binding = &st->ctx->AtomicBufferBindings[i];
147 struct st_buffer_object *st_obj = st_buffer_object(binding->BufferObject);
148 struct pipe_shader_buffer *sb = &buffers[i];
149
150 if (st_obj && st_obj->buffer) {
151 sb->buffer = st_obj->buffer;
152 sb->buffer_offset = binding->Offset;
153 sb->buffer_size = st_obj->buffer->width0 - binding->Offset;
154
155 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
156 * Take the minimum just to be sure.
157 */
158 if (!binding->AutomaticSize)
159 sb->buffer_size = MIN2(sb->buffer_size, (unsigned) binding->Size);
160 } else {
161 sb->buffer = NULL;
162 sb->buffer_offset = 0;
163 sb->buffer_size = 0;
164 }
165 }
166
167 st->pipe->set_hw_atomic_buffers(st->pipe, 0, st->ctx->Const.MaxAtomicBufferBindings, buffers);
168 }
169