blob: 5f154c28a73eb90bcec864f3cee7399585da8f1e [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 DCHECK(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ||
66 type == GL_SAMPLER_EXTERNAL_OES || type == GL_SAMPLER_2D_RECT_ARB);
67 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 }
[email protected]e259eb412012-10-13 05:47:2485};
86
zmo5ee097e2015-05-14 19:13:5287class GPU_EXPORT Vec4 {
88 public:
89 enum DataType {
90 kFloat,
91 kInt,
92 kUInt,
93 };
94
[email protected]af6380962012-11-29 23:24:1395 Vec4() {
zmo5ee097e2015-05-14 19:13:5296 v_[0].float_value = 0.0f;
97 v_[1].float_value = 0.0f;
98 v_[2].float_value = 0.0f;
99 v_[3].float_value = 1.0f;
100 type_ = kFloat;
[email protected]af6380962012-11-29 23:24:13101 }
zmo5ee097e2015-05-14 19:13:52102
103 template <typename T>
104 void GetValues(T* values) const;
105
106 template <typename T>
107 void SetValues(const T* values);
108
109 DataType type() const {
110 return type_;
111 }
112
113 bool Equal(const Vec4& other) const;
114
115 private:
116 union ValueUnion {
117 GLfloat float_value;
118 GLint int_value;
119 GLuint uint_value;
120 };
121
122 ValueUnion v_[4];
123 DataType type_;
[email protected]af6380962012-11-29 23:24:13124};
[email protected]e259eb412012-10-13 05:47:24125
zmo5ee097e2015-05-14 19:13:52126template <>
127GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const;
128template <>
129GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const;
130template <>
131GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const;
132
133template <>
134GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values);
135template <>
136GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values);
137template <>
138GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values);
139
[email protected]88a61bf2012-10-27 13:00:42140struct GPU_EXPORT ContextState {
[email protected]828a3932014-04-02 14:43:13141 ContextState(FeatureInfo* feature_info,
142 ErrorStateClient* error_state_client,
143 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24144 ~ContextState();
145
[email protected]f731b9462012-10-30 00:35:22146 void Initialize();
147
[email protected]454157e2014-05-03 02:49:45148 void SetIgnoreCachedStateForTest(bool ignore) {
149 ignore_cached_state = ignore;
150 }
151
[email protected]8875a5f2014-06-27 08:33:47152 void RestoreState(const ContextState* prev_state);
[email protected]88ba52f2014-04-09 12:39:34153 void InitCapabilities(const ContextState* prev_state) const;
154 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22155
[email protected]29a4d902013-02-26 20:18:06156 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16157 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27158 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52159 void RestoreVertexAttribValues() const;
160 void RestoreVertexAttribArrays(
161 const scoped_refptr<VertexAttribManager> attrib_manager) const;
162 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06163 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34164 void RestoreGlobalState(const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06165 void RestoreProgramBindings() const;
[email protected]8875a5f2014-06-27 08:33:47166 void RestoreRenderbufferBindings();
[email protected]5baa86bc2014-01-16 04:33:16167 void RestoreTextureUnitBindings(
168 GLuint unit, const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06169
[email protected]d058bca2012-11-26 10:27:26170 // Helper for getting cached state.
171 bool GetStateAsGLint(
172 GLenum pname, GLint* params, GLsizei* num_written) const;
173 bool GetStateAsGLfloat(
174 GLenum pname, GLfloat* params, GLsizei* num_written) const;
175 bool GetEnabled(GLenum cap) const;
176
[email protected]454157e2014-05-03 02:49:45177 inline void SetDeviceColorMask(GLboolean red,
178 GLboolean green,
179 GLboolean blue,
180 GLboolean alpha) {
181 if (cached_color_mask_red == red && cached_color_mask_green == green &&
182 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
183 !ignore_cached_state)
184 return;
185 cached_color_mask_red = red;
186 cached_color_mask_green = green;
187 cached_color_mask_blue = blue;
188 cached_color_mask_alpha = alpha;
189 glColorMask(red, green, blue, alpha);
190 }
191
192 inline void SetDeviceDepthMask(GLboolean mask) {
193 if (cached_depth_mask == mask && !ignore_cached_state)
194 return;
195 cached_depth_mask = mask;
196 glDepthMask(mask);
197 }
198
199 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
200 if (op == GL_FRONT) {
201 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
202 return;
203 cached_stencil_front_writemask = mask;
204 } else if (op == GL_BACK) {
205 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
206 return;
207 cached_stencil_back_writemask = mask;
208 } else {
209 NOTREACHED();
210 return;
211 }
212 glStencilMaskSeparate(op, mask);
213 }
214
[email protected]d3eba342013-04-18 21:11:50215 ErrorState* GetErrorState();
216
zmo4c0c3532015-05-22 20:04:48217 void SetBoundBuffer(GLenum target, Buffer* buffer);
218 void RemoveBoundBuffer(Buffer* buffer);
219
bajones2b98b2a2015-09-15 02:27:36220 void UnbindTexture(TextureRef* texture);
bajones5141d032015-12-07 21:13:39221 void UnbindSampler(Sampler* sampler);
bajones2b98b2a2015-09-15 02:27:36222
[email protected]f731b9462012-10-30 00:35:22223 #include "gpu/command_buffer/service/context_state_autogen.h"
224
225 EnableFlags enable_flags;
226
[email protected]e259eb412012-10-13 05:47:24227 // Current active texture by 0 - n index.
228 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
229 // be 2.
230 GLuint active_texture_unit;
231
[email protected]e259eb412012-10-13 05:47:24232 // The currently bound array buffer. If this is 0 it is illegal to call
233 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21234 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24235
zmo4c0c3532015-05-22 20:04:48236 scoped_refptr<Buffer> bound_copy_read_buffer;
237 scoped_refptr<Buffer> bound_copy_write_buffer;
238 scoped_refptr<Buffer> bound_pixel_pack_buffer;
239 scoped_refptr<Buffer> bound_pixel_unpack_buffer;
240 scoped_refptr<Buffer> bound_transform_feedback_buffer;
241 scoped_refptr<Buffer> bound_uniform_buffer;
242
[email protected]e259eb412012-10-13 05:47:24243 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02244 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24245
bajones5141d032015-12-07 21:13:39246 // Which samplers are bound to each texture unit;
247 std::vector<scoped_refptr<Sampler>> sampler_units;
248
[email protected]af6380962012-11-29 23:24:13249 // The values for each attrib.
250 std::vector<Vec4> attrib_values;
251
[email protected]e259eb412012-10-13 05:47:24252 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35253 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52254 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24255
256 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35257 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24258
[email protected]e259eb412012-10-13 05:47:24259 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35260 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]8875a5f2014-06-27 08:33:47261 bool bound_renderbuffer_valid;
[email protected]e259eb412012-10-13 05:47:24262
orglofchcad5a6742014-11-07 19:51:12263 // The currently bound valuebuffer
264 scoped_refptr<Valuebuffer> bound_valuebuffer;
265
[email protected]e259eb412012-10-13 05:47:24266 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45267 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36268
zmo8ac3bab2015-04-18 02:30:58269 mutable bool fbo_binding_for_scissor_workaround_dirty;
[email protected]d3eba342013-04-18 21:11:50270
271 private:
zmo8ac3bab2015-04-18 02:30:58272 void EnableDisable(GLenum pname, bool enable) const;
273
zmocdfe65d2015-12-02 17:35:56274 // If a buffer object is bound to PIXEL_PACK_BUFFER, set all pack parameters
275 // user values; otherwise, set them to 0.
276 void UpdatePackParameters() const;
277 // If a buffer object is bound to PIXEL_UNPACK_BUFFER, set all unpack
278 // parameters user values; otherwise, set them to 0.
279 void UpdateUnpackParameters() const;
280
zmo8ac3bab2015-04-18 02:30:58281 FeatureInfo* feature_info_;
[email protected]d3eba342013-04-18 21:11:50282 scoped_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24283};
284
285} // namespace gles2
286} // namespace gpu
287
288#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
289