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