WebGL2ContextSamplers.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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. #include "WebGL2Context.h"
  6. #include "WebGLSampler.h"
  7. #include "GLContext.h"
  8. namespace mozilla {
  9. already_AddRefed<WebGLSampler>
  10. WebGL2Context::CreateSampler()
  11. {
  12. if (IsContextLost())
  13. return nullptr;
  14. GLuint sampler;
  15. MakeContextCurrent();
  16. gl->fGenSamplers(1, &sampler);
  17. RefPtr<WebGLSampler> globj = new WebGLSampler(this, sampler);
  18. return globj.forget();
  19. }
  20. void
  21. WebGL2Context::DeleteSampler(WebGLSampler* sampler)
  22. {
  23. if (!ValidateDeleteObject("deleteSampler", sampler))
  24. return;
  25. for (int n = 0; n < mGLMaxTextureUnits; n++) {
  26. if (mBoundSamplers[n] == sampler) {
  27. mBoundSamplers[n] = nullptr;
  28. InvalidateResolveCacheForTextureWithTexUnit(n);
  29. }
  30. }
  31. sampler->RequestDelete();
  32. }
  33. bool
  34. WebGL2Context::IsSampler(const WebGLSampler* sampler)
  35. {
  36. if (!ValidateIsObject("isSampler", sampler))
  37. return false;
  38. MakeContextCurrent();
  39. return gl->fIsSampler(sampler->mGLName);
  40. }
  41. void
  42. WebGL2Context::BindSampler(GLuint unit, WebGLSampler* sampler)
  43. {
  44. if (IsContextLost())
  45. return;
  46. if (sampler && !ValidateObject("bindSampler", *sampler))
  47. return;
  48. if (GLint(unit) >= mGLMaxTextureUnits)
  49. return ErrorInvalidValue("bindSampler: unit must be < %d", mGLMaxTextureUnits);
  50. ////
  51. gl->MakeCurrent();
  52. gl->fBindSampler(unit, sampler ? sampler->mGLName : 0);
  53. InvalidateResolveCacheForTextureWithTexUnit(unit);
  54. mBoundSamplers[unit] = sampler;
  55. }
  56. void
  57. WebGL2Context::SamplerParameteri(WebGLSampler& sampler, GLenum pname, GLint param)
  58. {
  59. const char funcName[] = "samplerParameteri";
  60. if (IsContextLost())
  61. return;
  62. if (!ValidateObject(funcName, sampler))
  63. return;
  64. sampler.SamplerParameter(funcName, pname, FloatOrInt(param));
  65. }
  66. void
  67. WebGL2Context::SamplerParameterf(WebGLSampler& sampler, GLenum pname, GLfloat param)
  68. {
  69. const char funcName[] = "samplerParameterf";
  70. if (IsContextLost())
  71. return;
  72. if (!ValidateObject(funcName, sampler))
  73. return;
  74. sampler.SamplerParameter(funcName, pname, FloatOrInt(param));
  75. }
  76. void
  77. WebGL2Context::GetSamplerParameter(JSContext*, const WebGLSampler& sampler, GLenum pname,
  78. JS::MutableHandleValue retval)
  79. {
  80. const char funcName[] = "getSamplerParameter";
  81. retval.setNull();
  82. if (IsContextLost())
  83. return;
  84. if (!ValidateObject(funcName, sampler))
  85. return;
  86. ////
  87. gl->MakeCurrent();
  88. switch (pname) {
  89. case LOCAL_GL_TEXTURE_MIN_FILTER:
  90. case LOCAL_GL_TEXTURE_MAG_FILTER:
  91. case LOCAL_GL_TEXTURE_WRAP_S:
  92. case LOCAL_GL_TEXTURE_WRAP_T:
  93. case LOCAL_GL_TEXTURE_WRAP_R:
  94. case LOCAL_GL_TEXTURE_COMPARE_MODE:
  95. case LOCAL_GL_TEXTURE_COMPARE_FUNC:
  96. {
  97. GLint param = 0;
  98. gl->fGetSamplerParameteriv(sampler.mGLName, pname, &param);
  99. retval.set(JS::Int32Value(param));
  100. }
  101. return;
  102. case LOCAL_GL_TEXTURE_MIN_LOD:
  103. case LOCAL_GL_TEXTURE_MAX_LOD:
  104. {
  105. GLfloat param = 0;
  106. gl->fGetSamplerParameterfv(sampler.mGLName, pname, &param);
  107. retval.set(JS::Float32Value(param));
  108. }
  109. return;
  110. default:
  111. ErrorInvalidEnumArg(funcName, "pname", pname);
  112. return;
  113. }
  114. }
  115. } // namespace mozilla