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.
62 // These numbers must not change
63 COMPILE_ASSERT(kBuffers
== 0, kBuffers_is_not_0
);
64 COMPILE_ASSERT(kFramebuffers
== 1, kFramebuffers_is_not_1
);
65 COMPILE_ASSERT(kProgramsAndShaders
== 2, kProgramsAndShaders_is_not_2
);
66 COMPILE_ASSERT(kRenderbuffers
== 3, kRenderbuffers_is_not_3
);
67 COMPILE_ASSERT(kTextures
== 4, kTextures_is_not_4
);
69 } // namespace id_namespaces
71 // Used for some glGetXXX commands that return a result through a pointer. We
72 // need to know if the command succeeded or not and the size of the result. If
73 // the command failed its result size will 0.
79 return static_cast<T
*>(static_cast<void*>(&data
));
82 // Returns the total size in bytes of the SizedResult for a given number of
83 // results including the size field.
84 static size_t ComputeSize(size_t num_results
) {
85 return sizeof(T
) * num_results
+ sizeof(uint32
); // NOLINT
88 // Returns the total size in bytes of the SizedResult for a given size of
90 static size_t ComputeSizeFromBytes(size_t size_of_result_in_bytes
) {
91 return size_of_result_in_bytes
+ sizeof(uint32
); // NOLINT
94 // Returns the maximum number of results for a given buffer size.
95 static uint32
ComputeMaxResults(size_t size_of_buffer
) {
96 return (size_of_buffer
>= sizeof(uint32
)) ?
97 ((size_of_buffer
- sizeof(uint32
)) / sizeof(T
)) : 0; // NOLINT
100 // Set the size for a given number of results.
101 void SetNumResults(size_t num_results
) {
102 size
= sizeof(T
) * num_results
; // NOLINT
105 // Get the number of elements in the result
106 int32
GetNumResults() const {
107 return size
/ sizeof(T
); // NOLINT
111 void CopyResult(void* dst
) const {
112 memcpy(dst
, &data
, size
);
115 uint32 size
; // in bytes.
116 int32 data
; // this is just here to get an offset.
119 COMPILE_ASSERT(sizeof(SizedResult
<int8
>) == 8, SizedResult_size_not_8
);
120 COMPILE_ASSERT(offsetof(SizedResult
<int8
>, size
) == 0,
121 OffsetOf_SizedResult_size_not_0
);
122 COMPILE_ASSERT(offsetof(SizedResult
<int8
>, data
) == 4,
123 OffsetOf_SizedResult_data_not_4
);
125 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
126 struct ProgramInput
{
127 uint32 type
; // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
128 int32 size
; // The size (how big the array is for uniforms)
129 uint32 location_offset
; // offset from ProgramInfoHeader to 'size' locations
130 // for uniforms, 1 for attribs.
131 uint32 name_offset
; // offset from ProgrmaInfoHeader to start of name.
132 uint32 name_length
; // length of the name.
135 // The format of the bucket filled out by GetProgramInfoCHROMIUM
136 struct ProgramInfoHeader
{
140 // ProgramInput inputs[num_attribs + num_uniforms];
143 // The format of QuerySync used by EXT_occlusion_query_boolean
150 uint32 process_count
;
154 COMPILE_ASSERT(sizeof(ProgramInput
) == 20, ProgramInput_size_not_20
);
155 COMPILE_ASSERT(offsetof(ProgramInput
, type
) == 0,
156 OffsetOf_ProgramInput_type_not_0
);
157 COMPILE_ASSERT(offsetof(ProgramInput
, size
) == 4,
158 OffsetOf_ProgramInput_size_not_4
);
159 COMPILE_ASSERT(offsetof(ProgramInput
, location_offset
) == 8,
160 OffsetOf_ProgramInput_location_offset_not_8
);
161 COMPILE_ASSERT(offsetof(ProgramInput
, name_offset
) == 12,
162 OffsetOf_ProgramInput_name_offset_not_12
);
163 COMPILE_ASSERT(offsetof(ProgramInput
, name_length
) == 16,
164 OffsetOf_ProgramInput_name_length_not_16
);
166 COMPILE_ASSERT(sizeof(ProgramInfoHeader
) == 12, ProgramInfoHeader_size_not_12
);
167 COMPILE_ASSERT(offsetof(ProgramInfoHeader
, link_status
) == 0,
168 OffsetOf_ProgramInfoHeader_link_status_not_0
);
169 COMPILE_ASSERT(offsetof(ProgramInfoHeader
, num_attribs
) == 4,
170 OffsetOf_ProgramInfoHeader_num_attribs_not_4
);
171 COMPILE_ASSERT(offsetof(ProgramInfoHeader
, num_uniforms
) == 8,
172 OffsetOf_ProgramInfoHeader_num_uniforms_not_8
);
176 #include "../common/gles2_cmd_format_autogen.h"
178 // These are hand written commands.
179 // TODO(gman): Attempt to make these auto-generated.
181 struct GetAttribLocation
{
182 typedef GetAttribLocation ValueType
;
183 static const CommandId kCmdId
= kGetAttribLocation
;
184 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
186 typedef GLint Result
;
188 static uint32
ComputeSize() {
189 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
193 header
.SetCmd
<ValueType
>();
197 GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
198 uint32 _location_shm_id
, uint32 _location_shm_offset
,
202 name_shm_id
= _name_shm_id
;
203 name_shm_offset
= _name_shm_offset
;
204 location_shm_id
= _location_shm_id
;
205 location_shm_offset
= _location_shm_offset
;
206 data_size
= _data_size
;
210 void* cmd
, GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
211 uint32 _location_shm_id
, uint32 _location_shm_offset
,
213 static_cast<ValueType
*>(
215 _program
, _name_shm_id
, _name_shm_offset
, _location_shm_id
,
216 _location_shm_offset
, _data_size
);
217 return NextCmdAddress
<ValueType
>(cmd
);
220 CommandHeader header
;
223 uint32 name_shm_offset
;
224 uint32 location_shm_id
;
225 uint32 location_shm_offset
;
229 COMPILE_ASSERT(sizeof(GetAttribLocation
) == 28,
230 Sizeof_GetAttribLocation_is_not_28
);
231 COMPILE_ASSERT(offsetof(GetAttribLocation
, header
) == 0,
232 OffsetOf_GetAttribLocation_header_not_0
);
233 COMPILE_ASSERT(offsetof(GetAttribLocation
, program
) == 4,
234 OffsetOf_GetAttribLocation_program_not_4
);
235 COMPILE_ASSERT(offsetof(GetAttribLocation
, name_shm_id
) == 8,
236 OffsetOf_GetAttribLocation_name_shm_id_not_8
);
237 COMPILE_ASSERT(offsetof(GetAttribLocation
, name_shm_offset
) == 12,
238 OffsetOf_GetAttribLocation_name_shm_offset_not_12
);
239 COMPILE_ASSERT(offsetof(GetAttribLocation
, location_shm_id
) == 16,
240 OffsetOf_GetAttribLocation_location_shm_id_not_16
);
241 COMPILE_ASSERT(offsetof(GetAttribLocation
, location_shm_offset
) == 20,
242 OffsetOf_GetAttribLocation_location_shm_offset_not_20
);
243 COMPILE_ASSERT(offsetof(GetAttribLocation
, data_size
) == 24,
244 OffsetOf_GetAttribLocation_data_size_not_24
);
246 struct GetAttribLocationImmediate
{
247 typedef GetAttribLocationImmediate ValueType
;
248 static const CommandId kCmdId
= kGetAttribLocationImmediate
;
249 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
251 typedef GLint Result
;
253 static uint32
ComputeDataSize(const char* s
) {
254 return base::checked_numeric_cast
<uint32
>(strlen(s
));
257 static uint32
ComputeSize(const char* s
) {
258 return base::checked_numeric_cast
<uint32
>(sizeof(ValueType
) +
262 void SetHeader(const char* s
) {
263 header
.SetCmdByTotalSize
<ValueType
>(ComputeSize(s
));
267 GLuint _program
, const char* _name
,
268 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
271 location_shm_id
= _location_shm_id
;
272 location_shm_offset
= _location_shm_offset
;
273 data_size
= ComputeDataSize(_name
);
274 memcpy(ImmediateDataAddress(this), _name
, data_size
);
278 void* cmd
, GLuint _program
, const char* _name
,
279 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
280 uint32 total_size
= ComputeSize(_name
);
281 static_cast<ValueType
*>(
282 cmd
)->Init(_program
, _name
, _location_shm_id
, _location_shm_offset
);
283 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, total_size
);
286 CommandHeader header
;
288 uint32 location_shm_id
;
289 uint32 location_shm_offset
;
293 COMPILE_ASSERT(sizeof(GetAttribLocationImmediate
) == 20,
294 Sizeof_GetAttribLocationImmediate_is_not_20
);
295 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, header
) == 0,
296 OffsetOf_GetAttribLocationImmediate_header_not_0
);
297 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, program
) == 4,
298 OffsetOf_GetAttribLocationImmediate_program_not_4
);
299 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, location_shm_id
) == 8,
300 OffsetOf_GetAttribLocationImmediate_location_shm_id_not_8
);
301 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, location_shm_offset
) == 12,
302 OffsetOf_GetAttribLocationImmediate_location_shm_offset_not_12
);
303 COMPILE_ASSERT(offsetof(GetAttribLocationImmediate
, data_size
) == 16,
304 OffsetOf_GetAttribLocationImmediate_data_size_not_16
);
307 struct GetAttribLocationBucket
{
308 typedef GetAttribLocationBucket ValueType
;
309 static const CommandId kCmdId
= kGetAttribLocationBucket
;
310 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
312 typedef GLint Result
;
314 static uint32
ComputeSize() {
315 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
319 header
.SetCmd
<ValueType
>();
323 GLuint _program
, uint32 _name_bucket_id
,
324 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
327 name_bucket_id
= _name_bucket_id
;
328 location_shm_id
= _location_shm_id
;
329 location_shm_offset
= _location_shm_offset
;
333 void* cmd
, GLuint _program
, uint32 _name_bucket_id
,
334 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
335 static_cast<ValueType
*>(
337 _program
, _name_bucket_id
, _location_shm_id
,
338 _location_shm_offset
);
339 return NextCmdAddress
<ValueType
>(cmd
);
342 CommandHeader header
;
344 uint32 name_bucket_id
;
345 uint32 location_shm_id
;
346 uint32 location_shm_offset
;
349 COMPILE_ASSERT(sizeof(GetAttribLocationBucket
) == 20,
350 Sizeof_GetAttribLocationBucket_is_not_24
);
351 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, header
) == 0,
352 OffsetOf_GetAttribLocationBucket_header_not_0
);
353 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, program
) == 4,
354 OffsetOf_GetAttribLocationBucket_program_not_4
);
355 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, name_bucket_id
) == 8,
356 OffsetOf_GetAttribLocationBucket_name_bucket_id_not_8
);
357 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, location_shm_id
) == 12,
358 OffsetOf_GetAttribLocationBucket_location_shm_id_not_12
);
359 COMPILE_ASSERT(offsetof(GetAttribLocationBucket
, location_shm_offset
) == 16,
360 OffsetOf_GetAttribLocationBucket_location_shm_offset_not_16
);
362 struct GetUniformLocation
{
363 typedef GetUniformLocation ValueType
;
364 static const CommandId kCmdId
= kGetUniformLocation
;
365 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
367 typedef GLint Result
;
369 static uint32
ComputeSize() {
370 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
374 header
.SetCmd
<ValueType
>();
378 GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
379 uint32 _location_shm_id
, uint32 _location_shm_offset
,
383 name_shm_id
= _name_shm_id
;
384 name_shm_offset
= _name_shm_offset
;
385 location_shm_id
= _location_shm_id
;
386 location_shm_offset
= _location_shm_offset
;
387 data_size
= _data_size
;
391 void* cmd
, GLuint _program
, uint32 _name_shm_id
, uint32 _name_shm_offset
,
392 uint32 _location_shm_id
, uint32 _location_shm_offset
,
394 static_cast<ValueType
*>(
396 _program
, _name_shm_id
, _name_shm_offset
, _location_shm_id
,
397 _location_shm_offset
, _data_size
);
398 return NextCmdAddress
<ValueType
>(cmd
);
401 CommandHeader header
;
404 uint32 name_shm_offset
;
405 uint32 location_shm_id
;
406 uint32 location_shm_offset
;
410 COMPILE_ASSERT(sizeof(GetUniformLocation
) == 28,
411 Sizeof_GetUniformLocation_is_not_28
);
412 COMPILE_ASSERT(offsetof(GetUniformLocation
, header
) == 0,
413 OffsetOf_GetUniformLocation_header_not_0
);
414 COMPILE_ASSERT(offsetof(GetUniformLocation
, program
) == 4,
415 OffsetOf_GetUniformLocation_program_not_4
);
416 COMPILE_ASSERT(offsetof(GetUniformLocation
, name_shm_id
) == 8,
417 OffsetOf_GetUniformLocation_name_shm_id_not_8
);
418 COMPILE_ASSERT(offsetof(GetUniformLocation
, name_shm_offset
) == 12,
419 OffsetOf_GetUniformLocation_name_shm_offset_not_12
);
420 COMPILE_ASSERT(offsetof(GetUniformLocation
, location_shm_id
) == 16,
421 OffsetOf_GetUniformLocation_location_shm_id_not_16
);
422 COMPILE_ASSERT(offsetof(GetUniformLocation
, location_shm_offset
) == 20,
423 OffsetOf_GetUniformLocation_location_shm_offset_not_20
);
424 COMPILE_ASSERT(offsetof(GetUniformLocation
, data_size
) == 24,
425 OffsetOf_GetUniformLocation_data_size_not_24
);
427 struct GetUniformLocationImmediate
{
428 typedef GetUniformLocationImmediate ValueType
;
429 static const CommandId kCmdId
= kGetUniformLocationImmediate
;
430 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
432 typedef GLint Result
;
434 static uint32
ComputeDataSize(const char* s
) {
435 return base::checked_numeric_cast
<uint32
>(strlen(s
));
438 static uint32
ComputeSize(const char* s
) {
439 return base::checked_numeric_cast
<uint32
>(sizeof(ValueType
) +
443 void SetHeader(const char* s
) {
444 header
.SetCmdByTotalSize
<ValueType
>(ComputeSize(s
));
448 GLuint _program
, const char* _name
,
449 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
452 location_shm_id
= _location_shm_id
;
453 location_shm_offset
= _location_shm_offset
;
454 data_size
= ComputeDataSize(_name
);
455 memcpy(ImmediateDataAddress(this), _name
, data_size
);
459 void* cmd
, GLuint _program
, const char* _name
,
460 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
461 uint32 total_size
= ComputeSize(_name
);
462 static_cast<ValueType
*>(
463 cmd
)->Init(_program
, _name
, _location_shm_id
, _location_shm_offset
);
464 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, total_size
);
467 CommandHeader header
;
469 uint32 location_shm_id
;
470 uint32 location_shm_offset
;
474 COMPILE_ASSERT(sizeof(GetUniformLocationImmediate
) == 20,
475 Sizeof_GetUniformLocationImmediate_is_not_20
);
476 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, header
) == 0,
477 OffsetOf_GetUniformLocationImmediate_header_not_0
);
478 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, program
) == 4,
479 OffsetOf_GetUniformLocationImmediate_program_not_4
);
480 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, location_shm_id
) == 8,
481 OffsetOf_GetUniformLocationImmediate_location_shm_id_not_8
);
483 offsetof(GetUniformLocationImmediate
, location_shm_offset
) == 12,
484 OffsetOf_GetUniformLocationImmediate_location_shm_offset_not_12
);
485 COMPILE_ASSERT(offsetof(GetUniformLocationImmediate
, data_size
) == 16,
486 OffsetOf_GetUniformLocationImmediate_data_size_not_16
);
488 struct GetUniformLocationBucket
{
489 typedef GetUniformLocationBucket ValueType
;
490 static const CommandId kCmdId
= kGetUniformLocationBucket
;
491 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
493 typedef GLint Result
;
495 static uint32
ComputeSize() {
496 return static_cast<uint32
>(sizeof(ValueType
)); // NOLINT
500 header
.SetCmd
<ValueType
>();
504 GLuint _program
, uint32 _name_bucket_id
,
505 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
508 name_bucket_id
= _name_bucket_id
;
509 location_shm_id
= _location_shm_id
;
510 location_shm_offset
= _location_shm_offset
;
514 void* cmd
, GLuint _program
, uint32 _name_bucket_id
,
515 uint32 _location_shm_id
, uint32 _location_shm_offset
) {
516 static_cast<ValueType
*>(
518 _program
, _name_bucket_id
, _location_shm_id
,
519 _location_shm_offset
);
520 return NextCmdAddress
<ValueType
>(cmd
);
523 CommandHeader header
;
525 uint32 name_bucket_id
;
526 uint32 location_shm_id
;
527 uint32 location_shm_offset
;
530 COMPILE_ASSERT(sizeof(GetUniformLocationBucket
) == 20,
531 Sizeof_GetUniformLocationBucket_is_not_24
);
532 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, header
) == 0,
533 OffsetOf_GetUniformLocationBucket_header_not_0
);
534 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, program
) == 4,
535 OffsetOf_GetUniformLocationBucket_program_not_4
);
536 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, name_bucket_id
) == 8,
537 OffsetOf_GetUniformLocationBucket_name_bucket_id_not_8
);
538 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, location_shm_id
) == 12,
539 OffsetOf_GetUniformLocationBucket_location_shm_id_not_12
);
540 COMPILE_ASSERT(offsetof(GetUniformLocationBucket
, location_shm_offset
) == 16,
541 OffsetOf_GetUniformLocationBucket_location_shm_offset_not_16
);
543 struct InsertSyncPointCHROMIUM
{
544 typedef InsertSyncPointCHROMIUM ValueType
;
545 static const CommandId kCmdId
= kInsertSyncPointCHROMIUM
;
546 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
547 CommandHeader header
;
556 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_