| // 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); |
| } |
| 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); |
| } |
| 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; |
| } |
| |
| // Queue |
| |
| Queue::Queue(Device* device, QueueBuilder* builder) : device(device) { |
| } |
| |
| void Queue::Submit(uint32_t numCommands, CommandBuffer* const * commands) { |
| for (uint32_t i = 0; i < numCommands; ++i) { |
| commands[i]->Execute(); |
| } |
| } |
| |
| } |
| } |