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 int64_t GLint64
;
47 typedef uint64_t GLuint64
;
52 // Command buffer is GPU_COMMAND_BUFFER_ENTRY_ALIGNMENT byte aligned.
53 #pragma pack(push, GPU_COMMAND_BUFFER_ENTRY_ALIGNMENT)
55 namespace id_namespaces
{
57 // These are used when contexts share resources.
73 // These numbers must not change
74 static_assert(kBuffers
== 0, "kBuffers should equal 0");
75 static_assert(kFramebuffers
== 1, "kFramebuffers should equal 1");
76 static_assert(kProgramsAndShaders
== 2, "kProgramsAndShaders should equal 2");
77 static_assert(kRenderbuffers
== 3, "kRenderbuffers should equal 3");
78 static_assert(kTextures
== 4, "kTextures should equal 4");
80 } // namespace id_namespaces
82 // Used for some glGetXXX commands that return a result through a pointer. We
83 // need to know if the command succeeded or not and the size of the result. If
84 // the command failed its result size will 0.
90 return static_cast<T
*>(static_cast<void*>(&data
));
93 // Returns the total size in bytes of the SizedResult for a given number of
94 // results including the size field.
95 static size_t ComputeSize(size_t num_results
) {
96 return sizeof(T
) * num_results
+ sizeof(uint32_t); // NOLINT
99 // Returns the total size in bytes of the SizedResult for a given size of
101 static size_t ComputeSizeFromBytes(size_t size_of_result_in_bytes
) {
102 return size_of_result_in_bytes
+ sizeof(uint32_t); // NOLINT
105 // Returns the maximum number of results for a given buffer size.
106 static uint32_t ComputeMaxResults(size_t size_of_buffer
) {
107 return (size_of_buffer
>= sizeof(uint32_t)) ?
108 ((size_of_buffer
- sizeof(uint32_t)) / sizeof(T
)) : 0; // NOLINT
111 // Set the size for a given number of results.
112 void SetNumResults(size_t num_results
) {
113 size
= sizeof(T
) * num_results
; // NOLINT
116 // Get the number of elements in the result
117 int32_t GetNumResults() const {
118 return size
/ sizeof(T
); // NOLINT
122 void CopyResult(void* dst
) const {
123 memcpy(dst
, &data
, size
);
126 uint32_t size
; // in bytes.
127 int32_t data
; // this is just here to get an offset.
130 static_assert(sizeof(SizedResult
<int8_t>) == 8,
131 "size of SizedResult<int8_t> should be 8");
132 static_assert(offsetof(SizedResult
<int8_t>, size
) == 0,
133 "offset of SizedResult<int8_t>.size should be 0");
134 static_assert(offsetof(SizedResult
<int8_t>, data
) == 4,
135 "offset of SizedResult<int8_t>.data should be 4");
137 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
138 struct ProgramInput
{
139 uint32_t type
; // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
140 int32_t size
; // The size (how big the array is for uniforms)
141 uint32_t location_offset
; // offset from ProgramInfoHeader to 'size'
142 // locations for uniforms, 1 for attribs.
143 uint32_t name_offset
; // offset from ProgrmaInfoHeader to start of name.
144 uint32_t name_length
; // length of the name.
147 // The format of the bucket filled out by GetProgramInfoCHROMIUM
148 struct ProgramInfoHeader
{
149 uint32_t link_status
;
150 uint32_t num_attribs
;
151 uint32_t num_uniforms
;
152 // ProgramInput inputs[num_attribs + num_uniforms];
155 // The data for one UniformBlock from GetProgramInfoCHROMIUM
156 struct UniformBlockInfo
{
157 uint32_t binding
; // UNIFORM_BLOCK_BINDING
158 uint32_t data_size
; // UNIFORM_BLOCK_DATA_SIZE
159 uint32_t name_offset
; // offset from UniformBlocksHeader to start of name.
160 uint32_t name_length
; // UNIFORM_BLOCK_NAME_LENGTH
161 uint32_t active_uniforms
; // UNIFORM_BLOCK_ACTIVE_UNIFORMS
162 // offset from UniformBlocksHeader to |active_uniforms| indices.
163 uint32_t active_uniform_offset
;
164 // UNIFORM_BLOCK_REFERENDED_BY_VERTEX_SHADER
165 uint32_t referenced_by_vertex_shader
;
166 // UNIFORM_BLOCK_REFERENDED_BY_FRAGMENT_SHADER
167 uint32_t referenced_by_fragment_shader
;
170 // The format of the bucket filled out by GetUniformBlocksCHROMIUM
171 struct UniformBlocksHeader
{
172 uint32_t num_uniform_blocks
;
173 // UniformBlockInfo uniform_blocks[num_uniform_blocks];
176 // The data for one TransformFeedbackVarying from
177 // GetTransformFeedbackVaringCHROMIUM.
178 struct TransformFeedbackVaryingInfo
{
181 uint32_t name_offset
; // offset from Header to start of name.
182 uint32_t name_length
; // including the null terminator.
185 // The format of the bucket filled out by GetTransformFeedbackVaryingsCHROMIUM
186 struct TransformFeedbackVaryingsHeader
{
187 uint32_t num_transform_feedback_varyings
;
188 // TransformFeedbackVaryingInfo varyings[num_transform_feedback_varyings];
191 // Parameters of a uniform that can be queried through glGetActiveUniformsiv,
192 // but not through glGetActiveUniform.
193 struct UniformES3Info
{
196 int32_t array_stride
;
197 int32_t matrix_stride
;
198 int32_t is_row_major
;
201 // The format of the bucket filled out by GetUniformsivES3CHROMIUM
202 struct UniformsES3Header
{
203 uint32_t num_uniforms
;
204 // UniformES3Info uniforms[num_uniforms];
207 // The format of QuerySync used by EXT_occlusion_query_boolean
214 base::subtle::Atomic32 process_count
;
218 struct AsyncUploadSync
{
220 base::subtle::Release_Store(&async_upload_token
, 0);
223 void SetAsyncUploadToken(uint32_t token
) {
224 DCHECK_NE(token
, 0u);
225 base::subtle::Release_Store(&async_upload_token
, token
);
228 bool HasAsyncUploadTokenPassed(uint32_t token
) {
229 DCHECK_NE(token
, 0u);
230 uint32_t current_token
= base::subtle::Acquire_Load(&async_upload_token
);
231 return (current_token
- token
< 0x80000000);
234 base::subtle::Atomic32 async_upload_token
;
237 static_assert(sizeof(ProgramInput
) == 20, "size of ProgramInput should be 20");
238 static_assert(offsetof(ProgramInput
, type
) == 0,
239 "offset of ProgramInput.type should be 0");
240 static_assert(offsetof(ProgramInput
, size
) == 4,
241 "offset of ProgramInput.size should be 4");
242 static_assert(offsetof(ProgramInput
, location_offset
) == 8,
243 "offset of ProgramInput.location_offset should be 8");
244 static_assert(offsetof(ProgramInput
, name_offset
) == 12,
245 "offset of ProgramInput.name_offset should be 12");
246 static_assert(offsetof(ProgramInput
, name_length
) == 16,
247 "offset of ProgramInput.name_length should be 16");
249 static_assert(sizeof(ProgramInfoHeader
) == 12,
250 "size of ProgramInfoHeader should be 12");
251 static_assert(offsetof(ProgramInfoHeader
, link_status
) == 0,
252 "offset of ProgramInfoHeader.link_status should be 0");
253 static_assert(offsetof(ProgramInfoHeader
, num_attribs
) == 4,
254 "offset of ProgramInfoHeader.num_attribs should be 4");
255 static_assert(offsetof(ProgramInfoHeader
, num_uniforms
) == 8,
256 "offset of ProgramInfoHeader.num_uniforms should be 8");
258 static_assert(sizeof(UniformBlockInfo
) == 32,
259 "size of UniformBlockInfo should be 32");
260 static_assert(offsetof(UniformBlockInfo
, binding
) == 0,
261 "offset of UniformBlockInfo.binding should be 0");
262 static_assert(offsetof(UniformBlockInfo
, data_size
) == 4,
263 "offset of UniformBlockInfo.data_size should be 4");
264 static_assert(offsetof(UniformBlockInfo
, name_offset
) == 8,
265 "offset of UniformBlockInfo.name_offset should be 8");
266 static_assert(offsetof(UniformBlockInfo
, name_length
) == 12,
267 "offset of UniformBlockInfo.name_length should be 12");
268 static_assert(offsetof(UniformBlockInfo
, active_uniforms
) == 16,
269 "offset of UniformBlockInfo.active_uniforms should be 16");
270 static_assert(offsetof(UniformBlockInfo
, active_uniform_offset
) == 20,
271 "offset of UniformBlockInfo.active_uniform_offset should be 20");
272 static_assert(offsetof(UniformBlockInfo
, referenced_by_vertex_shader
) == 24,
273 "offset of UniformBlockInfo.referenced_by_vertex_shader "
275 static_assert(offsetof(UniformBlockInfo
, referenced_by_fragment_shader
) == 28,
276 "offset of UniformBlockInfo.referenced_by_fragment_shader "
279 static_assert(sizeof(UniformBlocksHeader
) == 4,
280 "size of UniformBlocksHeader should be 4");
281 static_assert(offsetof(UniformBlocksHeader
, num_uniform_blocks
) == 0,
282 "offset of UniformBlocksHeader.num_uniform_blocks should be 0");
286 #include "../common/gles2_cmd_format_autogen.h"
288 // These are hand written commands.
289 // TODO(gman): Attempt to make these auto-generated.
291 struct GenMailboxCHROMIUM
{
292 typedef GenMailboxCHROMIUM ValueType
;
293 static const CommandId kCmdId
= kGenMailboxCHROMIUM
;
294 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
295 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(3);
296 CommandHeader header
;
299 struct InsertSyncPointCHROMIUM
{
300 typedef InsertSyncPointCHROMIUM ValueType
;
301 static const CommandId kCmdId
= kInsertSyncPointCHROMIUM
;
302 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
303 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(3);
304 CommandHeader header
;
307 struct CreateAndConsumeTextureCHROMIUMImmediate
{
308 typedef CreateAndConsumeTextureCHROMIUMImmediate ValueType
;
309 static const CommandId kCmdId
= kCreateAndConsumeTextureCHROMIUMImmediate
;
310 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
311 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(1);
313 static uint32_t ComputeDataSize() {
314 return static_cast<uint32_t>(sizeof(GLbyte
) * 64); // NOLINT
317 static uint32_t ComputeSize() {
318 return static_cast<uint32_t>(sizeof(ValueType
) +
319 ComputeDataSize()); // NOLINT
322 void SetHeader(uint32_t size_in_bytes
) {
323 header
.SetCmdByTotalSize
<ValueType
>(size_in_bytes
);
326 void Init(GLenum _target
, uint32_t _client_id
, const GLbyte
* _mailbox
) {
327 SetHeader(ComputeSize());
329 client_id
= _client_id
;
330 memcpy(ImmediateDataAddress(this), _mailbox
, ComputeDataSize());
336 const GLbyte
* _mailbox
) {
337 static_cast<ValueType
*>(cmd
)->Init(_target
, _client_id
, _mailbox
);
338 const uint32_t size
= ComputeSize();
339 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, size
);
342 gpu::CommandHeader header
;
347 static_assert(sizeof(CreateAndConsumeTextureCHROMIUMImmediate
) == 12,
348 "size of CreateAndConsumeTextureCHROMIUMImmediate should be 12");
349 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, header
) == 0,
350 "offset of CreateAndConsumeTextureCHROMIUMImmediate.header "
352 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, target
) == 4,
353 "offset of CreateAndConsumeTextureCHROMIUMImmediate.target "
356 offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, client_id
) == 8,
357 "offset of CreateAndConsumeTextureCHROMIUMImmediate.client_id should be 8");
366 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_