blob: f20186ba851eb675311c3c99f6901b99f131fbbe [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"
[email protected]e259eb412012-10-13 05:47:2416#include "gpu/command_buffer/service/texture_manager.h"
[email protected]e259eb412012-10-13 05:47:2417#include "gpu/command_buffer/service/vertex_array_manager.h"
mostynb6682b1c42016-04-19 10:17:3018#include "gpu/command_buffer/service/vertex_attrib_manager.h"
[email protected]88a61bf2012-10-27 13:00:4219#include "gpu/gpu_export.h"
[email protected]e259eb412012-10-13 05:47:2420
21namespace gpu {
22namespace gles2 {
23
[email protected]31494b82013-02-28 10:10:2624class Buffer;
[email protected]d3eba342013-04-18 21:11:5025class ErrorState;
[email protected]828a3932014-04-02 14:43:1326class ErrorStateClient;
[email protected]b3cbad12012-12-05 19:56:3627class FeatureInfo;
[email protected]31494b82013-02-28 10:10:2628class Framebuffer;
zmo48ee6d3f2016-05-06 20:33:2629class IndexedBufferBindingHost;
dyenb245d372015-07-25 01:18:3830class Logger;
[email protected]31494b82013-02-28 10:10:2631class Program;
32class Renderbuffer;
zmo6c468ba2016-05-04 20:00:5133class TransformFeedback;
[email protected]b3cbad12012-12-05 19:56:3634
[email protected]e259eb412012-10-13 05:47:2435// State associated with each texture unit.
[email protected]88a61bf2012-10-27 13:00:4236struct GPU_EXPORT TextureUnit {
[email protected]e259eb412012-10-13 05:47:2437 TextureUnit();
vmpstr3b7b8b22016-03-01 23:00:2038 TextureUnit(const TextureUnit& other);
[email protected]e259eb412012-10-13 05:47:2439 ~TextureUnit();
40
41 // The last target that was bound to this texture unit.
42 GLenum bind_target;
43
44 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
[email protected]370eaf12013-05-18 09:19:4945 scoped_refptr<TextureRef> bound_texture_2d;
[email protected]e259eb412012-10-13 05:47:2446
47 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
48 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4949 scoped_refptr<TextureRef> bound_texture_cube_map;
[email protected]e259eb412012-10-13 05:47:2450
51 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
52 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4953 scoped_refptr<TextureRef> bound_texture_external_oes;
[email protected]e259eb412012-10-13 05:47:2454
55 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
56 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4957 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
[email protected]e259eb412012-10-13 05:47:2458
zmoea06a6f2015-04-30 01:15:4659 // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture
60 scoped_refptr<TextureRef> bound_texture_3d;
61
62 // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with
63 // glBindTexture
64 scoped_refptr<TextureRef> bound_texture_2d_array;
65
[email protected]370eaf12013-05-18 09:19:4966 scoped_refptr<TextureRef> GetInfoForSamplerType(
[email protected]ed9f9cd2013-02-27 21:12:3567 GLenum type) {
[email protected]e259eb412012-10-13 05:47:2468 switch (type) {
69 case GL_SAMPLER_2D:
70 return bound_texture_2d;
71 case GL_SAMPLER_CUBE:
72 return bound_texture_cube_map;
73 case GL_SAMPLER_EXTERNAL_OES:
74 return bound_texture_external_oes;
75 case GL_SAMPLER_2D_RECT_ARB:
76 return bound_texture_rectangle_arb;
zmoea06a6f2015-04-30 01:15:4677 case GL_SAMPLER_3D:
78 return bound_texture_3d;
79 case GL_SAMPLER_2D_ARRAY:
80 return bound_texture_2d_array;
[email protected]e259eb412012-10-13 05:47:2481 }
82
83 NOTREACHED();
84 return NULL;
85 }
erikchenffe273b2015-12-17 03:48:1386
87 scoped_refptr<TextureRef>& GetInfoForTarget(GLenum target) {
88 switch (target) {
89 case GL_TEXTURE_2D:
90 return bound_texture_2d;
91 case GL_TEXTURE_CUBE_MAP:
92 return bound_texture_cube_map;
93 case GL_TEXTURE_EXTERNAL_OES:
94 return bound_texture_external_oes;
95 case GL_TEXTURE_RECTANGLE_ARB:
96 return bound_texture_rectangle_arb;
97 case GL_TEXTURE_3D:
98 return bound_texture_3d;
99 case GL_TEXTURE_2D_ARRAY:
100 return bound_texture_2d_array;
101 }
102
103 NOTREACHED();
104 return bound_texture_2d;
105 }
[email protected]e259eb412012-10-13 05:47:24106};
107
zmo5ee097e2015-05-14 19:13:52108class GPU_EXPORT Vec4 {
109 public:
110 enum DataType {
111 kFloat,
112 kInt,
113 kUInt,
114 };
115
[email protected]af6380962012-11-29 23:24:13116 Vec4() {
zmo5ee097e2015-05-14 19:13:52117 v_[0].float_value = 0.0f;
118 v_[1].float_value = 0.0f;
119 v_[2].float_value = 0.0f;
120 v_[3].float_value = 1.0f;
121 type_ = kFloat;
[email protected]af6380962012-11-29 23:24:13122 }
zmo5ee097e2015-05-14 19:13:52123
124 template <typename T>
125 void GetValues(T* values) const;
126
127 template <typename T>
128 void SetValues(const T* values);
129
130 DataType type() const {
131 return type_;
132 }
133
134 bool Equal(const Vec4& other) const;
135
136 private:
137 union ValueUnion {
138 GLfloat float_value;
139 GLint int_value;
140 GLuint uint_value;
141 };
142
143 ValueUnion v_[4];
144 DataType type_;
[email protected]af6380962012-11-29 23:24:13145};
[email protected]e259eb412012-10-13 05:47:24146
zmo5ee097e2015-05-14 19:13:52147template <>
148GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const;
149template <>
150GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const;
151template <>
152GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const;
153
154template <>
155GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values);
156template <>
157GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values);
158template <>
159GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values);
160
[email protected]88a61bf2012-10-27 13:00:42161struct GPU_EXPORT ContextState {
zmo2b9c47392015-12-11 02:21:32162 enum Dimension {
163 k2D,
164 k3D
165 };
166
[email protected]828a3932014-04-02 14:43:13167 ContextState(FeatureInfo* feature_info,
168 ErrorStateClient* error_state_client,
169 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24170 ~ContextState();
171
[email protected]f731b9462012-10-30 00:35:22172 void Initialize();
173
[email protected]454157e2014-05-03 02:49:45174 void SetIgnoreCachedStateForTest(bool ignore) {
175 ignore_cached_state = ignore;
176 }
177
[email protected]8875a5f2014-06-27 08:33:47178 void RestoreState(const ContextState* prev_state);
[email protected]88ba52f2014-04-09 12:39:34179 void InitCapabilities(const ContextState* prev_state) const;
180 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22181
[email protected]29a4d902013-02-26 20:18:06182 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16183 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27184 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52185 void RestoreVertexAttribValues() const;
186 void RestoreVertexAttribArrays(
187 const scoped_refptr<VertexAttribManager> attrib_manager) const;
188 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06189 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34190 void RestoreGlobalState(const ContextState* prev_state) const;
zmo744d40e2016-05-10 20:56:22191 void RestoreProgramSettings(const ContextState* prev_state,
192 bool restore_transform_feedback_bindings) const;
[email protected]8875a5f2014-06-27 08:33:47193 void RestoreRenderbufferBindings();
zmo48ee6d3f2016-05-06 20:33:26194 void RestoreIndexedUniformBufferBindings(const ContextState* prev_state);
[email protected]5baa86bc2014-01-16 04:33:16195 void RestoreTextureUnitBindings(
196 GLuint unit, const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06197
[email protected]d058bca2012-11-26 10:27:26198 // Helper for getting cached state.
199 bool GetStateAsGLint(
200 GLenum pname, GLint* params, GLsizei* num_written) const;
201 bool GetStateAsGLfloat(
202 GLenum pname, GLfloat* params, GLsizei* num_written) const;
203 bool GetEnabled(GLenum cap) const;
204
[email protected]454157e2014-05-03 02:49:45205 inline void SetDeviceColorMask(GLboolean red,
206 GLboolean green,
207 GLboolean blue,
208 GLboolean alpha) {
209 if (cached_color_mask_red == red && cached_color_mask_green == green &&
210 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
211 !ignore_cached_state)
212 return;
213 cached_color_mask_red = red;
214 cached_color_mask_green = green;
215 cached_color_mask_blue = blue;
216 cached_color_mask_alpha = alpha;
217 glColorMask(red, green, blue, alpha);
218 }
219
220 inline void SetDeviceDepthMask(GLboolean mask) {
221 if (cached_depth_mask == mask && !ignore_cached_state)
222 return;
223 cached_depth_mask = mask;
224 glDepthMask(mask);
225 }
226
227 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
228 if (op == GL_FRONT) {
229 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
230 return;
231 cached_stencil_front_writemask = mask;
232 } else if (op == GL_BACK) {
233 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
234 return;
235 cached_stencil_back_writemask = mask;
236 } else {
237 NOTREACHED();
238 return;
239 }
240 glStencilMaskSeparate(op, mask);
241 }
242
[email protected]d3eba342013-04-18 21:11:50243 ErrorState* GetErrorState();
244
zmo4c0c3532015-05-22 20:04:48245 void SetBoundBuffer(GLenum target, Buffer* buffer);
246 void RemoveBoundBuffer(Buffer* buffer);
247
bajones2b98b2a2015-09-15 02:27:36248 void UnbindTexture(TextureRef* texture);
bajones5141d032015-12-07 21:13:39249 void UnbindSampler(Sampler* sampler);
bajones2b98b2a2015-09-15 02:27:36250
zmo2b9c47392015-12-11 02:21:32251 PixelStoreParams GetPackParams();
252 PixelStoreParams GetUnpackParams(Dimension dimension);
253
[email protected]f731b9462012-10-30 00:35:22254 #include "gpu/command_buffer/service/context_state_autogen.h"
255
256 EnableFlags enable_flags;
257
[email protected]e259eb412012-10-13 05:47:24258 // Current active texture by 0 - n index.
259 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
260 // be 2.
261 GLuint active_texture_unit;
262
[email protected]e259eb412012-10-13 05:47:24263 // The currently bound array buffer. If this is 0 it is illegal to call
264 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21265 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24266
zmo4c0c3532015-05-22 20:04:48267 scoped_refptr<Buffer> bound_copy_read_buffer;
268 scoped_refptr<Buffer> bound_copy_write_buffer;
269 scoped_refptr<Buffer> bound_pixel_pack_buffer;
270 scoped_refptr<Buffer> bound_pixel_unpack_buffer;
271 scoped_refptr<Buffer> bound_transform_feedback_buffer;
272 scoped_refptr<Buffer> bound_uniform_buffer;
273
[email protected]e259eb412012-10-13 05:47:24274 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02275 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24276
bajones5141d032015-12-07 21:13:39277 // Which samplers are bound to each texture unit;
278 std::vector<scoped_refptr<Sampler>> sampler_units;
279
zmo6c468ba2016-05-04 20:00:51280 // We create a transform feedback as the default one per ES3 enabled context
281 // instead of using GL's default one to make context switching easier.
282 // For other context, we will never change the default transform feedback's
283 // states, so we can just use the GL's default one.
284 scoped_refptr<TransformFeedback> default_transform_feedback;
285
286 scoped_refptr<TransformFeedback> bound_transform_feedback;
287
zmo48ee6d3f2016-05-06 20:33:26288 scoped_refptr<IndexedBufferBindingHost> indexed_uniform_buffer_bindings;
289
[email protected]af6380962012-11-29 23:24:13290 // The values for each attrib.
291 std::vector<Vec4> attrib_values;
292
[email protected]e259eb412012-10-13 05:47:24293 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35294 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52295 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24296
297 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35298 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24299
[email protected]e259eb412012-10-13 05:47:24300 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35301 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]8875a5f2014-06-27 08:33:47302 bool bound_renderbuffer_valid;
[email protected]e259eb412012-10-13 05:47:24303
[email protected]e259eb412012-10-13 05:47:24304 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45305 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36306
zmo8ac3bab2015-04-18 02:30:58307 mutable bool fbo_binding_for_scissor_workaround_dirty;
[email protected]d3eba342013-04-18 21:11:50308
309 private:
zmo8ac3bab2015-04-18 02:30:58310 void EnableDisable(GLenum pname, bool enable) const;
311
zmocdfe65d2015-12-02 17:35:56312 // If a buffer object is bound to PIXEL_PACK_BUFFER, set all pack parameters
313 // user values; otherwise, set them to 0.
314 void UpdatePackParameters() const;
315 // If a buffer object is bound to PIXEL_UNPACK_BUFFER, set all unpack
316 // parameters user values; otherwise, set them to 0.
317 void UpdateUnpackParameters() const;
318
zmob730f32b2016-01-06 20:39:08319 void InitStateManual(const ContextState* prev_state) const;
320
zmo8ac3bab2015-04-18 02:30:58321 FeatureInfo* feature_info_;
mostynb6682b1c42016-04-19 10:17:30322 std::unique_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24323};
324
325} // namespace gles2
326} // namespace gpu
327
328#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
329