Renderbuffer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Check from "../Core/Check.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import ContextLimits from "./ContextLimits.js";
  7. import RenderbufferFormat from "./RenderbufferFormat.js";
  8. /**
  9. * @private
  10. */
  11. function Renderbuffer(options) {
  12. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  13. //>>includeStart('debug', pragmas.debug);
  14. Check.defined("options.context", options.context);
  15. //>>includeEnd('debug');
  16. const context = options.context;
  17. const gl = context._gl;
  18. const maximumRenderbufferSize = ContextLimits.maximumRenderbufferSize;
  19. const format = defaultValue(options.format, RenderbufferFormat.RGBA4);
  20. const width = defined(options.width) ? options.width : gl.drawingBufferWidth;
  21. const height = defined(options.height)
  22. ? options.height
  23. : gl.drawingBufferHeight;
  24. const numSamples = defaultValue(options.numSamples, 1);
  25. //>>includeStart('debug', pragmas.debug);
  26. if (!RenderbufferFormat.validate(format)) {
  27. throw new DeveloperError("Invalid format.");
  28. }
  29. Check.typeOf.number.greaterThan("width", width, 0);
  30. if (width > maximumRenderbufferSize) {
  31. throw new DeveloperError(
  32. `Width must be less than or equal to the maximum renderbuffer size (${maximumRenderbufferSize}). Check maximumRenderbufferSize.`
  33. );
  34. }
  35. Check.typeOf.number.greaterThan("height", height, 0);
  36. if (height > maximumRenderbufferSize) {
  37. throw new DeveloperError(
  38. `Height must be less than or equal to the maximum renderbuffer size (${maximumRenderbufferSize}). Check maximumRenderbufferSize.`
  39. );
  40. }
  41. //>>includeEnd('debug');
  42. this._gl = gl;
  43. this._format = format;
  44. this._width = width;
  45. this._height = height;
  46. this._renderbuffer = this._gl.createRenderbuffer();
  47. gl.bindRenderbuffer(gl.RENDERBUFFER, this._renderbuffer);
  48. if (numSamples > 1) {
  49. gl.renderbufferStorageMultisample(
  50. gl.RENDERBUFFER,
  51. numSamples,
  52. format,
  53. width,
  54. height
  55. );
  56. } else {
  57. gl.renderbufferStorage(gl.RENDERBUFFER, format, width, height);
  58. }
  59. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  60. }
  61. Object.defineProperties(Renderbuffer.prototype, {
  62. format: {
  63. get: function () {
  64. return this._format;
  65. },
  66. },
  67. width: {
  68. get: function () {
  69. return this._width;
  70. },
  71. },
  72. height: {
  73. get: function () {
  74. return this._height;
  75. },
  76. },
  77. });
  78. Renderbuffer.prototype._getRenderbuffer = function () {
  79. return this._renderbuffer;
  80. };
  81. Renderbuffer.prototype.isDestroyed = function () {
  82. return false;
  83. };
  84. Renderbuffer.prototype.destroy = function () {
  85. this._gl.deleteRenderbuffer(this._renderbuffer);
  86. return destroyObject(this);
  87. };
  88. export default Renderbuffer;