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