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 is here so other GLES2 related files can have a common set of
6 // includes where appropriate.
8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_
15 #include "gpu/command_buffer/common/gles2_utils_export.h"
16 #include "gpu/command_buffer/common/types.h"
21 // Does a multiply and checks for overflow. If the multiply did not overflow
24 // Multiplies 2 32 bit unsigned numbers checking for overflow.
25 // If there was no overflow returns true.
26 inline bool SafeMultiplyUint32(uint32 a, uint32 b, uint32* dst) {
40 // Does an add checking for overflow. If there was no overflow returns true.
41 inline bool SafeAddUint32(uint32 a, uint32 b, uint32* dst) {
50 // Does an add checking for overflow. If there was no overflow returns true.
51 inline bool SafeAddInt32(int32 a, int32 b, int32* dst) {
52 int64 sum64 = static_cast<int64>(a) + b;
53 int32 sum32 = static_cast<int32>(sum64);
54 bool safe = sum64 == static_cast<int64>(sum32);
55 *dst = safe ? sum32 : 0;
59 // Utilties for GLES2 support.
60 class GLES2_UTILS_EXPORT GLES2Util {
62 static const int kNumFaces = 6;
64 // Bits returned by GetChannelsForFormat
73 kRGB = kRed | kGreen | kBlue,
83 : num_compressed_texture_formats_(0),
84 num_shader_binary_formats_(0) {
87 int num_compressed_texture_formats() const {
88 return num_compressed_texture_formats_;
91 void set_num_compressed_texture_formats(int num_compressed_texture_formats) {
92 num_compressed_texture_formats_ = num_compressed_texture_formats;
95 int num_shader_binary_formats() const {
96 return num_shader_binary_formats_;
99 void set_num_shader_binary_formats(int num_shader_binary_formats) {
100 num_shader_binary_formats_ = num_shader_binary_formats;
103 // Gets the number of values a particular id will return when a glGet
104 // function is called. If 0 is returned the id is invalid.
105 int GLGetNumValuesReturned(int id) const;
107 // Computes the size of a single group of elements from a format and type pair
108 static uint32 ComputeImageGroupSize(int format, int type);
110 // Computes the size of an image row including alignment padding
111 static bool ComputeImagePaddedRowSize(
112 int width, int format, int type, int unpack_alignment,
113 uint32* padded_row_size);
115 // Computes the size of image data for TexImage2D and TexSubImage2D.
116 // Optionally the unpadded and padded row sizes can be returned. If height < 2
117 // then the padded_row_size will be the same as the unpadded_row_size since
118 // padding is not necessary.
119 static bool ComputeImageDataSizes(
120 int width, int height, int format, int type, int unpack_alignment,
121 uint32* size, uint32* unpadded_row_size, uint32* padded_row_size);
123 static size_t RenderbufferBytesPerPixel(int format);
125 static uint32 GetGLDataTypeSizeForUniforms(int type);
127 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32 type);
129 static uint32 GLErrorToErrorBit(uint32 gl_error);
131 static uint32 GLErrorBitToGLError(uint32 error_bit);
133 static uint32 IndexToGLFaceTarget(int index);
135 // Returns a bitmask for the channels the given format supports.
137 static uint32 GetChannelsForFormat(int format);
139 // Returns a bitmask for the channels the given attachment type needs.
140 static uint32 GetChannelsNeededForAttachmentType(
141 int type, uint32 max_color_attachments);
143 static bool IsNPOT(uint32 value) {
144 return value > 0 && (value & (value - 1)) != 0;
147 static std::string GetStringEnum(uint32 value);
148 static std::string GetStringBool(uint32 value);
149 static std::string GetStringError(uint32 value);
151 // Parses a uniform name.
152 // array_pos: the position of the last '[' character in name.
153 // element_index: the index of the array element specifed in the name.
154 // getting_array: True if name refers to array.
155 // returns true of parsing was successful. Returing true does NOT mean
156 // it's a valid uniform name. On the otherhand, returning false does mean
157 // it's an invalid uniform name.
158 static bool ParseUniformName(
159 const std::string& name,
162 bool* getting_array);
164 #include "../common/gles2_cmd_utils_autogen.h"
167 static std::string GetQualifiedEnumString(
168 const EnumToString* table, size_t count, uint32 value);
170 static const EnumToString* const enum_to_string_table_;
171 static const size_t enum_to_string_table_len_;
173 int num_compressed_texture_formats_;
174 int num_shader_binary_formats_;
177 class GLES2_UTILS_EXPORT ContextCreationAttribHelper {
179 ContextCreationAttribHelper();
181 void Serialize(std::vector<int32>* attribs);
182 bool Parse(const std::vector<int32>& attribs);
184 // -1 if invalid or unspecified.
192 int32 sample_buffers_;
193 bool buffer_preserved_;
194 bool share_resources_;
195 bool bind_generates_resource_;
201 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_