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