gles2_cmd_format.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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.
4
5// This file defines the GLES2 command buffer commands.
6
7#ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_
8#define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_
9
10
11#include <KHR/khrplatform.h>
12
13#include <string.h>
14
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"
20
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).
24typedef unsigned int GLenum;
25typedef unsigned int GLbitfield;
26typedef unsigned int GLuint;
27typedef int GLint;
28typedef int GLsizei;
29typedef unsigned char GLboolean;
30typedef signed char GLbyte;
31typedef short GLshort;
32typedef unsigned char GLubyte;
33typedef unsigned short GLushort;
34typedef unsigned long GLulong;
35typedef float GLfloat;
36typedef float GLclampf;
37typedef double GLdouble;
38typedef double GLclampd;
39typedef void GLvoid;
40typedef khronos_intptr_t GLintptr;
41typedef khronos_ssize_t  GLsizeiptr;
42
43namespace gpu {
44namespace gles2 {
45
46#pragma pack(push, 1)
47
48namespace id_namespaces {
49
50// These are used when contexts share resources.
51enum IdNamespaces {
52  kBuffers,
53  kFramebuffers,
54  kProgramsAndShaders,
55  kRenderbuffers,
56  kTextures,
57  kQueries,
58  kVertexArrays,
59  kNumIdNamespaces
60};
61
62// These numbers must not change
63COMPILE_ASSERT(kBuffers == 0, kBuffers_is_not_0);
64COMPILE_ASSERT(kFramebuffers == 1, kFramebuffers_is_not_1);
65COMPILE_ASSERT(kProgramsAndShaders == 2, kProgramsAndShaders_is_not_2);
66COMPILE_ASSERT(kRenderbuffers == 3, kRenderbuffers_is_not_3);
67COMPILE_ASSERT(kTextures == 4, kTextures_is_not_4);
68
69}  // namespace id_namespaces
70
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.
74template <typename T>
75struct SizedResult {
76  typedef T Type;
77
78  T* GetData() {
79    return static_cast<T*>(static_cast<void*>(&data));
80  }
81
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
86  }
87
88  // Returns the total size in bytes of the SizedResult for a given size of
89  // results.
90  static size_t ComputeSizeFromBytes(size_t size_of_result_in_bytes) {
91    return size_of_result_in_bytes + sizeof(uint32);  // NOLINT
92  }
93
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
98  }
99
100  // Set the size for a given number of results.
101  void SetNumResults(size_t num_results) {
102    size = sizeof(T) * num_results;  // NOLINT
103  }
104
105  // Get the number of elements in the result
106  int32 GetNumResults() const {
107    return size / sizeof(T);  // NOLINT
108  }
109
110  // Copy the result.
111  void CopyResult(void* dst) const {
112    memcpy(dst, &data, size);
113  }
114
115  uint32 size;  // in bytes.
116  int32 data;  // this is just here to get an offset.
117};
118
119COMPILE_ASSERT(sizeof(SizedResult<int8>) == 8, SizedResult_size_not_8);
120COMPILE_ASSERT(offsetof(SizedResult<int8>, size) == 0,
121               OffsetOf_SizedResult_size_not_0);
122COMPILE_ASSERT(offsetof(SizedResult<int8>, data) == 4,
123               OffsetOf_SizedResult_data_not_4);
124
125// The data for one attrib or uniform from GetProgramInfoCHROMIUM.
126struct 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.
133};
134
135// The format of the bucket filled out by GetProgramInfoCHROMIUM
136struct ProgramInfoHeader {
137  uint32 link_status;
138  uint32 num_attribs;
139  uint32 num_uniforms;
140  // ProgramInput inputs[num_attribs + num_uniforms];
141};
142
143// The format of QuerySync used by EXT_occlusion_query_boolean
144struct QuerySync {
145  void Reset() {
146    process_count = 0;
147    result = 0;
148  }
149
150  uint32 process_count;
151  uint64 result;
152};
153
154COMPILE_ASSERT(sizeof(ProgramInput) == 20, ProgramInput_size_not_20);
155COMPILE_ASSERT(offsetof(ProgramInput, type) == 0,
156               OffsetOf_ProgramInput_type_not_0);
157COMPILE_ASSERT(offsetof(ProgramInput, size) == 4,
158               OffsetOf_ProgramInput_size_not_4);
159COMPILE_ASSERT(offsetof(ProgramInput, location_offset) == 8,
160               OffsetOf_ProgramInput_location_offset_not_8);
161COMPILE_ASSERT(offsetof(ProgramInput, name_offset) == 12,
162               OffsetOf_ProgramInput_name_offset_not_12);
163COMPILE_ASSERT(offsetof(ProgramInput, name_length) == 16,
164               OffsetOf_ProgramInput_name_length_not_16);
165
166COMPILE_ASSERT(sizeof(ProgramInfoHeader) == 12, ProgramInfoHeader_size_not_12);
167COMPILE_ASSERT(offsetof(ProgramInfoHeader, link_status) == 0,
168               OffsetOf_ProgramInfoHeader_link_status_not_0);
169COMPILE_ASSERT(offsetof(ProgramInfoHeader, num_attribs) == 4,
170               OffsetOf_ProgramInfoHeader_num_attribs_not_4);
171COMPILE_ASSERT(offsetof(ProgramInfoHeader, num_uniforms) == 8,
172               OffsetOf_ProgramInfoHeader_num_uniforms_not_8);
173
174namespace cmds {
175
176#include "../common/gles2_cmd_format_autogen.h"
177
178// These are hand written commands.
179// TODO(gman): Attempt to make these auto-generated.
180
181struct GetAttribLocation {
182  typedef GetAttribLocation ValueType;
183  static const CommandId kCmdId = kGetAttribLocation;
184  static const cmd::ArgFlags kArgFlags = cmd::kFixed;
185
186  typedef GLint Result;
187
188  static uint32 ComputeSize() {
189    return static_cast<uint32>(sizeof(ValueType));  // NOLINT
190  }
191
192  void SetHeader() {
193    header.SetCmd<ValueType>();
194  }
195
196  void Init(
197      GLuint _program, uint32 _name_shm_id, uint32 _name_shm_offset,
198      uint32 _location_shm_id, uint32 _location_shm_offset,
199      uint32 _data_size) {
200    SetHeader();
201    program = _program;
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;
207  }
208
209  void* Set(
210      void* cmd, GLuint _program, uint32 _name_shm_id, uint32 _name_shm_offset,
211      uint32 _location_shm_id, uint32 _location_shm_offset,
212      uint32 _data_size) {
213    static_cast<ValueType*>(
214        cmd)->Init(
215            _program, _name_shm_id, _name_shm_offset, _location_shm_id,
216            _location_shm_offset, _data_size);
217    return NextCmdAddress<ValueType>(cmd);
218  }
219
220  CommandHeader header;
221  uint32 program;
222  uint32 name_shm_id;
223  uint32 name_shm_offset;
224  uint32 location_shm_id;
225  uint32 location_shm_offset;
226  uint32 data_size;
227};
228
229COMPILE_ASSERT(sizeof(GetAttribLocation) == 28,
230               Sizeof_GetAttribLocation_is_not_28);
231COMPILE_ASSERT(offsetof(GetAttribLocation, header) == 0,
232               OffsetOf_GetAttribLocation_header_not_0);
233COMPILE_ASSERT(offsetof(GetAttribLocation, program) == 4,
234               OffsetOf_GetAttribLocation_program_not_4);
235COMPILE_ASSERT(offsetof(GetAttribLocation, name_shm_id) == 8,
236               OffsetOf_GetAttribLocation_name_shm_id_not_8);
237COMPILE_ASSERT(offsetof(GetAttribLocation, name_shm_offset) == 12,
238               OffsetOf_GetAttribLocation_name_shm_offset_not_12);
239COMPILE_ASSERT(offsetof(GetAttribLocation, location_shm_id) == 16,
240               OffsetOf_GetAttribLocation_location_shm_id_not_16);
241COMPILE_ASSERT(offsetof(GetAttribLocation, location_shm_offset) == 20,
242               OffsetOf_GetAttribLocation_location_shm_offset_not_20);
243COMPILE_ASSERT(offsetof(GetAttribLocation, data_size) == 24,
244               OffsetOf_GetAttribLocation_data_size_not_24);
245
246struct GetAttribLocationImmediate {
247  typedef GetAttribLocationImmediate ValueType;
248  static const CommandId kCmdId = kGetAttribLocationImmediate;
249  static const cmd::ArgFlags kArgFlags = cmd::kAtLeastN;
250
251  typedef GLint Result;
252
253  static uint32 ComputeDataSize(const char* s) {
254    return base::checked_numeric_cast<uint32>(strlen(s));
255  }
256
257  static uint32 ComputeSize(const char* s) {
258    return base::checked_numeric_cast<uint32>(sizeof(ValueType) +
259                                              ComputeDataSize(s));
260  }
261
262  void SetHeader(const char* s) {
263    header.SetCmdByTotalSize<ValueType>(ComputeSize(s));
264  }
265
266  void Init(
267      GLuint _program, const char* _name,
268      uint32 _location_shm_id, uint32 _location_shm_offset) {
269    SetHeader(_name);
270    program = _program;
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);
275  }
276
277  void* Set(
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);
284  }
285
286  CommandHeader header;
287  uint32 program;
288  uint32 location_shm_id;
289  uint32 location_shm_offset;
290  uint32 data_size;
291};
292
293COMPILE_ASSERT(sizeof(GetAttribLocationImmediate) == 20,
294               Sizeof_GetAttribLocationImmediate_is_not_20);
295COMPILE_ASSERT(offsetof(GetAttribLocationImmediate, header) == 0,
296               OffsetOf_GetAttribLocationImmediate_header_not_0);
297COMPILE_ASSERT(offsetof(GetAttribLocationImmediate, program) == 4,
298               OffsetOf_GetAttribLocationImmediate_program_not_4);
299COMPILE_ASSERT(offsetof(GetAttribLocationImmediate, location_shm_id) == 8,
300               OffsetOf_GetAttribLocationImmediate_location_shm_id_not_8);
301COMPILE_ASSERT(offsetof(GetAttribLocationImmediate, location_shm_offset) == 12,
302               OffsetOf_GetAttribLocationImmediate_location_shm_offset_not_12);
303COMPILE_ASSERT(offsetof(GetAttribLocationImmediate, data_size) == 16,
304               OffsetOf_GetAttribLocationImmediate_data_size_not_16);
305
306
307struct GetAttribLocationBucket {
308  typedef GetAttribLocationBucket ValueType;
309  static const CommandId kCmdId = kGetAttribLocationBucket;
310  static const cmd::ArgFlags kArgFlags = cmd::kFixed;
311
312  typedef GLint Result;
313
314  static uint32 ComputeSize() {
315    return static_cast<uint32>(sizeof(ValueType));  // NOLINT
316  }
317
318  void SetHeader() {
319    header.SetCmd<ValueType>();
320  }
321
322  void Init(
323      GLuint _program, uint32 _name_bucket_id,
324      uint32 _location_shm_id, uint32 _location_shm_offset) {
325    SetHeader();
326    program = _program;
327    name_bucket_id = _name_bucket_id;
328    location_shm_id = _location_shm_id;
329    location_shm_offset = _location_shm_offset;
330  }
331
332  void* Set(
333      void* cmd, GLuint _program, uint32 _name_bucket_id,
334      uint32 _location_shm_id, uint32 _location_shm_offset) {
335    static_cast<ValueType*>(
336        cmd)->Init(
337            _program, _name_bucket_id, _location_shm_id,
338            _location_shm_offset);
339    return NextCmdAddress<ValueType>(cmd);
340  }
341
342  CommandHeader header;
343  uint32 program;
344  uint32 name_bucket_id;
345  uint32 location_shm_id;
346  uint32 location_shm_offset;
347};
348
349COMPILE_ASSERT(sizeof(GetAttribLocationBucket) == 20,
350               Sizeof_GetAttribLocationBucket_is_not_24);
351COMPILE_ASSERT(offsetof(GetAttribLocationBucket, header) == 0,
352               OffsetOf_GetAttribLocationBucket_header_not_0);
353COMPILE_ASSERT(offsetof(GetAttribLocationBucket, program) == 4,
354               OffsetOf_GetAttribLocationBucket_program_not_4);
355COMPILE_ASSERT(offsetof(GetAttribLocationBucket, name_bucket_id) == 8,
356               OffsetOf_GetAttribLocationBucket_name_bucket_id_not_8);
357COMPILE_ASSERT(offsetof(GetAttribLocationBucket, location_shm_id) == 12,
358               OffsetOf_GetAttribLocationBucket_location_shm_id_not_12);
359COMPILE_ASSERT(offsetof(GetAttribLocationBucket, location_shm_offset) == 16,
360               OffsetOf_GetAttribLocationBucket_location_shm_offset_not_16);
361
362struct GetUniformLocation {
363  typedef GetUniformLocation ValueType;
364  static const CommandId kCmdId = kGetUniformLocation;
365  static const cmd::ArgFlags kArgFlags = cmd::kFixed;
366
367  typedef GLint Result;
368
369  static uint32 ComputeSize() {
370    return static_cast<uint32>(sizeof(ValueType));  // NOLINT
371  }
372
373  void SetHeader() {
374    header.SetCmd<ValueType>();
375  }
376
377  void Init(
378      GLuint _program, uint32 _name_shm_id, uint32 _name_shm_offset,
379      uint32 _location_shm_id, uint32 _location_shm_offset,
380      uint32 _data_size) {
381    SetHeader();
382    program = _program;
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;
388  }
389
390  void* Set(
391      void* cmd, GLuint _program, uint32 _name_shm_id, uint32 _name_shm_offset,
392      uint32 _location_shm_id, uint32 _location_shm_offset,
393      uint32 _data_size) {
394    static_cast<ValueType*>(
395        cmd)->Init(
396            _program, _name_shm_id, _name_shm_offset, _location_shm_id,
397            _location_shm_offset, _data_size);
398    return NextCmdAddress<ValueType>(cmd);
399  }
400
401  CommandHeader header;
402  uint32 program;
403  uint32 name_shm_id;
404  uint32 name_shm_offset;
405  uint32 location_shm_id;
406  uint32 location_shm_offset;
407  uint32 data_size;
408};
409
410COMPILE_ASSERT(sizeof(GetUniformLocation) == 28,
411               Sizeof_GetUniformLocation_is_not_28);
412COMPILE_ASSERT(offsetof(GetUniformLocation, header) == 0,
413               OffsetOf_GetUniformLocation_header_not_0);
414COMPILE_ASSERT(offsetof(GetUniformLocation, program) == 4,
415               OffsetOf_GetUniformLocation_program_not_4);
416COMPILE_ASSERT(offsetof(GetUniformLocation, name_shm_id) == 8,
417               OffsetOf_GetUniformLocation_name_shm_id_not_8);
418COMPILE_ASSERT(offsetof(GetUniformLocation, name_shm_offset) == 12,
419               OffsetOf_GetUniformLocation_name_shm_offset_not_12);
420COMPILE_ASSERT(offsetof(GetUniformLocation, location_shm_id) == 16,
421               OffsetOf_GetUniformLocation_location_shm_id_not_16);
422COMPILE_ASSERT(offsetof(GetUniformLocation, location_shm_offset) == 20,
423               OffsetOf_GetUniformLocation_location_shm_offset_not_20);
424COMPILE_ASSERT(offsetof(GetUniformLocation, data_size) == 24,
425               OffsetOf_GetUniformLocation_data_size_not_24);
426
427struct GetUniformLocationImmediate {
428  typedef GetUniformLocationImmediate ValueType;
429  static const CommandId kCmdId = kGetUniformLocationImmediate;
430  static const cmd::ArgFlags kArgFlags = cmd::kAtLeastN;
431
432  typedef GLint Result;
433
434  static uint32 ComputeDataSize(const char* s) {
435    return base::checked_numeric_cast<uint32>(strlen(s));
436  }
437
438  static uint32 ComputeSize(const char* s) {
439    return base::checked_numeric_cast<uint32>(sizeof(ValueType) +
440                                              ComputeDataSize(s));
441  }
442
443  void SetHeader(const char* s) {
444    header.SetCmdByTotalSize<ValueType>(ComputeSize(s));
445  }
446
447  void Init(
448      GLuint _program, const char* _name,
449      uint32 _location_shm_id, uint32 _location_shm_offset) {
450    SetHeader(_name);
451    program = _program;
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);
456  }
457
458  void* Set(
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);
465  }
466
467  CommandHeader header;
468  uint32 program;
469  uint32 location_shm_id;
470  uint32 location_shm_offset;
471  uint32 data_size;
472};
473
474COMPILE_ASSERT(sizeof(GetUniformLocationImmediate) == 20,
475               Sizeof_GetUniformLocationImmediate_is_not_20);
476COMPILE_ASSERT(offsetof(GetUniformLocationImmediate, header) == 0,
477               OffsetOf_GetUniformLocationImmediate_header_not_0);
478COMPILE_ASSERT(offsetof(GetUniformLocationImmediate, program) == 4,
479               OffsetOf_GetUniformLocationImmediate_program_not_4);
480COMPILE_ASSERT(offsetof(GetUniformLocationImmediate, location_shm_id) == 8,
481               OffsetOf_GetUniformLocationImmediate_location_shm_id_not_8);
482COMPILE_ASSERT(
483    offsetof(GetUniformLocationImmediate, location_shm_offset) == 12,
484               OffsetOf_GetUniformLocationImmediate_location_shm_offset_not_12);
485COMPILE_ASSERT(offsetof(GetUniformLocationImmediate, data_size) == 16,
486               OffsetOf_GetUniformLocationImmediate_data_size_not_16);
487
488struct GetUniformLocationBucket {
489  typedef GetUniformLocationBucket ValueType;
490  static const CommandId kCmdId = kGetUniformLocationBucket;
491  static const cmd::ArgFlags kArgFlags = cmd::kFixed;
492
493  typedef GLint Result;
494
495  static uint32 ComputeSize() {
496    return static_cast<uint32>(sizeof(ValueType));  // NOLINT
497  }
498
499  void SetHeader() {
500    header.SetCmd<ValueType>();
501  }
502
503  void Init(
504      GLuint _program, uint32 _name_bucket_id,
505      uint32 _location_shm_id, uint32 _location_shm_offset) {
506    SetHeader();
507    program = _program;
508    name_bucket_id = _name_bucket_id;
509    location_shm_id = _location_shm_id;
510    location_shm_offset = _location_shm_offset;
511  }
512
513  void* Set(
514      void* cmd, GLuint _program, uint32 _name_bucket_id,
515      uint32 _location_shm_id, uint32 _location_shm_offset) {
516    static_cast<ValueType*>(
517        cmd)->Init(
518            _program, _name_bucket_id, _location_shm_id,
519            _location_shm_offset);
520    return NextCmdAddress<ValueType>(cmd);
521  }
522
523  CommandHeader header;
524  uint32 program;
525  uint32 name_bucket_id;
526  uint32 location_shm_id;
527  uint32 location_shm_offset;
528};
529
530COMPILE_ASSERT(sizeof(GetUniformLocationBucket) == 20,
531               Sizeof_GetUniformLocationBucket_is_not_24);
532COMPILE_ASSERT(offsetof(GetUniformLocationBucket, header) == 0,
533               OffsetOf_GetUniformLocationBucket_header_not_0);
534COMPILE_ASSERT(offsetof(GetUniformLocationBucket, program) == 4,
535               OffsetOf_GetUniformLocationBucket_program_not_4);
536COMPILE_ASSERT(offsetof(GetUniformLocationBucket, name_bucket_id) == 8,
537               OffsetOf_GetUniformLocationBucket_name_bucket_id_not_8);
538COMPILE_ASSERT(offsetof(GetUniformLocationBucket, location_shm_id) == 12,
539               OffsetOf_GetUniformLocationBucket_location_shm_id_not_12);
540COMPILE_ASSERT(offsetof(GetUniformLocationBucket, location_shm_offset) == 16,
541               OffsetOf_GetUniformLocationBucket_location_shm_offset_not_16);
542
543struct InsertSyncPointCHROMIUM {
544  typedef InsertSyncPointCHROMIUM ValueType;
545  static const CommandId kCmdId = kInsertSyncPointCHROMIUM;
546  static const cmd::ArgFlags kArgFlags = cmd::kFixed;
547  CommandHeader header;
548};
549
550#pragma pack(pop)
551
552}  // namespace cmd
553}  // namespace gles2
554}  // namespace gpu
555
556#endif  // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_
557