WebGL2ContextSamplers.cpp (2989B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "GLContext.h" 7 #include "WebGL2Context.h" 8 #include "WebGLSampler.h" 9 10 namespace mozilla { 11 12 RefPtr<WebGLSampler> WebGL2Context::CreateSampler() { 13 const FuncScope funcScope(*this, "createSampler"); 14 if (IsContextLost()) return nullptr; 15 16 return new WebGLSampler(this); 17 } 18 19 void WebGL2Context::BindSampler(GLuint unit, WebGLSampler* sampler) { 20 FuncScope funcScope(*this, "bindSampler"); 21 if (IsContextLost()) return; 22 funcScope.mBindFailureGuard = true; 23 24 if (sampler && !ValidateObject("sampler", *sampler)) return; 25 26 if (unit >= mBoundSamplers.Length()) 27 return ErrorInvalidValue("unit must be < %u", mBoundSamplers.Length()); 28 29 //// 30 31 gl->fBindSampler(unit, sampler ? sampler->mGLName : 0); 32 33 mBoundSamplers[unit] = sampler; 34 35 funcScope.mBindFailureGuard = false; 36 } 37 38 void WebGL2Context::SamplerParameteri(WebGLSampler& sampler, GLenum pname, 39 GLint param) { 40 const FuncScope funcScope(*this, "samplerParameteri"); 41 if (IsContextLost()) return; 42 43 if (!ValidateObject("sampler", sampler)) return; 44 45 sampler.SamplerParameter(pname, FloatOrInt(param)); 46 } 47 48 void WebGL2Context::SamplerParameterf(WebGLSampler& sampler, GLenum pname, 49 GLfloat param) { 50 const FuncScope funcScope(*this, "samplerParameterf"); 51 if (IsContextLost()) return; 52 53 if (!ValidateObject("sampler", sampler)) return; 54 55 sampler.SamplerParameter(pname, FloatOrInt(param)); 56 } 57 58 Maybe<double> WebGL2Context::GetSamplerParameter(const WebGLSampler& sampler, 59 GLenum pname) const { 60 const FuncScope funcScope(*this, "getSamplerParameter"); 61 if (IsContextLost()) return {}; 62 63 if (!ValidateObject("sampler", sampler)) return {}; 64 65 //// 66 67 const auto fnAsFloat = [&]() { 68 GLfloat param = 0; 69 gl->fGetSamplerParameterfv(sampler.mGLName, pname, ¶m); 70 return param; 71 }; 72 73 switch (pname) { 74 case LOCAL_GL_TEXTURE_MIN_FILTER: 75 case LOCAL_GL_TEXTURE_MAG_FILTER: 76 case LOCAL_GL_TEXTURE_WRAP_S: 77 case LOCAL_GL_TEXTURE_WRAP_T: 78 case LOCAL_GL_TEXTURE_WRAP_R: 79 case LOCAL_GL_TEXTURE_COMPARE_MODE: 80 case LOCAL_GL_TEXTURE_COMPARE_FUNC: { 81 GLint param = 0; 82 gl->fGetSamplerParameteriv(sampler.mGLName, pname, ¶m); 83 return Some(param); 84 } 85 case LOCAL_GL_TEXTURE_MIN_LOD: 86 case LOCAL_GL_TEXTURE_MAX_LOD: 87 return Some(fnAsFloat()); 88 89 case LOCAL_GL_TEXTURE_MAX_ANISOTROPY: 90 if (!IsExtensionEnabled( 91 WebGLExtensionID::EXT_texture_filter_anisotropic)) { 92 break; 93 } 94 return Some(fnAsFloat()); 95 96 default: 97 break; 98 } 99 ErrorInvalidEnumInfo("pname", pname); 100 return {}; 101 } 102 103 } // namespace mozilla