st/mesa: empty buffer binding if the buffer's not really there
[mesa.git] / src / mesa / state_tracker / st_atom_atomicbuf.c
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
44 st_bind_atomics(struct st_context *st,
45 struct gl_shader_program *prog,
46 unsigned shader_type)
47 {
48 unsigned i;
49
50 if (!prog || !st->pipe->set_shader_buffers)
51 return;
52
53 for (i = 0; i < prog->NumAtomicBuffers; i++) {
54 struct gl_active_atomic_buffer *atomic = &prog->AtomicBuffers[i];
55 struct gl_atomic_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
67 st->pipe->set_shader_buffers(st->pipe, shader_type,
68 atomic->Binding, 1, &sb);
69 }
70 }
71
72 static void
73 bind_vs_atomics(struct st_context *st)
74 {
75 struct gl_shader_program *prog =
76 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
77
78 st_bind_atomics(st, prog, PIPE_SHADER_VERTEX);
79 }
80
81 const struct st_tracked_state st_bind_vs_atomics = {
82 "st_bind_vs_atomics",
83 {
84 0,
85 ST_NEW_VERTEX_PROGRAM | ST_NEW_ATOMIC_BUFFER,
86 },
87 bind_vs_atomics
88 };
89
90 static void
91 bind_fs_atomics(struct st_context *st)
92 {
93 struct gl_shader_program *prog =
94 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
95
96 st_bind_atomics(st, prog, PIPE_SHADER_FRAGMENT);
97 }
98
99 const struct st_tracked_state st_bind_fs_atomics = {
100 "st_bind_fs_atomics",
101 {
102 0,
103 ST_NEW_FRAGMENT_PROGRAM | ST_NEW_ATOMIC_BUFFER,
104 },
105 bind_fs_atomics
106 };
107
108 static void
109 bind_gs_atomics(struct st_context *st)
110 {
111 struct gl_shader_program *prog =
112 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
113
114 st_bind_atomics(st, prog, PIPE_SHADER_GEOMETRY);
115 }
116
117 const struct st_tracked_state st_bind_gs_atomics = {
118 "st_bind_gs_atomics",
119 {
120 0,
121 ST_NEW_GEOMETRY_PROGRAM | ST_NEW_ATOMIC_BUFFER,
122 },
123 bind_gs_atomics
124 };
125
126 static void
127 bind_tcs_atomics(struct st_context *st)
128 {
129 struct gl_shader_program *prog =
130 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
131
132 st_bind_atomics(st, prog, PIPE_SHADER_TESS_CTRL);
133 }
134
135 const struct st_tracked_state st_bind_tcs_atomics = {
136 "st_bind_tcs_atomics",
137 {
138 0,
139 ST_NEW_TESSCTRL_PROGRAM | ST_NEW_ATOMIC_BUFFER,
140 },
141 bind_tcs_atomics
142 };
143
144 static void
145 bind_tes_atomics(struct st_context *st)
146 {
147 struct gl_shader_program *prog =
148 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
149
150 st_bind_atomics(st, prog, PIPE_SHADER_TESS_EVAL);
151 }
152
153 const struct st_tracked_state st_bind_tes_atomics = {
154 "st_bind_tes_atomics",
155 {
156 0,
157 ST_NEW_TESSEVAL_PROGRAM | ST_NEW_ATOMIC_BUFFER,
158 },
159 bind_tes_atomics
160 };
161
162 static void
163 bind_cs_atomics(struct st_context *st)
164 {
165 struct gl_shader_program *prog =
166 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
167
168 st_bind_atomics(st, prog, PIPE_SHADER_COMPUTE);
169 }
170
171 const struct st_tracked_state st_bind_cs_atomics = {
172 "st_bind_cs_atomics",
173 {
174 0,
175 ST_NEW_COMPUTE_PROGRAM | ST_NEW_ATOMIC_BUFFER,
176 },
177 bind_cs_atomics
178 };