blob: 85cde3bfab3297f1782f67c74860de1c61d7c6cf [file] [log] [blame]
gman@chromium.orge259eb412012-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#include "gpu/command_buffer/service/context_state.h"
6
avif15d60a2015-12-21 17:06:337#include <stddef.h>
8
kkinnunen337d59632014-08-26 10:19:579#include <cmath>
10
gman@chromium.orgf731b9462012-10-30 00:35:2211#include "gpu/command_buffer/common/gles2_cmd_utils.h"
gman@chromium.org31494b82013-02-28 10:10:2612#include "gpu/command_buffer/service/buffer_manager.h"
kloveless@chromium.orgd3eba342013-04-18 21:11:5013#include "gpu/command_buffer/service/error_state.h"
gman@chromium.org31494b82013-02-28 10:10:2614#include "gpu/command_buffer/service/framebuffer_manager.h"
15#include "gpu/command_buffer/service/program_manager.h"
16#include "gpu/command_buffer/service/renderbuffer_manager.h"
gman@chromium.org1868a342012-11-07 15:56:0217#include "ui/gl/gl_bindings.h"
18#include "ui/gl/gl_implementation.h"
martina.kollarova5511bade2015-08-06 17:34:1419#include "ui/gl/gl_version_info.h"
gman@chromium.orgf731b9462012-10-30 00:35:2220
gman@chromium.orge259eb412012-10-13 05:47:2421namespace gpu {
22namespace gles2 {
23
gman@chromium.orgf731b9462012-10-30 00:35:2224namespace {
25
kaanb@chromium.org5baa86bc2014-01-16 04:33:1626GLuint Get2dServiceId(const TextureUnit& unit) {
27 return unit.bound_texture_2d.get()
28 ? unit.bound_texture_2d->service_id() : 0;
29}
30
31GLuint GetCubeServiceId(const TextureUnit& unit) {
32 return unit.bound_texture_cube_map.get()
33 ? unit.bound_texture_cube_map->service_id() : 0;
34}
35
36GLuint GetOesServiceId(const TextureUnit& unit) {
37 return unit.bound_texture_external_oes.get()
38 ? unit.bound_texture_external_oes->service_id() : 0;
39}
40
41GLuint GetArbServiceId(const TextureUnit& unit) {
42 return unit.bound_texture_rectangle_arb.get()
43 ? unit.bound_texture_rectangle_arb->service_id() : 0;
44}
45
epenner@chromium.org4b2d2b262014-03-21 22:05:2746GLuint GetServiceId(const TextureUnit& unit, GLuint target) {
47 switch (target) {
48 case GL_TEXTURE_2D:
49 return Get2dServiceId(unit);
50 case GL_TEXTURE_CUBE_MAP:
51 return GetCubeServiceId(unit);
52 case GL_TEXTURE_RECTANGLE_ARB:
53 return GetArbServiceId(unit);
54 case GL_TEXTURE_EXTERNAL_OES:
55 return GetOesServiceId(unit);
56 default:
57 NOTREACHED();
58 return 0;
59 }
60}
61
62bool TargetIsSupported(const FeatureInfo* feature_info, GLuint target) {
63 switch (target) {
64 case GL_TEXTURE_2D:
65 return true;
66 case GL_TEXTURE_CUBE_MAP:
67 return true;
68 case GL_TEXTURE_RECTANGLE_ARB:
69 return feature_info->feature_flags().arb_texture_rectangle;
70 case GL_TEXTURE_EXTERNAL_OES:
71 return feature_info->feature_flags().oes_egl_image_external;
72 default:
73 NOTREACHED();
74 return false;
75 }
76}
77
zmo4c0c3532015-05-22 20:04:4878GLuint GetBufferId(const Buffer* buffer) {
79 if (buffer)
80 return buffer->service_id();
81 return 0;
82}
83
gman@chromium.orgf731b9462012-10-30 00:35:2284} // anonymous namespace.
85
gman@chromium.orge259eb412012-10-13 05:47:2486TextureUnit::TextureUnit()
87 : bind_target(GL_TEXTURE_2D) {
88}
89
vmpstr3b7b8b22016-03-01 23:00:2090TextureUnit::TextureUnit(const TextureUnit& other) = default;
91
gman@chromium.orge259eb412012-10-13 05:47:2492TextureUnit::~TextureUnit() {
93}
94
zmo5ee097e2015-05-14 19:13:5295bool Vec4::Equal(const Vec4& other) const {
96 if (type_ != other.type_)
97 return false;
98 switch (type_) {
99 case kFloat:
100 for (size_t ii = 0; ii < 4; ++ii) {
101 if (v_[ii].float_value != other.v_[ii].float_value)
102 return false;
103 }
104 break;
105 case kInt:
106 for (size_t ii = 0; ii < 4; ++ii) {
107 if (v_[ii].int_value != other.v_[ii].int_value)
108 return false;
109 }
110 break;
111 case kUInt:
112 for (size_t ii = 0; ii < 4; ++ii) {
113 if (v_[ii].uint_value != other.v_[ii].uint_value)
114 return false;
115 }
116 break;
117 }
118 return true;
119}
120
121template <>
122void Vec4::GetValues<GLfloat>(GLfloat* values) const {
123 DCHECK(values);
124 switch (type_) {
125 case kFloat:
126 for (size_t ii = 0; ii < 4; ++ii)
127 values[ii] = v_[ii].float_value;
128 break;
129 case kInt:
130 for (size_t ii = 0; ii < 4; ++ii)
131 values[ii] = static_cast<GLfloat>(v_[ii].int_value);
132 break;
133 case kUInt:
134 for (size_t ii = 0; ii < 4; ++ii)
135 values[ii] = static_cast<GLfloat>(v_[ii].uint_value);
136 break;
137 }
138}
139
140template <>
141void Vec4::GetValues<GLint>(GLint* values) const {
142 DCHECK(values);
143 switch (type_) {
144 case kFloat:
145 for (size_t ii = 0; ii < 4; ++ii)
146 values[ii] = static_cast<GLint>(v_[ii].float_value);
147 break;
148 case kInt:
149 for (size_t ii = 0; ii < 4; ++ii)
150 values[ii] = v_[ii].int_value;
151 break;
152 case kUInt:
153 for (size_t ii = 0; ii < 4; ++ii)
154 values[ii] = static_cast<GLint>(v_[ii].uint_value);
155 break;
156 }
157}
158
159template<>
160void Vec4::GetValues<GLuint>(GLuint* values) const {
161 DCHECK(values);
162 switch (type_) {
163 case kFloat:
164 for (size_t ii = 0; ii < 4; ++ii)
165 values[ii] = static_cast<GLuint>(v_[ii].float_value);
166 break;
167 case kInt:
168 for (size_t ii = 0; ii < 4; ++ii)
169 values[ii] = static_cast<GLuint>(v_[ii].int_value);
170 break;
171 case kUInt:
172 for (size_t ii = 0; ii < 4; ++ii)
173 values[ii] = v_[ii].uint_value;
174 break;
175 }
176}
177
178template <>
179void Vec4::SetValues<GLfloat>(const GLfloat* values) {
180 DCHECK(values);
181 for (size_t ii = 0; ii < 4; ++ii)
182 v_[ii].float_value = values[ii];
183 type_ = kFloat;
184}
185
186template <>
187void Vec4::SetValues<GLint>(const GLint* values) {
188 DCHECK(values);
189 for (size_t ii = 0; ii < 4; ++ii)
190 v_[ii].int_value = values[ii];
191 type_ = kInt;
192}
193
194template <>
195void Vec4::SetValues<GLuint>(const GLuint* values) {
196 DCHECK(values);
197 for (size_t ii = 0; ii < 4; ++ii)
198 v_[ii].uint_value = values[ii];
199 type_ = kUInt;
200}
201
danakj@chromium.org828a3932014-04-02 14:43:13202ContextState::ContextState(FeatureInfo* feature_info,
203 ErrorStateClient* error_state_client,
204 Logger* logger)
skyostil@chromium.orgfcf1e7be2013-06-04 17:12:02205 : active_texture_unit(0),
vmiura@chromium.org8875a5f2014-06-27 08:33:47206 bound_renderbuffer_valid(false),
sievers@chromium.orgb3cbad12012-12-05 19:56:36207 pack_reverse_row_order(false),
vmiura@chromium.org454157e2014-05-03 02:49:45208 ignore_cached_state(false),
zmo8ac3bab2015-04-18 02:30:58209 fbo_binding_for_scissor_workaround_dirty(false),
kloveless@chromium.orgd3eba342013-04-18 21:11:50210 feature_info_(feature_info),
danakj@chromium.org828a3932014-04-02 14:43:13211 error_state_(ErrorState::Create(error_state_client, logger)) {
gman@chromium.orgf731b9462012-10-30 00:35:22212 Initialize();
gman@chromium.orge259eb412012-10-13 05:47:24213}
214
215ContextState::~ContextState() {
216}
217
kaanb@chromium.org5baa86bc2014-01-16 04:33:16218void ContextState::RestoreTextureUnitBindings(
219 GLuint unit, const ContextState* prev_state) const {
gman@chromium.org29a4d902013-02-26 20:18:06220 DCHECK_LT(unit, texture_units.size());
221 const TextureUnit& texture_unit = texture_units[unit];
kaanb@chromium.org5baa86bc2014-01-16 04:33:16222 GLuint service_id_2d = Get2dServiceId(texture_unit);
223 GLuint service_id_cube = GetCubeServiceId(texture_unit);
224 GLuint service_id_oes = GetOesServiceId(texture_unit);
225 GLuint service_id_arb = GetArbServiceId(texture_unit);
gman@chromium.org29a4d902013-02-26 20:18:06226
kaanb@chromium.org5baa86bc2014-01-16 04:33:16227 bool bind_texture_2d = true;
228 bool bind_texture_cube = true;
229 bool bind_texture_oes = feature_info_->feature_flags().oes_egl_image_external;
230 bool bind_texture_arb = feature_info_->feature_flags().arb_texture_rectangle;
231
232 if (prev_state) {
233 const TextureUnit& prev_unit = prev_state->texture_units[unit];
234 bind_texture_2d = service_id_2d != Get2dServiceId(prev_unit);
235 bind_texture_cube = service_id_cube != GetCubeServiceId(prev_unit);
236 bind_texture_oes =
237 bind_texture_oes && service_id_oes != GetOesServiceId(prev_unit);
238 bind_texture_arb =
239 bind_texture_arb && service_id_arb != GetArbServiceId(prev_unit);
gman@chromium.org29a4d902013-02-26 20:18:06240 }
241
kaanb@chromium.org5baa86bc2014-01-16 04:33:16242 // Early-out if nothing has changed from the previous state.
243 if (!bind_texture_2d && !bind_texture_cube
244 && !bind_texture_oes && !bind_texture_arb) {
245 return;
246 }
247
248 glActiveTexture(GL_TEXTURE0 + unit);
249 if (bind_texture_2d) {
250 glBindTexture(GL_TEXTURE_2D, service_id_2d);
251 }
252 if (bind_texture_cube) {
253 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id_cube);
254 }
255 if (bind_texture_oes) {
256 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id_oes);
257 }
258 if (bind_texture_arb) {
259 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id_arb);
gman@chromium.org29a4d902013-02-26 20:18:06260 }
261}
262
263void ContextState::RestoreBufferBindings() const {
rsleevi@chromium.org7cd76fd2013-06-02 21:11:11264 if (vertex_attrib_manager.get()) {
gman@chromium.org16ccec12013-02-28 03:40:21265 Buffer* element_array_buffer =
gman@chromium.org29a4d902013-02-26 20:18:06266 vertex_attrib_manager->element_array_buffer();
zmo4c0c3532015-05-22 20:04:48267 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, GetBufferId(element_array_buffer));
gman@chromium.org29a4d902013-02-26 20:18:06268 }
zmo4c0c3532015-05-22 20:04:48269 glBindBuffer(GL_ARRAY_BUFFER, GetBufferId(bound_array_buffer.get()));
bajonesb6964e62015-09-01 18:27:25270 if (feature_info_->IsES3Capable()) {
zmo4c0c3532015-05-22 20:04:48271 glBindBuffer(GL_COPY_READ_BUFFER,
272 GetBufferId(bound_copy_read_buffer.get()));
273 glBindBuffer(GL_COPY_WRITE_BUFFER,
274 GetBufferId(bound_copy_write_buffer.get()));
275 glBindBuffer(GL_PIXEL_PACK_BUFFER,
276 GetBufferId(bound_pixel_pack_buffer.get()));
zmocdfe65d2015-12-02 17:35:56277 UpdatePackParameters();
zmo4c0c3532015-05-22 20:04:48278 glBindBuffer(GL_PIXEL_UNPACK_BUFFER,
279 GetBufferId(bound_pixel_unpack_buffer.get()));
zmocdfe65d2015-12-02 17:35:56280 UpdateUnpackParameters();
zmo4c0c3532015-05-22 20:04:48281 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER,
282 GetBufferId(bound_transform_feedback_buffer.get()));
283 glBindBuffer(GL_UNIFORM_BUFFER, GetBufferId(bound_uniform_buffer.get()));
284 }
gman@chromium.org29a4d902013-02-26 20:18:06285}
286
vmiura@chromium.org8875a5f2014-06-27 08:33:47287void ContextState::RestoreRenderbufferBindings() {
288 // Require Renderbuffer rebind.
289 bound_renderbuffer_valid = false;
gman@chromium.org29a4d902013-02-26 20:18:06290}
291
292void ContextState::RestoreProgramBindings() const {
rsleevi@chromium.org7cd76fd2013-06-02 21:11:11293 glUseProgram(current_program.get() ? current_program->service_id() : 0);
gman@chromium.org29a4d902013-02-26 20:18:06294}
295
296void ContextState::RestoreActiveTexture() const {
297 glActiveTexture(GL_TEXTURE0 + active_texture_unit);
298}
299
kaanb@chromium.org5baa86bc2014-01-16 04:33:16300void ContextState::RestoreAllTextureUnitBindings(
301 const ContextState* prev_state) const {
backer@chromium.org217004512013-05-10 21:25:55302 // Restore Texture state.
303 for (size_t ii = 0; ii < texture_units.size(); ++ii) {
kaanb@chromium.org5baa86bc2014-01-16 04:33:16304 RestoreTextureUnitBindings(ii, prev_state);
backer@chromium.org217004512013-05-10 21:25:55305 }
306 RestoreActiveTexture();
307}
308
epenner@chromium.org4b2d2b262014-03-21 22:05:27309void ContextState::RestoreActiveTextureUnitBinding(unsigned int target) const {
310 DCHECK_LT(active_texture_unit, texture_units.size());
311 const TextureUnit& texture_unit = texture_units[active_texture_unit];
312 if (TargetIsSupported(feature_info_, target))
313 glBindTexture(target, GetServiceId(texture_unit, target));
314}
315
vmiura@chromium.org81f20a622014-04-18 01:54:52316void ContextState::RestoreVertexAttribValues() const {
317 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs();
318 ++attrib) {
zmo5ee097e2015-05-14 19:13:52319 switch (attrib_values[attrib].type()) {
320 case Vec4::kFloat:
321 {
322 GLfloat v[4];
323 attrib_values[attrib].GetValues(v);
324 glVertexAttrib4fv(attrib, v);
325 }
326 break;
327 case Vec4::kInt:
328 {
329 GLint v[4];
330 attrib_values[attrib].GetValues(v);
331 glVertexAttribI4iv(attrib, v);
332 }
333 break;
334 case Vec4::kUInt:
335 {
336 GLuint v[4];
337 attrib_values[attrib].GetValues(v);
338 glVertexAttribI4uiv(attrib, v);
339 }
340 break;
341 }
vmiura@chromium.org81f20a622014-04-18 01:54:52342 }
343}
344
345void ContextState::RestoreVertexAttribArrays(
346 const scoped_refptr<VertexAttribManager> attrib_manager) const {
347 // This is expected to be called only for VAO with service_id 0,
348 // either to restore the default VAO or a virtual VAO with service_id 0.
349 GLuint vao_service_id = attrib_manager->service_id();
350 DCHECK(vao_service_id == 0);
351
352 // Bind VAO if supported.
353 if (feature_info_->feature_flags().native_vertex_array_object)
354 glBindVertexArrayOES(vao_service_id);
355
356 // Restore vertex attrib arrays.
357 for (size_t attrib_index = 0; attrib_index < attrib_manager->num_attribs();
358 ++attrib_index) {
359 const VertexAttrib* attrib = attrib_manager->GetVertexAttrib(attrib_index);
360
361 // Restore vertex array.
362 Buffer* buffer = attrib->buffer();
363 GLuint buffer_service_id = buffer ? buffer->service_id() : 0;
364 glBindBuffer(GL_ARRAY_BUFFER, buffer_service_id);
365 const void* ptr = reinterpret_cast<const void*>(attrib->offset());
366 glVertexAttribPointer(attrib_index,
367 attrib->size(),
368 attrib->type(),
369 attrib->normalized(),
370 attrib->gl_stride(),
371 ptr);
372
373 // Restore attrib divisor if supported.
374 if (feature_info_->feature_flags().angle_instanced_arrays)
375 glVertexAttribDivisorANGLE(attrib_index, attrib->divisor());
376
377 // Never touch vertex attribute 0's state (in particular, never
zmof9d25cd72016-01-25 23:57:37378 // disable it) when running on desktop GL with compatibility profile
379 // because it will never be re-enabled.
vmiura@chromium.org81f20a622014-04-18 01:54:52380 if (attrib_index != 0 ||
zmof9d25cd72016-01-25 23:57:37381 feature_info_->gl_version_info().BehavesLikeGLES()) {
vmiura@chromium.org81f20a622014-04-18 01:54:52382 if (attrib->enabled()) {
383 glEnableVertexAttribArray(attrib_index);
384 } else {
385 glDisableVertexAttribArray(attrib_index);
386 }
gman@chromium.org29a4d902013-02-26 20:18:06387 }
388 }
vmiura@chromium.org81f20a622014-04-18 01:54:52389}
390
391void ContextState::RestoreVertexAttribs() const {
392 // Restore Vertex Attrib Arrays
zmofe1d1582016-01-22 21:11:56393 DCHECK(vertex_attrib_manager.get());
394 // Restore VAOs.
395 if (feature_info_->feature_flags().native_vertex_array_object) {
396 // If default VAO is still using shared id 0 instead of unique ids
397 // per-context, default VAO state must be restored.
398 GLuint default_vao_service_id =
399 default_vertex_attrib_manager->service_id();
400 if (default_vao_service_id == 0)
401 RestoreVertexAttribArrays(default_vertex_attrib_manager);
vmiura@chromium.org81f20a622014-04-18 01:54:52402
zmofe1d1582016-01-22 21:11:56403 // Restore the current VAO binding, unless it's the same as the
404 // default above.
405 GLuint curr_vao_service_id = vertex_attrib_manager->service_id();
406 if (curr_vao_service_id != 0)
407 glBindVertexArrayOES(curr_vao_service_id);
408 } else {
409 // If native VAO isn't supported, emulated VAOs are used.
410 // Restore to the currently bound VAO.
411 RestoreVertexAttribArrays(vertex_attrib_manager);
vmiura@chromium.org81f20a622014-04-18 01:54:52412 }
413
414 // glVertexAttrib4fv aren't part of VAO state and must be restored.
415 RestoreVertexAttribValues();
gman@chromium.org29a4d902013-02-26 20:18:06416}
417
vmiura@chromium.org88ba52f2014-04-09 12:39:34418void ContextState::RestoreGlobalState(const ContextState* prev_state) const {
419 InitCapabilities(prev_state);
420 InitState(prev_state);
gman@chromium.org29a4d902013-02-26 20:18:06421}
gman@chromium.org1868a342012-11-07 15:56:02422
vmiura@chromium.org8875a5f2014-06-27 08:33:47423void ContextState::RestoreState(const ContextState* prev_state) {
kaanb@chromium.org5baa86bc2014-01-16 04:33:16424 RestoreAllTextureUnitBindings(prev_state);
vmiura@chromium.org81f20a622014-04-18 01:54:52425 RestoreVertexAttribs();
gman@chromium.org15cc23fa2013-02-26 21:56:25426 RestoreBufferBindings();
gman@chromium.org29a4d902013-02-26 20:18:06427 RestoreRenderbufferBindings();
428 RestoreProgramBindings();
vmiura@chromium.org88ba52f2014-04-09 12:39:34429 RestoreGlobalState(prev_state);
gman@chromium.org1868a342012-11-07 15:56:02430}
431
kloveless@chromium.orgd3eba342013-04-18 21:11:50432ErrorState* ContextState::GetErrorState() {
433 return error_state_.get();
434}
435
zmo8ac3bab2015-04-18 02:30:58436void ContextState::EnableDisable(GLenum pname, bool enable) const {
437 if (pname == GL_PRIMITIVE_RESTART_FIXED_INDEX) {
zmo640dc652015-04-28 00:53:38438 if (feature_info_->feature_flags().emulate_primitive_restart_fixed_index)
439 pname = GL_PRIMITIVE_RESTART;
zmo8ac3bab2015-04-18 02:30:58440 }
441 if (enable) {
442 glEnable(pname);
443 } else {
444 glDisable(pname);
445 }
446}
447
zmocdfe65d2015-12-02 17:35:56448void ContextState::UpdatePackParameters() const {
449 if (!feature_info_->IsES3Capable())
450 return;
451 if (bound_pixel_pack_buffer.get()) {
452 glPixelStorei(GL_PACK_ROW_LENGTH, pack_row_length);
zmocdfe65d2015-12-02 17:35:56453 } else {
454 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
zmocdfe65d2015-12-02 17:35:56455 }
456}
457
458void ContextState::UpdateUnpackParameters() const {
459 if (!feature_info_->IsES3Capable())
460 return;
461 if (bound_pixel_unpack_buffer.get()) {
462 glPixelStorei(GL_UNPACK_ROW_LENGTH, unpack_row_length);
463 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, unpack_image_height);
zmocdfe65d2015-12-02 17:35:56464 } else {
465 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
466 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
zmocdfe65d2015-12-02 17:35:56467 }
468}
469
zmo4c0c3532015-05-22 20:04:48470void ContextState::SetBoundBuffer(GLenum target, Buffer* buffer) {
471 switch (target) {
472 case GL_ARRAY_BUFFER:
473 bound_array_buffer = buffer;
474 break;
475 case GL_ELEMENT_ARRAY_BUFFER:
476 vertex_attrib_manager->SetElementArrayBuffer(buffer);
477 break;
478 case GL_COPY_READ_BUFFER:
479 bound_copy_read_buffer = buffer;
480 break;
481 case GL_COPY_WRITE_BUFFER:
482 bound_copy_write_buffer = buffer;
483 break;
484 case GL_PIXEL_PACK_BUFFER:
485 bound_pixel_pack_buffer = buffer;
zmocdfe65d2015-12-02 17:35:56486 UpdatePackParameters();
zmo4c0c3532015-05-22 20:04:48487 break;
488 case GL_PIXEL_UNPACK_BUFFER:
489 bound_pixel_unpack_buffer = buffer;
zmocdfe65d2015-12-02 17:35:56490 UpdateUnpackParameters();
zmo4c0c3532015-05-22 20:04:48491 break;
492 case GL_TRANSFORM_FEEDBACK_BUFFER:
493 bound_transform_feedback_buffer = buffer;
494 break;
495 case GL_UNIFORM_BUFFER:
496 bound_uniform_buffer = buffer;
497 break;
498 default:
499 NOTREACHED();
500 break;
501 }
502}
503
504void ContextState::RemoveBoundBuffer(Buffer* buffer) {
505 DCHECK(buffer);
506 vertex_attrib_manager->Unbind(buffer);
507 if (bound_array_buffer.get() == buffer) {
508 bound_array_buffer = nullptr;
509 }
510 if (bound_copy_read_buffer.get() == buffer) {
511 bound_copy_read_buffer = nullptr;
512 }
513 if (bound_copy_write_buffer.get() == buffer) {
514 bound_copy_write_buffer = nullptr;
515 }
516 if (bound_pixel_pack_buffer.get() == buffer) {
517 bound_pixel_pack_buffer = nullptr;
zmocdfe65d2015-12-02 17:35:56518 UpdatePackParameters();
zmo4c0c3532015-05-22 20:04:48519 }
520 if (bound_pixel_unpack_buffer.get() == buffer) {
521 bound_pixel_unpack_buffer = nullptr;
zmocdfe65d2015-12-02 17:35:56522 UpdateUnpackParameters();
zmo4c0c3532015-05-22 20:04:48523 }
524 if (bound_transform_feedback_buffer.get() == buffer) {
525 bound_transform_feedback_buffer = nullptr;
526 }
527 if (bound_uniform_buffer.get() == buffer) {
528 bound_uniform_buffer = nullptr;
529 }
530}
531
bajones2b98b2a2015-09-15 02:27:36532void ContextState::UnbindTexture(TextureRef* texture) {
533 GLuint active_unit = active_texture_unit;
534 for (size_t jj = 0; jj < texture_units.size(); ++jj) {
535 TextureUnit& unit = texture_units[jj];
536 if (unit.bound_texture_2d.get() == texture) {
537 unit.bound_texture_2d = NULL;
538 if (active_unit != jj) {
539 glActiveTexture(GL_TEXTURE0 + jj);
540 active_unit = jj;
541 }
542 glBindTexture(GL_TEXTURE_2D, 0);
543 } else if (unit.bound_texture_cube_map.get() == texture) {
544 unit.bound_texture_cube_map = NULL;
545 if (active_unit != jj) {
546 glActiveTexture(GL_TEXTURE0 + jj);
547 active_unit = jj;
548 }
549 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
550 } else if (unit.bound_texture_external_oes.get() == texture) {
551 unit.bound_texture_external_oes = NULL;
552 if (active_unit != jj) {
553 glActiveTexture(GL_TEXTURE0 + jj);
554 active_unit = jj;
555 }
556 glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
erikchena50b9c62015-12-16 21:14:49557 } else if (unit.bound_texture_rectangle_arb.get() == texture) {
558 unit.bound_texture_rectangle_arb = NULL;
559 if (active_unit != jj) {
560 glActiveTexture(GL_TEXTURE0 + jj);
561 active_unit = jj;
562 }
563 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
bajones2b98b2a2015-09-15 02:27:36564 } else if (unit.bound_texture_3d.get() == texture) {
565 unit.bound_texture_3d = NULL;
566 if (active_unit != jj) {
567 glActiveTexture(GL_TEXTURE0 + jj);
568 active_unit = jj;
569 }
570 glBindTexture(GL_TEXTURE_3D, 0);
571 } else if (unit.bound_texture_2d_array.get() == texture) {
572 unit.bound_texture_2d_array = NULL;
573 if (active_unit != jj) {
574 glActiveTexture(GL_TEXTURE0 + jj);
575 active_unit = jj;
576 }
577 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
578 }
579 }
580
581 if (active_unit != active_texture_unit) {
582 glActiveTexture(GL_TEXTURE0 + active_texture_unit);
583 }
584}
585
bajones5141d032015-12-07 21:13:39586void ContextState::UnbindSampler(Sampler* sampler) {
587 for (size_t jj = 0; jj < sampler_units.size(); ++jj) {
588 if (sampler_units[jj].get() == sampler) {
589 sampler_units[jj] = nullptr;
590 glBindSampler(jj, 0);
591 }
592 }
593}
594
zmo2b9c47392015-12-11 02:21:32595PixelStoreParams ContextState::GetPackParams() {
zmoccd0b342016-03-09 23:47:36596 DCHECK_EQ(0, pack_skip_pixels);
597 DCHECK_EQ(0, pack_skip_rows);
zmo2b9c47392015-12-11 02:21:32598 PixelStoreParams params;
599 params.alignment = pack_alignment;
600 params.row_length = pack_row_length;
zmo2b9c47392015-12-11 02:21:32601 return params;
602}
603
604PixelStoreParams ContextState::GetUnpackParams(Dimension dimension) {
zmoccd0b342016-03-09 23:47:36605 DCHECK_EQ(0, unpack_skip_pixels);
606 DCHECK_EQ(0, unpack_skip_rows);
607 DCHECK_EQ(0, unpack_skip_images);
zmo2b9c47392015-12-11 02:21:32608 PixelStoreParams params;
609 params.alignment = unpack_alignment;
610 params.row_length = unpack_row_length;
zmo2b9c47392015-12-11 02:21:32611 if (dimension == k3D) {
612 params.image_height = unpack_image_height;
zmo2b9c47392015-12-11 02:21:32613 }
614 return params;
615}
616
zmob730f32b2016-01-06 20:39:08617void ContextState::InitStateManual(const ContextState*) const {
618 // Here we always reset the states whether it's different from previous ones.
619 // We have very limited states here; also, once we switch to MANGLE, MANGLE
620 // will opmitize this.
621 UpdatePackParameters();
622 UpdateUnpackParameters();
623}
624
gman@chromium.orgf731b9462012-10-30 00:35:22625// Include the auto-generated part of this file. We split this because it means
626// we can easily edit the non-auto generated parts right here in this file
627// instead of having to edit some template or the code generator.
628#include "gpu/command_buffer/service/context_state_impl_autogen.h"
629
gman@chromium.orge259eb412012-10-13 05:47:24630} // namespace gles2
631} // namespace gpu