blob: 3a44730c40b37e49128c4a1692aac8dc3052fc90 [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;
dyenb245d372015-07-25 01:18:3829class Logger;
[email protected]31494b82013-02-28 10:10:2630class Program;
31class Renderbuffer;
zmo6c468ba2016-05-04 20:00:5132class TransformFeedback;
[email protected]b3cbad12012-12-05 19:56:3633
[email protected]e259eb412012-10-13 05:47:2434// State associated with each texture unit.
[email protected]88a61bf2012-10-27 13:00:4235struct GPU_EXPORT TextureUnit {
[email protected]e259eb412012-10-13 05:47:2436 TextureUnit();
vmpstr3b7b8b22016-03-01 23:00:2037 TextureUnit(const TextureUnit& other);
[email protected]e259eb412012-10-13 05:47:2438 ~TextureUnit();
39
40 // The last target that was bound to this texture unit.
41 GLenum bind_target;
42
43 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
[email protected]370eaf12013-05-18 09:19:4944 scoped_refptr<TextureRef> bound_texture_2d;
[email protected]e259eb412012-10-13 05:47:2445
46 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
47 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4948 scoped_refptr<TextureRef> bound_texture_cube_map;
[email protected]e259eb412012-10-13 05:47:2449
50 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
51 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4952 scoped_refptr<TextureRef> bound_texture_external_oes;
[email protected]e259eb412012-10-13 05:47:2453
54 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
55 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4956 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
[email protected]e259eb412012-10-13 05:47:2457
zmoea06a6f2015-04-30 01:15:4658 // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture
59 scoped_refptr<TextureRef> bound_texture_3d;
60
61 // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with
62 // glBindTexture
63 scoped_refptr<TextureRef> bound_texture_2d_array;
64
[email protected]370eaf12013-05-18 09:19:4965 scoped_refptr<TextureRef> GetInfoForSamplerType(
[email protected]ed9f9cd2013-02-27 21:12:3566 GLenum type) {
[email protected]e259eb412012-10-13 05:47:2467 switch (type) {
68 case GL_SAMPLER_2D:
69 return bound_texture_2d;
70 case GL_SAMPLER_CUBE:
71 return bound_texture_cube_map;
72 case GL_SAMPLER_EXTERNAL_OES:
73 return bound_texture_external_oes;
74 case GL_SAMPLER_2D_RECT_ARB:
75 return bound_texture_rectangle_arb;
zmoea06a6f2015-04-30 01:15:4676 case GL_SAMPLER_3D:
77 return bound_texture_3d;
78 case GL_SAMPLER_2D_ARRAY:
79 return bound_texture_2d_array;
[email protected]e259eb412012-10-13 05:47:2480 }
81
82 NOTREACHED();
83 return NULL;
84 }
erikchenffe273b2015-12-17 03:48:1385
86 scoped_refptr<TextureRef>& GetInfoForTarget(GLenum target) {
87 switch (target) {
88 case GL_TEXTURE_2D:
89 return bound_texture_2d;
90 case GL_TEXTURE_CUBE_MAP:
91 return bound_texture_cube_map;
92 case GL_TEXTURE_EXTERNAL_OES:
93 return bound_texture_external_oes;
94 case GL_TEXTURE_RECTANGLE_ARB:
95 return bound_texture_rectangle_arb;
96 case GL_TEXTURE_3D:
97 return bound_texture_3d;
98 case GL_TEXTURE_2D_ARRAY:
99 return bound_texture_2d_array;
100 }
101
102 NOTREACHED();
103 return bound_texture_2d;
104 }
[email protected]e259eb412012-10-13 05:47:24105};
106
zmo5ee097e2015-05-14 19:13:52107class GPU_EXPORT Vec4 {
108 public:
109 enum DataType {
110 kFloat,
111 kInt,
112 kUInt,
113 };
114
[email protected]af6380962012-11-29 23:24:13115 Vec4() {
zmo5ee097e2015-05-14 19:13:52116 v_[0].float_value = 0.0f;
117 v_[1].float_value = 0.0f;
118 v_[2].float_value = 0.0f;
119 v_[3].float_value = 1.0f;
120 type_ = kFloat;
[email protected]af6380962012-11-29 23:24:13121 }
zmo5ee097e2015-05-14 19:13:52122
123 template <typename T>
124 void GetValues(T* values) const;
125
126 template <typename T>
127 void SetValues(const T* values);
128
129 DataType type() const {
130 return type_;
131 }
132
133 bool Equal(const Vec4& other) const;
134
135 private:
136 union ValueUnion {
137 GLfloat float_value;
138 GLint int_value;
139 GLuint uint_value;
140 };
141
142 ValueUnion v_[4];
143 DataType type_;
[email protected]af6380962012-11-29 23:24:13144};
[email protected]e259eb412012-10-13 05:47:24145
zmo5ee097e2015-05-14 19:13:52146template <>
147GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const;
148template <>
149GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const;
150template <>
151GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const;
152
153template <>
154GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values);
155template <>
156GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values);
157template <>
158GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values);
159
[email protected]88a61bf2012-10-27 13:00:42160struct GPU_EXPORT ContextState {
zmo2b9c47392015-12-11 02:21:32161 enum Dimension {
162 k2D,
163 k3D
164 };
165
[email protected]828a3932014-04-02 14:43:13166 ContextState(FeatureInfo* feature_info,
167 ErrorStateClient* error_state_client,
168 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24169 ~ContextState();
170
[email protected]f731b9462012-10-30 00:35:22171 void Initialize();
172
[email protected]454157e2014-05-03 02:49:45173 void SetIgnoreCachedStateForTest(bool ignore) {
174 ignore_cached_state = ignore;
175 }
176
[email protected]8875a5f2014-06-27 08:33:47177 void RestoreState(const ContextState* prev_state);
[email protected]88ba52f2014-04-09 12:39:34178 void InitCapabilities(const ContextState* prev_state) const;
179 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22180
[email protected]29a4d902013-02-26 20:18:06181 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16182 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27183 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52184 void RestoreVertexAttribValues() const;
185 void RestoreVertexAttribArrays(
186 const scoped_refptr<VertexAttribManager> attrib_manager) const;
187 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06188 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34189 void RestoreGlobalState(const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06190 void RestoreProgramBindings() const;
[email protected]8875a5f2014-06-27 08:33:47191 void RestoreRenderbufferBindings();
zmo6c468ba2016-05-04 20:00:51192 void RestoreTransformFeedbackBindings(const ContextState* prev_state);
[email protected]5baa86bc2014-01-16 04:33:16193 void RestoreTextureUnitBindings(
194 GLuint unit, const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06195
[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
bajones2b98b2a2015-09-15 02:27:36246 void UnbindTexture(TextureRef* texture);
bajones5141d032015-12-07 21:13:39247 void UnbindSampler(Sampler* sampler);
bajones2b98b2a2015-09-15 02:27:36248
zmo2b9c47392015-12-11 02:21:32249 PixelStoreParams GetPackParams();
250 PixelStoreParams GetUnpackParams(Dimension dimension);
251
[email protected]f731b9462012-10-30 00:35:22252 #include "gpu/command_buffer/service/context_state_autogen.h"
253
254 EnableFlags enable_flags;
255
[email protected]e259eb412012-10-13 05:47:24256 // Current active texture by 0 - n index.
257 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
258 // be 2.
259 GLuint active_texture_unit;
260
[email protected]e259eb412012-10-13 05:47:24261 // The currently bound array buffer. If this is 0 it is illegal to call
262 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21263 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24264
zmo4c0c3532015-05-22 20:04:48265 scoped_refptr<Buffer> bound_copy_read_buffer;
266 scoped_refptr<Buffer> bound_copy_write_buffer;
267 scoped_refptr<Buffer> bound_pixel_pack_buffer;
268 scoped_refptr<Buffer> bound_pixel_unpack_buffer;
269 scoped_refptr<Buffer> bound_transform_feedback_buffer;
270 scoped_refptr<Buffer> bound_uniform_buffer;
271
[email protected]e259eb412012-10-13 05:47:24272 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02273 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24274
bajones5141d032015-12-07 21:13:39275 // Which samplers are bound to each texture unit;
276 std::vector<scoped_refptr<Sampler>> sampler_units;
277
zmo6c468ba2016-05-04 20:00:51278 // We create a transform feedback as the default one per ES3 enabled context
279 // instead of using GL's default one to make context switching easier.
280 // For other context, we will never change the default transform feedback's
281 // states, so we can just use the GL's default one.
282 scoped_refptr<TransformFeedback> default_transform_feedback;
283
284 scoped_refptr<TransformFeedback> bound_transform_feedback;
285
[email protected]af6380962012-11-29 23:24:13286 // The values for each attrib.
287 std::vector<Vec4> attrib_values;
288
[email protected]e259eb412012-10-13 05:47:24289 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35290 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52291 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24292
293 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35294 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24295
[email protected]e259eb412012-10-13 05:47:24296 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35297 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]8875a5f2014-06-27 08:33:47298 bool bound_renderbuffer_valid;
[email protected]e259eb412012-10-13 05:47:24299
[email protected]e259eb412012-10-13 05:47:24300 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45301 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36302
zmo8ac3bab2015-04-18 02:30:58303 mutable bool fbo_binding_for_scissor_workaround_dirty;
[email protected]d3eba342013-04-18 21:11:50304
305 private:
zmo8ac3bab2015-04-18 02:30:58306 void EnableDisable(GLenum pname, bool enable) const;
307
zmocdfe65d2015-12-02 17:35:56308 // If a buffer object is bound to PIXEL_PACK_BUFFER, set all pack parameters
309 // user values; otherwise, set them to 0.
310 void UpdatePackParameters() const;
311 // If a buffer object is bound to PIXEL_UNPACK_BUFFER, set all unpack
312 // parameters user values; otherwise, set them to 0.
313 void UpdateUnpackParameters() const;
314
zmob730f32b2016-01-06 20:39:08315 void InitStateManual(const ContextState* prev_state) const;
316
zmo8ac3bab2015-04-18 02:30:58317 FeatureInfo* feature_info_;
mostynb6682b1c42016-04-19 10:17:30318 std::unique_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24319};
320
321} // namespace gles2
322} // namespace gpu
323
324#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
325