Home | History | Annotate | Download | only in common
      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).
     24 typedef unsigned int GLenum;
     25 typedef unsigned int GLbitfield;
     26 typedef unsigned int GLuint;
     27 typedef int GLint;
     28 typedef int GLsizei;
     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;
     39 typedef void GLvoid;
     40 typedef khronos_intptr_t GLintptr;
     41 typedef khronos_ssize_t  GLsizeiptr;
     42 
     43 namespace gpu {
     44 namespace gles2 {
     45 
     46 #pragma pack(push, 1)
     47 
     48 namespace id_namespaces {
     49 
     50 // These are used when contexts share resources.
     51 enum 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
     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);
     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.
     74 template <typename T>
     75 struct 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 
    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);
    124 
    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.
    133 };
    134 
    135 // The format of the bucket filled out by GetProgramInfoCHROMIUM
    136 struct 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
    144 struct QuerySync {
    145   void Reset() {
    146     process_count = 0;
    147     result = 0;
    148   }
    149 
    150   uint32 process_count;
    151   uint64 result;
    152 };
    153 
    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);
    165 
    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);
    173 
    174 namespace 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 
    181 struct 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 
    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);
    245 
    246 
    247 struct GetAttribLocationBucket {
    248   typedef GetAttribLocationBucket ValueType;
    249   static const CommandId kCmdId = kGetAttribLocationBucket;
    250   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
    251 
    252   typedef GLint Result;
    253 
    254   static uint32 ComputeSize() {
    255     return static_cast<uint32>(sizeof(ValueType));  // NOLINT
    256   }
    257 
    258   void SetHeader() {
    259     header.SetCmd<ValueType>();
    260   }
    261 
    262   void Init(
    263       GLuint _program, uint32 _name_bucket_id,
    264       uint32 _location_shm_id, uint32 _location_shm_offset) {
    265     SetHeader();
    266     program = _program;
    267     name_bucket_id = _name_bucket_id;
    268     location_shm_id = _location_shm_id;
    269     location_shm_offset = _location_shm_offset;
    270   }
    271 
    272   void* Set(
    273       void* cmd, GLuint _program, uint32 _name_bucket_id,
    274       uint32 _location_shm_id, uint32 _location_shm_offset) {
    275     static_cast<ValueType*>(
    276         cmd)->Init(
    277             _program, _name_bucket_id, _location_shm_id,
    278             _location_shm_offset);
    279     return NextCmdAddress<ValueType>(cmd);
    280   }
    281 
    282   CommandHeader header;
    283   uint32 program;
    284   uint32 name_bucket_id;
    285   uint32 location_shm_id;
    286   uint32 location_shm_offset;
    287 };
    288 
    289 COMPILE_ASSERT(sizeof(GetAttribLocationBucket) == 20,
    290                Sizeof_GetAttribLocationBucket_is_not_24);
    291 COMPILE_ASSERT(offsetof(GetAttribLocationBucket, header) == 0,
    292                OffsetOf_GetAttribLocationBucket_header_not_0);
    293 COMPILE_ASSERT(offsetof(GetAttribLocationBucket, program) == 4,
    294                OffsetOf_GetAttribLocationBucket_program_not_4);
    295 COMPILE_ASSERT(offsetof(GetAttribLocationBucket, name_bucket_id) == 8,
    296                OffsetOf_GetAttribLocationBucket_name_bucket_id_not_8);
    297 COMPILE_ASSERT(offsetof(GetAttribLocationBucket, location_shm_id) == 12,
    298                OffsetOf_GetAttribLocationBucket_location_shm_id_not_12);
    299 COMPILE_ASSERT(offsetof(GetAttribLocationBucket, location_shm_offset) == 16,
    300                OffsetOf_GetAttribLocationBucket_location_shm_offset_not_16);
    301 
    302 struct GetUniformLocation {
    303   typedef GetUniformLocation ValueType;
    304   static const CommandId kCmdId = kGetUniformLocation;
    305   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
    306 
    307   typedef GLint Result;
    308 
    309   static uint32 ComputeSize() {
    310     return static_cast<uint32>(sizeof(ValueType));  // NOLINT
    311   }
    312 
    313   void SetHeader() {
    314     header.SetCmd<ValueType>();
    315   }
    316 
    317   void Init(
    318       GLuint _program, uint32 _name_shm_id, uint32 _name_shm_offset,
    319       uint32 _location_shm_id, uint32 _location_shm_offset,
    320       uint32 _data_size) {
    321     SetHeader();
    322     program = _program;
    323     name_shm_id = _name_shm_id;
    324     name_shm_offset = _name_shm_offset;
    325     location_shm_id = _location_shm_id;
    326     location_shm_offset = _location_shm_offset;
    327     data_size = _data_size;
    328   }
    329 
    330   void* Set(
    331       void* cmd, GLuint _program, uint32 _name_shm_id, uint32 _name_shm_offset,
    332       uint32 _location_shm_id, uint32 _location_shm_offset,
    333       uint32 _data_size) {
    334     static_cast<ValueType*>(
    335         cmd)->Init(
    336             _program, _name_shm_id, _name_shm_offset, _location_shm_id,
    337             _location_shm_offset, _data_size);
    338     return NextCmdAddress<ValueType>(cmd);
    339   }
    340 
    341   CommandHeader header;
    342   uint32 program;
    343   uint32 name_shm_id;
    344   uint32 name_shm_offset;
    345   uint32 location_shm_id;
    346   uint32 location_shm_offset;
    347   uint32 data_size;
    348 };
    349 
    350 COMPILE_ASSERT(sizeof(GetUniformLocation) == 28,
    351                Sizeof_GetUniformLocation_is_not_28);
    352 COMPILE_ASSERT(offsetof(GetUniformLocation, header) == 0,
    353                OffsetOf_GetUniformLocation_header_not_0);
    354 COMPILE_ASSERT(offsetof(GetUniformLocation, program) == 4,
    355                OffsetOf_GetUniformLocation_program_not_4);
    356 COMPILE_ASSERT(offsetof(GetUniformLocation, name_shm_id) == 8,
    357                OffsetOf_GetUniformLocation_name_shm_id_not_8);
    358 COMPILE_ASSERT(offsetof(GetUniformLocation, name_shm_offset) == 12,
    359                OffsetOf_GetUniformLocation_name_shm_offset_not_12);
    360 COMPILE_ASSERT(offsetof(GetUniformLocation, location_shm_id) == 16,
    361                OffsetOf_GetUniformLocation_location_shm_id_not_16);
    362 COMPILE_ASSERT(offsetof(GetUniformLocation, location_shm_offset) == 20,
    363                OffsetOf_GetUniformLocation_location_shm_offset_not_20);
    364 COMPILE_ASSERT(offsetof(GetUniformLocation, data_size) == 24,
    365                OffsetOf_GetUniformLocation_data_size_not_24);
    366 
    367 struct GetUniformLocationBucket {
    368   typedef GetUniformLocationBucket ValueType;
    369   static const CommandId kCmdId = kGetUniformLocationBucket;
    370   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
    371 
    372   typedef GLint Result;
    373 
    374   static uint32 ComputeSize() {
    375     return static_cast<uint32>(sizeof(ValueType));  // NOLINT
    376   }
    377 
    378   void SetHeader() {
    379     header.SetCmd<ValueType>();
    380   }
    381 
    382   void Init(
    383       GLuint _program, uint32 _name_bucket_id,
    384       uint32 _location_shm_id, uint32 _location_shm_offset) {
    385     SetHeader();
    386     program = _program;
    387     name_bucket_id = _name_bucket_id;
    388     location_shm_id = _location_shm_id;
    389     location_shm_offset = _location_shm_offset;
    390   }
    391 
    392   void* Set(
    393       void* cmd, GLuint _program, uint32 _name_bucket_id,
    394       uint32 _location_shm_id, uint32 _location_shm_offset) {
    395     static_cast<ValueType*>(
    396         cmd)->Init(
    397             _program, _name_bucket_id, _location_shm_id,
    398             _location_shm_offset);
    399     return NextCmdAddress<ValueType>(cmd);
    400   }
    401 
    402   CommandHeader header;
    403   uint32 program;
    404   uint32 name_bucket_id;
    405   uint32 location_shm_id;
    406   uint32 location_shm_offset;
    407 };
    408 
    409 COMPILE_ASSERT(sizeof(GetUniformLocationBucket) == 20,
    410                Sizeof_GetUniformLocationBucket_is_not_24);
    411 COMPILE_ASSERT(offsetof(GetUniformLocationBucket, header) == 0,
    412                OffsetOf_GetUniformLocationBucket_header_not_0);
    413 COMPILE_ASSERT(offsetof(GetUniformLocationBucket, program) == 4,
    414                OffsetOf_GetUniformLocationBucket_program_not_4);
    415 COMPILE_ASSERT(offsetof(GetUniformLocationBucket, name_bucket_id) == 8,
    416                OffsetOf_GetUniformLocationBucket_name_bucket_id_not_8);
    417 COMPILE_ASSERT(offsetof(GetUniformLocationBucket, location_shm_id) == 12,
    418                OffsetOf_GetUniformLocationBucket_location_shm_id_not_12);
    419 COMPILE_ASSERT(offsetof(GetUniformLocationBucket, location_shm_offset) == 16,
    420                OffsetOf_GetUniformLocationBucket_location_shm_offset_not_16);
    421 
    422 struct GenMailboxCHROMIUM {
    423   typedef GenMailboxCHROMIUM ValueType;
    424   static const CommandId kCmdId = kGenMailboxCHROMIUM;
    425   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
    426   CommandHeader header;
    427 };
    428 
    429 struct InsertSyncPointCHROMIUM {
    430   typedef InsertSyncPointCHROMIUM ValueType;
    431   static const CommandId kCmdId = kInsertSyncPointCHROMIUM;
    432   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
    433   CommandHeader header;
    434 };
    435 
    436 #pragma pack(pop)
    437 
    438 }  // namespace cmd
    439 }  // namespace gles2
    440 }  // namespace gpu
    441 
    442 #endif  // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_
    443