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_
17 #include "gpu/command_buffer/common/gles2_utils_export.h"
22 // Does a multiply and checks for overflow. If the multiply did not overflow
25 // Multiplies 2 32 bit unsigned numbers checking for overflow.
26 // If there was no overflow returns true.
27 inline bool SafeMultiplyUint32(uint32_t a, uint32_t b, uint32_t* dst) {
41 // Does an add checking for overflow. If there was no overflow returns true.
42 inline bool SafeAddUint32(uint32_t a, uint32_t b, uint32_t* dst) {
51 // Does an add checking for overflow. If there was no overflow returns true.
52 inline bool SafeAddInt32(int32_t a, int32_t b, int32_t* dst) {
53 int64_t sum64 = static_cast<int64_t>(a) + b;
54 int32_t sum32 = static_cast<int32_t>(sum64);
55 bool safe = sum64 == static_cast<int64_t>(sum32);
56 *dst = safe ? sum32 : 0;
60 // Return false if |value| is more than a 32 bit integer can represent.
62 inline bool FitInt32NonNegative(T value) {
63 const int32_t max = std::numeric_limits<int32_t>::max();
64 return (std::numeric_limits<T>::max() <= max ||
65 value <= static_cast<T>(max));
68 // Utilties for GLES2 support.
69 class GLES2_UTILS_EXPORT GLES2Util {
71 static const int kNumFaces = 6;
73 // Bits returned by GetChannelsForFormat
82 kRGB = kRed | kGreen | kBlue,
92 : num_compressed_texture_formats_(0),
93 num_shader_binary_formats_(0) {
96 int num_compressed_texture_formats() const {
97 return num_compressed_texture_formats_;
100 void set_num_compressed_texture_formats(int num_compressed_texture_formats) {
101 num_compressed_texture_formats_ = num_compressed_texture_formats;
104 int num_shader_binary_formats() const {
105 return num_shader_binary_formats_;
108 void set_num_shader_binary_formats(int num_shader_binary_formats) {
109 num_shader_binary_formats_ = num_shader_binary_formats;
112 // Gets the number of values a particular id will return when a glGet
113 // function is called. If 0 is returned the id is invalid.
114 int GLGetNumValuesReturned(int id) const;
116 // Computes the size of a single group of elements from a format and type pair
117 static uint32_t ComputeImageGroupSize(int format, int type);
119 // Computes the size of an image row including alignment padding
120 static bool ComputeImagePaddedRowSize(
121 int width, int format, int type, int unpack_alignment,
122 uint32_t* padded_row_size);
124 // Computes the size of image data for TexImage2D and TexSubImage2D.
125 // Optionally the unpadded and padded row sizes can be returned. If height < 2
126 // then the padded_row_size will be the same as the unpadded_row_size since
127 // padding is not necessary.
128 static bool ComputeImageDataSizes(
129 int width, int height, int format, int type, int unpack_alignment,
130 uint32_t* size, uint32_t* unpadded_row_size, uint32_t* padded_row_size);
132 static size_t RenderbufferBytesPerPixel(int format);
134 static uint32_t GetGLDataTypeSizeForUniforms(int type);
136 static size_t GetGLTypeSizeForTexturesAndBuffers(uint32_t type);
138 static uint32_t GLErrorToErrorBit(uint32_t gl_error);
140 static uint32_t GLErrorBitToGLError(uint32_t error_bit);
142 static uint32_t IndexToGLFaceTarget(int index);
144 static uint32_t GetPreferredGLReadPixelsFormat(uint32_t internal_format);
146 static uint32_t GetPreferredGLReadPixelsType(
147 uint32_t internal_format, uint32_t texture_type);
149 // Returns a bitmask for the channels the given format supports.
151 static uint32_t GetChannelsForFormat(int format);
153 // Returns a bitmask for the channels the given attachment type needs.
154 static uint32_t GetChannelsNeededForAttachmentType(
155 int type, uint32_t max_color_attachments);
157 static bool IsNPOT(uint32_t value) {
158 return value > 0 && (value & (value - 1)) != 0;
161 static std::string GetStringEnum(uint32_t value);
162 static std::string GetStringBool(uint32_t value);
163 static std::string GetStringError(uint32_t value);
165 // Parses a uniform name.
166 // array_pos: the position of the last '[' character in name.
167 // element_index: the index of the array element specifed in the name.
168 // getting_array: True if name refers to array.
169 // returns true of parsing was successful. Returing true does NOT mean
170 // it's a valid uniform name. On the otherhand, returning false does mean
171 // it's an invalid uniform name.
172 static bool ParseUniformName(
173 const std::string& name,
176 bool* getting_array);
178 #include "../common/gles2_cmd_utils_autogen.h"
181 static std::string GetQualifiedEnumString(
182 const EnumToString* table, size_t count, uint32_t value);
184 static const EnumToString* const enum_to_string_table_;
185 static const size_t enum_to_string_table_len_;
187 int num_compressed_texture_formats_;
188 int num_shader_binary_formats_;
191 class GLES2_UTILS_EXPORT ContextCreationAttribHelper {
193 ContextCreationAttribHelper();
195 void Serialize(std::vector<int32_t>* attribs);
196 bool Parse(const std::vector<int32_t>& attribs);
198 // -1 if invalid or unspecified.
204 int32_t stencil_size_;
206 int32_t sample_buffers_;
207 bool buffer_preserved_;
208 bool share_resources_;
209 bool bind_generates_resource_;
210 bool fail_if_major_perf_caveat_;
211 bool lose_context_when_out_of_memory_;
217 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_