axmol/core/renderer/backend/opengl/DeviceGL.cpp

138 lines
3.9 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
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 "DeviceGL.h"
#include "RenderPipelineGL.h"
#include "BufferGL.h"
#include "ShaderModuleGL.h"
#include "CommandBufferGL.h"
#include "TextureGL.h"
#include "DepthStencilStateGL.h"
#include "ProgramGL.h"
#include "DeviceInfoGL.h"
#include "RenderTargetGL.h"
2023-08-25 16:46:32 +08:00
2022-10-12 19:44:31 +08:00
#include "renderer/backend/ProgramManager.h"
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
Device* Device::getInstance()
{
if (!_instance)
2021-12-08 00:11:53 +08:00
_instance = new DeviceGL();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return _instance;
}
DeviceGL::DeviceGL()
{
2021-12-08 00:11:53 +08:00
_deviceInfo = new DeviceInfoGL();
2021-12-25 10:04:45 +08:00
if (!_deviceInfo->init())
2019-11-23 20:27:39 +08:00
{
delete _deviceInfo;
_deviceInfo = nullptr;
}
2020-10-27 16:58:37 +08:00
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_defaultFBO);
2019-11-23 20:27:39 +08:00
}
DeviceGL::~DeviceGL()
{
2022-10-12 19:44:31 +08:00
ProgramManager::destroyInstance();
2019-11-23 20:27:39 +08:00
delete _deviceInfo;
_deviceInfo = nullptr;
}
2020-10-27 16:58:37 +08:00
GLint DeviceGL::getDefaultFBO() const
{
return _defaultFBO;
}
2019-11-23 20:27:39 +08:00
CommandBuffer* DeviceGL::newCommandBuffer()
{
2021-12-08 00:11:53 +08:00
return new CommandBufferGL();
2019-11-23 20:27:39 +08:00
}
Buffer* DeviceGL::newBuffer(std::size_t size, BufferType type, BufferUsage usage)
{
2021-12-08 00:11:53 +08:00
return new BufferGL(size, type, usage);
2019-11-23 20:27:39 +08:00
}
TextureBackend* DeviceGL::newTexture(const TextureDescriptor& descriptor)
{
switch (descriptor.textureType)
{
case TextureType::TEXTURE_2D:
2021-12-08 00:11:53 +08:00
return new Texture2DGL(descriptor);
2019-11-23 20:27:39 +08:00
case TextureType::TEXTURE_CUBE:
2021-12-08 00:11:53 +08:00
return new TextureCubeGL(descriptor);
2019-11-23 20:27:39 +08:00
default:
return nullptr;
}
}
RenderTarget* DeviceGL::newDefaultRenderTarget(TargetBufferFlags rtf)
{
2020-10-27 16:58:37 +08:00
auto rtGL = new RenderTargetGL(true, this);
rtGL->setTargetFlags(rtf);
return rtGL;
}
RenderTarget* DeviceGL::newRenderTarget(TargetBufferFlags rtf,
2021-12-25 10:04:45 +08:00
TextureBackend* colorAttachment,
TextureBackend* depthAttachment,
TextureBackend* stencilAttachhment)
{
2020-10-27 16:58:37 +08:00
auto rtGL = new RenderTargetGL(false, this);
rtGL->setTargetFlags(rtf);
rtGL->bindFrameBuffer();
2021-12-25 10:04:45 +08:00
RenderTarget::ColorAttachment colors{{colorAttachment, 0}};
rtGL->setColorAttachment(colors);
rtGL->setDepthAttachment(depthAttachment);
rtGL->setStencilAttachment(stencilAttachhment);
return rtGL;
}
ShaderModule* DeviceGL::newShaderModule(ShaderStage stage, std::string_view source)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
return new ShaderModuleGL(stage, source);
2019-11-23 20:27:39 +08:00
}
DepthStencilState* DeviceGL::newDepthStencilState()
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
return new DepthStencilStateGL();
2019-11-23 20:27:39 +08:00
}
RenderPipeline* DeviceGL::newRenderPipeline()
{
2021-12-08 00:11:53 +08:00
return new RenderPipelineGL();
2019-11-23 20:27:39 +08:00
}
Program* DeviceGL::newProgram(std::string_view vertexShader, std::string_view fragmentShader)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
return new ProgramGL(vertexShader, fragmentShader);
2019-11-23 20:27:39 +08:00
}
NS_AX_BACKEND_END