[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 1 | // 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] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 10 | #include <vector> |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 13 | #include "gpu/command_buffer/service/gl_utils.h" |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 14 | #include "gpu/command_buffer/service/query_manager.h" |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 15 | #include "gpu/command_buffer/service/texture_manager.h" |
orglofch | cad5a674 | 2014-11-07 19:51:12 | [diff] [blame] | 16 | #include "gpu/command_buffer/service/valuebuffer_manager.h" |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 17 | #include "gpu/command_buffer/service/vertex_attrib_manager.h" |
| 18 | #include "gpu/command_buffer/service/vertex_array_manager.h" |
[email protected] | 88a61bf | 2012-10-27 13:00:42 | [diff] [blame] | 19 | #include "gpu/gpu_export.h" |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 20 | |
| 21 | namespace gpu { |
| 22 | namespace gles2 { |
| 23 | |
[email protected] | 31494b8 | 2013-02-28 10:10:26 | [diff] [blame] | 24 | class Buffer; |
[email protected] | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 25 | class ErrorState; |
[email protected] | 828a393 | 2014-04-02 14:43:13 | [diff] [blame] | 26 | class ErrorStateClient; |
[email protected] | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 27 | class FeatureInfo; |
[email protected] | 31494b8 | 2013-02-28 10:10:26 | [diff] [blame] | 28 | class Framebuffer; |
| 29 | class Program; |
| 30 | class Renderbuffer; |
[email protected] | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 31 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 32 | // State associated with each texture unit. |
[email protected] | 88a61bf | 2012-10-27 13:00:42 | [diff] [blame] | 33 | struct GPU_EXPORT TextureUnit { |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 34 | TextureUnit(); |
| 35 | ~TextureUnit(); |
| 36 | |
| 37 | // The last target that was bound to this texture unit. |
| 38 | GLenum bind_target; |
| 39 | |
| 40 | // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture |
[email protected] | 370eaf1 | 2013-05-18 09:19:49 | [diff] [blame] | 41 | scoped_refptr<TextureRef> bound_texture_2d; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 42 | |
| 43 | // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with |
| 44 | // glBindTexture |
[email protected] | 370eaf1 | 2013-05-18 09:19:49 | [diff] [blame] | 45 | scoped_refptr<TextureRef> bound_texture_cube_map; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 46 | |
| 47 | // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with |
| 48 | // glBindTexture |
[email protected] | 370eaf1 | 2013-05-18 09:19:49 | [diff] [blame] | 49 | scoped_refptr<TextureRef> bound_texture_external_oes; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 50 | |
| 51 | // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with |
| 52 | // glBindTexture |
[email protected] | 370eaf1 | 2013-05-18 09:19:49 | [diff] [blame] | 53 | scoped_refptr<TextureRef> bound_texture_rectangle_arb; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 54 | |
zmo | ea06a6f | 2015-04-30 01:15:46 | [diff] [blame] | 55 | // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture |
| 56 | scoped_refptr<TextureRef> bound_texture_3d; |
| 57 | |
| 58 | // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with |
| 59 | // glBindTexture |
| 60 | scoped_refptr<TextureRef> bound_texture_2d_array; |
| 61 | |
[email protected] | 370eaf1 | 2013-05-18 09:19:49 | [diff] [blame] | 62 | scoped_refptr<TextureRef> GetInfoForSamplerType( |
[email protected] | ed9f9cd | 2013-02-27 21:12:35 | [diff] [blame] | 63 | GLenum type) { |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 64 | DCHECK(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || |
| 65 | type == GL_SAMPLER_EXTERNAL_OES || type == GL_SAMPLER_2D_RECT_ARB); |
| 66 | switch (type) { |
| 67 | case GL_SAMPLER_2D: |
| 68 | return bound_texture_2d; |
| 69 | case GL_SAMPLER_CUBE: |
| 70 | return bound_texture_cube_map; |
| 71 | case GL_SAMPLER_EXTERNAL_OES: |
| 72 | return bound_texture_external_oes; |
| 73 | case GL_SAMPLER_2D_RECT_ARB: |
| 74 | return bound_texture_rectangle_arb; |
zmo | ea06a6f | 2015-04-30 01:15:46 | [diff] [blame] | 75 | case GL_SAMPLER_3D: |
| 76 | return bound_texture_3d; |
| 77 | case GL_SAMPLER_2D_ARRAY: |
| 78 | return bound_texture_2d_array; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | NOTREACHED(); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
[email protected] | 370eaf1 | 2013-05-18 09:19:49 | [diff] [blame] | 85 | void Unbind(TextureRef* texture) { |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 86 | if (bound_texture_2d.get() == texture) { |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 87 | bound_texture_2d = NULL; |
| 88 | } |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 89 | if (bound_texture_cube_map.get() == texture) { |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 90 | bound_texture_cube_map = NULL; |
| 91 | } |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 92 | if (bound_texture_external_oes.get() == texture) { |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 93 | bound_texture_external_oes = NULL; |
| 94 | } |
zmo | ea06a6f | 2015-04-30 01:15:46 | [diff] [blame] | 95 | if (bound_texture_3d.get() == texture) { |
| 96 | bound_texture_3d = NULL; |
| 97 | } |
| 98 | if (bound_texture_2d_array.get() == texture) { |
| 99 | bound_texture_2d_array = NULL; |
| 100 | } |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
zmo | 5ee097e | 2015-05-14 19:13:52 | [diff] [blame] | 104 | class GPU_EXPORT Vec4 { |
| 105 | public: |
| 106 | enum DataType { |
| 107 | kFloat, |
| 108 | kInt, |
| 109 | kUInt, |
| 110 | }; |
| 111 | |
[email protected] | af638096 | 2012-11-29 23:24:13 | [diff] [blame] | 112 | Vec4() { |
zmo | 5ee097e | 2015-05-14 19:13:52 | [diff] [blame] | 113 | v_[0].float_value = 0.0f; |
| 114 | v_[1].float_value = 0.0f; |
| 115 | v_[2].float_value = 0.0f; |
| 116 | v_[3].float_value = 1.0f; |
| 117 | type_ = kFloat; |
[email protected] | af638096 | 2012-11-29 23:24:13 | [diff] [blame] | 118 | } |
zmo | 5ee097e | 2015-05-14 19:13:52 | [diff] [blame] | 119 | |
| 120 | template <typename T> |
| 121 | void GetValues(T* values) const; |
| 122 | |
| 123 | template <typename T> |
| 124 | void SetValues(const T* values); |
| 125 | |
| 126 | DataType type() const { |
| 127 | return type_; |
| 128 | } |
| 129 | |
| 130 | bool Equal(const Vec4& other) const; |
| 131 | |
| 132 | private: |
| 133 | union ValueUnion { |
| 134 | GLfloat float_value; |
| 135 | GLint int_value; |
| 136 | GLuint uint_value; |
| 137 | }; |
| 138 | |
| 139 | ValueUnion v_[4]; |
| 140 | DataType type_; |
[email protected] | af638096 | 2012-11-29 23:24:13 | [diff] [blame] | 141 | }; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 142 | |
zmo | 5ee097e | 2015-05-14 19:13:52 | [diff] [blame] | 143 | template <> |
| 144 | GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const; |
| 145 | template <> |
| 146 | GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const; |
| 147 | template <> |
| 148 | GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const; |
| 149 | |
| 150 | template <> |
| 151 | GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values); |
| 152 | template <> |
| 153 | GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values); |
| 154 | template <> |
| 155 | GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values); |
| 156 | |
[email protected] | 88a61bf | 2012-10-27 13:00:42 | [diff] [blame] | 157 | struct GPU_EXPORT ContextState { |
[email protected] | 828a393 | 2014-04-02 14:43:13 | [diff] [blame] | 158 | ContextState(FeatureInfo* feature_info, |
| 159 | ErrorStateClient* error_state_client, |
| 160 | Logger* logger); |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 161 | ~ContextState(); |
| 162 | |
[email protected] | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 163 | void Initialize(); |
| 164 | |
[email protected] | 454157e | 2014-05-03 02:49:45 | [diff] [blame] | 165 | void SetIgnoreCachedStateForTest(bool ignore) { |
| 166 | ignore_cached_state = ignore; |
| 167 | } |
| 168 | |
[email protected] | 8875a5f | 2014-06-27 08:33:47 | [diff] [blame] | 169 | void RestoreState(const ContextState* prev_state); |
[email protected] | 88ba52f | 2014-04-09 12:39:34 | [diff] [blame] | 170 | void InitCapabilities(const ContextState* prev_state) const; |
| 171 | void InitState(const ContextState* prev_state) const; |
[email protected] | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 172 | |
[email protected] | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 173 | void RestoreActiveTexture() const; |
[email protected] | 5baa86bc | 2014-01-16 04:33:16 | [diff] [blame] | 174 | void RestoreAllTextureUnitBindings(const ContextState* prev_state) const; |
[email protected] | 4b2d2b26 | 2014-03-21 22:05:27 | [diff] [blame] | 175 | void RestoreActiveTextureUnitBinding(unsigned int target) const; |
[email protected] | 81f20a62 | 2014-04-18 01:54:52 | [diff] [blame] | 176 | void RestoreVertexAttribValues() const; |
| 177 | void RestoreVertexAttribArrays( |
| 178 | const scoped_refptr<VertexAttribManager> attrib_manager) const; |
| 179 | void RestoreVertexAttribs() const; |
[email protected] | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 180 | void RestoreBufferBindings() const; |
[email protected] | 88ba52f | 2014-04-09 12:39:34 | [diff] [blame] | 181 | void RestoreGlobalState(const ContextState* prev_state) const; |
[email protected] | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 182 | void RestoreProgramBindings() const; |
[email protected] | 8875a5f | 2014-06-27 08:33:47 | [diff] [blame] | 183 | void RestoreRenderbufferBindings(); |
[email protected] | 5baa86bc | 2014-01-16 04:33:16 | [diff] [blame] | 184 | void RestoreTextureUnitBindings( |
| 185 | GLuint unit, const ContextState* prev_state) const; |
[email protected] | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 186 | |
[email protected] | d058bca | 2012-11-26 10:27:26 | [diff] [blame] | 187 | // Helper for getting cached state. |
| 188 | bool GetStateAsGLint( |
| 189 | GLenum pname, GLint* params, GLsizei* num_written) const; |
| 190 | bool GetStateAsGLfloat( |
| 191 | GLenum pname, GLfloat* params, GLsizei* num_written) const; |
| 192 | bool GetEnabled(GLenum cap) const; |
| 193 | |
[email protected] | 454157e | 2014-05-03 02:49:45 | [diff] [blame] | 194 | inline void SetDeviceColorMask(GLboolean red, |
| 195 | GLboolean green, |
| 196 | GLboolean blue, |
| 197 | GLboolean alpha) { |
| 198 | if (cached_color_mask_red == red && cached_color_mask_green == green && |
| 199 | cached_color_mask_blue == blue && cached_color_mask_alpha == alpha && |
| 200 | !ignore_cached_state) |
| 201 | return; |
| 202 | cached_color_mask_red = red; |
| 203 | cached_color_mask_green = green; |
| 204 | cached_color_mask_blue = blue; |
| 205 | cached_color_mask_alpha = alpha; |
| 206 | glColorMask(red, green, blue, alpha); |
| 207 | } |
| 208 | |
| 209 | inline void SetDeviceDepthMask(GLboolean mask) { |
| 210 | if (cached_depth_mask == mask && !ignore_cached_state) |
| 211 | return; |
| 212 | cached_depth_mask = mask; |
| 213 | glDepthMask(mask); |
| 214 | } |
| 215 | |
| 216 | inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) { |
| 217 | if (op == GL_FRONT) { |
| 218 | if (cached_stencil_front_writemask == mask && !ignore_cached_state) |
| 219 | return; |
| 220 | cached_stencil_front_writemask = mask; |
| 221 | } else if (op == GL_BACK) { |
| 222 | if (cached_stencil_back_writemask == mask && !ignore_cached_state) |
| 223 | return; |
| 224 | cached_stencil_back_writemask = mask; |
| 225 | } else { |
| 226 | NOTREACHED(); |
| 227 | return; |
| 228 | } |
| 229 | glStencilMaskSeparate(op, mask); |
| 230 | } |
| 231 | |
[email protected] | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 232 | ErrorState* GetErrorState(); |
| 233 | |
zmo | 4c0c353 | 2015-05-22 20:04:48 | [diff] [blame^] | 234 | void SetBoundBuffer(GLenum target, Buffer* buffer); |
| 235 | void RemoveBoundBuffer(Buffer* buffer); |
| 236 | |
[email protected] | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 237 | #include "gpu/command_buffer/service/context_state_autogen.h" |
| 238 | |
| 239 | EnableFlags enable_flags; |
| 240 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 241 | // Current active texture by 0 - n index. |
| 242 | // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would |
| 243 | // be 2. |
| 244 | GLuint active_texture_unit; |
| 245 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 246 | // The currently bound array buffer. If this is 0 it is illegal to call |
| 247 | // glVertexAttribPointer. |
[email protected] | 16ccec1 | 2013-02-28 03:40:21 | [diff] [blame] | 248 | scoped_refptr<Buffer> bound_array_buffer; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 249 | |
zmo | 4c0c353 | 2015-05-22 20:04:48 | [diff] [blame^] | 250 | scoped_refptr<Buffer> bound_copy_read_buffer; |
| 251 | scoped_refptr<Buffer> bound_copy_write_buffer; |
| 252 | scoped_refptr<Buffer> bound_pixel_pack_buffer; |
| 253 | scoped_refptr<Buffer> bound_pixel_unpack_buffer; |
| 254 | scoped_refptr<Buffer> bound_transform_feedback_buffer; |
| 255 | scoped_refptr<Buffer> bound_uniform_buffer; |
| 256 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 257 | // Which textures are bound to texture units through glActiveTexture. |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 258 | std::vector<TextureUnit> texture_units; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 259 | |
[email protected] | af638096 | 2012-11-29 23:24:13 | [diff] [blame] | 260 | // The values for each attrib. |
| 261 | std::vector<Vec4> attrib_values; |
| 262 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 263 | // Class that manages vertex attribs. |
[email protected] | ed9f9cd | 2013-02-27 21:12:35 | [diff] [blame] | 264 | scoped_refptr<VertexAttribManager> vertex_attrib_manager; |
[email protected] | 81f20a62 | 2014-04-18 01:54:52 | [diff] [blame] | 265 | scoped_refptr<VertexAttribManager> default_vertex_attrib_manager; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 266 | |
| 267 | // The program in use by glUseProgram |
[email protected] | ed9f9cd | 2013-02-27 21:12:35 | [diff] [blame] | 268 | scoped_refptr<Program> current_program; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 269 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 270 | // The currently bound renderbuffer |
[email protected] | ed9f9cd | 2013-02-27 21:12:35 | [diff] [blame] | 271 | scoped_refptr<Renderbuffer> bound_renderbuffer; |
[email protected] | 8875a5f | 2014-06-27 08:33:47 | [diff] [blame] | 272 | bool bound_renderbuffer_valid; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 273 | |
orglofch | cad5a674 | 2014-11-07 19:51:12 | [diff] [blame] | 274 | // The currently bound valuebuffer |
| 275 | scoped_refptr<Valuebuffer> bound_valuebuffer; |
| 276 | |
[email protected] | 8ebd46c | 2014-01-08 12:06:13 | [diff] [blame] | 277 | // A map of of target -> Query for current queries |
| 278 | typedef std::map<GLuint, scoped_refptr<QueryManager::Query> > QueryMap; |
| 279 | QueryMap current_queries; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 280 | |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 281 | bool pack_reverse_row_order; |
[email protected] | 454157e | 2014-05-03 02:49:45 | [diff] [blame] | 282 | bool ignore_cached_state; |
[email protected] | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 283 | |
zmo | 8ac3bab | 2015-04-18 02:30:58 | [diff] [blame] | 284 | mutable bool fbo_binding_for_scissor_workaround_dirty; |
[email protected] | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 285 | |
| 286 | private: |
zmo | 8ac3bab | 2015-04-18 02:30:58 | [diff] [blame] | 287 | void EnableDisable(GLenum pname, bool enable) const; |
| 288 | |
| 289 | FeatureInfo* feature_info_; |
[email protected] | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 290 | scoped_ptr<ErrorState> error_state_; |
[email protected] | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | } // namespace gles2 |
| 294 | } // namespace gpu |
| 295 | |
| 296 | #endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_ |
| 297 | |