2019-11-23 20:27:39 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
2022-03-13 12:05:22 +08:00
|
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
****************************************************************************/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
#include "CommandBufferGL.h"
|
|
|
|
|
#include "BufferGL.h"
|
|
|
|
|
#include "RenderPipelineGL.h"
|
|
|
|
|
#include "TextureGL.h"
|
|
|
|
|
#include "DepthStencilStateGL.h"
|
|
|
|
|
#include "ProgramGL.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
|
#include "base/EventDispatcher.h"
|
|
|
|
|
#include "base/EventType.h"
|
|
|
|
|
#include "base/Director.h"
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#include "MacrosGL.h"
|
|
|
|
|
#include "UtilsGL.h"
|
2020-09-21 22:10:50 +08:00
|
|
|
|
#include "RenderTargetGL.h"
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#include "DeviceGL.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void applyTexture(TextureBackend* texture, int slot, int index)
|
|
|
|
|
{
|
|
|
|
|
switch (texture->getTextureType())
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
case TextureType::TEXTURE_2D:
|
|
|
|
|
static_cast<Texture2DGL*>(texture)->apply(slot, index);
|
|
|
|
|
break;
|
|
|
|
|
case TextureType::TEXTURE_CUBE:
|
|
|
|
|
static_cast<TextureCubeGL*>(texture)->apply(slot, index);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
CommandBufferGL::CommandBufferGL() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
CommandBufferGL::~CommandBufferGL()
|
|
|
|
|
{
|
|
|
|
|
cleanResources();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
|
bool CommandBufferGL::beginFrame()
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
|
return true;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
|
void CommandBufferGL::beginRenderPass(const RenderTarget* rt, const RenderPassDescriptor& descirptor)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
|
auto rtGL = static_cast<const RenderTargetGL*>(rt);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
|
rtGL->bindFrameBuffer();
|
2022-06-24 14:18:48 +08:00
|
|
|
|
rtGL->update();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
|
auto clearFlags = descirptor.flags.clear;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
// set clear color, depth and stencil
|
|
|
|
|
GLbitfield mask = 0;
|
2020-09-21 22:10:50 +08:00
|
|
|
|
if (bitmask::any(clearFlags, TargetBufferFlags::COLOR))
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
mask |= GL_COLOR_BUFFER_BIT;
|
|
|
|
|
const auto& clearColor = descirptor.clearColorValue;
|
|
|
|
|
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
|
|
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
GLboolean oldDepthWrite = GL_FALSE;
|
|
|
|
|
GLboolean oldDepthTest = GL_FALSE;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
GLfloat oldDepthClearValue = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
GLint oldDepthFunc = GL_LESS;
|
2020-09-21 22:10:50 +08:00
|
|
|
|
if (bitmask::any(clearFlags, TargetBufferFlags::DEPTH))
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
glGetBooleanv(GL_DEPTH_WRITEMASK, &oldDepthWrite);
|
|
|
|
|
glGetBooleanv(GL_DEPTH_TEST, &oldDepthTest);
|
|
|
|
|
glGetFloatv(GL_DEPTH_CLEAR_VALUE, &oldDepthClearValue);
|
|
|
|
|
glGetIntegerv(GL_DEPTH_FUNC, &oldDepthFunc);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
mask |= GL_DEPTH_BUFFER_BIT;
|
|
|
|
|
glClearDepth(descirptor.clearDepthValue);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->enableDepthTest();
|
|
|
|
|
|
|
|
|
|
__gl->depthMask(GL_TRUE);
|
|
|
|
|
__gl->depthFunc(GL_ALWAYS);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
|
|
|
|
if (bitmask::any(clearFlags, TargetBufferFlags::STENCIL))
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
mask |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
|
glClearStencil(descirptor.clearStencilValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (mask)
|
|
|
|
|
glClear(mask);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
// restore depth test
|
2020-09-21 22:10:50 +08:00
|
|
|
|
if (bitmask::any(clearFlags, TargetBufferFlags::DEPTH))
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
if (!oldDepthTest)
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->disableDepthTest();
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->depthMask(oldDepthWrite);
|
|
|
|
|
__gl->depthFunc(oldDepthFunc);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
glClearDepth(oldDepthClearValue);
|
|
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 16:32:17 +08:00
|
|
|
|
void CommandBufferGL::setDepthStencilState(DepthStencilState* depthStencilState)
|
|
|
|
|
{
|
|
|
|
|
_depthStencilStateGL = static_cast<DepthStencilStateGL*>(depthStencilState);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
void CommandBufferGL::setRenderPipeline(RenderPipeline* renderPipeline)
|
|
|
|
|
{
|
2020-09-22 16:32:17 +08:00
|
|
|
|
_renderPipeline = static_cast<RenderPipelineGL*>(renderPipeline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* Update depthStencil status, improvment: for metal backend cache it
|
|
|
|
|
* @param depthStencilState Specifies the depth and stencil status
|
|
|
|
|
*/
|
2020-09-22 16:32:17 +08:00
|
|
|
|
void CommandBufferGL::updateDepthStencilState(const DepthStencilDescriptor& descriptor)
|
|
|
|
|
{
|
|
|
|
|
_depthStencilStateGL->update(descriptor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update render pipeline status
|
|
|
|
|
* @param depthStencilState Specifies the depth and stencil status
|
|
|
|
|
*/
|
|
|
|
|
void CommandBufferGL::updatePipelineState(const RenderTarget* rt, const PipelineDescriptor& descriptor)
|
|
|
|
|
{
|
|
|
|
|
_renderPipeline->update(rt, descriptor);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setViewport(int x, int y, unsigned int w, unsigned int h)
|
|
|
|
|
{
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->viewport(_viewPort.set(x, y, w, h));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setCullMode(CullMode mode)
|
|
|
|
|
{
|
|
|
|
|
_cullMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setWinding(Winding winding)
|
|
|
|
|
{
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->winding(winding);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setIndexBuffer(Buffer* buffer)
|
|
|
|
|
{
|
|
|
|
|
assert(buffer != nullptr);
|
2022-03-13 12:05:22 +08:00
|
|
|
|
if (buffer == nullptr || _indexBuffer == buffer)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
buffer->retain();
|
2022-07-16 10:43:05 +08:00
|
|
|
|
AX_SAFE_RELEASE(_indexBuffer);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
_indexBuffer = static_cast<BufferGL*>(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
void CommandBufferGL::setInstanceBuffer(Buffer* buffer)
|
|
|
|
|
{
|
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
|
if (buffer == nullptr || _instanceTransformBuffer == buffer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
buffer->retain();
|
|
|
|
|
AX_SAFE_RELEASE(_instanceTransformBuffer);
|
|
|
|
|
_instanceTransformBuffer = static_cast<BufferGL*>(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
void CommandBufferGL::setVertexBuffer(Buffer* buffer)
|
|
|
|
|
{
|
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
|
if (buffer == nullptr || _vertexBuffer == buffer)
|
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
buffer->retain();
|
2022-07-16 10:43:05 +08:00
|
|
|
|
AX_SAFE_RELEASE(_vertexBuffer);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
_vertexBuffer = static_cast<BufferGL*>(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setProgramState(ProgramState* programState)
|
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
|
AX_SAFE_RETAIN(programState);
|
|
|
|
|
AX_SAFE_RELEASE(_programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
_programState = programState;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 16:17:55 +08:00
|
|
|
|
void CommandBufferGL::drawArrays(PrimitiveType primitiveType, std::size_t start, std::size_t count, bool wireframe)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
prepareDrawing();
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#if !AX_GLES_PROFILE // glPolygonMode is only supported in Desktop OpenGL
|
|
|
|
|
if (wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
2022-08-06 16:17:55 +08:00
|
|
|
|
#else
|
2023-08-13 23:56:58 +08:00
|
|
|
|
if (wireframe)
|
|
|
|
|
primitiveType = PrimitiveType::LINE;
|
2022-08-06 16:17:55 +08:00
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
glDrawArrays(UtilsGL::toGLPrimitiveType(primitiveType), start, count);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#if !AX_GLES_PROFILE // glPolygonMode is only supported in Desktop OpenGL
|
|
|
|
|
if (wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
2022-08-06 16:17:55 +08:00
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
cleanResources();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void CommandBufferGL::drawElements(PrimitiveType primitiveType,
|
|
|
|
|
IndexFormat indexType,
|
|
|
|
|
std::size_t count,
|
2022-08-06 16:17:55 +08:00
|
|
|
|
std::size_t offset,
|
|
|
|
|
bool wireframe)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
prepareDrawing();
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#if !AX_GLES_PROFILE // glPolygonMode is only supported in Desktop OpenGL
|
|
|
|
|
if (wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
2022-08-06 16:17:55 +08:00
|
|
|
|
#else
|
2023-08-13 23:56:58 +08:00
|
|
|
|
if (wireframe)
|
|
|
|
|
primitiveType = PrimitiveType::LINE;
|
2022-08-06 16:17:55 +08:00
|
|
|
|
#endif
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->bindBuffer(BufferType::ELEMENT_ARRAY_BUFFER, _indexBuffer->getHandler());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
glDrawElements(UtilsGL::toGLPrimitiveType(primitiveType), count, UtilsGL::toGLIndexType(indexType),
|
|
|
|
|
(GLvoid*)offset);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#if !AX_GLES_PROFILE // glPolygonMode is only supported in Desktop OpenGL
|
|
|
|
|
if (wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
2023-07-28 14:13:01 +08:00
|
|
|
|
#endif
|
|
|
|
|
cleanResources();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
void CommandBufferGL::drawElementsInstanced(PrimitiveType primitiveType,
|
|
|
|
|
IndexFormat indexType,
|
|
|
|
|
std::size_t count,
|
|
|
|
|
std::size_t offset,
|
|
|
|
|
int instanceCount,
|
|
|
|
|
bool wireframe)
|
|
|
|
|
{
|
|
|
|
|
#if AX_GLES_PROFILE != 200
|
|
|
|
|
prepareDrawing();
|
|
|
|
|
#if !AX_GLES_PROFILE // glPolygonMode is only supported in Desktop OpenGL
|
|
|
|
|
if (wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
|
#else
|
|
|
|
|
if (wireframe)
|
|
|
|
|
primitiveType = PrimitiveType::LINE;
|
|
|
|
|
#endif
|
|
|
|
|
__gl->bindBuffer(BufferType::ELEMENT_ARRAY_BUFFER, _indexBuffer->getHandler());
|
|
|
|
|
glDrawElementsInstanced(UtilsGL::toGLPrimitiveType(primitiveType), count, UtilsGL::toGLIndexType(indexType),
|
|
|
|
|
(GLvoid*)offset, instanceCount);
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
#if !AX_GLES_PROFILE // glPolygonMode is only supported in Desktop OpenGL
|
|
|
|
|
if (wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
|
#endif
|
|
|
|
|
cleanResources();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
void CommandBufferGL::endRenderPass()
|
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
|
AX_SAFE_RELEASE_NULL(_indexBuffer);
|
|
|
|
|
AX_SAFE_RELEASE_NULL(_vertexBuffer);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
AX_SAFE_RELEASE_NULL(_instanceTransformBuffer);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void CommandBufferGL::endFrame() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
void CommandBufferGL::prepareDrawing() const
|
2021-12-25 10:04:45 +08:00
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
const auto& program = _renderPipeline->getProgram();
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->useProgram(program->getHandler());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
bindVertexBuffer(program);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
bindUniforms(program);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
// Set depth/stencil state.
|
2020-09-22 16:32:17 +08:00
|
|
|
|
if (_depthStencilStateGL->isEnabled())
|
2019-11-23 20:27:39 +08:00
|
|
|
|
_depthStencilStateGL->apply(_stencilReferenceValueFront, _stencilReferenceValueBack);
|
|
|
|
|
else
|
|
|
|
|
DepthStencilStateGL::reset();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
// Set cull mode.
|
2023-08-13 23:56:58 +08:00
|
|
|
|
if (_cullMode != CullMode::NONE)
|
|
|
|
|
__gl->enableCullFace(UtilsGL::toGLCullMode(_cullMode));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
else
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->disableCullFace();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void CommandBufferGL::bindVertexBuffer(ProgramGL* program) const
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
// Bind vertex buffers and set the attributes.
|
|
|
|
|
auto vertexLayout = _programState->getVertexLayout();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
if (!vertexLayout->isValid())
|
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
// Bind VAO, engine share 1 VAO for all vertexLayouts aka vfmts
|
|
|
|
|
// optimize proposal: create VAO per vertexLayout, just need bind VAO
|
|
|
|
|
__gl->bindBuffer(BufferType::ARRAY_BUFFER, _vertexBuffer->getHandler());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
const auto& attributes = vertexLayout->getAttributes();
|
|
|
|
|
for (const auto& attributeInfo : attributes)
|
|
|
|
|
{
|
|
|
|
|
const auto& attribute = attributeInfo.second;
|
|
|
|
|
glEnableVertexAttribArray(attribute.index);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
glVertexAttribPointer(attribute.index, UtilsGL::getGLAttributeSize(attribute.format),
|
|
|
|
|
UtilsGL::toGLAttributeType(attribute.format), attribute.needToBeNormallized,
|
|
|
|
|
vertexLayout->getStride(), (GLvoid*)attribute.offset);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
2023-08-13 23:56:58 +08:00
|
|
|
|
|
|
|
|
|
#if AX_GLES_PROFILE != 200
|
|
|
|
|
// if we have an instance transform buffer pointer then we must be rendering in instance mode.
|
|
|
|
|
if (_instanceTransformBuffer)
|
|
|
|
|
{
|
|
|
|
|
auto instaceLoc = _programState->getProgram()->getAttributeLocation(Attribute::INSTANCE);
|
|
|
|
|
if (instaceLoc != -1)
|
|
|
|
|
{
|
|
|
|
|
__gl->bindBuffer(BufferType::ARRAY_BUFFER, _instanceTransformBuffer->getHandler());
|
|
|
|
|
|
|
|
|
|
// Enable 4 attrib arrays for each matrix row.
|
|
|
|
|
glEnableVertexAttribArray(instaceLoc);
|
|
|
|
|
glEnableVertexAttribArray(instaceLoc + 1);
|
|
|
|
|
glEnableVertexAttribArray(instaceLoc + 2);
|
|
|
|
|
glEnableVertexAttribArray(instaceLoc + 3);
|
|
|
|
|
|
|
|
|
|
// Since OpenGL sucks we need to Specify vertex attribute pointers for
|
|
|
|
|
// instance transforms for each matrix row containting 16 bytes or 4 floats
|
|
|
|
|
glVertexAttribPointer(instaceLoc, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 16, (void*)0);
|
|
|
|
|
glVertexAttribPointer(instaceLoc + 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 16,
|
|
|
|
|
(void*)(sizeof(float) * 4));
|
|
|
|
|
glVertexAttribPointer(instaceLoc + 2, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 16,
|
|
|
|
|
(void*)(2 * sizeof(float) * 4));
|
|
|
|
|
glVertexAttribPointer(instaceLoc + 3, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 16,
|
|
|
|
|
(void*)(3 * sizeof(float) * 4));
|
|
|
|
|
|
|
|
|
|
// Set the divisor for the instance attributes to 1 indicating that it should advance one matrix per
|
|
|
|
|
// instance.
|
|
|
|
|
glVertexAttribDivisor(instaceLoc, 1);
|
|
|
|
|
glVertexAttribDivisor(instaceLoc + 1, 1);
|
|
|
|
|
glVertexAttribDivisor(instaceLoc + 2, 1);
|
|
|
|
|
glVertexAttribDivisor(instaceLoc + 3, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
void CommandBufferGL::bindUniforms(ProgramGL* program) const
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
if (_programState)
|
|
|
|
|
{
|
2023-08-13 23:56:58 +08:00
|
|
|
|
assert(program == _programState->getProgram());
|
|
|
|
|
|
2021-09-30 22:33:23 +08:00
|
|
|
|
auto& callbacks = _programState->getCallbackUniforms();
|
2022-07-21 19:19:08 +08:00
|
|
|
|
for (auto&& cb : callbacks)
|
2021-09-30 22:33:23 +08:00
|
|
|
|
cb.second(_programState, cb.first);
|
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
auto& uniformInfos = program->getAllActiveUniformInfo(ShaderStage::VERTEX);
|
2023-08-13 23:58:18 +08:00
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
std::size_t bufferSize = 0;
|
|
|
|
|
auto buffer = _programState->getVertexUniformBuffer(bufferSize);
|
|
|
|
|
program->bindUniformBuffers(buffer, bufferSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
const auto& textureInfo = _programState->getVertexTextureInfos();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
for (const auto& iter : textureInfo)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2020-08-29 17:39:17 +08:00
|
|
|
|
/* About mutli textures support
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* a. sampler2DArray, sampler2D[2], bind BackendTexture one by one, not use GL_TEXTURE_2D_ARRAY, not used
|
|
|
|
|
* at all engine interanl b. texture slot, one BackendTexture, multi GPU texture handlers, used by etc1,
|
|
|
|
|
* restrict: textures must have same size c. Bind multi BackendTexture to 1 Shader Program, see the
|
|
|
|
|
* ShaderTest
|
|
|
|
|
*/
|
2020-08-29 16:56:48 +08:00
|
|
|
|
auto& textures = iter.second.textures;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
auto& slots = iter.second.slots;
|
|
|
|
|
auto& indexs = iter.second.indexs;
|
|
|
|
|
auto location = iter.first;
|
2022-07-16 10:43:05 +08:00
|
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2019-11-23 20:27:39 +08:00
|
|
|
|
location = iter.second.location;
|
|
|
|
|
#endif
|
|
|
|
|
int i = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
for (const auto& texture : textures)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2020-08-29 16:56:48 +08:00
|
|
|
|
applyTexture(texture, slots[i], indexs[i]);
|
|
|
|
|
++i;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
2021-09-30 08:18:01 +08:00
|
|
|
|
|
2020-08-29 16:56:48 +08:00
|
|
|
|
auto arrayCount = slots.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (arrayCount == 1) // Most of the time, not use sampler2DArray, should be 1
|
2020-08-29 16:56:48 +08:00
|
|
|
|
glUniform1i(location, slots[0]);
|
2020-08-29 17:39:17 +08:00
|
|
|
|
else
|
2021-09-30 08:18:44 +08:00
|
|
|
|
glUniform1iv(location, static_cast<GLsizei>(arrayCount), static_cast<const GLint*>(slots.data()));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
void CommandBufferGL::cleanResources()
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#if AX_GLES_PROFILE != 200
|
|
|
|
|
if (_instanceTransformBuffer)
|
2023-07-28 14:13:01 +08:00
|
|
|
|
{
|
2023-08-13 23:56:58 +08:00
|
|
|
|
const auto& attribOffset = _programState->getVertexLayout()->getAttributes().size();
|
2023-08-13 22:09:02 +08:00
|
|
|
|
|
2023-08-13 23:56:58 +08:00
|
|
|
|
for (GLubyte i = attribOffset; i < attribOffset + 4; i++)
|
|
|
|
|
glVertexAttribDivisor(i, 0);
|
2023-07-28 14:13:01 +08:00
|
|
|
|
}
|
2023-08-13 23:56:58 +08:00
|
|
|
|
#endif
|
2023-07-28 14:13:01 +08:00
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setLineWidth(float lineWidth)
|
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (lineWidth > 0.0f)
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->lineWidth(lineWidth);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
else
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->lineWidth(1.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandBufferGL::setScissorRect(bool isEnabled, float x, float y, float width, float height)
|
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (isEnabled)
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->enableScissor(x, y, width, height);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
else
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->disableScissor();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-09-21 22:10:50 +08:00
|
|
|
|
void CommandBufferGL::readPixels(RenderTarget* rt, std::function<void(const PixelBufferDescriptor&)> callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
2020-09-11 01:19:10 +08:00
|
|
|
|
PixelBufferDescriptor pbd;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (rt->isDefaultRenderTarget())
|
|
|
|
|
{ // read pixels from screen
|
2023-08-13 23:56:58 +08:00
|
|
|
|
readPixels(rt, _viewPort.x, _viewPort.y, _viewPort.width, _viewPort.height, _viewPort.width * 4, pbd);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
|
// we only readPixels from the COLOR0 attachment.
|
|
|
|
|
auto colorAttachment = rt->_color[0].texture;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (colorAttachment)
|
|
|
|
|
{
|
|
|
|
|
readPixels(rt, 0, 0, colorAttachment->getWidth(), colorAttachment->getHeight(),
|
|
|
|
|
colorAttachment->getWidth() * 4, pbd);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-11 01:19:10 +08:00
|
|
|
|
callback(pbd);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void CommandBufferGL::readPixels(RenderTarget* rt,
|
|
|
|
|
int x,
|
|
|
|
|
int y,
|
|
|
|
|
uint32_t width,
|
|
|
|
|
uint32_t height,
|
|
|
|
|
uint32_t bytesPerRow,
|
|
|
|
|
PixelBufferDescriptor& pbd)
|
2020-09-21 22:10:50 +08:00
|
|
|
|
{
|
2022-06-24 14:18:48 +08:00
|
|
|
|
auto rtGL = static_cast<RenderTargetGL*>(rt);
|
|
|
|
|
rtGL->bindFrameBuffer();
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
|
|
|
|
|
|
auto bufferSize = bytesPerRow * height;
|
2022-07-16 10:43:05 +08:00
|
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 && defined(GL_ES_VERSION_3_0)) || \
|
|
|
|
|
(AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID && defined(GL_PIXEL_PACK_BUFFER))
|
2020-09-21 22:10:50 +08:00
|
|
|
|
GLuint pbo;
|
|
|
|
|
glGenBuffers(1, &pbo);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->bindBuffer(BufferType::PIXEL_PACK_BUFFER, pbo);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
glBufferData(GL_PIXEL_PACK_BUFFER, bufferSize, nullptr, GL_STATIC_DRAW);
|
|
|
|
|
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
|
auto buffer = (uint8_t*)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, bufferSize, GL_MAP_READ_BIT);
|
|
|
|
|
#else
|
|
|
|
|
std::unique_ptr<uint8_t[]> bufferStorage(new uint8_t[bufferSize]);
|
|
|
|
|
auto buffer = bufferStorage.get();
|
|
|
|
|
memset(buffer, 0, bufferSize);
|
|
|
|
|
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
|
|
|
|
|
#endif
|
|
|
|
|
uint8_t* wptr = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (buffer && (wptr = pbd._data.resize(bufferSize)))
|
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
|
auto rptr = buffer + (height - 1) * bytesPerRow;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
for (int row = 0; row < height; ++row)
|
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
|
memcpy(wptr, rptr, bytesPerRow);
|
|
|
|
|
wptr += bytesPerRow;
|
|
|
|
|
rptr -= bytesPerRow;
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
pbd._width = width;
|
2020-09-21 22:10:50 +08:00
|
|
|
|
pbd._height = height;
|
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 && defined(GL_ES_VERSION_3_0)) || \
|
|
|
|
|
(AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID && defined(GL_PIXEL_PACK_BUFFER))
|
2020-09-21 22:10:50 +08:00
|
|
|
|
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
|
2023-08-13 23:56:58 +08:00
|
|
|
|
__gl->bindBuffer(BufferType::PIXEL_PACK_BUFFER, 0);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
glDeleteBuffers(1, &pbo);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
|
if (!rtGL->isDefaultRenderTarget())
|
|
|
|
|
rtGL->unbindFrameBuffer();
|
2020-09-21 22:10:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
|
NS_AX_BACKEND_END
|