mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-14 07:36:15 +00:00
Add DepthStencilState
Add depth and stencil tests. This is currently only implemented for the OpenGL API. HelloDepthStencil is a test using the depth and stencil buffers to do reflections. Currently clearing / stencil clearing is not working properly.
This commit is contained in:
committed by
Corentin Wallez
parent
2711e9658d
commit
3efcf2172d
@@ -1,199 +1,300 @@
|
||||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "OpenGLBackend.h"
|
||||
|
||||
#include "CommandBufferGL.h"
|
||||
#include "PipelineGL.h"
|
||||
#include "PipelineLayoutGL.h"
|
||||
#include "ShaderModuleGL.h"
|
||||
#include "SamplerGL.h"
|
||||
#include "TextureGL.h"
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
nxtProcTable GetNonValidatingProcs();
|
||||
nxtProcTable GetValidatingProcs();
|
||||
|
||||
void HACKCLEAR() {
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void Init(void* (*getProc)(const char*), nxtProcTable* procs, nxtDevice* device) {
|
||||
*device = nullptr;
|
||||
|
||||
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(getProc));
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
HACKCLEAR();
|
||||
|
||||
*procs = GetValidatingProcs();
|
||||
*device = reinterpret_cast<nxtDevice>(new Device);
|
||||
}
|
||||
|
||||
// Device
|
||||
|
||||
BindGroupBase* Device::CreateBindGroup(BindGroupBuilder* builder) {
|
||||
return new BindGroup(this, builder);
|
||||
}
|
||||
BindGroupLayoutBase* Device::CreateBindGroupLayout(BindGroupLayoutBuilder* builder) {
|
||||
return new BindGroupLayout(this, builder);
|
||||
}
|
||||
BufferBase* Device::CreateBuffer(BufferBuilder* builder) {
|
||||
return new Buffer(this, builder);
|
||||
}
|
||||
BufferViewBase* Device::CreateBufferView(BufferViewBuilder* builder) {
|
||||
return new BufferView(this, builder);
|
||||
}
|
||||
CommandBufferBase* Device::CreateCommandBuffer(CommandBufferBuilder* builder) {
|
||||
return new CommandBuffer(this, builder);
|
||||
}
|
||||
InputStateBase* Device::CreateInputState(InputStateBuilder* builder) {
|
||||
return new InputState(this, builder);
|
||||
}
|
||||
FramebufferBase* Device::CreateFramebuffer(FramebufferBuilder* builder) {
|
||||
return new Framebuffer(this, builder);
|
||||
}
|
||||
PipelineBase* Device::CreatePipeline(PipelineBuilder* builder) {
|
||||
return new Pipeline(this, builder);
|
||||
}
|
||||
PipelineLayoutBase* Device::CreatePipelineLayout(PipelineLayoutBuilder* builder) {
|
||||
return new PipelineLayout(this, builder);
|
||||
}
|
||||
QueueBase* Device::CreateQueue(QueueBuilder* builder) {
|
||||
return new Queue(this, builder);
|
||||
}
|
||||
RenderPassBase* Device::CreateRenderPass(RenderPassBuilder* builder) {
|
||||
return new RenderPass(this, builder);
|
||||
}
|
||||
SamplerBase* Device::CreateSampler(SamplerBuilder* builder) {
|
||||
return new Sampler(this, builder);
|
||||
}
|
||||
ShaderModuleBase* Device::CreateShaderModule(ShaderModuleBuilder* builder) {
|
||||
return new ShaderModule(this, builder);
|
||||
}
|
||||
TextureBase* Device::CreateTexture(TextureBuilder* builder) {
|
||||
return new Texture(this, builder);
|
||||
}
|
||||
TextureViewBase* Device::CreateTextureView(TextureViewBuilder* builder) {
|
||||
return new TextureView(this, builder);
|
||||
}
|
||||
|
||||
void Device::Reference() {
|
||||
}
|
||||
|
||||
void Device::Release() {
|
||||
}
|
||||
|
||||
// Bind Group
|
||||
|
||||
BindGroup::BindGroup(Device* device, BindGroupBuilder* builder)
|
||||
: BindGroupBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// Bind Group Layout
|
||||
|
||||
BindGroupLayout::BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder)
|
||||
: BindGroupLayoutBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// Buffer
|
||||
|
||||
Buffer::Buffer(Device* device, BufferBuilder* builder)
|
||||
: BufferBase(builder), device(device) {
|
||||
glGenBuffers(1, &buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, GetSize(), nullptr, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
GLuint Buffer::GetHandle() const {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, start * sizeof(uint32_t), count * sizeof(uint32_t), data);
|
||||
}
|
||||
|
||||
// BufferView
|
||||
|
||||
BufferView::BufferView(Device* device, BufferViewBuilder* builder)
|
||||
: BufferViewBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// InputState
|
||||
|
||||
InputState::InputState(Device* device, InputStateBuilder* builder)
|
||||
: InputStateBase(builder), device(device) {
|
||||
glGenVertexArrays(1, &vertexArrayObject);
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
auto& attributesSetMask = GetAttributesSetMask();
|
||||
for (uint32_t location = 0; location < attributesSetMask.size(); ++location) {
|
||||
if (!attributesSetMask[location]) {
|
||||
continue;
|
||||
}
|
||||
auto attribute = GetAttribute(location);
|
||||
glEnableVertexAttribArray(location);
|
||||
|
||||
auto input = GetInput(attribute.bindingSlot);
|
||||
if (input.stride == 0) {
|
||||
// Emulate a stride of zero (constant vertex attribute) by
|
||||
// setting the attribute instance divisor to a huge number.
|
||||
glVertexAttribDivisor(location, 0xffffffff);
|
||||
} else {
|
||||
switch (input.stepMode) {
|
||||
case nxt::InputStepMode::Vertex:
|
||||
break;
|
||||
case nxt::InputStepMode::Instance:
|
||||
glVertexAttribDivisor(location, 1);
|
||||
break;
|
||||
default:
|
||||
ASSERT(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLuint InputState::GetVAO() {
|
||||
return vertexArrayObject;
|
||||
}
|
||||
|
||||
// Framebuffer
|
||||
|
||||
Framebuffer::Framebuffer(Device* device, FramebufferBuilder* builder)
|
||||
: FramebufferBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// Queue
|
||||
|
||||
Queue::Queue(Device* device, QueueBuilder* builder)
|
||||
: QueueBase(builder), device(device) {
|
||||
}
|
||||
|
||||
void Queue::Submit(uint32_t numCommands, CommandBuffer* const * commands) {
|
||||
for (uint32_t i = 0; i < numCommands; ++i) {
|
||||
commands[i]->Execute();
|
||||
}
|
||||
}
|
||||
|
||||
// RenderPass
|
||||
|
||||
RenderPass::RenderPass(Device* device, RenderPassBuilder* builder)
|
||||
: RenderPassBase(builder), device(device) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "OpenGLBackend.h"
|
||||
|
||||
#include "CommandBufferGL.h"
|
||||
#include "PipelineGL.h"
|
||||
#include "PipelineLayoutGL.h"
|
||||
#include "ShaderModuleGL.h"
|
||||
#include "SamplerGL.h"
|
||||
#include "TextureGL.h"
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
nxtProcTable GetNonValidatingProcs();
|
||||
nxtProcTable GetValidatingProcs();
|
||||
|
||||
void HACKCLEAR() {
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearStencil(0);
|
||||
glClear(GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void Init(void* (*getProc)(const char*), nxtProcTable* procs, nxtDevice* device) {
|
||||
*device = nullptr;
|
||||
|
||||
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(getProc));
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
HACKCLEAR();
|
||||
|
||||
*procs = GetValidatingProcs();
|
||||
*device = reinterpret_cast<nxtDevice>(new Device);
|
||||
}
|
||||
|
||||
static GLuint OpenGLCompareFunction(nxt::CompareFunction compareFunction) {
|
||||
switch (compareFunction) {
|
||||
case nxt::CompareFunction::Never:
|
||||
return GL_NEVER;
|
||||
case nxt::CompareFunction::Less:
|
||||
return GL_LESS;
|
||||
case nxt::CompareFunction::LessEqual:
|
||||
return GL_LEQUAL;
|
||||
case nxt::CompareFunction::Greater:
|
||||
return GL_GREATER;
|
||||
case nxt::CompareFunction::GreaterEqual:
|
||||
return GL_GEQUAL;
|
||||
case nxt::CompareFunction::NotEqual:
|
||||
return GL_NOTEQUAL;
|
||||
case nxt::CompareFunction::Equal:
|
||||
return GL_EQUAL;
|
||||
case nxt::CompareFunction::Always:
|
||||
return GL_ALWAYS;
|
||||
default:
|
||||
ASSERT(false);
|
||||
}
|
||||
}
|
||||
|
||||
static GLuint OpenGLStencilOperation(nxt::StencilOperation stencilOperation) {
|
||||
switch (stencilOperation) {
|
||||
case nxt::StencilOperation::Keep:
|
||||
return GL_KEEP;
|
||||
case nxt::StencilOperation::Zero:
|
||||
return GL_ZERO;
|
||||
case nxt::StencilOperation::Replace:
|
||||
return GL_REPLACE;
|
||||
case nxt::StencilOperation::Invert:
|
||||
return GL_INVERT;
|
||||
case nxt::StencilOperation::IncrementClamp:
|
||||
return GL_INCR;
|
||||
case nxt::StencilOperation::DecrementClamp:
|
||||
return GL_DECR;
|
||||
case nxt::StencilOperation::IncrementWrap:
|
||||
return GL_INCR_WRAP;
|
||||
case nxt::StencilOperation::DecrementWrap:
|
||||
return GL_DECR_WRAP;
|
||||
default:
|
||||
ASSERT(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Device
|
||||
|
||||
BindGroupBase* Device::CreateBindGroup(BindGroupBuilder* builder) {
|
||||
return new BindGroup(this, builder);
|
||||
}
|
||||
BindGroupLayoutBase* Device::CreateBindGroupLayout(BindGroupLayoutBuilder* builder) {
|
||||
return new BindGroupLayout(this, builder);
|
||||
}
|
||||
BufferBase* Device::CreateBuffer(BufferBuilder* builder) {
|
||||
return new Buffer(this, builder);
|
||||
}
|
||||
BufferViewBase* Device::CreateBufferView(BufferViewBuilder* builder) {
|
||||
return new BufferView(this, builder);
|
||||
}
|
||||
CommandBufferBase* Device::CreateCommandBuffer(CommandBufferBuilder* builder) {
|
||||
return new CommandBuffer(this, builder);
|
||||
}
|
||||
DepthStencilStateBase* Device::CreateDepthStencilState(DepthStencilStateBuilder* builder) {
|
||||
return new DepthStencilState(this, builder);
|
||||
}
|
||||
InputStateBase* Device::CreateInputState(InputStateBuilder* builder) {
|
||||
return new InputState(this, builder);
|
||||
}
|
||||
FramebufferBase* Device::CreateFramebuffer(FramebufferBuilder* builder) {
|
||||
return new Framebuffer(this, builder);
|
||||
}
|
||||
PipelineBase* Device::CreatePipeline(PipelineBuilder* builder) {
|
||||
return new Pipeline(this, builder);
|
||||
}
|
||||
PipelineLayoutBase* Device::CreatePipelineLayout(PipelineLayoutBuilder* builder) {
|
||||
return new PipelineLayout(this, builder);
|
||||
}
|
||||
QueueBase* Device::CreateQueue(QueueBuilder* builder) {
|
||||
return new Queue(this, builder);
|
||||
}
|
||||
RenderPassBase* Device::CreateRenderPass(RenderPassBuilder* builder) {
|
||||
return new RenderPass(this, builder);
|
||||
}
|
||||
SamplerBase* Device::CreateSampler(SamplerBuilder* builder) {
|
||||
return new Sampler(this, builder);
|
||||
}
|
||||
ShaderModuleBase* Device::CreateShaderModule(ShaderModuleBuilder* builder) {
|
||||
return new ShaderModule(this, builder);
|
||||
}
|
||||
TextureBase* Device::CreateTexture(TextureBuilder* builder) {
|
||||
return new Texture(this, builder);
|
||||
}
|
||||
TextureViewBase* Device::CreateTextureView(TextureViewBuilder* builder) {
|
||||
return new TextureView(this, builder);
|
||||
}
|
||||
|
||||
void Device::Reference() {
|
||||
}
|
||||
|
||||
void Device::Release() {
|
||||
}
|
||||
|
||||
// Bind Group
|
||||
|
||||
BindGroup::BindGroup(Device* device, BindGroupBuilder* builder)
|
||||
: BindGroupBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// Bind Group Layout
|
||||
|
||||
BindGroupLayout::BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder)
|
||||
: BindGroupLayoutBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// Buffer
|
||||
|
||||
Buffer::Buffer(Device* device, BufferBuilder* builder)
|
||||
: BufferBase(builder), device(device) {
|
||||
glGenBuffers(1, &buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, GetSize(), nullptr, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
GLuint Buffer::GetHandle() const {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, start * sizeof(uint32_t), count * sizeof(uint32_t), data);
|
||||
}
|
||||
|
||||
// BufferView
|
||||
|
||||
BufferView::BufferView(Device* device, BufferViewBuilder* builder)
|
||||
: BufferViewBase(builder), device(device) {
|
||||
}
|
||||
|
||||
DepthStencilState::DepthStencilState(Device* device, DepthStencilStateBuilder* builder)
|
||||
: DepthStencilStateBase(builder), device(device) {
|
||||
|
||||
}
|
||||
|
||||
void DepthStencilState::Apply() {
|
||||
if (DepthIsEnabled()) {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
auto& depth = GetDepth();
|
||||
glDepthFunc(OpenGLCompareFunction(depth.compareFunction));
|
||||
switch (depth.depthWriteMode) {
|
||||
case nxt::DepthWriteMode::Disabled:
|
||||
glDepthMask(GL_FALSE);
|
||||
break;
|
||||
case nxt::DepthWriteMode::Enabled:
|
||||
glDepthMask(GL_TRUE);
|
||||
break;
|
||||
default:
|
||||
ASSERT(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
|
||||
static const GLuint GL_FACES[2] = { GL_BACK, GL_FRONT };
|
||||
static const nxt::Face NXT_FACES[2] = { nxt::Face::Back, nxt::Face::Front };
|
||||
if (StencilIsEnabled()) {
|
||||
glEnable(GL_STENCIL_TEST);
|
||||
for (uint32_t i = 0; i < 2; ++i) {
|
||||
auto& stencil = GetStencil(NXT_FACES[i]);
|
||||
glStencilFuncSeparate(GL_FACES[i],
|
||||
OpenGLCompareFunction(stencil.compareFunction),
|
||||
stencil.reference,
|
||||
stencil.readMask
|
||||
);
|
||||
glStencilOpSeparate(GL_FACES[i],
|
||||
OpenGLStencilOperation(stencil.stencilFail),
|
||||
OpenGLStencilOperation(stencil.depthFail),
|
||||
OpenGLStencilOperation(stencil.stencilPass)
|
||||
);
|
||||
glStencilMaskSeparate(GL_FACES[i], stencil.writeMask);
|
||||
}
|
||||
}
|
||||
else {
|
||||
glDisable(GL_STENCIL_TEST);
|
||||
}
|
||||
}
|
||||
|
||||
// InputState
|
||||
|
||||
InputState::InputState(Device* device, InputStateBuilder* builder)
|
||||
: InputStateBase(builder), device(device) {
|
||||
glGenVertexArrays(1, &vertexArrayObject);
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
auto& attributesSetMask = GetAttributesSetMask();
|
||||
for (uint32_t location = 0; location < attributesSetMask.size(); ++location) {
|
||||
if (!attributesSetMask[location]) {
|
||||
continue;
|
||||
}
|
||||
auto attribute = GetAttribute(location);
|
||||
glEnableVertexAttribArray(location);
|
||||
|
||||
auto input = GetInput(attribute.bindingSlot);
|
||||
if (input.stride == 0) {
|
||||
// Emulate a stride of zero (constant vertex attribute) by
|
||||
// setting the attribute instance divisor to a huge number.
|
||||
glVertexAttribDivisor(location, 0xffffffff);
|
||||
} else {
|
||||
switch (input.stepMode) {
|
||||
case nxt::InputStepMode::Vertex:
|
||||
break;
|
||||
case nxt::InputStepMode::Instance:
|
||||
glVertexAttribDivisor(location, 1);
|
||||
break;
|
||||
default:
|
||||
ASSERT(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLuint InputState::GetVAO() {
|
||||
return vertexArrayObject;
|
||||
}
|
||||
|
||||
// Framebuffer
|
||||
|
||||
Framebuffer::Framebuffer(Device* device, FramebufferBuilder* builder)
|
||||
: FramebufferBase(builder), device(device) {
|
||||
}
|
||||
|
||||
// Queue
|
||||
|
||||
Queue::Queue(Device* device, QueueBuilder* builder)
|
||||
: QueueBase(builder), device(device) {
|
||||
}
|
||||
|
||||
void Queue::Submit(uint32_t numCommands, CommandBuffer* const * commands) {
|
||||
for (uint32_t i = 0; i < numCommands; ++i) {
|
||||
commands[i]->Execute();
|
||||
}
|
||||
}
|
||||
|
||||
// RenderPass
|
||||
|
||||
RenderPass::RenderPass(Device* device, RenderPassBuilder* builder)
|
||||
: RenderPassBase(builder), device(device) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,175 +1,188 @@
|
||||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef BACKEND_OPENGL_OPENGLBACKEND_H_
|
||||
#define BACKEND_OPENGL_OPENGLBACKEND_H_
|
||||
|
||||
#include "nxt/nxtcpp.h"
|
||||
|
||||
#include "common/Buffer.h"
|
||||
#include "common/BindGroup.h"
|
||||
#include "common/BindGroupLayout.h"
|
||||
#include "common/Device.h"
|
||||
#include "common/Framebuffer.h"
|
||||
#include "common/InputState.h"
|
||||
#include "common/Queue.h"
|
||||
#include "common/RenderPass.h"
|
||||
#include "common/ToBackend.h"
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
|
||||
class BindGroup;
|
||||
class BindGroupLayout;
|
||||
class Buffer;
|
||||
class BufferView;
|
||||
class CommandBuffer;
|
||||
class InputState;
|
||||
class Pipeline;
|
||||
class PipelineLayout;
|
||||
class Queue;
|
||||
class Sampler;
|
||||
class ShaderModule;
|
||||
class Texture;
|
||||
class TextureView;
|
||||
class Framebuffer;
|
||||
class RenderPass;
|
||||
|
||||
struct OpenGLBackendTraits {
|
||||
using BindGroupType = BindGroup;
|
||||
using BindGroupLayoutType = BindGroupLayout;
|
||||
using BufferType = Buffer;
|
||||
using BufferViewType = BufferView;
|
||||
using CommandBufferType = CommandBuffer;
|
||||
using InputStateType = InputState;
|
||||
using PipelineType = Pipeline;
|
||||
using PipelineLayoutType = PipelineLayout;
|
||||
using QueueType = Queue;
|
||||
using SamplerType = Sampler;
|
||||
using ShaderModuleType = ShaderModule;
|
||||
using TextureType = Texture;
|
||||
using TextureViewType = TextureView;
|
||||
using FramebufferType = Framebuffer;
|
||||
using RenderPassType = RenderPass;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
auto ToBackend(T&& common) -> decltype(ToBackendBase<OpenGLBackendTraits>(common)) {
|
||||
return ToBackendBase<OpenGLBackendTraits>(common);
|
||||
}
|
||||
|
||||
// Definition of backend types
|
||||
class Device : public DeviceBase {
|
||||
public:
|
||||
BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override;
|
||||
BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) override;
|
||||
BufferBase* CreateBuffer(BufferBuilder* builder) override;
|
||||
BufferViewBase* CreateBufferView(BufferViewBuilder* builder) override;
|
||||
CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override;
|
||||
InputStateBase* CreateInputState(InputStateBuilder* builder) override;
|
||||
FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) override;
|
||||
PipelineBase* CreatePipeline(PipelineBuilder* builder) override;
|
||||
PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) override;
|
||||
QueueBase* CreateQueue(QueueBuilder* builder) override;
|
||||
RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) override;
|
||||
SamplerBase* CreateSampler(SamplerBuilder* builder) override;
|
||||
ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) override;
|
||||
TextureBase* CreateTexture(TextureBuilder* builder) override;
|
||||
TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override;
|
||||
|
||||
// NXT API
|
||||
void Reference();
|
||||
void Release();
|
||||
};
|
||||
|
||||
class BindGroup : public BindGroupBase {
|
||||
public:
|
||||
BindGroup(Device* device, BindGroupBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class BindGroupLayout : public BindGroupLayoutBase {
|
||||
public:
|
||||
BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class Buffer : public BufferBase {
|
||||
public:
|
||||
Buffer(Device* device, BufferBuilder* builder);
|
||||
|
||||
GLuint GetHandle() const;
|
||||
|
||||
private:
|
||||
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
|
||||
|
||||
Device* device;
|
||||
GLuint buffer = 0;
|
||||
};
|
||||
|
||||
class BufferView : public BufferViewBase {
|
||||
public:
|
||||
BufferView(Device* device, BufferViewBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class Framebuffer : public FramebufferBase {
|
||||
public:
|
||||
Framebuffer(Device* device, FramebufferBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class InputState : public InputStateBase {
|
||||
public:
|
||||
InputState(Device* device, InputStateBuilder* builder);
|
||||
GLuint GetVAO();
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
GLuint vertexArrayObject;
|
||||
};
|
||||
|
||||
class Queue : public QueueBase {
|
||||
public:
|
||||
Queue(Device* device, QueueBuilder* builder);
|
||||
|
||||
// NXT API
|
||||
void Submit(uint32_t numCommands, CommandBuffer* const * commands);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class RenderPass : public RenderPassBase {
|
||||
public:
|
||||
RenderPass(Device* device, RenderPassBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // BACKEND_OPENGL_OPENGLBACKEND_H_
|
||||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef BACKEND_OPENGL_OPENGLBACKEND_H_
|
||||
#define BACKEND_OPENGL_OPENGLBACKEND_H_
|
||||
|
||||
#include "nxt/nxtcpp.h"
|
||||
|
||||
#include "common/Buffer.h"
|
||||
#include "common/BindGroup.h"
|
||||
#include "common/BindGroupLayout.h"
|
||||
#include "common/Device.h"
|
||||
#include "common/DepthStencilState.h"
|
||||
#include "common/Framebuffer.h"
|
||||
#include "common/InputState.h"
|
||||
#include "common/Queue.h"
|
||||
#include "common/RenderPass.h"
|
||||
#include "common/ToBackend.h"
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
|
||||
class BindGroup;
|
||||
class BindGroupLayout;
|
||||
class Buffer;
|
||||
class BufferView;
|
||||
class CommandBuffer;
|
||||
class DepthStencilState;
|
||||
class InputState;
|
||||
class Pipeline;
|
||||
class PipelineLayout;
|
||||
class Queue;
|
||||
class Sampler;
|
||||
class ShaderModule;
|
||||
class Texture;
|
||||
class TextureView;
|
||||
class Framebuffer;
|
||||
class RenderPass;
|
||||
|
||||
struct OpenGLBackendTraits {
|
||||
using BindGroupType = BindGroup;
|
||||
using BindGroupLayoutType = BindGroupLayout;
|
||||
using BufferType = Buffer;
|
||||
using BufferViewType = BufferView;
|
||||
using CommandBufferType = CommandBuffer;
|
||||
using DepthStencilStateType = DepthStencilState;
|
||||
using InputStateType = InputState;
|
||||
using PipelineType = Pipeline;
|
||||
using PipelineLayoutType = PipelineLayout;
|
||||
using QueueType = Queue;
|
||||
using SamplerType = Sampler;
|
||||
using ShaderModuleType = ShaderModule;
|
||||
using TextureType = Texture;
|
||||
using TextureViewType = TextureView;
|
||||
using FramebufferType = Framebuffer;
|
||||
using RenderPassType = RenderPass;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
auto ToBackend(T&& common) -> decltype(ToBackendBase<OpenGLBackendTraits>(common)) {
|
||||
return ToBackendBase<OpenGLBackendTraits>(common);
|
||||
}
|
||||
|
||||
// Definition of backend types
|
||||
class Device : public DeviceBase {
|
||||
public:
|
||||
BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override;
|
||||
BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) override;
|
||||
BufferBase* CreateBuffer(BufferBuilder* builder) override;
|
||||
BufferViewBase* CreateBufferView(BufferViewBuilder* builder) override;
|
||||
CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override;
|
||||
DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override;
|
||||
InputStateBase* CreateInputState(InputStateBuilder* builder) override;
|
||||
FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) override;
|
||||
PipelineBase* CreatePipeline(PipelineBuilder* builder) override;
|
||||
PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) override;
|
||||
QueueBase* CreateQueue(QueueBuilder* builder) override;
|
||||
RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) override;
|
||||
SamplerBase* CreateSampler(SamplerBuilder* builder) override;
|
||||
ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) override;
|
||||
TextureBase* CreateTexture(TextureBuilder* builder) override;
|
||||
TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override;
|
||||
|
||||
// NXT API
|
||||
void Reference();
|
||||
void Release();
|
||||
};
|
||||
|
||||
class BindGroup : public BindGroupBase {
|
||||
public:
|
||||
BindGroup(Device* device, BindGroupBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class BindGroupLayout : public BindGroupLayoutBase {
|
||||
public:
|
||||
BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class Buffer : public BufferBase {
|
||||
public:
|
||||
Buffer(Device* device, BufferBuilder* builder);
|
||||
|
||||
GLuint GetHandle() const;
|
||||
|
||||
private:
|
||||
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
|
||||
|
||||
Device* device;
|
||||
GLuint buffer = 0;
|
||||
};
|
||||
|
||||
class BufferView : public BufferViewBase {
|
||||
public:
|
||||
BufferView(Device* device, BufferViewBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class DepthStencilState : public DepthStencilStateBase {
|
||||
public:
|
||||
DepthStencilState(Device* device, DepthStencilStateBuilder* builder);
|
||||
void Apply();
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class Framebuffer : public FramebufferBase {
|
||||
public:
|
||||
Framebuffer(Device* device, FramebufferBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class InputState : public InputStateBase {
|
||||
public:
|
||||
InputState(Device* device, InputStateBuilder* builder);
|
||||
GLuint GetVAO();
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
GLuint vertexArrayObject;
|
||||
};
|
||||
|
||||
class Queue : public QueueBase {
|
||||
public:
|
||||
Queue(Device* device, QueueBuilder* builder);
|
||||
|
||||
// NXT API
|
||||
void Submit(uint32_t numCommands, CommandBuffer* const * commands);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
class RenderPass : public RenderPassBase {
|
||||
public:
|
||||
RenderPass(Device* device, RenderPassBuilder* builder);
|
||||
|
||||
private:
|
||||
Device* device;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // BACKEND_OPENGL_OPENGLBACKEND_H_
|
||||
|
||||
@@ -1,213 +1,215 @@
|
||||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "PipelineGL.h"
|
||||
|
||||
#include "OpenGLBackend.h"
|
||||
#include "PipelineLayoutGL.h"
|
||||
#include "ShaderModuleGL.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
|
||||
namespace {
|
||||
|
||||
GLenum GLShaderType(nxt::ShaderStage stage) {
|
||||
switch (stage) {
|
||||
case nxt::ShaderStage::Vertex:
|
||||
return GL_VERTEX_SHADER;
|
||||
case nxt::ShaderStage::Fragment:
|
||||
return GL_FRAGMENT_SHADER;
|
||||
case nxt::ShaderStage::Compute:
|
||||
return GL_COMPUTE_SHADER;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Pipeline::Pipeline(Device* device, PipelineBuilder* builder) : PipelineBase(builder), device(device) {
|
||||
auto CreateShader = [](GLenum type, const char* source) -> GLuint {
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &source, nullptr);
|
||||
glCompileShader(shader);
|
||||
|
||||
GLint compileStatus = GL_FALSE;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
|
||||
if (compileStatus == GL_FALSE) {
|
||||
GLint infoLogLength = 0;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
|
||||
if (infoLogLength > 1) {
|
||||
std::vector<char> buffer(infoLogLength);
|
||||
glGetShaderInfoLog(shader, infoLogLength, nullptr, &buffer[0]);
|
||||
std::cout << source << std::endl;
|
||||
std::cout << "Program compilation failed:\n";
|
||||
std::cout << buffer.data() << std::endl;
|
||||
}
|
||||
}
|
||||
return shader;
|
||||
};
|
||||
|
||||
auto FillPushConstants = [](const ShaderModule* module, GLPushConstantInfo* info, GLuint program) {
|
||||
const auto& moduleInfo = module->GetPushConstants();
|
||||
for (uint32_t i = 0; i < moduleInfo.names.size(); i++) {
|
||||
(*info)[i] = -1;
|
||||
|
||||
unsigned int size = moduleInfo.sizes[i];
|
||||
if (size == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
GLint location = glGetUniformLocation(program, moduleInfo.names[i].c_str());
|
||||
if (location == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint32_t offset = 0; offset < size; offset++) {
|
||||
(*info)[i + offset] = location + offset;
|
||||
}
|
||||
i += size - 1;
|
||||
}
|
||||
};
|
||||
|
||||
program = glCreateProgram();
|
||||
|
||||
for (auto stage : IterateStages(GetStageMask())) {
|
||||
const ShaderModule* module = ToBackend(builder->GetStageInfo(stage).module.Get());
|
||||
|
||||
GLuint shader = CreateShader(GLShaderType(stage), module->GetSource());
|
||||
glAttachShader(program, shader);
|
||||
}
|
||||
|
||||
glLinkProgram(program);
|
||||
|
||||
GLint linkStatus = GL_FALSE;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
|
||||
if (linkStatus == GL_FALSE) {
|
||||
GLint infoLogLength = 0;
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
|
||||
if (infoLogLength > 1) {
|
||||
std::vector<char> buffer(infoLogLength);
|
||||
glGetProgramInfoLog(program, infoLogLength, nullptr, &buffer[0]);
|
||||
std::cout << "Program link failed:\n";
|
||||
std::cout << buffer.data() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto stage : IterateStages(GetStageMask())) {
|
||||
const ShaderModule* module = ToBackend(builder->GetStageInfo(stage).module.Get());
|
||||
FillPushConstants(module, &glPushConstants[stage], program);
|
||||
}
|
||||
|
||||
glUseProgram(program);
|
||||
|
||||
// The uniforms are part of the program state so we can pre-bind buffer units, texture units etc.
|
||||
const auto& layout = ToBackend(GetLayout());
|
||||
const auto& indices = layout->GetBindingIndexInfo();
|
||||
|
||||
for (uint32_t group = 0; group < kMaxBindGroups; ++group) {
|
||||
const auto& groupInfo = layout->GetBindGroupLayout(group)->GetBindingInfo();
|
||||
|
||||
for (uint32_t binding = 0; binding < kMaxBindingsPerGroup; ++binding) {
|
||||
if (!groupInfo.mask[binding]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string name = GetBindingName(group, binding);
|
||||
switch (groupInfo.types[binding]) {
|
||||
case nxt::BindingType::UniformBuffer:
|
||||
{
|
||||
GLint location = glGetUniformBlockIndex(program, name.c_str());
|
||||
glUniformBlockBinding(program, location, indices[group][binding]);
|
||||
}
|
||||
break;
|
||||
|
||||
case nxt::BindingType::StorageBuffer:
|
||||
{
|
||||
GLuint location = glGetProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, name.c_str());
|
||||
glShaderStorageBlockBinding(program, location, indices[group][binding]);
|
||||
}
|
||||
break;
|
||||
|
||||
case nxt::BindingType::Sampler:
|
||||
case nxt::BindingType::SampledTexture:
|
||||
// These binding types are handled in the separate sampler and texture emulation
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute links between stages for combined samplers, then bind them to texture units
|
||||
{
|
||||
std::set<CombinedSampler> combinedSamplersSet;
|
||||
for (auto stage : IterateStages(GetStageMask())) {
|
||||
const auto& module = ToBackend(builder->GetStageInfo(stage).module);
|
||||
|
||||
for (const auto& combined : module->GetCombinedSamplerInfo()) {
|
||||
combinedSamplersSet.insert(combined);
|
||||
}
|
||||
}
|
||||
|
||||
unitsForSamplers.resize(layout->GetNumSamplers());
|
||||
unitsForTextures.resize(layout->GetNumSampledTextures());
|
||||
|
||||
GLuint textureUnit = layout->GetTextureUnitsUsed();
|
||||
for (const auto& combined : combinedSamplersSet) {
|
||||
std::string name = combined.GetName();
|
||||
GLint location = glGetUniformLocation(program, name.c_str());
|
||||
glUniform1i(location, textureUnit);
|
||||
|
||||
GLuint samplerIndex = indices[combined.samplerLocation.group][combined.samplerLocation.binding];
|
||||
unitsForSamplers[samplerIndex].push_back(textureUnit);
|
||||
|
||||
GLuint textureIndex = indices[combined.textureLocation.group][combined.textureLocation.binding];
|
||||
unitsForTextures[textureIndex].push_back(textureUnit);
|
||||
|
||||
textureUnit ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Pipeline::GLPushConstantInfo& Pipeline::GetGLPushConstants(nxt::ShaderStage stage) const {
|
||||
return glPushConstants[stage];
|
||||
}
|
||||
|
||||
const std::vector<GLuint>& Pipeline::GetTextureUnitsForSampler(GLuint index) const {
|
||||
ASSERT(index >= 0 && index < unitsForSamplers.size());
|
||||
return unitsForSamplers[index];
|
||||
}
|
||||
|
||||
const std::vector<GLuint>& Pipeline::GetTextureUnitsForTexture(GLuint index) const {
|
||||
ASSERT(index >= 0 && index < unitsForSamplers.size());
|
||||
return unitsForTextures[index];
|
||||
}
|
||||
|
||||
GLuint Pipeline::GetProgramHandle() const {
|
||||
return program;
|
||||
}
|
||||
|
||||
void Pipeline::ApplyNow() {
|
||||
glUseProgram(program);
|
||||
|
||||
auto inputState = ToBackend(GetInputState());
|
||||
glBindVertexArray(inputState->GetVAO());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// Copyright 2017 The NXT Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "PipelineGL.h"
|
||||
|
||||
#include "OpenGLBackend.h"
|
||||
#include "PipelineLayoutGL.h"
|
||||
#include "ShaderModuleGL.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
|
||||
namespace {
|
||||
|
||||
GLenum GLShaderType(nxt::ShaderStage stage) {
|
||||
switch (stage) {
|
||||
case nxt::ShaderStage::Vertex:
|
||||
return GL_VERTEX_SHADER;
|
||||
case nxt::ShaderStage::Fragment:
|
||||
return GL_FRAGMENT_SHADER;
|
||||
case nxt::ShaderStage::Compute:
|
||||
return GL_COMPUTE_SHADER;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Pipeline::Pipeline(Device* device, PipelineBuilder* builder) : PipelineBase(builder), device(device) {
|
||||
auto CreateShader = [](GLenum type, const char* source) -> GLuint {
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &source, nullptr);
|
||||
glCompileShader(shader);
|
||||
|
||||
GLint compileStatus = GL_FALSE;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
|
||||
if (compileStatus == GL_FALSE) {
|
||||
GLint infoLogLength = 0;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
|
||||
if (infoLogLength > 1) {
|
||||
std::vector<char> buffer(infoLogLength);
|
||||
glGetShaderInfoLog(shader, infoLogLength, nullptr, &buffer[0]);
|
||||
std::cout << source << std::endl;
|
||||
std::cout << "Program compilation failed:\n";
|
||||
std::cout << buffer.data() << std::endl;
|
||||
}
|
||||
}
|
||||
return shader;
|
||||
};
|
||||
|
||||
auto FillPushConstants = [](const ShaderModule* module, GLPushConstantInfo* info, GLuint program) {
|
||||
const auto& moduleInfo = module->GetPushConstants();
|
||||
for (uint32_t i = 0; i < moduleInfo.names.size(); i++) {
|
||||
(*info)[i] = -1;
|
||||
|
||||
unsigned int size = moduleInfo.sizes[i];
|
||||
if (size == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
GLint location = glGetUniformLocation(program, moduleInfo.names[i].c_str());
|
||||
if (location == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint32_t offset = 0; offset < size; offset++) {
|
||||
(*info)[i + offset] = location + offset;
|
||||
}
|
||||
i += size - 1;
|
||||
}
|
||||
};
|
||||
|
||||
program = glCreateProgram();
|
||||
|
||||
for (auto stage : IterateStages(GetStageMask())) {
|
||||
const ShaderModule* module = ToBackend(builder->GetStageInfo(stage).module.Get());
|
||||
|
||||
GLuint shader = CreateShader(GLShaderType(stage), module->GetSource());
|
||||
glAttachShader(program, shader);
|
||||
}
|
||||
|
||||
glLinkProgram(program);
|
||||
|
||||
GLint linkStatus = GL_FALSE;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
|
||||
if (linkStatus == GL_FALSE) {
|
||||
GLint infoLogLength = 0;
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
|
||||
if (infoLogLength > 1) {
|
||||
std::vector<char> buffer(infoLogLength);
|
||||
glGetProgramInfoLog(program, infoLogLength, nullptr, &buffer[0]);
|
||||
std::cout << "Program link failed:\n";
|
||||
std::cout << buffer.data() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto stage : IterateStages(GetStageMask())) {
|
||||
const ShaderModule* module = ToBackend(builder->GetStageInfo(stage).module.Get());
|
||||
FillPushConstants(module, &glPushConstants[stage], program);
|
||||
}
|
||||
|
||||
glUseProgram(program);
|
||||
|
||||
// The uniforms are part of the program state so we can pre-bind buffer units, texture units etc.
|
||||
const auto& layout = ToBackend(GetLayout());
|
||||
const auto& indices = layout->GetBindingIndexInfo();
|
||||
|
||||
for (uint32_t group = 0; group < kMaxBindGroups; ++group) {
|
||||
const auto& groupInfo = layout->GetBindGroupLayout(group)->GetBindingInfo();
|
||||
|
||||
for (uint32_t binding = 0; binding < kMaxBindingsPerGroup; ++binding) {
|
||||
if (!groupInfo.mask[binding]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string name = GetBindingName(group, binding);
|
||||
switch (groupInfo.types[binding]) {
|
||||
case nxt::BindingType::UniformBuffer:
|
||||
{
|
||||
GLint location = glGetUniformBlockIndex(program, name.c_str());
|
||||
glUniformBlockBinding(program, location, indices[group][binding]);
|
||||
}
|
||||
break;
|
||||
|
||||
case nxt::BindingType::StorageBuffer:
|
||||
{
|
||||
GLuint location = glGetProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, name.c_str());
|
||||
glShaderStorageBlockBinding(program, location, indices[group][binding]);
|
||||
}
|
||||
break;
|
||||
|
||||
case nxt::BindingType::Sampler:
|
||||
case nxt::BindingType::SampledTexture:
|
||||
// These binding types are handled in the separate sampler and texture emulation
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute links between stages for combined samplers, then bind them to texture units
|
||||
{
|
||||
std::set<CombinedSampler> combinedSamplersSet;
|
||||
for (auto stage : IterateStages(GetStageMask())) {
|
||||
const auto& module = ToBackend(builder->GetStageInfo(stage).module);
|
||||
|
||||
for (const auto& combined : module->GetCombinedSamplerInfo()) {
|
||||
combinedSamplersSet.insert(combined);
|
||||
}
|
||||
}
|
||||
|
||||
unitsForSamplers.resize(layout->GetNumSamplers());
|
||||
unitsForTextures.resize(layout->GetNumSampledTextures());
|
||||
|
||||
GLuint textureUnit = layout->GetTextureUnitsUsed();
|
||||
for (const auto& combined : combinedSamplersSet) {
|
||||
std::string name = combined.GetName();
|
||||
GLint location = glGetUniformLocation(program, name.c_str());
|
||||
glUniform1i(location, textureUnit);
|
||||
|
||||
GLuint samplerIndex = indices[combined.samplerLocation.group][combined.samplerLocation.binding];
|
||||
unitsForSamplers[samplerIndex].push_back(textureUnit);
|
||||
|
||||
GLuint textureIndex = indices[combined.textureLocation.group][combined.textureLocation.binding];
|
||||
unitsForTextures[textureIndex].push_back(textureUnit);
|
||||
|
||||
textureUnit ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Pipeline::GLPushConstantInfo& Pipeline::GetGLPushConstants(nxt::ShaderStage stage) const {
|
||||
return glPushConstants[stage];
|
||||
}
|
||||
|
||||
const std::vector<GLuint>& Pipeline::GetTextureUnitsForSampler(GLuint index) const {
|
||||
ASSERT(index >= 0 && index < unitsForSamplers.size());
|
||||
return unitsForSamplers[index];
|
||||
}
|
||||
|
||||
const std::vector<GLuint>& Pipeline::GetTextureUnitsForTexture(GLuint index) const {
|
||||
ASSERT(index >= 0 && index < unitsForSamplers.size());
|
||||
return unitsForTextures[index];
|
||||
}
|
||||
|
||||
GLuint Pipeline::GetProgramHandle() const {
|
||||
return program;
|
||||
}
|
||||
|
||||
void Pipeline::ApplyNow() {
|
||||
glUseProgram(program);
|
||||
|
||||
auto inputState = ToBackend(GetInputState());
|
||||
glBindVertexArray(inputState->GetVAO());
|
||||
auto depthStencilState = ToBackend(GetDepthStencilState());
|
||||
depthStencilState->Apply();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user