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 static uint32
GetPreferredGLReadPixelsFormat(uint32 internal_format
);
137 static uint32
GetPreferredGLReadPixelsType(
138 uint32 internal_format
, uint32 texture_type
);
140 // Returns a bitmask for the channels the given format supports.
142 static uint32
GetChannelsForFormat(int format
);
144 // Returns a bitmask for the channels the given attachment type needs.
145 static uint32
GetChannelsNeededForAttachmentType(
146 int type
, uint32 max_color_attachments
);
148 static bool IsNPOT(uint32 value
) {
149 return value
> 0 && (value
& (value
- 1)) != 0;
152 static std::string
GetStringEnum(uint32 value
);
153 static std::string
GetStringBool(uint32 value
);
154 static std::string
GetStringError(uint32 value
);
156 // Parses a uniform name.
157 // array_pos: the position of the last '[' character in name.
158 // element_index: the index of the array element specifed in the name.
159 // getting_array: True if name refers to array.
160 // returns true of parsing was successful. Returing true does NOT mean
161 // it's a valid uniform name. On the otherhand, returning false does mean
162 // it's an invalid uniform name.
163 static bool ParseUniformName(
164 const std::string
& name
,
167 bool* getting_array
);
169 #include "../common/gles2_cmd_utils_autogen.h"
172 static std::string
GetQualifiedEnumString(
173 const EnumToString
* table
, size_t count
, uint32 value
);
175 static const EnumToString
* const enum_to_string_table_
;
176 static const size_t enum_to_string_table_len_
;
178 int num_compressed_texture_formats_
;
179 int num_shader_binary_formats_
;
182 class GLES2_UTILS_EXPORT ContextCreationAttribHelper
{
184 ContextCreationAttribHelper();
186 void Serialize(std::vector
<int32
>* attribs
);
187 bool Parse(const std::vector
<int32
>& attribs
);
189 // -1 if invalid or unspecified.
197 int32 sample_buffers_
;
198 bool buffer_preserved_
;
199 bool share_resources_
;
200 bool bind_generates_resource_
;
201 bool fail_if_major_perf_caveat_
;
207 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_