• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "api_exec_decl.h"
28 
29 struct marshal_cmd_ShaderSource
30 {
31    struct marshal_cmd_base cmd_base;
32    GLuint shader;
33    GLsizei count;
34    /* Followed by GLint length[count], then the contents of all strings,
35     * concatenated.
36     */
37 };
38 
39 
40 uint32_t
_mesa_unmarshal_ShaderSource(struct gl_context * ctx,const struct marshal_cmd_ShaderSource * cmd,const uint64_t * last)41 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
42                              const struct marshal_cmd_ShaderSource *cmd,
43                              const uint64_t *last)
44 {
45    const GLint *cmd_length = (const GLint *) (cmd + 1);
46    const GLchar *cmd_strings = (const GLchar *) (cmd_length + cmd->count);
47    /* TODO: how to deal with malloc failure? */
48    const GLchar * *string = malloc(cmd->count * sizeof(const GLchar *));
49    int i;
50 
51    for (i = 0; i < cmd->count; ++i) {
52       string[i] = cmd_strings;
53       cmd_strings += cmd_length[i];
54    }
55    CALL_ShaderSource(ctx->CurrentServerDispatch,
56                      (cmd->shader, cmd->count, string, cmd_length));
57    free((void *)string);
58    return cmd->cmd_base.cmd_size;
59 }
60 
61 
62 static size_t
measure_ShaderSource_strings(GLsizei count,const GLchar * const * string,const GLint * length_in,GLint * length_out)63 measure_ShaderSource_strings(GLsizei count, const GLchar * const *string,
64                              const GLint *length_in, GLint *length_out)
65 {
66    int i;
67    size_t total_string_length = 0;
68 
69    for (i = 0; i < count; ++i) {
70       if (length_in == NULL || length_in[i] < 0) {
71          if (string[i])
72             length_out[i] = strlen(string[i]);
73       } else {
74          length_out[i] = length_in[i];
75       }
76       total_string_length += length_out[i];
77    }
78    return total_string_length;
79 }
80 
81 
82 void GLAPIENTRY
_mesa_marshal_ShaderSource(GLuint shader,GLsizei count,const GLchar * const * string,const GLint * length)83 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
84                            const GLchar * const *string, const GLint *length)
85 {
86    /* TODO: how to report an error if count < 0? */
87 
88    GET_CURRENT_CONTEXT(ctx);
89    /* TODO: how to deal with malloc failure? */
90    const size_t fixed_cmd_size = sizeof(struct marshal_cmd_ShaderSource);
91    STATIC_ASSERT(sizeof(struct marshal_cmd_ShaderSource) % sizeof(GLint) == 0);
92    size_t length_size = count * sizeof(GLint);
93    GLint *length_tmp = malloc(length_size);
94    size_t total_string_length =
95       measure_ShaderSource_strings(count, string, length, length_tmp);
96    size_t total_cmd_size = fixed_cmd_size + length_size + total_string_length;
97 
98    if (total_cmd_size <= MARSHAL_MAX_CMD_SIZE && count > 0) {
99       struct marshal_cmd_ShaderSource *cmd =
100          _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_ShaderSource,
101                                          total_cmd_size);
102       GLint *cmd_length = (GLint *) (cmd + 1);
103       GLchar *cmd_strings = (GLchar *) (cmd_length + count);
104       int i;
105 
106       cmd->shader = shader;
107       cmd->count = count;
108       memcpy(cmd_length, length_tmp, length_size);
109       for (i = 0; i < count; ++i) {
110          memcpy(cmd_strings, string[i], cmd_length[i]);
111          cmd_strings += cmd_length[i];
112       }
113    } else {
114       _mesa_glthread_finish(ctx);
115       CALL_ShaderSource(ctx->CurrentServerDispatch,
116                         (shader, count, string, length_tmp));
117    }
118    free(length_tmp);
119 }
120 
121 void
_mesa_glthread_ProgramChanged(struct gl_context * ctx)122 _mesa_glthread_ProgramChanged(struct gl_context *ctx)
123 {
124    struct glthread_state *glthread = &ctx->GLThread;
125 
126    /* Track the last change. */
127    p_atomic_set(&glthread->LastProgramChangeBatch, glthread->next);
128    _mesa_glthread_flush_batch(ctx);
129 }
130 
131 uint32_t
_mesa_unmarshal_GetActiveUniform(struct gl_context * ctx,const struct marshal_cmd_GetActiveUniform * cmd,const uint64_t * last)132 _mesa_unmarshal_GetActiveUniform(struct gl_context *ctx,
133                                  const struct marshal_cmd_GetActiveUniform *cmd,
134                                  const uint64_t *last)
135 {
136    unreachable("never executed");
137    return 0;
138 }
139 
140 static void
wait_for_glLinkProgram(struct gl_context * ctx)141 wait_for_glLinkProgram(struct gl_context *ctx)
142 {
143    /* Wait for the last glLinkProgram call. */
144    int batch = p_atomic_read(&ctx->GLThread.LastProgramChangeBatch);
145    if (batch != -1) {
146       util_queue_fence_wait(&ctx->GLThread.batches[batch].fence);
147       assert(p_atomic_read(&ctx->GLThread.LastProgramChangeBatch) == -1);
148    }
149 }
150 
151 void GLAPIENTRY
_mesa_marshal_GetActiveUniform(GLuint program,GLuint index,GLsizei bufSize,GLsizei * length,GLint * size,GLenum * type,GLchar * name)152 _mesa_marshal_GetActiveUniform(GLuint program, GLuint index, GLsizei bufSize,
153                                GLsizei *length, GLint *size, GLenum *type,
154                                GLchar * name)
155 {
156    GET_CURRENT_CONTEXT(ctx);
157 
158    wait_for_glLinkProgram(ctx);
159 
160    /* We can execute glGetActiveUniform without syncing if we are sync'd to
161     * the last calls of glLinkProgram and glDeleteProgram because shader
162     * object IDs and their contents are immutable after those calls and
163     * also thread-safe because they are shared between contexts.
164     * glCreateShaderProgram calls glLinkProgram internally and it always
165     * syncs, so it doesn't need any handling.
166     */
167    _mesa_GetActiveUniform_impl(program, index, bufSize, length, size, type,
168                                name, true);
169 }
170 
171 uint32_t
_mesa_unmarshal_GetUniformLocation(struct gl_context * ctx,const struct marshal_cmd_GetUniformLocation * cmd,const uint64_t * last)172 _mesa_unmarshal_GetUniformLocation(struct gl_context *ctx,
173                                    const struct marshal_cmd_GetUniformLocation *cmd,
174                                    const uint64_t *last)
175 {
176    unreachable("never executed");
177    return 0;
178 }
179 
180 GLint GLAPIENTRY
_mesa_marshal_GetUniformLocation(GLuint program,const GLchar * name)181 _mesa_marshal_GetUniformLocation(GLuint program, const GLchar *name)
182 {
183    GET_CURRENT_CONTEXT(ctx);
184 
185    wait_for_glLinkProgram(ctx);
186 
187    /* This is thread-safe. See the comment in _mesa_marshal_GetActiveUniform. */
188    return _mesa_GetUniformLocation_impl(program, name, true);
189 }
190