Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_format.h
blobe4d00dd41ea7ee54d64e191e1db885e06dc05020
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>
13 #include <stdint.h>
14 #include <string.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;
30 typedef int GLint;
31 typedef int GLsizei;
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;
42 typedef void GLvoid;
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;
49 namespace gpu {
50 namespace gles2 {
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.
58 enum IdNamespaces {
59 kBuffers,
60 kFramebuffers,
61 kProgramsAndShaders,
62 kRenderbuffers,
63 kTextures,
64 kQueries,
65 kVertexArrays,
66 kValuebuffers,
67 kSamplers,
68 kTransformFeedbacks,
69 kSyncs,
70 kNumIdNamespaces
73 enum RangeIdNamespaces { kPaths, kNumRangeIdNamespaces };
75 // These numbers must not change
76 static_assert(kBuffers == 0, "kBuffers should equal 0");
77 static_assert(kFramebuffers == 1, "kFramebuffers should equal 1");
78 static_assert(kProgramsAndShaders == 2, "kProgramsAndShaders should equal 2");
79 static_assert(kRenderbuffers == 3, "kRenderbuffers should equal 3");
80 static_assert(kTextures == 4, "kTextures should equal 4");
81 static_assert(kPaths == 0, "kPaths should equal 0");
83 } // namespace id_namespaces
85 // Used for some glGetXXX commands that return a result through a pointer. We
86 // need to know if the command succeeded or not and the size of the result. If
87 // the command failed its result size will 0.
88 template <typename T>
89 struct SizedResult {
90 typedef T Type;
92 T* GetData() {
93 return static_cast<T*>(static_cast<void*>(&data));
96 // Returns the total size in bytes of the SizedResult for a given number of
97 // results including the size field.
98 static size_t ComputeSize(size_t num_results) {
99 return sizeof(T) * num_results + sizeof(uint32_t); // NOLINT
102 // Returns the maximum number of results for a given buffer size.
103 static uint32_t ComputeMaxResults(size_t size_of_buffer) {
104 return (size_of_buffer >= sizeof(uint32_t)) ?
105 ((size_of_buffer - sizeof(uint32_t)) / sizeof(T)) : 0; // NOLINT
108 // Set the size for a given number of results.
109 void SetNumResults(size_t num_results) {
110 size = sizeof(T) * num_results; // NOLINT
113 // Get the number of elements in the result
114 int32_t GetNumResults() const {
115 return size / sizeof(T); // NOLINT
118 // Copy the result.
119 void CopyResult(void* dst) const {
120 memcpy(dst, &data, size);
123 uint32_t size; // in bytes.
124 int32_t data; // this is just here to get an offset.
127 static_assert(sizeof(SizedResult<int8_t>) == 8,
128 "size of SizedResult<int8_t> should be 8");
129 static_assert(offsetof(SizedResult<int8_t>, size) == 0,
130 "offset of SizedResult<int8_t>.size should be 0");
131 static_assert(offsetof(SizedResult<int8_t>, data) == 4,
132 "offset of SizedResult<int8_t>.data should be 4");
134 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
135 struct ProgramInput {
136 uint32_t type; // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
137 int32_t size; // The size (how big the array is for uniforms)
138 uint32_t location_offset; // offset from ProgramInfoHeader to 'size'
139 // locations for uniforms, 1 for attribs.
140 uint32_t name_offset; // offset from ProgrmaInfoHeader to start of name.
141 uint32_t name_length; // length of the name.
144 // The format of the bucket filled out by GetProgramInfoCHROMIUM
145 struct ProgramInfoHeader {
146 uint32_t link_status;
147 uint32_t num_attribs;
148 uint32_t num_uniforms;
149 // ProgramInput inputs[num_attribs + num_uniforms];
152 // The data for one UniformBlock from GetProgramInfoCHROMIUM
153 struct UniformBlockInfo {
154 uint32_t binding; // UNIFORM_BLOCK_BINDING
155 uint32_t data_size; // UNIFORM_BLOCK_DATA_SIZE
156 uint32_t name_offset; // offset from UniformBlocksHeader to start of name.
157 uint32_t name_length; // UNIFORM_BLOCK_NAME_LENGTH
158 uint32_t active_uniforms; // UNIFORM_BLOCK_ACTIVE_UNIFORMS
159 // offset from UniformBlocksHeader to |active_uniforms| indices.
160 uint32_t active_uniform_offset;
161 // UNIFORM_BLOCK_REFERENDED_BY_VERTEX_SHADER
162 uint32_t referenced_by_vertex_shader;
163 // UNIFORM_BLOCK_REFERENDED_BY_FRAGMENT_SHADER
164 uint32_t referenced_by_fragment_shader;
167 // The format of the bucket filled out by GetUniformBlocksCHROMIUM
168 struct UniformBlocksHeader {
169 uint32_t num_uniform_blocks;
170 // UniformBlockInfo uniform_blocks[num_uniform_blocks];
173 // The data for one TransformFeedbackVarying from
174 // GetTransformFeedbackVaringCHROMIUM.
175 struct TransformFeedbackVaryingInfo {
176 uint32_t size;
177 uint32_t type;
178 uint32_t name_offset; // offset from Header to start of name.
179 uint32_t name_length; // including the null terminator.
182 // The format of the bucket filled out by GetTransformFeedbackVaryingsCHROMIUM
183 struct TransformFeedbackVaryingsHeader {
184 uint32_t transform_feedback_buffer_mode;
185 uint32_t num_transform_feedback_varyings;
186 // TransformFeedbackVaryingInfo varyings[num_transform_feedback_varyings];
189 // Parameters of a uniform that can be queried through glGetActiveUniformsiv,
190 // but not through glGetActiveUniform.
191 struct UniformES3Info {
192 int32_t block_index;
193 int32_t offset;
194 int32_t array_stride;
195 int32_t matrix_stride;
196 int32_t is_row_major;
199 // The format of the bucket filled out by GetUniformsivES3CHROMIUM
200 struct UniformsES3Header {
201 uint32_t num_uniforms;
202 // UniformES3Info uniforms[num_uniforms];
205 // The format of QuerySync used by EXT_occlusion_query_boolean
206 struct QuerySync {
207 void Reset() {
208 process_count = 0;
209 result = 0;
212 base::subtle::Atomic32 process_count;
213 uint64_t result;
216 struct AsyncUploadSync {
217 void Reset() {
218 base::subtle::Release_Store(&async_upload_token, 0);
221 void SetAsyncUploadToken(uint32_t token) {
222 DCHECK_NE(token, 0u);
223 base::subtle::Release_Store(&async_upload_token, token);
226 bool HasAsyncUploadTokenPassed(uint32_t token) {
227 DCHECK_NE(token, 0u);
228 uint32_t current_token = base::subtle::Acquire_Load(&async_upload_token);
229 return (current_token - token < 0x80000000);
232 base::subtle::Atomic32 async_upload_token;
235 struct DisjointValueSync {
236 void Reset() {
237 base::subtle::Release_Store(&disjoint_count, 0);
240 void SetDisjointCount(uint32_t token) {
241 DCHECK_NE(token, 0u);
242 base::subtle::Release_Store(&disjoint_count, token);
245 uint32_t GetDisjointCount() {
246 return base::subtle::Acquire_Load(&disjoint_count);
249 base::subtle::Atomic32 disjoint_count;
252 static_assert(sizeof(ProgramInput) == 20, "size of ProgramInput should be 20");
253 static_assert(offsetof(ProgramInput, type) == 0,
254 "offset of ProgramInput.type should be 0");
255 static_assert(offsetof(ProgramInput, size) == 4,
256 "offset of ProgramInput.size should be 4");
257 static_assert(offsetof(ProgramInput, location_offset) == 8,
258 "offset of ProgramInput.location_offset should be 8");
259 static_assert(offsetof(ProgramInput, name_offset) == 12,
260 "offset of ProgramInput.name_offset should be 12");
261 static_assert(offsetof(ProgramInput, name_length) == 16,
262 "offset of ProgramInput.name_length should be 16");
264 static_assert(sizeof(ProgramInfoHeader) == 12,
265 "size of ProgramInfoHeader should be 12");
266 static_assert(offsetof(ProgramInfoHeader, link_status) == 0,
267 "offset of ProgramInfoHeader.link_status should be 0");
268 static_assert(offsetof(ProgramInfoHeader, num_attribs) == 4,
269 "offset of ProgramInfoHeader.num_attribs should be 4");
270 static_assert(offsetof(ProgramInfoHeader, num_uniforms) == 8,
271 "offset of ProgramInfoHeader.num_uniforms should be 8");
273 static_assert(sizeof(UniformBlockInfo) == 32,
274 "size of UniformBlockInfo should be 32");
275 static_assert(offsetof(UniformBlockInfo, binding) == 0,
276 "offset of UniformBlockInfo.binding should be 0");
277 static_assert(offsetof(UniformBlockInfo, data_size) == 4,
278 "offset of UniformBlockInfo.data_size should be 4");
279 static_assert(offsetof(UniformBlockInfo, name_offset) == 8,
280 "offset of UniformBlockInfo.name_offset should be 8");
281 static_assert(offsetof(UniformBlockInfo, name_length) == 12,
282 "offset of UniformBlockInfo.name_length should be 12");
283 static_assert(offsetof(UniformBlockInfo, active_uniforms) == 16,
284 "offset of UniformBlockInfo.active_uniforms should be 16");
285 static_assert(offsetof(UniformBlockInfo, active_uniform_offset) == 20,
286 "offset of UniformBlockInfo.active_uniform_offset should be 20");
287 static_assert(offsetof(UniformBlockInfo, referenced_by_vertex_shader) == 24,
288 "offset of UniformBlockInfo.referenced_by_vertex_shader "
289 "should be 24");
290 static_assert(offsetof(UniformBlockInfo, referenced_by_fragment_shader) == 28,
291 "offset of UniformBlockInfo.referenced_by_fragment_shader "
292 "should be 28");
294 static_assert(sizeof(UniformBlocksHeader) == 4,
295 "size of UniformBlocksHeader should be 4");
296 static_assert(offsetof(UniformBlocksHeader, num_uniform_blocks) == 0,
297 "offset of UniformBlocksHeader.num_uniform_blocks should be 0");
299 namespace cmds {
301 #include "../common/gles2_cmd_format_autogen.h"
303 // These are hand written commands.
304 // TODO(gman): Attempt to make these auto-generated.
306 struct GenMailboxCHROMIUM {
307 typedef GenMailboxCHROMIUM ValueType;
308 static const CommandId kCmdId = kGenMailboxCHROMIUM;
309 static const cmd::ArgFlags kArgFlags = cmd::kFixed;
310 static const uint8 cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(3);
311 CommandHeader header;
314 struct InsertSyncPointCHROMIUM {
315 typedef InsertSyncPointCHROMIUM ValueType;
316 static const CommandId kCmdId = kInsertSyncPointCHROMIUM;
317 static const cmd::ArgFlags kArgFlags = cmd::kFixed;
318 static const uint8 cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(3);
319 CommandHeader header;
322 struct CreateAndConsumeTextureCHROMIUMImmediate {
323 typedef CreateAndConsumeTextureCHROMIUMImmediate ValueType;
324 static const CommandId kCmdId = kCreateAndConsumeTextureCHROMIUMImmediate;
325 static const cmd::ArgFlags kArgFlags = cmd::kAtLeastN;
326 static const uint8 cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(1);
328 static uint32_t ComputeDataSize() {
329 return static_cast<uint32_t>(sizeof(GLbyte) * 64); // NOLINT
332 static uint32_t ComputeSize() {
333 return static_cast<uint32_t>(sizeof(ValueType) +
334 ComputeDataSize()); // NOLINT
337 void SetHeader(uint32_t size_in_bytes) {
338 header.SetCmdByTotalSize<ValueType>(size_in_bytes);
341 void Init(GLenum _target, uint32_t _client_id, const GLbyte* _mailbox) {
342 SetHeader(ComputeSize());
343 target = _target;
344 client_id = _client_id;
345 memcpy(ImmediateDataAddress(this), _mailbox, ComputeDataSize());
348 void* Set(void* cmd,
349 GLenum _target,
350 uint32_t _client_id,
351 const GLbyte* _mailbox) {
352 static_cast<ValueType*>(cmd)->Init(_target, _client_id, _mailbox);
353 const uint32_t size = ComputeSize();
354 return NextImmediateCmdAddressTotalSize<ValueType>(cmd, size);
357 gpu::CommandHeader header;
358 uint32_t target;
359 uint32_t client_id;
362 static_assert(sizeof(CreateAndConsumeTextureCHROMIUMImmediate) == 12,
363 "size of CreateAndConsumeTextureCHROMIUMImmediate should be 12");
364 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate, header) == 0,
365 "offset of CreateAndConsumeTextureCHROMIUMImmediate.header "
366 "should be 0");
367 static_assert(offsetof(CreateAndConsumeTextureCHROMIUMImmediate, target) == 4,
368 "offset of CreateAndConsumeTextureCHROMIUMImmediate.target "
369 "should be 4");
370 static_assert(
371 offsetof(CreateAndConsumeTextureCHROMIUMImmediate, client_id) == 8,
372 "offset of CreateAndConsumeTextureCHROMIUMImmediate.client_id should be 8");
375 #pragma pack(pop)
377 } // namespace cmd
378 } // namespace gles2
379 } // namespace gpu
381 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_