2 * Copyright © 2012 Intel Corporation
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:
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
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
26 * Custom functions for marshalling GL calls from the main thread to a worker
27 * thread when automatic code generation isn't appropriate.
32 #include "marshal_generated.h"
34 struct marshal_cmd_ShaderSource
36 struct marshal_cmd_base cmd_base
;
39 /* Followed by GLint length[count], then the contents of all strings,
46 _mesa_unmarshal_ShaderSource(struct gl_context
*ctx
,
47 const struct marshal_cmd_ShaderSource
*cmd
)
49 const GLint
*cmd_length
= (const GLint
*) (cmd
+ 1);
50 const GLchar
*cmd_strings
= (const GLchar
*) (cmd_length
+ cmd
->count
);
51 /* TODO: how to deal with malloc failure? */
52 const GLchar
* *string
= malloc(cmd
->count
* sizeof(const GLchar
*));
55 for (i
= 0; i
< cmd
->count
; ++i
) {
56 string
[i
] = cmd_strings
;
57 cmd_strings
+= cmd_length
[i
];
59 CALL_ShaderSource(ctx
->CurrentServerDispatch
,
60 (cmd
->shader
, cmd
->count
, string
, cmd_length
));
66 measure_ShaderSource_strings(GLsizei count
, const GLchar
* const *string
,
67 const GLint
*length_in
, GLint
*length_out
)
70 size_t total_string_length
= 0;
72 for (i
= 0; i
< count
; ++i
) {
73 if (length_in
== NULL
|| length_in
[i
] < 0) {
75 length_out
[i
] = strlen(string
[i
]);
77 length_out
[i
] = length_in
[i
];
79 total_string_length
+= length_out
[i
];
81 return total_string_length
;
86 _mesa_marshal_ShaderSource(GLuint shader
, GLsizei count
,
87 const GLchar
* const *string
, const GLint
*length
)
89 /* TODO: how to report an error if count < 0? */
91 GET_CURRENT_CONTEXT(ctx
);
92 /* TODO: how to deal with malloc failure? */
93 const size_t fixed_cmd_size
= sizeof(struct marshal_cmd_ShaderSource
);
94 STATIC_ASSERT(sizeof(struct marshal_cmd_ShaderSource
) % sizeof(GLint
) == 0);
95 size_t length_size
= count
* sizeof(GLint
);
96 GLint
*length_tmp
= malloc(length_size
);
97 size_t total_string_length
=
98 measure_ShaderSource_strings(count
, string
, length
, length_tmp
);
99 size_t total_cmd_size
= fixed_cmd_size
+ length_size
+ total_string_length
;
101 if (total_cmd_size
<= MARSHAL_MAX_CMD_SIZE
) {
102 struct marshal_cmd_ShaderSource
*cmd
=
103 _mesa_glthread_allocate_command(ctx
, DISPATCH_CMD_ShaderSource
,
105 GLint
*cmd_length
= (GLint
*) (cmd
+ 1);
106 GLchar
*cmd_strings
= (GLchar
*) (cmd_length
+ count
);
109 cmd
->shader
= shader
;
111 memcpy(cmd_length
, length_tmp
, length_size
);
112 for (i
= 0; i
< count
; ++i
) {
113 memcpy(cmd_strings
, string
[i
], cmd_length
[i
]);
114 cmd_strings
+= cmd_length
[i
];
116 _mesa_post_marshal_hook(ctx
);
118 _mesa_glthread_finish(ctx
);
119 CALL_ShaderSource(ctx
->CurrentServerDispatch
,
120 (shader
, count
, string
, length_tmp
));