Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / common / gles2_cmd_format.h
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 <stdint.h>
14 #include <string.h>
15
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
23 // GL types are forward declared to avoid including the GL headers. The problem
24 // is determining which GL headers to include from code that is common to the
25 // client and service sides (GLES2 or one of several GL implementations).
26 typedef unsigned int GLenum;
27 typedef unsigned int GLbitfield;
28 typedef unsigned int GLuint;
29 typedef int GLint;
30 typedef int GLsizei;
31 typedef unsigned char GLboolean;
32 typedef signed char GLbyte;
33 typedef short GLshort;
34 typedef unsigned char GLubyte;
35 typedef unsigned short GLushort;
36 typedef unsigned long GLulong;
37 typedef float GLfloat;
38 typedef float GLclampf;
39 typedef double GLdouble;
40 typedef double GLclampd;
41 typedef void GLvoid;
42 typedef khronos_intptr_t GLintptr;
43 typedef khronos_ssize_t  GLsizeiptr;
44
45 namespace gpu {
46 namespace gles2 {
47
48 // Command buffer is GPU_COMMAND_BUFFER_ENTRY_ALIGNMENT byte aligned.
49 #pragma pack(push, GPU_COMMAND_BUFFER_ENTRY_ALIGNMENT)
50
51 namespace id_namespaces {
52
53 // These are used when contexts share resources.
54 enum IdNamespaces {
55   kBuffers,
56   kFramebuffers,
57   kProgramsAndShaders,
58   kRenderbuffers,
59   kTextures,
60   kQueries,
61   kVertexArrays,
62   kValuebuffers,
63   kNumIdNamespaces
64 };
65
66 // These numbers must not change
67 COMPILE_ASSERT(kBuffers == 0, kBuffers_is_not_0);
68 COMPILE_ASSERT(kFramebuffers == 1, kFramebuffers_is_not_1);
69 COMPILE_ASSERT(kProgramsAndShaders == 2, kProgramsAndShaders_is_not_2);
70 COMPILE_ASSERT(kRenderbuffers == 3, kRenderbuffers_is_not_3);
71 COMPILE_ASSERT(kTextures == 4, kTextures_is_not_4);
72
73 }  // namespace id_namespaces
74
75 // Used for some glGetXXX commands that return a result through a pointer. We
76 // need to know if the command succeeded or not and the size of the result. If
77 // the command failed its result size will 0.
78 template <typename T>
79 struct SizedResult {
80   typedef T Type;
81
82   T* GetData() {
83     return static_cast<T*>(static_cast<void*>(&data));
84   }
85
86   // Returns the total size in bytes of the SizedResult for a given number of
87   // results including the size field.
88   static size_t ComputeSize(size_t num_results) {
89     return sizeof(T) * num_results + sizeof(uint32_t);  // NOLINT
90   }
91
92   // Returns the total size in bytes of the SizedResult for a given size of
93   // results.
94   static size_t ComputeSizeFromBytes(size_t size_of_result_in_bytes) {
95     return size_of_result_in_bytes + sizeof(uint32_t);  // NOLINT
96   }
97
98   // Returns the maximum number of results for a given buffer size.
99   static uint32_t ComputeMaxResults(size_t size_of_buffer) {
100     return (size_of_buffer >= sizeof(uint32_t)) ?
101         ((size_of_buffer - sizeof(uint32_t)) / sizeof(T)) : 0;  // NOLINT
102   }
103
104   // Set the size for a given number of results.
105   void SetNumResults(size_t num_results) {
106     size = sizeof(T) * num_results;  // NOLINT
107   }
108
109   // Get the number of elements in the result
110   int32_t GetNumResults() const {
111     return size / sizeof(T);  // NOLINT
112   }
113
114   // Copy the result.
115   void CopyResult(void* dst) const {
116     memcpy(dst, &data, size);
117   }
118
119   uint32_t size;  // in bytes.
120   int32_t data;  // this is just here to get an offset.
121 };
122
123 COMPILE_ASSERT(sizeof(SizedResult<int8_t>) == 8, SizedResult_size_not_8);
124 COMPILE_ASSERT(offsetof(SizedResult<int8_t>, size) == 0,
125                OffsetOf_SizedResult_size_not_0);
126 COMPILE_ASSERT(offsetof(SizedResult<int8_t>, data) == 4,
127                OffsetOf_SizedResult_data_not_4);
128
129 // The data for one attrib or uniform from GetProgramInfoCHROMIUM.
130 struct ProgramInput {
131   uint32_t type;             // The type (GL_VEC3, GL_MAT3, GL_SAMPLER_2D, etc.
132   int32_t size;              // The size (how big the array is for uniforms)
133   uint32_t location_offset;  // offset from ProgramInfoHeader to 'size'
134                              // locations for uniforms, 1 for attribs.
135   uint32_t name_offset;      // offset from ProgrmaInfoHeader to start of name.
136   uint32_t name_length;      // length of the name.
137 };
138
139 // The format of the bucket filled out by GetProgramInfoCHROMIUM
140 struct ProgramInfoHeader {
141   uint32_t link_status;
142   uint32_t num_attribs;
143   uint32_t num_uniforms;
144   // ProgramInput inputs[num_attribs + num_uniforms];
145 };
146
147 // The format of QuerySync used by EXT_occlusion_query_boolean
148 struct QuerySync {
149   void Reset() {
150     process_count = 0;
151     result = 0;
152   }
153
154   base::subtle::Atomic32 process_count;
155   uint64_t result;
156 };
157
158 struct AsyncUploadSync {
159   void Reset() {
160     base::subtle::Release_Store(&async_upload_token, 0);
161   }
162
163   void SetAsyncUploadToken(uint32_t token) {
164     DCHECK_NE(token, 0u);
165     base::subtle::Release_Store(&async_upload_token, token);
166   }
167
168   bool HasAsyncUploadTokenPassed(uint32_t token) {
169     DCHECK_NE(token, 0u);
170     uint32_t current_token = base::subtle::Acquire_Load(&async_upload_token);
171     return (current_token - token < 0x80000000);
172   }
173
174   base::subtle::Atomic32 async_upload_token;
175 };
176
177 COMPILE_ASSERT(sizeof(ProgramInput) == 20, ProgramInput_size_not_20);
178 COMPILE_ASSERT(offsetof(ProgramInput, type) == 0,
179                OffsetOf_ProgramInput_type_not_0);
180 COMPILE_ASSERT(offsetof(ProgramInput, size) == 4,
181                OffsetOf_ProgramInput_size_not_4);
182 COMPILE_ASSERT(offsetof(ProgramInput, location_offset) == 8,
183                OffsetOf_ProgramInput_location_offset_not_8);
184 COMPILE_ASSERT(offsetof(ProgramInput, name_offset) == 12,
185                OffsetOf_ProgramInput_name_offset_not_12);
186 COMPILE_ASSERT(offsetof(ProgramInput, name_length) == 16,
187                OffsetOf_ProgramInput_name_length_not_16);
188
189 COMPILE_ASSERT(sizeof(ProgramInfoHeader) == 12, ProgramInfoHeader_size_not_12);
190 COMPILE_ASSERT(offsetof(ProgramInfoHeader, link_status) == 0,
191                OffsetOf_ProgramInfoHeader_link_status_not_0);
192 COMPILE_ASSERT(offsetof(ProgramInfoHeader, num_attribs) == 4,
193                OffsetOf_ProgramInfoHeader_num_attribs_not_4);
194 COMPILE_ASSERT(offsetof(ProgramInfoHeader, num_uniforms) == 8,
195                OffsetOf_ProgramInfoHeader_num_uniforms_not_8);
196
197 namespace cmds {
198
199 #include "../common/gles2_cmd_format_autogen.h"
200
201 // These are hand written commands.
202 // TODO(gman): Attempt to make these auto-generated.
203
204 struct GenMailboxCHROMIUM {
205   typedef GenMailboxCHROMIUM ValueType;
206   static const CommandId kCmdId = kGenMailboxCHROMIUM;
207   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
208   static const uint8 cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(3);
209   CommandHeader header;
210 };
211
212 struct InsertSyncPointCHROMIUM {
213   typedef InsertSyncPointCHROMIUM ValueType;
214   static const CommandId kCmdId = kInsertSyncPointCHROMIUM;
215   static const cmd::ArgFlags kArgFlags = cmd::kFixed;
216   static const uint8 cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(3);
217   CommandHeader header;
218 };
219
220 struct CreateAndConsumeTextureCHROMIUMImmediate {
221   typedef CreateAndConsumeTextureCHROMIUMImmediate ValueType;
222   static const CommandId kCmdId = kCreateAndConsumeTextureCHROMIUMImmediate;
223   static const cmd::ArgFlags kArgFlags = cmd::kAtLeastN;
224   static const uint8 cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(1);
225
226   static uint32_t ComputeDataSize() {
227     return static_cast<uint32_t>(sizeof(GLbyte) * 64);  // NOLINT
228   }
229
230   static uint32_t ComputeSize() {
231     return static_cast<uint32_t>(sizeof(ValueType) +
232                                  ComputeDataSize());  // NOLINT
233   }
234
235   void SetHeader(uint32_t size_in_bytes) {
236     header.SetCmdByTotalSize<ValueType>(size_in_bytes);
237   }
238
239   void Init(GLenum _target, uint32_t _client_id, const GLbyte* _mailbox) {
240     SetHeader(ComputeSize());
241     target = _target;
242     client_id = _client_id;
243     memcpy(ImmediateDataAddress(this), _mailbox, ComputeDataSize());
244   }
245
246   void* Set(void* cmd,
247             GLenum _target,
248             uint32_t _client_id,
249             const GLbyte* _mailbox) {
250     static_cast<ValueType*>(cmd)->Init(_target, _client_id, _mailbox);
251     const uint32_t size = ComputeSize();
252     return NextImmediateCmdAddressTotalSize<ValueType>(cmd, size);
253   }
254
255   gpu::CommandHeader header;
256   uint32_t target;
257   uint32_t client_id;
258 };
259
260 COMPILE_ASSERT(sizeof(CreateAndConsumeTextureCHROMIUMImmediate) == 12,
261                Sizeof_CreateAndConsumeTextureCHROMIUMImmediate_is_not_12);
262 COMPILE_ASSERT(offsetof(CreateAndConsumeTextureCHROMIUMImmediate, header) == 0,
263                OffsetOf_CreateAndConsumeTextureCHROMIUMImmediate_header_not_0);
264 COMPILE_ASSERT(offsetof(CreateAndConsumeTextureCHROMIUMImmediate, target) == 4,
265                OffsetOf_CreateAndConsumeTextureCHROMIUMImmediate_target_not_4);
266 COMPILE_ASSERT(
267     offsetof(CreateAndConsumeTextureCHROMIUMImmediate, client_id) == 8,
268     OffsetOf_CreateAndConsumeTextureCHROMIUMImmediate_client_id_not_8);
269
270
271 #pragma pack(pop)
272
273 }  // namespace cmd
274 }  // namespace gles2
275 }  // namespace gpu
276
277 #endif  // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_FORMAT_H_