1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "glthread_marshal.h"
25 #include "dispatch.h"
26 #include "uniforms.h"
27
28 struct marshal_cmd_ShaderSource
29 {
30 struct marshal_cmd_base cmd_base;
31 GLuint shader;
32 GLsizei count;
33 /* Followed by GLint length[count], then the contents of all strings,
34 * concatenated.
35 */
36 };
37
38
39 uint32_t
_mesa_unmarshal_ShaderSource(struct gl_context * ctx,const struct marshal_cmd_ShaderSource * cmd,const uint64_t * last)40 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
41 const struct marshal_cmd_ShaderSource *cmd,
42 const uint64_t *last)
43 {
44 const GLint *cmd_length = (const GLint *) (cmd + 1);
45 const GLchar *cmd_strings = (const GLchar *) (cmd_length + cmd->count);
46 /* TODO: how to deal with malloc failure? */
47 const GLchar * *string = malloc(cmd->count * sizeof(const GLchar *));
48 int i;
49
50 for (i = 0; i < cmd->count; ++i) {
51 string[i] = cmd_strings;
52 cmd_strings += cmd_length[i];
53 }
54 CALL_ShaderSource(ctx->CurrentServerDispatch,
55 (cmd->shader, cmd->count, string, cmd_length));
56 free((void *)string);
57 return cmd->cmd_base.cmd_size;
58 }
59
60
61 static size_t
measure_ShaderSource_strings(GLsizei count,const GLchar * const * string,const GLint * length_in,GLint * length_out)62 measure_ShaderSource_strings(GLsizei count, const GLchar * const *string,
63 const GLint *length_in, GLint *length_out)
64 {
65 int i;
66 size_t total_string_length = 0;
67
68 for (i = 0; i < count; ++i) {
69 if (length_in == NULL || length_in[i] < 0) {
70 if (string[i])
71 length_out[i] = strlen(string[i]);
72 } else {
73 length_out[i] = length_in[i];
74 }
75 total_string_length += length_out[i];
76 }
77 return total_string_length;
78 }
79
80
81 void GLAPIENTRY
_mesa_marshal_ShaderSource(GLuint shader,GLsizei count,const GLchar * const * string,const GLint * length)82 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
83 const GLchar * const *string, const GLint *length)
84 {
85 /* TODO: how to report an error if count < 0? */
86
87 GET_CURRENT_CONTEXT(ctx);
88 /* TODO: how to deal with malloc failure? */
89 const size_t fixed_cmd_size = sizeof(struct marshal_cmd_ShaderSource);
90 STATIC_ASSERT(sizeof(struct marshal_cmd_ShaderSource) % sizeof(GLint) == 0);
91 size_t length_size = count * sizeof(GLint);
92 GLint *length_tmp = malloc(length_size);
93 size_t total_string_length =
94 measure_ShaderSource_strings(count, string, length, length_tmp);
95 size_t total_cmd_size = fixed_cmd_size + length_size + total_string_length;
96
97 if (total_cmd_size <= MARSHAL_MAX_CMD_SIZE && count > 0) {
98 struct marshal_cmd_ShaderSource *cmd =
99 _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_ShaderSource,
100 total_cmd_size);
101 GLint *cmd_length = (GLint *) (cmd + 1);
102 GLchar *cmd_strings = (GLchar *) (cmd_length + count);
103 int i;
104
105 cmd->shader = shader;
106 cmd->count = count;
107 memcpy(cmd_length, length_tmp, length_size);
108 for (i = 0; i < count; ++i) {
109 memcpy(cmd_strings, string[i], cmd_length[i]);
110 cmd_strings += cmd_length[i];
111 }
112 } else {
113 _mesa_glthread_finish(ctx);
114 CALL_ShaderSource(ctx->CurrentServerDispatch,
115 (shader, count, string, length_tmp));
116 }
117 free(length_tmp);
118 }
119
120 void
_mesa_glthread_ProgramChanged(struct gl_context * ctx)121 _mesa_glthread_ProgramChanged(struct gl_context *ctx)
122 {
123 struct glthread_state *glthread = &ctx->GLThread;
124
125 /* Track the last change. */
126 p_atomic_set(&glthread->LastProgramChangeBatch, glthread->next);
127 _mesa_glthread_flush_batch(ctx);
128 }
129
130 uint32_t
_mesa_unmarshal_GetActiveUniform(struct gl_context * ctx,const struct marshal_cmd_GetActiveUniform * cmd,const uint64_t * last)131 _mesa_unmarshal_GetActiveUniform(struct gl_context *ctx,
132 const struct marshal_cmd_GetActiveUniform *cmd,
133 const uint64_t *last)
134 {
135 unreachable("never executed");
136 return 0;
137 }
138
139 static void
wait_for_glLinkProgram(struct gl_context * ctx)140 wait_for_glLinkProgram(struct gl_context *ctx)
141 {
142 /* Wait for the last glLinkProgram call. */
143 int batch = p_atomic_read(&ctx->GLThread.LastProgramChangeBatch);
144 if (batch != -1) {
145 util_queue_fence_wait(&ctx->GLThread.batches[batch].fence);
146 assert(p_atomic_read(&ctx->GLThread.LastProgramChangeBatch) == -1);
147 }
148 }
149
150 void GLAPIENTRY
_mesa_marshal_GetActiveUniform(GLuint program,GLuint index,GLsizei bufSize,GLsizei * length,GLint * size,GLenum * type,GLchar * name)151 _mesa_marshal_GetActiveUniform(GLuint program, GLuint index, GLsizei bufSize,
152 GLsizei *length, GLint *size, GLenum *type,
153 GLchar * name)
154 {
155 GET_CURRENT_CONTEXT(ctx);
156
157 wait_for_glLinkProgram(ctx);
158
159 /* We can execute glGetActiveUniform without syncing if we are sync'd to
160 * the last calls of glLinkProgram and glDeleteProgram because shader
161 * object IDs and their contents are immutable after those calls and
162 * also thread-safe because they are shared between contexts.
163 * glCreateShaderProgram calls glLinkProgram internally and it always
164 * syncs, so it doesn't need any handling.
165 */
166 _mesa_GetActiveUniform_impl(program, index, bufSize, length, size, type,
167 name, true);
168 }
169
170 uint32_t
_mesa_unmarshal_GetUniformLocation(struct gl_context * ctx,const struct marshal_cmd_GetUniformLocation * cmd,const uint64_t * last)171 _mesa_unmarshal_GetUniformLocation(struct gl_context *ctx,
172 const struct marshal_cmd_GetUniformLocation *cmd,
173 const uint64_t *last)
174 {
175 unreachable("never executed");
176 return 0;
177 }
178
179 GLint GLAPIENTRY
_mesa_marshal_GetUniformLocation(GLuint program,const GLchar * name)180 _mesa_marshal_GetUniformLocation(GLuint program, const GLchar *name)
181 {
182 GET_CURRENT_CONTEXT(ctx);
183
184 wait_for_glLinkProgram(ctx);
185
186 /* This is thread-safe. See the comment in _mesa_marshal_GetActiveUniform. */
187 return _mesa_GetUniformLocation_impl(program, name, true);
188 }
189