blob: 6e2d2931883ee8a1f1237e6ff1998a390cfb2be1 [file] [log] [blame]
[email protected]e259eb412012-10-13 05:47:241// 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 contains the ContextState class.
6
7#ifndef GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
8#define GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
9
mostynb6682b1c42016-04-19 10:17:3010#include <memory>
[email protected]1868a342012-11-07 15:56:0211#include <vector>
mostynb6682b1c42016-04-19 10:17:3012
[email protected]e259eb412012-10-13 05:47:2413#include "base/logging.h"
[email protected]e259eb412012-10-13 05:47:2414#include "gpu/command_buffer/service/gl_utils.h"
bajones5141d032015-12-07 21:13:3915#include "gpu/command_buffer/service/sampler_manager.h"
zmoeaae3bb2016-07-15 19:23:1916#include "gpu/command_buffer/service/shader_manager.h"
[email protected]e259eb412012-10-13 05:47:2417#include "gpu/command_buffer/service/texture_manager.h"
[email protected]e259eb412012-10-13 05:47:2418#include "gpu/command_buffer/service/vertex_array_manager.h"
mostynb6682b1c42016-04-19 10:17:3019#include "gpu/command_buffer/service/vertex_attrib_manager.h"
[email protected]88a61bf2012-10-27 13:00:4220#include "gpu/gpu_export.h"
[email protected]e259eb412012-10-13 05:47:2421
22namespace gpu {
23namespace gles2 {
24
[email protected]31494b82013-02-28 10:10:2625class Buffer;
[email protected]d3eba342013-04-18 21:11:5026class ErrorState;
[email protected]828a3932014-04-02 14:43:1327class ErrorStateClient;
[email protected]b3cbad12012-12-05 19:56:3628class FeatureInfo;
[email protected]31494b82013-02-28 10:10:2629class Framebuffer;
zmo48ee6d3f2016-05-06 20:33:2630class IndexedBufferBindingHost;
dyenb245d372015-07-25 01:18:3831class Logger;
[email protected]31494b82013-02-28 10:10:2632class Program;
33class Renderbuffer;
zmo6c468ba2016-05-04 20:00:5134class TransformFeedback;
[email protected]b3cbad12012-12-05 19:56:3635
[email protected]e259eb412012-10-13 05:47:2436// State associated with each texture unit.
[email protected]88a61bf2012-10-27 13:00:4237struct GPU_EXPORT TextureUnit {
[email protected]e259eb412012-10-13 05:47:2438 TextureUnit();
vmpstr3b7b8b22016-03-01 23:00:2039 TextureUnit(const TextureUnit& other);
[email protected]e259eb412012-10-13 05:47:2440 ~TextureUnit();
41
42 // The last target that was bound to this texture unit.
43 GLenum bind_target;
44
45 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
[email protected]370eaf12013-05-18 09:19:4946 scoped_refptr<TextureRef> bound_texture_2d;
[email protected]e259eb412012-10-13 05:47:2447
48 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
49 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4950 scoped_refptr<TextureRef> bound_texture_cube_map;
[email protected]e259eb412012-10-13 05:47:2451
52 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
53 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4954 scoped_refptr<TextureRef> bound_texture_external_oes;
[email protected]e259eb412012-10-13 05:47:2455
56 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
57 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4958 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
[email protected]e259eb412012-10-13 05:47:2459
zmoea06a6f2015-04-30 01:15:4660 // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture
61 scoped_refptr<TextureRef> bound_texture_3d;
62
63 // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with
64 // glBindTexture
65 scoped_refptr<TextureRef> bound_texture_2d_array;
66
[email protected]370eaf12013-05-18 09:19:4967 scoped_refptr<TextureRef> GetInfoForSamplerType(
[email protected]ed9f9cd2013-02-27 21:12:3568 GLenum type) {
[email protected]e259eb412012-10-13 05:47:2469 switch (type) {
70 case GL_SAMPLER_2D:
71 return bound_texture_2d;
72 case GL_SAMPLER_CUBE:
73 return bound_texture_cube_map;
74 case GL_SAMPLER_EXTERNAL_OES:
75 return bound_texture_external_oes;
76 case GL_SAMPLER_2D_RECT_ARB:
77 return bound_texture_rectangle_arb;
zmoea06a6f2015-04-30 01:15:4678 case GL_SAMPLER_3D:
79 return bound_texture_3d;
80 case GL_SAMPLER_2D_ARRAY:
81 return bound_texture_2d_array;
[email protected]e259eb412012-10-13 05:47:2482 }
83
84 NOTREACHED();
85 return NULL;
86 }
erikchenffe273b2015-12-17 03:48:1387
88 scoped_refptr<TextureRef>& GetInfoForTarget(GLenum target) {
89 switch (target) {
90 case GL_TEXTURE_2D:
91 return bound_texture_2d;
92 case GL_TEXTURE_CUBE_MAP:
93 return bound_texture_cube_map;
94 case GL_TEXTURE_EXTERNAL_OES:
95 return bound_texture_external_oes;
96 case GL_TEXTURE_RECTANGLE_ARB:
97 return bound_texture_rectangle_arb;
98 case GL_TEXTURE_3D:
99 return bound_texture_3d;
100 case GL_TEXTURE_2D_ARRAY:
101 return bound_texture_2d_array;
102 }
103
104 NOTREACHED();
105 return bound_texture_2d;
106 }
[email protected]e259eb412012-10-13 05:47:24107};
108
zmo5ee097e2015-05-14 19:13:52109class GPU_EXPORT Vec4 {
110 public:
[email protected]af6380962012-11-29 23:24:13111 Vec4() {
zmo5ee097e2015-05-14 19:13:52112 v_[0].float_value = 0.0f;
113 v_[1].float_value = 0.0f;
114 v_[2].float_value = 0.0f;
115 v_[3].float_value = 1.0f;
zmoeaae3bb2016-07-15 19:23:19116 type_ = SHADER_VARIABLE_FLOAT;
[email protected]af6380962012-11-29 23:24:13117 }
zmo5ee097e2015-05-14 19:13:52118
119 template <typename T>
120 void GetValues(T* values) const;
121
122 template <typename T>
123 void SetValues(const T* values);
124
zmoeaae3bb2016-07-15 19:23:19125 ShaderVariableBaseType type() const {
zmo5ee097e2015-05-14 19:13:52126 return type_;
127 }
128
129 bool Equal(const Vec4& other) const;
130
131 private:
132 union ValueUnion {
133 GLfloat float_value;
134 GLint int_value;
135 GLuint uint_value;
136 };
137
138 ValueUnion v_[4];
zmoeaae3bb2016-07-15 19:23:19139 ShaderVariableBaseType type_;
[email protected]af6380962012-11-29 23:24:13140};
[email protected]e259eb412012-10-13 05:47:24141
zmo5ee097e2015-05-14 19:13:52142template <>
143GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const;
144template <>
145GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const;
146template <>
147GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const;
148
149template <>
150GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values);
151template <>
152GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values);
153template <>
154GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values);
155
[email protected]88a61bf2012-10-27 13:00:42156struct GPU_EXPORT ContextState {
zmo2b9c47392015-12-11 02:21:32157 enum Dimension {
158 k2D,
159 k3D
160 };
161
[email protected]828a3932014-04-02 14:43:13162 ContextState(FeatureInfo* feature_info,
163 ErrorStateClient* error_state_client,
164 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24165 ~ContextState();
166
[email protected]f731b9462012-10-30 00:35:22167 void Initialize();
168
[email protected]454157e2014-05-03 02:49:45169 void SetIgnoreCachedStateForTest(bool ignore) {
170 ignore_cached_state = ignore;
171 }
172
[email protected]8875a5f2014-06-27 08:33:47173 void RestoreState(const ContextState* prev_state);
[email protected]88ba52f2014-04-09 12:39:34174 void InitCapabilities(const ContextState* prev_state) const;
175 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22176
[email protected]29a4d902013-02-26 20:18:06177 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16178 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27179 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52180 void RestoreVertexAttribValues() const;
181 void RestoreVertexAttribArrays(
182 const scoped_refptr<VertexAttribManager> attrib_manager) const;
183 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06184 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34185 void RestoreGlobalState(const ContextState* prev_state) const;
zmo744d40e2016-05-10 20:56:22186 void RestoreProgramSettings(const ContextState* prev_state,
187 bool restore_transform_feedback_bindings) const;
[email protected]8875a5f2014-06-27 08:33:47188 void RestoreRenderbufferBindings();
zmo48ee6d3f2016-05-06 20:33:26189 void RestoreIndexedUniformBufferBindings(const ContextState* prev_state);
[email protected]5baa86bc2014-01-16 04:33:16190 void RestoreTextureUnitBindings(
191 GLuint unit, const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06192
geofflang398fb212016-07-13 16:27:42193 void PushTextureDecompressionUnpackState() const;
194 void RestoreUnpackState() const;
195
[email protected]d058bca2012-11-26 10:27:26196 // Helper for getting cached state.
197 bool GetStateAsGLint(
198 GLenum pname, GLint* params, GLsizei* num_written) const;
199 bool GetStateAsGLfloat(
200 GLenum pname, GLfloat* params, GLsizei* num_written) const;
201 bool GetEnabled(GLenum cap) const;
202
[email protected]454157e2014-05-03 02:49:45203 inline void SetDeviceColorMask(GLboolean red,
204 GLboolean green,
205 GLboolean blue,
206 GLboolean alpha) {
207 if (cached_color_mask_red == red && cached_color_mask_green == green &&
208 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
209 !ignore_cached_state)
210 return;
211 cached_color_mask_red = red;
212 cached_color_mask_green = green;
213 cached_color_mask_blue = blue;
214 cached_color_mask_alpha = alpha;
215 glColorMask(red, green, blue, alpha);
216 }
217
218 inline void SetDeviceDepthMask(GLboolean mask) {
219 if (cached_depth_mask == mask && !ignore_cached_state)
220 return;
221 cached_depth_mask = mask;
222 glDepthMask(mask);
223 }
224
225 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
226 if (op == GL_FRONT) {
227 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
228 return;
229 cached_stencil_front_writemask = mask;
230 } else if (op == GL_BACK) {
231 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
232 return;
233 cached_stencil_back_writemask = mask;
234 } else {
235 NOTREACHED();
236 return;
237 }
238 glStencilMaskSeparate(op, mask);
239 }
240
[email protected]d3eba342013-04-18 21:11:50241 ErrorState* GetErrorState();
242
zmo4c0c3532015-05-22 20:04:48243 void SetBoundBuffer(GLenum target, Buffer* buffer);
244 void RemoveBoundBuffer(Buffer* buffer);
245
mpearsone9c2cd92016-07-26 04:43:48246 void InitGenericAttribBaseType(GLuint max_vertex_attribs) {
247 max_vertex_attribs_ = max_vertex_attribs;
yunchao.hec4875312016-07-22 16:11:07248
mpearsone9c2cd92016-07-26 04:43:48249 uint32_t packed_size = max_vertex_attribs_ / 16;
250 packed_size += (max_vertex_attribs_ % 16 == 0) ? 0 : 1;
251 generic_attrib_base_type_mask_.resize(packed_size);
252 for (uint32_t i = 0; i < packed_size; ++i) {
253 // All generic attribs are float type by default.
254 generic_attrib_base_type_mask_[i] = 0x55555555u * SHADER_VARIABLE_FLOAT;
255 }
yunchao.hec4875312016-07-22 16:11:07256 }
257
258 void SetGenericVertexAttribBaseType(GLuint index, GLenum base_type) {
mpearsone9c2cd92016-07-26 04:43:48259 DCHECK(index < max_vertex_attribs_);
yunchao.hec4875312016-07-22 16:11:07260 int shift_bits = (index % 16) * 2;
261 generic_attrib_base_type_mask_[index / 16] &= ~(0x3 << shift_bits);
262 generic_attrib_base_type_mask_[index / 16] |= (base_type << shift_bits);
263 }
264
mpearsone9c2cd92016-07-26 04:43:48265 // Return 16 attributes' base types, in which the generic attribute
266 // specified by argument 'index' located.
267 uint32_t GetGenericVertexAttribBaseTypeMask(GLuint index) {
268 DCHECK(index < max_vertex_attribs_);
269 return generic_attrib_base_type_mask_[index / 16];
yunchao.hec4875312016-07-22 16:11:07270 }
271
bajones2b98b2a2015-09-15 02:27:36272 void UnbindTexture(TextureRef* texture);
bajones5141d032015-12-07 21:13:39273 void UnbindSampler(Sampler* sampler);
bajones2b98b2a2015-09-15 02:27:36274
zmo2b9c47392015-12-11 02:21:32275 PixelStoreParams GetPackParams();
276 PixelStoreParams GetUnpackParams(Dimension dimension);
277
zmo0ed71922016-06-23 01:18:42278 void EnableDisableFramebufferSRGB(bool enable);
279
[email protected]f731b9462012-10-30 00:35:22280 #include "gpu/command_buffer/service/context_state_autogen.h"
281
282 EnableFlags enable_flags;
283
[email protected]e259eb412012-10-13 05:47:24284 // Current active texture by 0 - n index.
285 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
286 // be 2.
287 GLuint active_texture_unit;
288
[email protected]e259eb412012-10-13 05:47:24289 // The currently bound array buffer. If this is 0 it is illegal to call
290 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21291 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24292
zmo4c0c3532015-05-22 20:04:48293 scoped_refptr<Buffer> bound_copy_read_buffer;
294 scoped_refptr<Buffer> bound_copy_write_buffer;
295 scoped_refptr<Buffer> bound_pixel_pack_buffer;
296 scoped_refptr<Buffer> bound_pixel_unpack_buffer;
297 scoped_refptr<Buffer> bound_transform_feedback_buffer;
298 scoped_refptr<Buffer> bound_uniform_buffer;
299
[email protected]e259eb412012-10-13 05:47:24300 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02301 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24302
bajones5141d032015-12-07 21:13:39303 // Which samplers are bound to each texture unit;
304 std::vector<scoped_refptr<Sampler>> sampler_units;
305
zmo6c468ba2016-05-04 20:00:51306 // We create a transform feedback as the default one per ES3 enabled context
307 // instead of using GL's default one to make context switching easier.
308 // For other context, we will never change the default transform feedback's
309 // states, so we can just use the GL's default one.
310 scoped_refptr<TransformFeedback> default_transform_feedback;
311
312 scoped_refptr<TransformFeedback> bound_transform_feedback;
313
zmo48ee6d3f2016-05-06 20:33:26314 scoped_refptr<IndexedBufferBindingHost> indexed_uniform_buffer_bindings;
315
[email protected]af6380962012-11-29 23:24:13316 // The values for each attrib.
317 std::vector<Vec4> attrib_values;
318
[email protected]e259eb412012-10-13 05:47:24319 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35320 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52321 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24322
323 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35324 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24325
[email protected]e259eb412012-10-13 05:47:24326 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35327 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]8875a5f2014-06-27 08:33:47328 bool bound_renderbuffer_valid;
[email protected]e259eb412012-10-13 05:47:24329
[email protected]e259eb412012-10-13 05:47:24330 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45331 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36332
zmo8ac3bab2015-04-18 02:30:58333 mutable bool fbo_binding_for_scissor_workaround_dirty;
[email protected]d3eba342013-04-18 21:11:50334
335 private:
zmo8ac3bab2015-04-18 02:30:58336 void EnableDisable(GLenum pname, bool enable) const;
337
zmocdfe65d2015-12-02 17:35:56338 // If a buffer object is bound to PIXEL_PACK_BUFFER, set all pack parameters
339 // user values; otherwise, set them to 0.
340 void UpdatePackParameters() const;
341 // If a buffer object is bound to PIXEL_UNPACK_BUFFER, set all unpack
342 // parameters user values; otherwise, set them to 0.
343 void UpdateUnpackParameters() const;
344
zmob730f32b2016-01-06 20:39:08345 void InitStateManual(const ContextState* prev_state) const;
346
zmo0ed71922016-06-23 01:18:42347 bool framebuffer_srgb_;
348
mpearsone9c2cd92016-07-26 04:43:48349 uint32_t max_vertex_attribs_;
yunchao.hec4875312016-07-22 16:11:07350 // Generic vertex attrib base types: FLOAT, INT, or UINT.
351 // Each base type is encoded into 2 bits, the lowest 2 bits for location 0,
mpearsone9c2cd92016-07-26 04:43:48352 // the highest 2 bits for location (max_vertex_attribs_ - 1).
yunchao.hec4875312016-07-22 16:11:07353 std::vector<uint32_t> generic_attrib_base_type_mask_;
354
zmo8ac3bab2015-04-18 02:30:58355 FeatureInfo* feature_info_;
mostynb6682b1c42016-04-19 10:17:30356 std::unique_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24357};
358
359} // namespace gles2
360} // namespace gpu
361
362#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
363