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 maximum number of results for a given buffer size.
100 static uint32_t ComputeMaxResults(size_t size_of_buffer
) {
101 return (size_of_buffer
>= sizeof(uint32_t)) ?
102 ((size_of_buffer
- sizeof(uint32_t)) / sizeof(T
)) : 0; // NOLINT
105 // Set the size for a given number of results.
106 void SetNumResults(size_t num_results
) {
107 size
= sizeof(T
) * num_results
; // NOLINT
110 // Get the number of elements in the result
111 int32_t GetNumResults() const {
112 return size
/ sizeof(T
); // NOLINT
116 void CopyResult(void* dst
) const {
117 memcpy(dst
, &data
, size
);
120 uint32_t size
; // in bytes.
121 int32_t data
; // this is just here to get an offset.
124 static_assert(sizeof(SizedResult
<int8_t>) == 8,
125 "size of SizedResult<int8_t> should be 8");
126 static_assert(offsetof(SizedResult
<int8_t>, size
) == 0,
127 "offset of SizedResult<int8_t>.size should be 0");
128 static_assert(offsetof(SizedResult
<int8_t>, data
) == 4,
129 "offset of SizedResult<int8_t>.data should be 4");
131 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
132 struct ProgramInput
{
133 uint32_t type
; // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
134 int32_t size
; // The size (how big the array is for uniforms)
135 uint32_t location_offset
; // offset from ProgramInfoHeader to 'size'
136 // locations for uniforms, 1 for attribs.
137 uint32_t name_offset
; // offset from ProgrmaInfoHeader to start of name.
138 uint32_t name_length
; // length of the name.
141 // The format of the bucket filled out by GetProgramInfoCHROMIUM
142 struct ProgramInfoHeader
{
143 uint32_t link_status
;
144 uint32_t num_attribs
;
145 uint32_t num_uniforms
;
146 // ProgramInput inputs[num_attribs + num_uniforms];
149 // The data for one UniformBlock from GetProgramInfoCHROMIUM
150 struct UniformBlockInfo
{
151 uint32_t binding
; // UNIFORM_BLOCK_BINDING
152 uint32_t data_size
; // UNIFORM_BLOCK_DATA_SIZE
153 uint32_t name_offset
; // offset from UniformBlocksHeader to start of name.
154 uint32_t name_length
; // UNIFORM_BLOCK_NAME_LENGTH
155 uint32_t active_uniforms
; // UNIFORM_BLOCK_ACTIVE_UNIFORMS
156 // offset from UniformBlocksHeader to |active_uniforms| indices.
157 uint32_t active_uniform_offset
;
158 // UNIFORM_BLOCK_REFERENDED_BY_VERTEX_SHADER
159 uint32_t referenced_by_vertex_shader
;
160 // UNIFORM_BLOCK_REFERENDED_BY_FRAGMENT_SHADER
161 uint32_t referenced_by_fragment_shader
;
164 // The format of the bucket filled out by GetUniformBlocksCHROMIUM
165 struct UniformBlocksHeader
{
166 uint32_t num_uniform_blocks
;
167 // UniformBlockInfo uniform_blocks[num_uniform_blocks];
170 // The data for one TransformFeedbackVarying from
171 // GetTransformFeedbackVaringCHROMIUM.
172 struct TransformFeedbackVaryingInfo
{
175 uint32_t name_offset
; // offset from Header to start of name.
176 uint32_t name_length
; // including the null terminator.
179 // The format of the bucket filled out by GetTransformFeedbackVaryingsCHROMIUM
180 struct TransformFeedbackVaryingsHeader
{
181 uint32_t num_transform_feedback_varyings
;
182 // TransformFeedbackVaryingInfo varyings[num_transform_feedback_varyings];
185 // Parameters of a uniform that can be queried through glGetActiveUniformsiv,
186 // but not through glGetActiveUniform.
187 struct UniformES3Info
{
190 int32_t array_stride
;
191 int32_t matrix_stride
;
192 int32_t is_row_major
;
195 // The format of the bucket filled out by GetUniformsivES3CHROMIUM
196 struct UniformsES3Header
{
197 uint32_t num_uniforms
;
198 // UniformES3Info uniforms[num_uniforms];
201 // The format of QuerySync used by EXT_occlusion_query_boolean
208 base::subtle::Atomic32 process_count
;
212 struct AsyncUploadSync
{
214 base::subtle::Release_Store(&async_upload_token
, 0);
217 void SetAsyncUploadToken(uint32_t token
) {
218 DCHECK_NE(token
, 0u);
219 base::subtle::Release_Store(&async_upload_token
, token
);
222 bool HasAsyncUploadTokenPassed(uint32_t token
) {
223 DCHECK_NE(token
, 0u);
224 uint32_t current_token
= base::subtle::Acquire_Load(&async_upload_token
);
225 return (current_token
- token
< 0x80000000);
228 base::subtle::Atomic32 async_upload_token
;
231 static_assert(sizeof(ProgramInput
) == 20, "size of ProgramInput should be 20");
232 static_assert(offsetof(ProgramInput
, type
) == 0,
233 "offset of ProgramInput.type should be 0");
234 static_assert(offsetof(ProgramInput
, size
) == 4,
235 "offset of ProgramInput.size should be 4");
236 static_assert(offsetof(ProgramInput
, location_offset
) == 8,
237 "offset of ProgramInput.location_offset should be 8");
238 static_assert(offsetof(ProgramInput
, name_offset
) == 12,
239 "offset of ProgramInput.name_offset should be 12");
240 static_assert(offsetof(ProgramInput
, name_length
) == 16,
241 "offset of ProgramInput.name_length should be 16");
243 static_assert(sizeof(ProgramInfoHeader
) == 12,
244 "size of ProgramInfoHeader should be 12");
245 static_assert(offsetof(ProgramInfoHeader
, link_status
) == 0,
246 "offset of ProgramInfoHeader.link_status should be 0");
247 static_assert(offsetof(ProgramInfoHeader
, num_attribs
) == 4,
248 "offset of ProgramInfoHeader.num_attribs should be 4");
249 static_assert(offsetof(ProgramInfoHeader
, num_uniforms
) == 8,
250 "offset of ProgramInfoHeader.num_uniforms should be 8");
252 static_assert(sizeof(UniformBlockInfo
) == 32,
253 "size of UniformBlockInfo should be 32");
254 static_assert(offsetof(UniformBlockInfo
, binding
) == 0,
255 "offset of UniformBlockInfo.binding should be 0");
256 static_assert(offsetof(UniformBlockInfo
, data_size
) == 4,
257 "offset of UniformBlockInfo.data_size should be 4");
258 static_assert(offsetof(UniformBlockInfo
, name_offset
) == 8,
259 "offset of UniformBlockInfo.name_offset should be 8");
260 static_assert(offsetof(UniformBlockInfo
, name_length
) == 12,
261 "offset of UniformBlockInfo.name_length should be 12");
262 static_assert(offsetof(UniformBlockInfo
, active_uniforms
) == 16,
263 "offset of UniformBlockInfo.active_uniforms should be 16");
264 static_assert(offsetof(UniformBlockInfo
, active_uniform_offset
) == 20,
265 "offset of UniformBlockInfo.active_uniform_offset should be 20");
266 static_assert(offsetof(UniformBlockInfo
, referenced_by_vertex_shader
) == 24,
267 "offset of UniformBlockInfo.referenced_by_vertex_shader "
269 static_assert(offsetof(UniformBlockInfo
, referenced_by_fragment_shader
) == 28,
270 "offset of UniformBlockInfo.referenced_by_fragment_shader "
273 static_assert(sizeof(UniformBlocksHeader
) == 4,
274 "size of UniformBlocksHeader should be 4");
275 static_assert(offsetof(UniformBlocksHeader
, num_uniform_blocks
) == 0,
276 "offset of UniformBlocksHeader.num_uniform_blocks should be 0");
280 #include "../common/gles2_cmd_format_autogen.h"
282 // These are hand written commands.
283 // TODO(gman): Attempt to make these auto-generated.
285 struct GenMailboxCHROMIUM
{
286 typedef GenMailboxCHROMIUM ValueType
;
287 static const CommandId kCmdId
= kGenMailboxCHROMIUM
;
288 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
289 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(3);
290 CommandHeader header
;
293 struct InsertSyncPointCHROMIUM
{
294 typedef InsertSyncPointCHROMIUM ValueType
;
295 static const CommandId kCmdId
= kInsertSyncPointCHROMIUM
;
296 static const cmd::ArgFlags kArgFlags
= cmd::kFixed
;
297 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(3);
298 CommandHeader header
;
301 struct CreateAndConsumeTextureCHROMIUMImmediate
{
302 typedef CreateAndConsumeTextureCHROMIUMImmediate ValueType
;
303 static const CommandId kCmdId
= kCreateAndConsumeTextureCHROMIUMImmediate
;
304 static const cmd::ArgFlags kArgFlags
= cmd::kAtLeastN
;
305 static const uint8 cmd_flags
= CMD_FLAG_SET_TRACE_LEVEL(1);
307 static uint32_t ComputeDataSize() {
308 return static_cast<uint32_t>(sizeof(GLbyte
) * 64); // NOLINT
311 static uint32_t ComputeSize() {
312 return static_cast<uint32_t>(sizeof(ValueType
) +
313 ComputeDataSize()); // NOLINT
316 void SetHeader(uint32_t size_in_bytes
) {
317 header
.SetCmdByTotalSize
<ValueType
>(size_in_bytes
);
320 void Init(GLenum _target
, uint32_t _client_id
, const GLbyte
* _mailbox
) {
321 SetHeader(ComputeSize());
323 client_id
= _client_id
;
324 memcpy(ImmediateDataAddress(this), _mailbox
, ComputeDataSize());
330 const GLbyte
* _mailbox
) {
331 static_cast<ValueType
*>(cmd
)->Init(_target
, _client_id
, _mailbox
);
332 const uint32_t size
= ComputeSize();
333 return NextImmediateCmdAddressTotalSize
<ValueType
>(cmd
, size
);
336 gpu::CommandHeader header
;
341 static_assert(sizeof(CreateAndConsumeTextureCHROMIUMImmediate
) == 12,
342 "size of CreateAndConsumeTextureCHROMIUMImmediate should be 12");
343 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, header
) == 0,
344 "offset of CreateAndConsumeTextureCHROMIUMImmediate.header "
346 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, target
) == 4,
347 "offset of CreateAndConsumeTextureCHROMIUMImmediate.target "
350 offsetof(CreateAndConsumeTextureCHROMIUMImmediate
, client_id
) == 8,
351 "offset of CreateAndConsumeTextureCHROMIUMImmediate.client_id should be 8");
360 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_