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
31 #include "pipe/p_context.h"
32 #include "pipe/p_defines.h"
33 #include "util/u_inlines.h"
34 #include "util/u_surface.h"
35
36 #include "st_debug.h"
37 #include "st_context.h"
38 #include "st_atom.h"
39 #include "st_program.h"
40
41 static void
st_bind_ssbos(struct st_context * st,struct gl_program * prog,enum pipe_shader_type shader_type)42 st_bind_ssbos(struct st_context *st, struct gl_program *prog,
43 enum pipe_shader_type shader_type)
44 {
45 unsigned i;
46 struct pipe_shader_buffer buffers[MAX_SHADER_STORAGE_BUFFERS];
47 if (!prog || !st->pipe->set_shader_buffers)
48 return;
49
50 for (i = 0; i < prog->info.num_ssbos; i++) {
51 struct gl_buffer_binding *binding;
52 struct gl_buffer_object *st_obj;
53 struct pipe_shader_buffer *sb = &buffers[i];
54
55 binding = &st->ctx->ShaderStorageBufferBindings[
56 prog->sh.ShaderStorageBlocks[i]->Binding];
57 st_obj = binding->BufferObject;
58
59 sb->buffer = st_obj ? st_obj->buffer : NULL;
60
61 if (sb->buffer) {
62 sb->buffer_offset = binding->Offset;
63 sb->buffer_size = sb->buffer->width0 - binding->Offset;
64
65 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
66 * Take the minimum just to be sure.
67 */
68 if (!binding->AutomaticSize)
69 sb->buffer_size = MIN2(sb->buffer_size, (unsigned) binding->Size);
70 }
71 else {
72 sb->buffer_offset = 0;
73 sb->buffer_size = 0;
74 }
75 }
76 st->pipe->set_shader_buffers(st->pipe, shader_type, 0,
77 prog->info.num_ssbos, buffers,
78 prog->sh.ShaderStorageBlocksWriteAccess);
79
80 /* Clear out any stale shader buffers (or lowered atomic counters). */
81 int num_ssbos = prog->info.num_ssbos;
82 if (!st->has_hw_atomics)
83 num_ssbos += st->last_used_atomic_bindings[shader_type];
84 if (st->last_num_ssbos[shader_type] > num_ssbos) {
85 st->pipe->set_shader_buffers(
86 st->pipe, shader_type,
87 num_ssbos,
88 st->last_num_ssbos[shader_type] - num_ssbos,
89 NULL, 0);
90 st->last_num_ssbos[shader_type] = num_ssbos;
91 }
92 }
93
st_bind_vs_ssbos(struct st_context * st)94 void st_bind_vs_ssbos(struct st_context *st)
95 {
96 struct gl_program *prog =
97 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
98
99 st_bind_ssbos(st, prog, PIPE_SHADER_VERTEX);
100 }
101
st_bind_fs_ssbos(struct st_context * st)102 void st_bind_fs_ssbos(struct st_context *st)
103 {
104 struct gl_program *prog =
105 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
106
107 st_bind_ssbos(st, prog, PIPE_SHADER_FRAGMENT);
108 }
109
st_bind_gs_ssbos(struct st_context * st)110 void st_bind_gs_ssbos(struct st_context *st)
111 {
112 struct gl_program *prog =
113 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
114
115 st_bind_ssbos(st, prog, PIPE_SHADER_GEOMETRY);
116 }
117
st_bind_tcs_ssbos(struct st_context * st)118 void st_bind_tcs_ssbos(struct st_context *st)
119 {
120 struct gl_program *prog =
121 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
122
123 st_bind_ssbos(st, prog, PIPE_SHADER_TESS_CTRL);
124 }
125
st_bind_tes_ssbos(struct st_context * st)126 void st_bind_tes_ssbos(struct st_context *st)
127 {
128 struct gl_program *prog =
129 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
130
131 st_bind_ssbos(st, prog, PIPE_SHADER_TESS_EVAL);
132 }
133
st_bind_cs_ssbos(struct st_context * st)134 void st_bind_cs_ssbos(struct st_context *st)
135 {
136 struct gl_program *prog =
137 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
138
139 st_bind_ssbos(st, prog, PIPE_SHADER_COMPUTE);
140 }
141