1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file defines the GLES2 command buffer commands.
7 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_
8 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_
11 #include <KHR/khrplatform.h>
15 #include "base/safe_numerics.h"
16 #include "gpu/command_buffer/common/bitfield_helpers.h"
17 #include "gpu/command_buffer/common/cmd_buffer_common.h"
18 #include "gpu/command_buffer/common/gles2_cmd_ids.h"
19 #include "gpu/command_buffer/common/types.h"
21 // GL types are forward declared to avoid including the GL headers. The problem
22 // is determining which GL headers to include from code that is common to the
23 // client and service sides (GLES2 or one of several GL implementations).
24 typedef unsigned int GLenum
;
25 typedef unsigned int GLbitfield
;
26 typedef unsigned int GLuint
;
29 typedef unsigned char GLboolean
;
30 typedef signed char GLbyte
;
31 typedef short GLshort
;
32 typedef unsigned char GLubyte
;
33 typedef unsigned short GLushort
;
34 typedef unsigned long GLulong
;
35 typedef float GLfloat
;
36 typedef float GLclampf
;
37 typedef double GLdouble
;
38 typedef double GLclampd
;
40 typedef khronos_intptr_t GLintptr
;
41 typedef khronos_ssize_t GLsizeiptr
;
48 namespace id_namespaces
{
50 // These are used when contexts share resources.
63 // These numbers must not change
64 COMPILE_ASSERT(kBuffers
== 0, kBuffers_is_not_0
);
65 COMPILE_ASSERT(kFramebuffers
== 1, kFramebuffers_is_not_1
);
66 COMPILE_ASSERT(kProgramsAndShaders
== 2, kProgramsAndShaders_is_not_2
);
67 COMPILE_ASSERT(kRenderbuffers
== 3, kRenderbuffers_is_not_3
);
68 COMPILE_ASSERT(kTextures
== 4, kTextures_is_not_4
);
70 } // namespace id_namespaces
72 // Used for some glGetXXX commands that return a result through a pointer. We
73 // need to know if the command succeeded or not and the size of the result. If
74 // the command failed its result size will 0.
80 return static_cast<T
*>(static_cast<void*>(&data
));
83 // Returns the total size in bytes of the SizedResult for a given number of
84 // results including the size field.
85 static size_t ComputeSize(size_t num_results
) {
86 return sizeof(T
) * num_results
+ sizeof(uint32
); // NOLINT
89 // Returns the total size in bytes of the SizedResult for a given size of
91 static size_t ComputeSizeFromBytes(size_t size_of_result_in_bytes
) {
92 return size_of_result_in_bytes
+ sizeof(uint32
); // NOLINT
95 // Returns the maximum number of results for a given buffer size.
96 static uint32
ComputeMaxResults(size_t size_of_buffer
) {
97 return (size_of_buffer
>= sizeof(uint32
)) ?
98 ((size_of_buffer
- sizeof(uint32
)) / sizeof(T
)) : 0; // NOLINT
101 // Set the size for a given number of results.
102 void SetNumResults(size_t num_results
) {
103 size
= sizeof(T
) * num_results
; // NOLINT
106 // Get the number of elements in the result
107 int32
GetNumResults() const {
108 return size
/ sizeof(T
); // NOLINT
112 void CopyResult(void* dst
) const {
113 memcpy(dst
, &data
, size
);
116 uint32 size
; // in bytes.
117 int32 data
; // this is just here to get an offset.
120 COMPILE_ASSERT(sizeof(SizedResult
<int8
>) == 8, SizedResult_size_not_8
);
121 COMPILE_ASSERT(offsetof(SizedResult
<int8
>, size
) == 0,
122 OffsetOf_SizedResult_size_not_0
);
123 COMPILE_ASSERT(offsetof(SizedResult
<int8
>, data
) == 4,
124 OffsetOf_SizedResult_data_not_4
);
126 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
127 struct ProgramInput
{
128 uint32 type
; // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
129 int32 size
; // The size (how big the array is for uniforms)
130 uint32 location_offset
; // offset from ProgramInfoHeader to 'size' locations
131 // for uniforms, 1 for attribs.
132 uint32 name_offset
; // offset from ProgrmaInfoHeader to start of name.
133 uint32 name_length
; // length of the name.
136 // The format of the bucket filled out by GetProgramInfoCHROMIUM
137 struct ProgramInfoHeader
{
141 // ProgramInput inputs[num_attribs + num_uniforms];
144 // The format of QuerySync used by EXT_occlusion_query_boolean
151 uint32 process_count
;
155 COMPILE_ASSERT(sizeof(ProgramInput
) == 20, ProgramInput_size_not_20
);
156 COMPILE_ASSERT(offsetof(ProgramInput
, type
) == 0,
157 OffsetOf_ProgramInput_type_not_0
);
158 COMPILE_ASSERT(offsetof(ProgramInput
, size
) == 4,
159 OffsetOf_ProgramInput_size_not_4
);
160 COMPILE_ASSERT(offsetof(ProgramInput
, location_offset
) == 8,
161 OffsetOf_ProgramInput_location_offset_not_8
);
162 COMPILE_ASSERT(offsetof(ProgramInput
, name_offset
) == 12,
163 OffsetOf_ProgramInput_name_offset_not_12
);
164 COMPILE_ASSERT(offsetof(ProgramInput
, name_length
) == 16,
165 OffsetOf_ProgramInput_name_length_not_16
);
167 COMPILE_ASSERT(sizeof(ProgramInfoHeader
) == 12, ProgramInfoHeader_size_not_12
);
168 COMPILE_ASSERT(offsetof(ProgramInfoHeader
, link_status
) == 0,
169 OffsetOf_ProgramInfoHeader_link_status_not_0
);
170 COMPILE_ASSERT(offsetof(ProgramInfoHeader
, num_attribs
) == 4,
171 OffsetOf_ProgramInfoHeader_num_attribs_not_4
);
172 COMPILE_ASSERT(offsetof(ProgramInfoHeader
, num_uniforms
) == 8,
173 OffsetOf_ProgramInfoHeader_num_uniforms_not_8
);
177 #include "../common/gles2_cmd_format_autogen.h"
179 // These are hand written commands.
180 // TODO(gman): Attempt to make these auto-generated.
182 struct GetAttribLocation
{
183 typedef GetAttribLocation ValueType
;
184 static const CommandId kCmdId
= kGetAttribLocation
;
185 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
187 typedef GLint Result
;
189 static uint32
ComputeSize() {
190 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
194 header
.SetCmd
<ValueType
>();
198 GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
199 uint32 _location_shm_id
, uint32 _location_shm_offset
,
203 name_shm_id
= _name_shm_id
;
204 name_shm_offset
= _name_shm_offset
;
205 location_shm_id
= _location_shm_id
;
206 location_shm_offset
= _location_shm_offset
;
207 data_size
= _data_size
;
211 void* cmd
, GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
212 uint32 _location_shm_id
, uint32 _location_shm_offset
,
214 static_cast<ValueType
*>(
216 _program
, _name_shm_id
, _name_shm_offset
, _location_shm_id
,
217 _location_shm_offset
, _data_size
);
218 return NextCmdAddress
<ValueType
>(cmd
);
221 CommandHeader header
;
224 uint32 name_shm_offset
;
225 uint32 location_shm_id
;
226 uint32 location_shm_offset
;
230 COMPILE_ASSERT(sizeof(GetAttribLocation
) == 28,
231 Sizeof_GetAttribLocation_is_not_28
);
232 COMPILE_ASSERT(offsetof(GetAttribLocation
, header
) == 0,
233 OffsetOf_GetAttribLocation_header_not_0
);
234 COMPILE_ASSERT(offsetof(GetAttribLocation
, program
) == 4,
235 OffsetOf_GetAttribLocation_program_not_4
);
236 COMPILE_ASSERT(offsetof(GetAttribLocation
, name_shm_id
) == 8,
237 OffsetOf_GetAttribLocation_name_shm_id_not_8
);
238 COMPILE_ASSERT(offsetof(GetAttribLocation
, name_shm_offset
) == 12,
239 OffsetOf_GetAttribLocation_name_shm_offset_not_12
);
240 COMPILE_ASSERT(offsetof(GetAttribLocation
, location_shm_id
) == 16,
241 OffsetOf_GetAttribLocation_location_shm_id_not_16
);
242 COMPILE_ASSERT(offsetof(GetAttribLocation
, location_shm_offset
) == 20,
243 OffsetOf_GetAttribLocation_location_shm_offset_not_20
);
244 COMPILE_ASSERT(offsetof(GetAttribLocation
, data_size
) == 24,
245 OffsetOf_GetAttribLocation_data_size_not_24
);
247 struct GetAttribLocationImmediate
{
248 typedef GetAttribLocationImmediate ValueType
;
249 static const CommandId kCmdId
= kGetAttribLocationImmediate
;
250 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
252 typedef GLint Result
;
254 static uint32
ComputeDataSize(const char* s
) {
255 return base::checked_numeric_cast
<uint32
>(strlen(s
));
258 static uint32
ComputeSize(const char* s
) {
259 return base::checked_numeric_cast
<uint32
>(sizeof(ValueType
) +
263 void SetHeader(const char* s
) {
264 header
.SetCmdByTotalSize
<ValueType
>(ComputeSize(s
));
268 GLuint _program
, const char* _name
,
269 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
272 location_shm_id
= _location_shm_id
;
273 location_shm_offset
= _location_shm_offset
;
274 data_size
= ComputeDataSize(_name
);
275 memcpy(ImmediateDataAddress(this), _name
, data_size
);
279 void* cmd
, GLuint _program
, const char* _name
,
280 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
281 uint32 total_size
= ComputeSize(_name
);
282 static_cast<ValueType
*>(
283 cmd
)->Init(_program
, _name
, _location_shm_id
, _location_shm_offset
);
284 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, total_size
);
287 CommandHeader header
;
289 uint32 location_shm_id
;
290 uint32 location_shm_offset
;
294 COMPILE_ASSERT(sizeof(GetAttribLocationImmediate
) == 20,
295 Sizeof_GetAttribLocationImmediate_is_not_20
);
296 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, header
) == 0,
297 OffsetOf_GetAttribLocationImmediate_header_not_0
);
298 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, program
) == 4,
299 OffsetOf_GetAttribLocationImmediate_program_not_4
);
300 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, location_shm_id
) == 8,
301 OffsetOf_GetAttribLocationImmediate_location_shm_id_not_8
);
302 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, location_shm_offset
) == 12,
303 OffsetOf_GetAttribLocationImmediate_location_shm_offset_not_12
);
304 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, data_size
) == 16,
305 OffsetOf_GetAttribLocationImmediate_data_size_not_16
);
308 struct GetAttribLocationBucket
{
309 typedef GetAttribLocationBucket ValueType
;
310 static const CommandId kCmdId
= kGetAttribLocationBucket
;
311 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
313 typedef GLint Result
;
315 static uint32
ComputeSize() {
316 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
320 header
.SetCmd
<ValueType
>();
324 GLuint _program
, uint32 _name_bucket_id
,
325 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
328 name_bucket_id
= _name_bucket_id
;
329 location_shm_id
= _location_shm_id
;
330 location_shm_offset
= _location_shm_offset
;
334 void* cmd
, GLuint _program
, uint32 _name_bucket_id
,
335 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
336 static_cast<ValueType
*>(
338 _program
, _name_bucket_id
, _location_shm_id
,
339 _location_shm_offset
);
340 return NextCmdAddress
<ValueType
>(cmd
);
343 CommandHeader header
;
345 uint32 name_bucket_id
;
346 uint32 location_shm_id
;
347 uint32 location_shm_offset
;
350 COMPILE_ASSERT(sizeof(GetAttribLocationBucket
) == 20,
351 Sizeof_GetAttribLocationBucket_is_not_24
);
352 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, header
) == 0,
353 OffsetOf_GetAttribLocationBucket_header_not_0
);
354 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, program
) == 4,
355 OffsetOf_GetAttribLocationBucket_program_not_4
);
356 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, name_bucket_id
) == 8,
357 OffsetOf_GetAttribLocationBucket_name_bucket_id_not_8
);
358 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, location_shm_id
) == 12,
359 OffsetOf_GetAttribLocationBucket_location_shm_id_not_12
);
360 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, location_shm_offset
) == 16,
361 OffsetOf_GetAttribLocationBucket_location_shm_offset_not_16
);
363 struct GetUniformLocation
{
364 typedef GetUniformLocation ValueType
;
365 static const CommandId kCmdId
= kGetUniformLocation
;
366 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
368 typedef GLint Result
;
370 static uint32
ComputeSize() {
371 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
375 header
.SetCmd
<ValueType
>();
379 GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
380 uint32 _location_shm_id
, uint32 _location_shm_offset
,
384 name_shm_id
= _name_shm_id
;
385 name_shm_offset
= _name_shm_offset
;
386 location_shm_id
= _location_shm_id
;
387 location_shm_offset
= _location_shm_offset
;
388 data_size
= _data_size
;
392 void* cmd
, GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
393 uint32 _location_shm_id
, uint32 _location_shm_offset
,
395 static_cast<ValueType
*>(
397 _program
, _name_shm_id
, _name_shm_offset
, _location_shm_id
,
398 _location_shm_offset
, _data_size
);
399 return NextCmdAddress
<ValueType
>(cmd
);
402 CommandHeader header
;
405 uint32 name_shm_offset
;
406 uint32 location_shm_id
;
407 uint32 location_shm_offset
;
411 COMPILE_ASSERT(sizeof(GetUniformLocation
) == 28,
412 Sizeof_GetUniformLocation_is_not_28
);
413 COMPILE_ASSERT(offsetof(GetUniformLocation
, header
) == 0,
414 OffsetOf_GetUniformLocation_header_not_0
);
415 COMPILE_ASSERT(offsetof(GetUniformLocation
, program
) == 4,
416 OffsetOf_GetUniformLocation_program_not_4
);
417 COMPILE_ASSERT(offsetof(GetUniformLocation
, name_shm_id
) == 8,
418 OffsetOf_GetUniformLocation_name_shm_id_not_8
);
419 COMPILE_ASSERT(offsetof(GetUniformLocation
, name_shm_offset
) == 12,
420 OffsetOf_GetUniformLocation_name_shm_offset_not_12
);
421 COMPILE_ASSERT(offsetof(GetUniformLocation
, location_shm_id
) == 16,
422 OffsetOf_GetUniformLocation_location_shm_id_not_16
);
423 COMPILE_ASSERT(offsetof(GetUniformLocation
, location_shm_offset
) == 20,
424 OffsetOf_GetUniformLocation_location_shm_offset_not_20
);
425 COMPILE_ASSERT(offsetof(GetUniformLocation
, data_size
) == 24,
426 OffsetOf_GetUniformLocation_data_size_not_24
);
428 struct GetUniformLocationImmediate
{
429 typedef GetUniformLocationImmediate ValueType
;
430 static const CommandId kCmdId
= kGetUniformLocationImmediate
;
431 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
433 typedef GLint Result
;
435 static uint32
ComputeDataSize(const char* s
) {
436 return base::checked_numeric_cast
<uint32
>(strlen(s
));
439 static uint32
ComputeSize(const char* s
) {
440 return base::checked_numeric_cast
<uint32
>(sizeof(ValueType
) +
444 void SetHeader(const char* s
) {
445 header
.SetCmdByTotalSize
<ValueType
>(ComputeSize(s
));
449 GLuint _program
, const char* _name
,
450 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
453 location_shm_id
= _location_shm_id
;
454 location_shm_offset
= _location_shm_offset
;
455 data_size
= ComputeDataSize(_name
);
456 memcpy(ImmediateDataAddress(this), _name
, data_size
);
460 void* cmd
, GLuint _program
, const char* _name
,
461 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
462 uint32 total_size
= ComputeSize(_name
);
463 static_cast<ValueType
*>(
464 cmd
)->Init(_program
, _name
, _location_shm_id
, _location_shm_offset
);
465 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, total_size
);
468 CommandHeader header
;
470 uint32 location_shm_id
;
471 uint32 location_shm_offset
;
475 COMPILE_ASSERT(sizeof(GetUniformLocationImmediate
) == 20,
476 Sizeof_GetUniformLocationImmediate_is_not_20
);
477 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, header
) == 0,
478 OffsetOf_GetUniformLocationImmediate_header_not_0
);
479 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, program
) == 4,
480 OffsetOf_GetUniformLocationImmediate_program_not_4
);
481 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, location_shm_id
) == 8,
482 OffsetOf_GetUniformLocationImmediate_location_shm_id_not_8
);
484 offsetof(GetUniformLocationImmediate
, location_shm_offset
) == 12,
485 OffsetOf_GetUniformLocationImmediate_location_shm_offset_not_12
);
486 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, data_size
) == 16,
487 OffsetOf_GetUniformLocationImmediate_data_size_not_16
);
489 struct GetUniformLocationBucket
{
490 typedef GetUniformLocationBucket ValueType
;
491 static const CommandId kCmdId
= kGetUniformLocationBucket
;
492 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
494 typedef GLint Result
;
496 static uint32
ComputeSize() {
497 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
501 header
.SetCmd
<ValueType
>();
505 GLuint _program
, uint32 _name_bucket_id
,
506 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
509 name_bucket_id
= _name_bucket_id
;
510 location_shm_id
= _location_shm_id
;
511 location_shm_offset
= _location_shm_offset
;
515 void* cmd
, GLuint _program
, uint32 _name_bucket_id
,
516 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
517 static_cast<ValueType
*>(
519 _program
, _name_bucket_id
, _location_shm_id
,
520 _location_shm_offset
);
521 return NextCmdAddress
<ValueType
>(cmd
);
524 CommandHeader header
;
526 uint32 name_bucket_id
;
527 uint32 location_shm_id
;
528 uint32 location_shm_offset
;
531 COMPILE_ASSERT(sizeof(GetUniformLocationBucket
) == 20,
532 Sizeof_GetUniformLocationBucket_is_not_24
);
533 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, header
) == 0,
534 OffsetOf_GetUniformLocationBucket_header_not_0
);
535 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, program
) == 4,
536 OffsetOf_GetUniformLocationBucket_program_not_4
);
537 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, name_bucket_id
) == 8,
538 OffsetOf_GetUniformLocationBucket_name_bucket_id_not_8
);
539 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, location_shm_id
) == 12,
540 OffsetOf_GetUniformLocationBucket_location_shm_id_not_12
);
541 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, location_shm_offset
) == 16,
542 OffsetOf_GetUniformLocationBucket_location_shm_offset_not_16
);
544 struct InsertSyncPointCHROMIUM
{
545 typedef InsertSyncPointCHROMIUM ValueType
;
546 static const CommandId kCmdId
= kInsertSyncPointCHROMIUM
;
547 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
548 CommandHeader header
;
557 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_