blob: 5a58916e93e8701864e8a8d210773499de42dd9a [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
[email protected]1868a342012-11-07 15:56:0210#include <vector>
[email protected]e259eb412012-10-13 05:47:2411#include "base/logging.h"
[email protected]d3eba342013-04-18 21:11:5012#include "base/memory/scoped_ptr.h"
[email protected]e259eb412012-10-13 05:47:2413#include "gpu/command_buffer/service/gl_utils.h"
bajones5141d032015-12-07 21:13:3914#include "gpu/command_buffer/service/sampler_manager.h"
[email protected]e259eb412012-10-13 05:47:2415#include "gpu/command_buffer/service/texture_manager.h"
orglofchcad5a6742014-11-07 19:51:1216#include "gpu/command_buffer/service/valuebuffer_manager.h"
[email protected]e259eb412012-10-13 05:47:2417#include "gpu/command_buffer/service/vertex_attrib_manager.h"
18#include "gpu/command_buffer/service/vertex_array_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;
[email protected]b3cbad12012-12-05 19:56:3632
[email protected]e259eb412012-10-13 05:47:2433// State associated with each texture unit.
[email protected]88a61bf2012-10-27 13:00:4234struct GPU_EXPORT TextureUnit {
[email protected]e259eb412012-10-13 05:47:2435 TextureUnit();
36 ~TextureUnit();
37
38 // The last target that was bound to this texture unit.
39 GLenum bind_target;
40
41 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
[email protected]370eaf12013-05-18 09:19:4942 scoped_refptr<TextureRef> bound_texture_2d;
[email protected]e259eb412012-10-13 05:47:2443
44 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
45 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4946 scoped_refptr<TextureRef> bound_texture_cube_map;
[email protected]e259eb412012-10-13 05:47:2447
48 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
49 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4950 scoped_refptr<TextureRef> bound_texture_external_oes;
[email protected]e259eb412012-10-13 05:47:2451
52 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
53 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4954 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
[email protected]e259eb412012-10-13 05:47:2455
zmoea06a6f2015-04-30 01:15:4656 // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture
57 scoped_refptr<TextureRef> bound_texture_3d;
58
59 // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with
60 // glBindTexture
61 scoped_refptr<TextureRef> bound_texture_2d_array;
62
[email protected]370eaf12013-05-18 09:19:4963 scoped_refptr<TextureRef> GetInfoForSamplerType(
[email protected]ed9f9cd2013-02-27 21:12:3564 GLenum type) {
[email protected]e259eb412012-10-13 05:47:2465 switch (type) {
66 case GL_SAMPLER_2D:
67 return bound_texture_2d;
68 case GL_SAMPLER_CUBE:
69 return bound_texture_cube_map;
70 case GL_SAMPLER_EXTERNAL_OES:
71 return bound_texture_external_oes;
72 case GL_SAMPLER_2D_RECT_ARB:
73 return bound_texture_rectangle_arb;
zmoea06a6f2015-04-30 01:15:4674 case GL_SAMPLER_3D:
75 return bound_texture_3d;
76 case GL_SAMPLER_2D_ARRAY:
77 return bound_texture_2d_array;
[email protected]e259eb412012-10-13 05:47:2478 }
79
80 NOTREACHED();
81 return NULL;
82 }
erikchenffe273b2015-12-17 03:48:1383
84 scoped_refptr<TextureRef>& GetInfoForTarget(GLenum target) {
85 switch (target) {
86 case GL_TEXTURE_2D:
87 return bound_texture_2d;
88 case GL_TEXTURE_CUBE_MAP:
89 return bound_texture_cube_map;
90 case GL_TEXTURE_EXTERNAL_OES:
91 return bound_texture_external_oes;
92 case GL_TEXTURE_RECTANGLE_ARB:
93 return bound_texture_rectangle_arb;
94 case GL_TEXTURE_3D:
95 return bound_texture_3d;
96 case GL_TEXTURE_2D_ARRAY:
97 return bound_texture_2d_array;
98 }
99
100 NOTREACHED();
101 return bound_texture_2d;
102 }
[email protected]e259eb412012-10-13 05:47:24103};
104
zmo5ee097e2015-05-14 19:13:52105class GPU_EXPORT Vec4 {
106 public:
107 enum DataType {
108 kFloat,
109 kInt,
110 kUInt,
111 };
112
[email protected]af6380962012-11-29 23:24:13113 Vec4() {
zmo5ee097e2015-05-14 19:13:52114 v_[0].float_value = 0.0f;
115 v_[1].float_value = 0.0f;
116 v_[2].float_value = 0.0f;
117 v_[3].float_value = 1.0f;
118 type_ = kFloat;
[email protected]af6380962012-11-29 23:24:13119 }
zmo5ee097e2015-05-14 19:13:52120
121 template <typename T>
122 void GetValues(T* values) const;
123
124 template <typename T>
125 void SetValues(const T* values);
126
127 DataType type() const {
128 return type_;
129 }
130
131 bool Equal(const Vec4& other) const;
132
133 private:
134 union ValueUnion {
135 GLfloat float_value;
136 GLint int_value;
137 GLuint uint_value;
138 };
139
140 ValueUnion v_[4];
141 DataType type_;
[email protected]af6380962012-11-29 23:24:13142};
[email protected]e259eb412012-10-13 05:47:24143
zmo5ee097e2015-05-14 19:13:52144template <>
145GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const;
146template <>
147GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const;
148template <>
149GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const;
150
151template <>
152GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values);
153template <>
154GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values);
155template <>
156GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values);
157
[email protected]88a61bf2012-10-27 13:00:42158struct GPU_EXPORT ContextState {
zmo2b9c47392015-12-11 02:21:32159 enum Dimension {
160 k2D,
161 k3D
162 };
163
[email protected]828a3932014-04-02 14:43:13164 ContextState(FeatureInfo* feature_info,
165 ErrorStateClient* error_state_client,
166 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24167 ~ContextState();
168
[email protected]f731b9462012-10-30 00:35:22169 void Initialize();
170
[email protected]454157e2014-05-03 02:49:45171 void SetIgnoreCachedStateForTest(bool ignore) {
172 ignore_cached_state = ignore;
173 }
174
[email protected]8875a5f2014-06-27 08:33:47175 void RestoreState(const ContextState* prev_state);
[email protected]88ba52f2014-04-09 12:39:34176 void InitCapabilities(const ContextState* prev_state) const;
177 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22178
[email protected]29a4d902013-02-26 20:18:06179 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16180 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27181 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52182 void RestoreVertexAttribValues() const;
183 void RestoreVertexAttribArrays(
184 const scoped_refptr<VertexAttribManager> attrib_manager) const;
185 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06186 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34187 void RestoreGlobalState(const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06188 void RestoreProgramBindings() const;
[email protected]8875a5f2014-06-27 08:33:47189 void RestoreRenderbufferBindings();
[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
[email protected]d058bca2012-11-26 10:27:26193 // Helper for getting cached state.
194 bool GetStateAsGLint(
195 GLenum pname, GLint* params, GLsizei* num_written) const;
196 bool GetStateAsGLfloat(
197 GLenum pname, GLfloat* params, GLsizei* num_written) const;
198 bool GetEnabled(GLenum cap) const;
199
[email protected]454157e2014-05-03 02:49:45200 inline void SetDeviceColorMask(GLboolean red,
201 GLboolean green,
202 GLboolean blue,
203 GLboolean alpha) {
204 if (cached_color_mask_red == red && cached_color_mask_green == green &&
205 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
206 !ignore_cached_state)
207 return;
208 cached_color_mask_red = red;
209 cached_color_mask_green = green;
210 cached_color_mask_blue = blue;
211 cached_color_mask_alpha = alpha;
212 glColorMask(red, green, blue, alpha);
213 }
214
215 inline void SetDeviceDepthMask(GLboolean mask) {
216 if (cached_depth_mask == mask && !ignore_cached_state)
217 return;
218 cached_depth_mask = mask;
219 glDepthMask(mask);
220 }
221
222 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
223 if (op == GL_FRONT) {
224 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
225 return;
226 cached_stencil_front_writemask = mask;
227 } else if (op == GL_BACK) {
228 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
229 return;
230 cached_stencil_back_writemask = mask;
231 } else {
232 NOTREACHED();
233 return;
234 }
235 glStencilMaskSeparate(op, mask);
236 }
237
[email protected]d3eba342013-04-18 21:11:50238 ErrorState* GetErrorState();
239
zmo4c0c3532015-05-22 20:04:48240 void SetBoundBuffer(GLenum target, Buffer* buffer);
241 void RemoveBoundBuffer(Buffer* buffer);
242
bajones2b98b2a2015-09-15 02:27:36243 void UnbindTexture(TextureRef* texture);
bajones5141d032015-12-07 21:13:39244 void UnbindSampler(Sampler* sampler);
bajones2b98b2a2015-09-15 02:27:36245
zmo2b9c47392015-12-11 02:21:32246 PixelStoreParams GetPackParams();
247 PixelStoreParams GetUnpackParams(Dimension dimension);
248
[email protected]f731b9462012-10-30 00:35:22249 #include "gpu/command_buffer/service/context_state_autogen.h"
250
251 EnableFlags enable_flags;
252
[email protected]e259eb412012-10-13 05:47:24253 // Current active texture by 0 - n index.
254 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
255 // be 2.
256 GLuint active_texture_unit;
257
[email protected]e259eb412012-10-13 05:47:24258 // The currently bound array buffer. If this is 0 it is illegal to call
259 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21260 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24261
zmo4c0c3532015-05-22 20:04:48262 scoped_refptr<Buffer> bound_copy_read_buffer;
263 scoped_refptr<Buffer> bound_copy_write_buffer;
264 scoped_refptr<Buffer> bound_pixel_pack_buffer;
265 scoped_refptr<Buffer> bound_pixel_unpack_buffer;
266 scoped_refptr<Buffer> bound_transform_feedback_buffer;
267 scoped_refptr<Buffer> bound_uniform_buffer;
268
[email protected]e259eb412012-10-13 05:47:24269 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02270 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24271
bajones5141d032015-12-07 21:13:39272 // Which samplers are bound to each texture unit;
273 std::vector<scoped_refptr<Sampler>> sampler_units;
274
[email protected]af6380962012-11-29 23:24:13275 // The values for each attrib.
276 std::vector<Vec4> attrib_values;
277
[email protected]e259eb412012-10-13 05:47:24278 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35279 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52280 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24281
282 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35283 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24284
[email protected]e259eb412012-10-13 05:47:24285 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35286 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]8875a5f2014-06-27 08:33:47287 bool bound_renderbuffer_valid;
[email protected]e259eb412012-10-13 05:47:24288
orglofchcad5a6742014-11-07 19:51:12289 // The currently bound valuebuffer
290 scoped_refptr<Valuebuffer> bound_valuebuffer;
291
[email protected]e259eb412012-10-13 05:47:24292 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45293 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36294
zmo8ac3bab2015-04-18 02:30:58295 mutable bool fbo_binding_for_scissor_workaround_dirty;
[email protected]d3eba342013-04-18 21:11:50296
297 private:
zmo8ac3bab2015-04-18 02:30:58298 void EnableDisable(GLenum pname, bool enable) const;
299
zmocdfe65d2015-12-02 17:35:56300 // If a buffer object is bound to PIXEL_PACK_BUFFER, set all pack parameters
301 // user values; otherwise, set them to 0.
302 void UpdatePackParameters() const;
303 // If a buffer object is bound to PIXEL_UNPACK_BUFFER, set all unpack
304 // parameters user values; otherwise, set them to 0.
305 void UpdateUnpackParameters() const;
306
zmob730f32b2016-01-06 20:39:08307 void InitStateManual(const ContextState* prev_state) const;
308
zmo8ac3bab2015-04-18 02:30:58309 FeatureInfo* feature_info_;
[email protected]d3eba342013-04-18 21:11:50310 scoped_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24311};
312
313} // namespace gles2
314} // namespace gpu
315
316#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
317