| 1 | /* |
| 2 | * Copyright (C) 2015-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(WEBGL2) |
| 29 | |
| 30 | #include "WebGLRenderingContextBase.h" |
| 31 | #include <memory> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | class WebGLQuery; |
| 36 | class WebGLSampler; |
| 37 | class WebGLSync; |
| 38 | class WebGLTransformFeedback; |
| 39 | class WebGLVertexArrayObject; |
| 40 | |
| 41 | class WebGL2RenderingContext final : public WebGLRenderingContextBase { |
| 42 | WTF_MAKE_ISO_ALLOCATED(WebGL2RenderingContext); |
| 43 | public: |
| 44 | static std::unique_ptr<WebGL2RenderingContext> create(CanvasBase&, GraphicsContext3DAttributes); |
| 45 | static std::unique_ptr<WebGL2RenderingContext> create(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes); |
| 46 | |
| 47 | // Buffer objects |
| 48 | using WebGLRenderingContextBase::bufferData; |
| 49 | using WebGLRenderingContextBase::bufferSubData; |
| 50 | void bufferData(GC3Denum target, const ArrayBufferView& data, GC3Denum usage, GC3Duint srcOffset, GC3Duint length); |
| 51 | void bufferSubData(GC3Denum target, long long offset, const ArrayBufferView& data, GC3Duint srcOffset, GC3Duint length); |
| 52 | void copyBufferSubData(GC3Denum readTarget, GC3Denum writeTarget, GC3Dint64 readOffset, GC3Dint64 writeOffset, GC3Dint64 size); |
| 53 | void getBufferSubData(GC3Denum target, long long srcByteOffset, RefPtr<ArrayBufferView>&& dstData, GC3Duint dstOffset = 0, GC3Duint length = 0); |
| 54 | |
| 55 | // Framebuffer objects |
| 56 | WebGLAny getFramebufferAttachmentParameter(GC3Denum target, GC3Denum attachment, GC3Denum pname) final; |
| 57 | void blitFramebuffer(GC3Dint srcX0, GC3Dint srcY0, GC3Dint srcX1, GC3Dint srcY1, GC3Dint dstX0, GC3Dint dstY0, GC3Dint dstX1, GC3Dint dstY1, GC3Dbitfield mask, GC3Denum filter); |
| 58 | void framebufferTextureLayer(GC3Denum target, GC3Denum attachment, WebGLTexture*, GC3Dint level, GC3Dint layer); |
| 59 | WebGLAny getInternalformatParameter(GC3Denum target, GC3Denum internalformat, GC3Denum pname); |
| 60 | void invalidateFramebuffer(GC3Denum target, const Vector<GC3Denum>& attachments); |
| 61 | void invalidateSubFramebuffer(GC3Denum target, const Vector<GC3Denum>& attachments, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height); |
| 62 | void readBuffer(GC3Denum src); |
| 63 | |
| 64 | // Renderbuffer objects |
| 65 | void renderbufferStorageMultisample(GC3Denum target, GC3Dsizei samples, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height); |
| 66 | |
| 67 | // Texture objects |
| 68 | void texStorage2D(GC3Denum target, GC3Dsizei levels, GC3Denum internalFormat, GC3Dsizei width, GC3Dsizei height); |
| 69 | void texStorage3D(GC3Denum target, GC3Dsizei levels, GC3Denum internalFormat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth); |
| 70 | |
| 71 | #if ENABLE(VIDEO) |
| 72 | using TexImageSource = WTF::Variant<RefPtr<ImageBitmap>, RefPtr<ImageData>, RefPtr<HTMLImageElement>, RefPtr<HTMLCanvasElement>, RefPtr<HTMLVideoElement>>; |
| 73 | #else |
| 74 | using TexImageSource = WTF::Variant<RefPtr<ImageBitmap>, RefPtr<ImageData>, RefPtr<HTMLImageElement>, RefPtr<HTMLCanvasElement>>; |
| 75 | #endif |
| 76 | |
| 77 | using WebGLRenderingContextBase::texImage2D; |
| 78 | void texImage2D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint64 pboOffset); |
| 79 | void texImage2D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, TexImageSource&&); |
| 80 | void texImage2D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, RefPtr<ArrayBufferView>&& srcData, GC3Duint srcOffset); |
| 81 | |
| 82 | void texImage3D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Dint border, GC3Denum format, GC3Denum type, GC3Dint64 pboOffset); |
| 83 | void texImage3D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Dint border, GC3Denum format, GC3Denum type, TexImageSource&&); |
| 84 | void texImage3D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Dint border, GC3Denum format, GC3Denum type, RefPtr<ArrayBufferView>&& pixels); |
| 85 | void texImage3D(GC3Denum target, GC3Dint level, GC3Dint internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Dint border, GC3Denum format, GC3Denum type, RefPtr<ArrayBufferView>&& srcData, GC3Duint srcOffset); |
| 86 | |
| 87 | using WebGLRenderingContextBase::texSubImage2D; |
| 88 | void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, GC3Dint64 pboOffset); |
| 89 | void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, TexImageSource&&); |
| 90 | void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, RefPtr<ArrayBufferView>&& srcData, GC3Duint srcOffset); |
| 91 | |
| 92 | void texSubImage3D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint zoffset, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Denum format, GC3Denum type, GC3Dint64 pboOffset); |
| 93 | void texSubImage3D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint zoffset, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Denum format, GC3Denum type, RefPtr<ArrayBufferView>&& pixels, GC3Duint srcOffset); |
| 94 | void texSubImage3D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint zoffset, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Denum format, GC3Denum type, TexImageSource&&); |
| 95 | |
| 96 | void copyTexSubImage3D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint zoffset, GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height); |
| 97 | |
| 98 | void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Dsizei imageSize, GC3Dint64 offset); |
| 99 | void compressedTexImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, ArrayBufferView& data, GC3Duint, GC3Duint); |
| 100 | void compressedTexImage3D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Dint border, GC3Dsizei imageSize, GC3Dint64 offset); |
| 101 | void compressedTexImage3D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Dint border, ArrayBufferView& srcData, GC3Duint srcOffset, GC3Duint srcLengthOverride); |
| 102 | |
| 103 | void compressedTexSubImage3D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint zoffset, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Denum format, GC3Dsizei imageSize, GC3Dint64 offset); |
| 104 | void compressedTexSubImage3D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dint zoffset, GC3Dsizei width, GC3Dsizei height, GC3Dsizei depth, GC3Denum format, ArrayBufferView& data, GC3Duint srcOffset, GC3Duint srcLengthOverride); |
| 105 | |
| 106 | // Programs and shaders |
| 107 | GC3Dint getFragDataLocation(WebGLProgram&, const String& name); |
| 108 | |
| 109 | // Uniforms and attributes |
| 110 | using Uint32List = TypedList<Uint32Array, uint32_t>; |
| 111 | using Float32List = TypedList<Float32Array, float>; |
| 112 | void uniform1ui(WebGLUniformLocation*, GC3Duint v0); |
| 113 | void uniform2ui(WebGLUniformLocation*, GC3Duint v0, GC3Duint v1); |
| 114 | void uniform3ui(WebGLUniformLocation*, GC3Duint v0, GC3Duint v1, GC3Duint v2); |
| 115 | void uniform4ui(WebGLUniformLocation*, GC3Duint v0, GC3Duint v1, GC3Duint v2, GC3Duint v3); |
| 116 | void uniform1uiv(WebGLUniformLocation*, Uint32List&& data, GC3Duint srcOffset, GC3Duint srcLength); |
| 117 | void uniform2uiv(WebGLUniformLocation*, Uint32List&& data, GC3Duint srcOffset, GC3Duint srcLength); |
| 118 | void uniform3uiv(WebGLUniformLocation*, Uint32List&& data, GC3Duint srcOffset, GC3Duint srcLength); |
| 119 | void uniform4uiv(WebGLUniformLocation*, Uint32List&& data, GC3Duint srcOffset, GC3Duint srcLength); |
| 120 | void uniformMatrix2x3fv(WebGLUniformLocation*, GC3Dboolean transpose, Float32List&& value, GC3Duint srcOffset, GC3Duint srcLength); |
| 121 | void uniformMatrix3x2fv(WebGLUniformLocation*, GC3Dboolean transpose, Float32List&& value, GC3Duint srcOffset, GC3Duint srcLength); |
| 122 | void uniformMatrix2x4fv(WebGLUniformLocation*, GC3Dboolean transpose, Float32List&& value, GC3Duint srcOffset, GC3Duint srcLength); |
| 123 | void uniformMatrix4x2fv(WebGLUniformLocation*, GC3Dboolean transpose, Float32List&& value, GC3Duint srcOffset, GC3Duint srcLength); |
| 124 | void uniformMatrix3x4fv(WebGLUniformLocation*, GC3Dboolean transpose, Float32List&& value, GC3Duint srcOffset, GC3Duint srcLength); |
| 125 | void uniformMatrix4x3fv(WebGLUniformLocation*, GC3Dboolean transpose, Float32List&& value, GC3Duint srcOffset, GC3Duint srcLength); |
| 126 | void vertexAttribI4i(GC3Duint index, GC3Dint x, GC3Dint y, GC3Dint z, GC3Dint w); |
| 127 | void vertexAttribI4iv(GC3Duint index, Int32List&& v); |
| 128 | void vertexAttribI4ui(GC3Duint index, GC3Duint x, GC3Duint y, GC3Duint z, GC3Duint w); |
| 129 | void vertexAttribI4uiv(GC3Duint index, Uint32List&& v); |
| 130 | void vertexAttribIPointer(GC3Duint index, GC3Dint size, GC3Denum type, GC3Dsizei stride, GC3Dint64 offset); |
| 131 | |
| 132 | // Writing to the drawing buffer |
| 133 | void clear(GC3Dbitfield mask) final; |
| 134 | void vertexAttribDivisor(GC3Duint index, GC3Duint divisor); |
| 135 | void drawArraysInstanced(GC3Denum mode, GC3Dint first, GC3Dsizei count, GC3Dsizei instanceCount); |
| 136 | void drawElementsInstanced(GC3Denum mode, GC3Dsizei count, GC3Denum type, GC3Dint64 offset, GC3Dsizei instanceCount); |
| 137 | void drawRangeElements(GC3Denum mode, GC3Duint start, GC3Duint end, GC3Dsizei count, GC3Denum type, GC3Dint64 offset); |
| 138 | |
| 139 | // Multiple render targets |
| 140 | void drawBuffers(const Vector<GC3Denum>& buffers); |
| 141 | void clearBufferiv(GC3Denum buffer, GC3Dint drawbuffer, Int32List&& values, GC3Duint srcOffset); |
| 142 | void clearBufferuiv(GC3Denum buffer, GC3Dint drawbuffer, Uint32List&& values, GC3Duint srcOffset); |
| 143 | void clearBufferfv(GC3Denum buffer, GC3Dint drawbuffer, Float32List&& values, GC3Duint srcOffset); |
| 144 | void clearBufferfi(GC3Denum buffer, GC3Dint drawbuffer, GC3Dfloat depth, GC3Dint stencil); |
| 145 | |
| 146 | // Query objects |
| 147 | RefPtr<WebGLQuery> createQuery(); |
| 148 | void deleteQuery(WebGLQuery*); |
| 149 | GC3Dboolean isQuery(WebGLQuery*); |
| 150 | void beginQuery(GC3Denum target, WebGLQuery&); |
| 151 | void endQuery(GC3Denum target); |
| 152 | RefPtr<WebGLQuery> getQuery(GC3Denum target, GC3Denum pname); |
| 153 | WebGLAny getQueryParameter(WebGLQuery&, GC3Denum pname); |
| 154 | |
| 155 | // Sampler objects |
| 156 | RefPtr<WebGLSampler> createSampler(); |
| 157 | void deleteSampler(WebGLSampler*); |
| 158 | GC3Dboolean isSampler(WebGLSampler*); |
| 159 | void bindSampler(GC3Duint unit, WebGLSampler*); |
| 160 | void samplerParameteri(WebGLSampler&, GC3Denum pname, GC3Dint param); |
| 161 | void samplerParameterf(WebGLSampler&, GC3Denum pname, GC3Dfloat param); |
| 162 | WebGLAny getSamplerParameter(WebGLSampler&, GC3Denum pname); |
| 163 | |
| 164 | // Sync objects |
| 165 | RefPtr<WebGLSync> fenceSync(GC3Denum condition, GC3Dbitfield flags); |
| 166 | GC3Dboolean isSync(WebGLSync*); |
| 167 | void deleteSync(WebGLSync*); |
| 168 | GC3Denum clientWaitSync(WebGLSync&, GC3Dbitfield flags, GC3Duint64 timeout); |
| 169 | void waitSync(WebGLSync&, GC3Dbitfield flags, GC3Dint64 timeout); |
| 170 | WebGLAny getSyncParameter(WebGLSync&, GC3Denum pname); |
| 171 | |
| 172 | // Transform feedback |
| 173 | RefPtr<WebGLTransformFeedback> createTransformFeedback(); |
| 174 | void deleteTransformFeedback(WebGLTransformFeedback* id); |
| 175 | GC3Dboolean isTransformFeedback(WebGLTransformFeedback* id); |
| 176 | void bindTransformFeedback(GC3Denum target, WebGLTransformFeedback* id); |
| 177 | void beginTransformFeedback(GC3Denum primitiveMode); |
| 178 | void endTransformFeedback(); |
| 179 | void transformFeedbackVaryings(WebGLProgram&, const Vector<String>& varyings, GC3Denum bufferMode); |
| 180 | RefPtr<WebGLActiveInfo> getTransformFeedbackVarying(WebGLProgram&, GC3Duint index); |
| 181 | void pauseTransformFeedback(); |
| 182 | void resumeTransformFeedback(); |
| 183 | |
| 184 | // Uniform buffer objects and transform feedback buffers |
| 185 | void bindBufferBase(GC3Denum target, GC3Duint index, WebGLBuffer*); |
| 186 | void bindBufferRange(GC3Denum target, GC3Duint index, WebGLBuffer*, GC3Dint64 offset, GC3Dint64 size); |
| 187 | WebGLAny getIndexedParameter(GC3Denum target, GC3Duint index); |
| 188 | Optional<Vector<GC3Duint>> getUniformIndices(WebGLProgram&, const Vector<String>& uniformNames); |
| 189 | WebGLAny getActiveUniforms(WebGLProgram&, const Vector<GC3Duint>& uniformIndices, GC3Denum pname); |
| 190 | GC3Duint getUniformBlockIndex(WebGLProgram&, const String& uniformBlockName); |
| 191 | WebGLAny getActiveUniformBlockParameter(WebGLProgram&, GC3Duint uniformBlockIndex, GC3Denum pname); |
| 192 | WebGLAny getActiveUniformBlockName(WebGLProgram&, GC3Duint uniformBlockIndex); |
| 193 | void uniformBlockBinding(WebGLProgram&, GC3Duint uniformBlockIndex, GC3Duint uniformBlockBinding); |
| 194 | |
| 195 | // Vertex array objects |
| 196 | RefPtr<WebGLVertexArrayObject> createVertexArray(); |
| 197 | void deleteVertexArray(WebGLVertexArrayObject* vertexArray); |
| 198 | GC3Dboolean isVertexArray(WebGLVertexArrayObject* vertexArray); |
| 199 | void bindVertexArray(WebGLVertexArrayObject* vertexArray); |
| 200 | |
| 201 | WebGLExtension* getExtension(const String&) final; |
| 202 | Optional<Vector<String>> getSupportedExtensions() final; |
| 203 | WebGLAny getParameter(GC3Denum pname) final; |
| 204 | |
| 205 | void renderbufferStorage(GC3Denum target, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height) final; |
| 206 | void hint(GC3Denum target, GC3Denum mode) final; |
| 207 | |
| 208 | private: |
| 209 | WebGL2RenderingContext(CanvasBase&, GraphicsContext3DAttributes); |
| 210 | WebGL2RenderingContext(CanvasBase&, Ref<GraphicsContext3D>&&, GraphicsContext3DAttributes); |
| 211 | |
| 212 | bool isWebGL2() const final { return true; } |
| 213 | |
| 214 | void initializeVertexArrayObjects() final; |
| 215 | GC3Dint getMaxDrawBuffers() final; |
| 216 | GC3Dint getMaxColorAttachments() final; |
| 217 | bool validateIndexArrayConservative(GC3Denum type, unsigned& numElementsRequired) final; |
| 218 | bool validateBlendEquation(const char* functionName, GC3Denum mode) final; |
| 219 | bool validateCapability(const char* functionName, GC3Denum cap) final; |
| 220 | bool validateFramebufferFuncParameters(const char* functionName, GC3Denum target, GC3Denum attachment) final; |
| 221 | bool validateFramebufferTarget(const char* functionName, GC3Denum target); |
| 222 | bool validateNonDefaultFramebufferAttachment(const char* functionName, GC3Denum attachment); |
| 223 | |
| 224 | GC3Denum baseInternalFormatFromInternalFormat(GC3Denum internalformat); |
| 225 | bool isIntegerFormat(GC3Denum internalformat); |
| 226 | void initializeShaderExtensions(); |
| 227 | |
| 228 | bool validateTexStorageFuncParameters(GC3Denum target, GC3Dsizei levels, GC3Denum internalFormat, GC3Dsizei width, GC3Dsizei height, const char* functionName); |
| 229 | }; |
| 230 | |
| 231 | } // namespace WebCore |
| 232 | |
| 233 | SPECIALIZE_TYPE_TRAITS_CANVASRENDERINGCONTEXT(WebCore::WebGL2RenderingContext, isWebGL2()) |
| 234 | |
| 235 | #endif // WEBGL2 |
| 236 | |