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>
16 #include "base/atomicops.h"
17 #include "base/logging.h"
18 #include "base/macros.h"
19 #include "gpu/command_buffer/common/bitfield_helpers.h"
20 #include "gpu/command_buffer/common/cmd_buffer_common.h"
21 #include "gpu/command_buffer/common/gles2_cmd_ids.h"
22 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
24 // GL types are forward declared to avoid including the GL headers. The problem
25 // is determining which GL headers to include from code that is common to the
26 // client and service sides (GLES2 or one of several GL implementations).
27 typedef unsigned int GLenum
;
28 typedef unsigned int GLbitfield
;
29 typedef unsigned int GLuint
;
32 typedef unsigned char GLboolean
;
33 typedef signed char GLbyte
;
34 typedef short GLshort
;
35 typedef unsigned char GLubyte
;
36 typedef unsigned short GLushort
;
37 typedef unsigned long GLulong
;
38 typedef float GLfloat
;
39 typedef float GLclampf
;
40 typedef double GLdouble
;
41 typedef double GLclampd
;
43 typedef khronos_intptr_t GLintptr
;
44 typedef khronos_ssize_t GLsizeiptr
;
45 typedef struct __GLsync
*GLsync
;
46 typedef uint64_t GLuint64
;
51 // Command buffer is GPU_COMMAND_BUFFER_ENTRY_ALIGNMENT byte aligned.
52 #pragma pack(push, GPU_COMMAND_BUFFER_ENTRY_ALIGNMENT)
54 namespace id_namespaces
{
56 // These are used when contexts share resources.
72 // These numbers must not change
73 static_assert(kBuffers
== 0, "kBuffers should equal 0");
74 static_assert(kFramebuffers
== 1, "kFramebuffers should equal 1");
75 static_assert(kProgramsAndShaders
== 2, "kProgramsAndShaders should equal 2");
76 static_assert(kRenderbuffers
== 3, "kRenderbuffers should equal 3");
77 static_assert(kTextures
== 4, "kTextures should equal 4");
79 } // namespace id_namespaces
81 // Used for some glGetXXX commands that return a result through a pointer. We
82 // need to know if the command succeeded or not and the size of the result. If
83 // the command failed its result size will 0.
89 return static_cast<T
*>(static_cast<void*>(&data
));
92 // Returns the total size in bytes of the SizedResult for a given number of
93 // results including the size field.
94 static size_t ComputeSize(size_t num_results
) {
95 return sizeof(T
) * num_results
+ sizeof(uint32_t); // NOLINT
98 // Returns the total size in bytes of the SizedResult for a given size of
100 static size_t ComputeSizeFromBytes(size_t size_of_result_in_bytes
) {
101 return size_of_result_in_bytes
+ sizeof(uint32_t); // NOLINT
104 // Returns the maximum number of results for a given buffer size.
105 static uint32_t ComputeMaxResults(size_t size_of_buffer
) {
106 return (size_of_buffer
>= sizeof(uint32_t)) ?
107 ((size_of_buffer
- sizeof(uint32_t)) / sizeof(T
)) : 0; // NOLINT
110 // Set the size for a given number of results.
111 void SetNumResults(size_t num_results
) {
112 size
= sizeof(T
) * num_results
; // NOLINT
115 // Get the number of elements in the result
116 int32_t GetNumResults() const {
117 return size
/ sizeof(T
); // NOLINT
121 void CopyResult(void* dst
) const {
122 memcpy(dst
, &data
, size
);
125 uint32_t size
; // in bytes.
126 int32_t data
; // this is just here to get an offset.
129 static_assert(sizeof(SizedResult
<int8_t>) == 8,
130 "size of SizedResult<int8_t> should be 8");
131 static_assert(offsetof(SizedResult
<int8_t>, size
) == 0,
132 "offset of SizedResult<int8_t>.size should be 0");
133 static_assert(offsetof(SizedResult
<int8_t>, data
) == 4,
134 "offset of SizedResult<int8_t>.data should be 4");
136 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
137 struct ProgramInput
{
138 uint32_t type
; // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
139 int32_t size
; // The size (how big the array is for uniforms)
140 uint32_t location_offset
; // offset from ProgramInfoHeader to 'size'
141 // locations for uniforms, 1 for attribs.
142 uint32_t name_offset
; // offset from ProgrmaInfoHeader to start of name.
143 uint32_t name_length
; // length of the name.
146 // The format of the bucket filled out by GetProgramInfoCHROMIUM
147 struct ProgramInfoHeader
{
148 uint32_t link_status
;
149 uint32_t num_attribs
;
150 uint32_t num_uniforms
;
151 // ProgramInput inputs[num_attribs + num_uniforms];
154 // The data for one UniformBlock from GetProgramInfoCHROMIUM
155 struct UniformBlockInfo
{
156 uint32_t binding
; // UNIFORM_BLOCK_BINDING
157 uint32_t data_size
; // UNIFORM_BLOCK_DATA_SIZE
158 uint32_t name_offset
; // offset from UniformBlocksHeader to start of name.
159 uint32_t name_length
; // UNIFORM_BLOCK_NAME_LENGTH
160 uint32_t active_uniforms
; // UNIFORM_BLOCK_ACTIVE_UNIFORMS
161 // offset from UniformBlocksHeader to |active_uniforms| indices.
162 uint32_t active_uniform_offset
;
163 // UNIFORM_BLOCK_REFERENDED_BY_VERTEX_SHADER
164 uint32_t referenced_by_vertex_shader
;
165 // UNIFORM_BLOCK_REFERENDED_BY_FRAGMENT_SHADER
166 uint32_t referenced_by_fragment_shader
;
169 // The format of the bucket filled out by GetUniformBlocksCHROMIUM
170 struct UniformBlocksHeader
{
171 uint32_t num_uniform_blocks
;
172 // UniformBlockInfo uniform_blocks[num_uniform_blocks];
175 // The data for one TransformFeedbackVarying from
176 // GetTransformFeedbackVaringCHROMIUM.
177 struct TransformFeedbackVaryingInfo
{
180 uint32_t name_offset
; // offset from Header to start of name.
181 uint32_t name_length
; // including the null terminator.
184 // The format of the bucket filled out by GetTransformFeedbackVaryingsCHROMIUM
185 struct TransformFeedbackVaryingsHeader
{
186 uint32_t num_transform_feedback_varyings
;
187 // TransformFeedbackVaryingInfo varyings[num_transform_feedback_varyings];
190 // Parameters of a uniform that can be queried through glGetActiveUniformsiv,
191 // but not through glGetActiveUniform.
192 struct UniformES3Info
{
195 int32_t array_stride
;
196 int32_t matrix_stride
;
197 int32_t is_row_major
;
200 // The format of the bucket filled out by GetUniformsivES3CHROMIUM
201 struct UniformsES3Header
{
202 uint32_t num_uniforms
;
203 // UniformES3Info uniforms[num_uniforms];
206 // The format of QuerySync used by EXT_occlusion_query_boolean
213 base::subtle::Atomic32 process_count
;
217 struct AsyncUploadSync
{
219 base::subtle::Release_Store(&async_upload_token
, 0);
222 void SetAsyncUploadToken(uint32_t token
) {
223 DCHECK_NE(token
, 0u);
224 base::subtle::Release_Store(&async_upload_token
, token
);
227 bool HasAsyncUploadTokenPassed(uint32_t token
) {
228 DCHECK_NE(token
, 0u);
229 uint32_t current_token
= base::subtle::Acquire_Load(&async_upload_token
);
230 return (current_token
- token
< 0x80000000);
233 base::subtle::Atomic32 async_upload_token
;
236 static_assert(sizeof(ProgramInput
) == 20, "size of ProgramInput should be 20");
237 static_assert(offsetof(ProgramInput
, type
) == 0,
238 "offset of ProgramInput.type should be 0");
239 static_assert(offsetof(ProgramInput
, size
) == 4,
240 "offset of ProgramInput.size should be 4");
241 static_assert(offsetof(ProgramInput
, location_offset
) == 8,
242 "offset of ProgramInput.location_offset should be 8");
243 static_assert(offsetof(ProgramInput
, name_offset
) == 12,
244 "offset of ProgramInput.name_offset should be 12");
245 static_assert(offsetof(ProgramInput
, name_length
) == 16,
246 "offset of ProgramInput.name_length should be 16");
248 static_assert(sizeof(ProgramInfoHeader
) == 12,
249 "size of ProgramInfoHeader should be 12");
250 static_assert(offsetof(ProgramInfoHeader
, link_status
) == 0,
251 "offset of ProgramInfoHeader.link_status should be 0");
252 static_assert(offsetof(ProgramInfoHeader
, num_attribs
) == 4,
253 "offset of ProgramInfoHeader.num_attribs should be 4");
254 static_assert(offsetof(ProgramInfoHeader
, num_uniforms
) == 8,
255 "offset of ProgramInfoHeader.num_uniforms should be 8");
257 static_assert(sizeof(UniformBlockInfo
) == 32,
258 "size of UniformBlockInfo should be 32");
259 static_assert(offsetof(UniformBlockInfo
, binding
) == 0,
260 "offset of UniformBlockInfo.binding should be 0");
261 static_assert(offsetof(UniformBlockInfo
, data_size
) == 4,
262 "offset of UniformBlockInfo.data_size should be 4");
263 static_assert(offsetof(UniformBlockInfo
, name_offset
) == 8,
264 "offset of UniformBlockInfo.name_offset should be 8");
265 static_assert(offsetof(UniformBlockInfo
, name_length
) == 12,
266 "offset of UniformBlockInfo.name_length should be 12");
267 static_assert(offsetof(UniformBlockInfo
, active_uniforms
) == 16,
268 "offset of UniformBlockInfo.active_uniforms should be 16");
269 static_assert(offsetof(UniformBlockInfo
, active_uniform_offset
) == 20,
270 "offset of UniformBlockInfo.active_uniform_offset should be 20");
271 static_assert(offsetof(UniformBlockInfo
, referenced_by_vertex_shader
) == 24,
272 "offset of UniformBlockInfo.referenced_by_vertex_shader "
274 static_assert(offsetof(UniformBlockInfo
, referenced_by_fragment_shader
) == 28,
275 "offset of UniformBlockInfo.referenced_by_fragment_shader "
278 static_assert(sizeof(UniformBlocksHeader
) == 4,
279 "size of UniformBlocksHeader should be 4");
280 static_assert(offsetof(UniformBlocksHeader
, num_uniform_blocks
) == 0,
281 "offset of UniformBlocksHeader.num_uniform_blocks should be 0");
285 #include "../common/gles2_cmd_format_autogen.h"
287 // These are hand written commands.
288 // TODO(gman): Attempt to make these auto-generated.
290 struct GenMailboxCHROMIUM
{
291 typedef GenMailboxCHROMIUM ValueType
;
292 static const CommandId kCmdId
= kGenMailboxCHROMIUM
;
293 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
294 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(3);
295 CommandHeader header
;
298 struct InsertSyncPointCHROMIUM
{
299 typedef InsertSyncPointCHROMIUM ValueType
;
300 static const CommandId kCmdId
= kInsertSyncPointCHROMIUM
;
301 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
302 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(3);
303 CommandHeader header
;
306 struct CreateAndConsumeTextureCHROMIUMImmediate
{
307 typedef CreateAndConsumeTextureCHROMIUMImmediate ValueType
;
308 static const CommandId kCmdId
= kCreateAndConsumeTextureCHROMIUMImmediate
;
309 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
310 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(1);
312 static uint32_t ComputeDataSize() {
313 return static_cast<uint32_t>(sizeof(GLbyte
) * 64); // NOLINT
316 static uint32_t ComputeSize() {
317 return static_cast<uint32_t>(sizeof(ValueType
) +
318 ComputeDataSize()); // NOLINT
321 void SetHeader(uint32_t size_in_bytes
) {
322 header
.SetCmdByTotalSize
<ValueType
>(size_in_bytes
);
325 void Init(GLenum _target
, uint32_t _client_id
, const GLbyte
* _mailbox
) {
326 SetHeader(ComputeSize());
328 client_id
= _client_id
;
329 memcpy(ImmediateDataAddress(this), _mailbox
, ComputeDataSize());
335 const GLbyte
* _mailbox
) {
336 static_cast<ValueType
*>(cmd
)->Init(_target
, _client_id
, _mailbox
);
337 const uint32_t size
= ComputeSize();
338 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, size
);
341 gpu::CommandHeader header
;
346 static_assert(sizeof(CreateAndConsumeTextureCHROMIUMImmediate
) == 12,
347 "size of CreateAndConsumeTextureCHROMIUMImmediate should be 12");
348 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, header
) == 0,
349 "offset of CreateAndConsumeTextureCHROMIUMImmediate.header "
351 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, target
) == 4,
352 "offset of CreateAndConsumeTextureCHROMIUMImmediate.target "
355 offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, client_id
) == 8,
356 "offset of CreateAndConsumeTextureCHROMIUMImmediate.client_id should be 8");
365 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_