Create a Null backend.

This will be useful to run validation unittests without using the GPU.
This commit is contained in:
Corentin Wallez 2017-05-24 16:04:55 +02:00 committed by Corentin Wallez
parent 68df8b0a3a
commit 79a62bf6e3
6 changed files with 317 additions and 4 deletions

View File

@ -59,6 +59,24 @@ class OpenGLBinding : public BackendBinding {
}
};
namespace backend {
namespace null {
void Init(nxtProcTable* procs, nxtDevice* device);
}
}
class NullBinding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
}
void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override {
backend::null::Init(procs, device);
}
void SwapBuffers() override {
}
};
void PrintDeviceError(const char* message, nxt::CallbackUserdata) {
std::cout << "Device error: " << message << std::endl;
}
@ -66,6 +84,7 @@ void PrintDeviceError(const char* message, nxt::CallbackUserdata) {
enum class BackendType {
OpenGL,
Metal,
Null,
};
enum class CmdBufType {
@ -97,6 +116,9 @@ void GetProcTableAndDevice(nxtProcTable* procs, nxt::Device* device) {
fprintf(stderr, "Metal backend no present on this platform\n");
#endif
break;
case BackendType::Null:
binding = new NullBinding;
break;
}
if (!glfwInit()) {
@ -226,7 +248,11 @@ extern "C" {
backendType = BackendType::Metal;
continue;
}
fprintf(stderr, "--backend expects a backend name (opengl, metal)\n");
if (i < argc && std::string("null") == argv[i]) {
backendType = BackendType::Null;
continue;
}
fprintf(stderr, "--backend expects a backend name (opengl, metal, null)\n");
return false;
}
if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) {
@ -244,7 +270,7 @@ extern "C" {
}
if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
printf(" BACKEND is one of: opengl, metal\n");
printf(" BACKEND is one of: opengl, metal, null\n");
printf(" COMMAND_BUFFER is one of: none, terrible\n");
return false;
}

View File

@ -340,7 +340,7 @@ def debug(text):
print(text)
def main():
targets = ['nxt', 'nxtcpp', 'mock_nxt', 'opengl', 'metal', 'wire', 'blink']
targets = ['nxt', 'nxtcpp', 'mock_nxt', 'opengl', 'metal', 'null', 'wire', 'blink']
parser = argparse.ArgumentParser(
description = 'Generates code for various target for NXT.',
@ -424,6 +424,12 @@ def main():
}
renders.append(FileRender('BackendProcTable.cpp', 'metal/ProcTable.mm', base_backend_params + [metal_params]))
if 'null' in targets:
null_params = {
'namespace': 'null',
}
renders.append(FileRender('BackendProcTable.cpp', 'null/ProcTable.mm', base_backend_params + [null_params]))
if 'wire' in targets:
renders.append(FileRender('wire/WireCmd.h', 'wire/WireCmd_autogen.h', base_backend_params))
renders.append(FileRender('wire/WireCmd.cpp', 'wire/WireCmd.cpp', base_backend_params))

View File

@ -14,6 +14,7 @@
set(COMMON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/common)
set(METAL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/metal)
set(NULL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/null)
set(OPENGL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl)
set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
@ -94,6 +95,26 @@ list(APPEND BACKEND_SOURCES
${OPENGL_DIR}/TextureGL.h
)
# Null backend
Generate(
LIB_NAME null_autogen
LIB_TYPE STATIC
PRINT_NAME "Null backend autogenerated files"
COMMAND_LINE_ARGS
${GENERATOR_COMMON_ARGS}
-T null
)
target_link_libraries(null_autogen nxtcpp)
target_include_directories(null_autogen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(null_autogen PUBLIC ${GENERATED_DIR})
SetCXX14(null_autogen)
SetPIC(null_autogen)
list(APPEND BACKEND_SOURCES
${NULL_DIR}/NullBackend.cpp
${NULL_DIR}/NullBackend.h
)
# Metal Backend
if (APPLE)
@ -118,7 +139,7 @@ if (APPLE)
endif()
add_library(nxt_backend SHARED ${BACKEND_SOURCES})
target_link_libraries(nxt_backend opengl_autogen glfw glad spirv-cross)
target_link_libraries(nxt_backend opengl_autogen null_autogen glfw glad spirv-cross)
if (APPLE)
target_link_libraries(nxt_backend metal_autogen)
endif()

View File

@ -0,0 +1,15 @@
// 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 "NullBackend.h"

View File

@ -0,0 +1,120 @@
// 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 "NullBackend.h"
#include <spirv-cross/spirv_cross.hpp>
namespace backend {
namespace null {
nxtProcTable GetNonValidatingProcs();
nxtProcTable GetValidatingProcs();
void Init(nxtProcTable* procs, nxtDevice* device) {
*procs = GetValidatingProcs();
*device = reinterpret_cast<nxtDevice>(new Device);
}
// Device
Device::Device() {
}
Device::~Device() {
}
BindGroupBase* Device::CreateBindGroup(BindGroupBuilder* builder) {
return new BindGroupBase(builder);
}
BindGroupLayoutBase* Device::CreateBindGroupLayout(BindGroupLayoutBuilder* builder) {
return new BindGroupLayoutBase(builder);
}
BufferBase* Device::CreateBuffer(BufferBuilder* builder) {
return new Buffer(builder);
}
BufferViewBase* Device::CreateBufferView(BufferViewBuilder* builder) {
return new BufferViewBase(builder);
}
CommandBufferBase* Device::CreateCommandBuffer(CommandBufferBuilder* builder) {
return new CommandBufferBase(builder);
}
InputStateBase* Device::CreateInputState(InputStateBuilder* builder) {
return new InputStateBase(builder);
}
FramebufferBase* Device::CreateFramebuffer(FramebufferBuilder* builder) {
return new FramebufferBase(builder);
}
PipelineBase* Device::CreatePipeline(PipelineBuilder* builder) {
return new PipelineBase(builder);
}
PipelineLayoutBase* Device::CreatePipelineLayout(PipelineLayoutBuilder* builder) {
return new PipelineLayoutBase(builder);
}
QueueBase* Device::CreateQueue(QueueBuilder* builder) {
return new Queue(builder);
}
RenderPassBase* Device::CreateRenderPass(RenderPassBuilder* builder) {
return new RenderPassBase(builder);
}
SamplerBase* Device::CreateSampler(SamplerBuilder* builder) {
return new SamplerBase(builder);
}
ShaderModuleBase* Device::CreateShaderModule(ShaderModuleBuilder* builder) {
auto module = new ShaderModuleBase(builder);
spirv_cross::Compiler compiler(builder->AcquireSpirv());
module->ExtractSpirvInfo(compiler);
return module;
}
TextureBase* Device::CreateTexture(TextureBuilder* builder) {
return new TextureBase(builder);
}
TextureViewBase* Device::CreateTextureView(TextureViewBuilder* builder) {
return new TextureViewBase(builder);
}
void Device::Reference() {
}
void Device::Release() {
}
// Buffer
Buffer::Buffer(BufferBuilder* builder)
: BufferBase(builder) {
}
Buffer::~Buffer() {
}
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
}
// Queue
Queue::Queue(QueueBuilder* builder)
: QueueBase(builder) {
}
Queue::~Queue() {
}
void Queue::Submit(uint32_t numCommands, CommandBuffer* const * commands) {
}
}
}

View File

@ -0,0 +1,125 @@
// 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_NULL_NULLBACKEND_H_
#define BACKEND_NULL_NULLBACKEND_H_
#include "nxt/nxtcpp.h"
#include "common/Buffer.h"
#include "common/BindGroup.h"
#include "common/BindGroupLayout.h"
#include "common/Device.h"
#include "common/CommandBuffer.h"
#include "common/InputState.h"
#include "common/Framebuffer.h"
#include "common/Pipeline.h"
#include "common/PipelineLayout.h"
#include "common/Queue.h"
#include "common/RenderPass.h"
#include "common/Sampler.h"
#include "common/ShaderModule.h"
#include "common/Texture.h"
#include "common/ToBackend.h"
namespace backend {
namespace null {
using BindGroup = BindGroupBase;
using BindGroupLayout = BindGroupLayoutBase;
class Buffer;
using BufferView = BufferViewBase;
using CommandBuffer = CommandBufferBase;
using InputState = InputStateBase;
using Framebuffer = FramebufferBase;
using Pipeline = PipelineBase;
using PipelineLayout = PipelineLayoutBase;
class Queue;
using RenderPass = RenderPassBase;
using Sampler = SamplerBase;
using ShaderModule = ShaderModuleBase;
using Texture = TextureBase;
using TextureView = TextureViewBase;
struct NullBackendTraits {
using BindGroupType = BindGroup;
using BindGroupLayoutType = BindGroupLayout;
using BufferType = Buffer;
using BufferViewType = BufferView;
using CommandBufferType = CommandBuffer;
using InputStateType = InputState;
using FramebufferType = Framebuffer;
using PipelineType = Pipeline;
using PipelineLayoutType = PipelineLayout;
using QueueType = Queue;
using RenderPassType = RenderPass;
using SamplerType = Sampler;
using ShaderModuleType = ShaderModule;
using TextureType = Texture;
using TextureViewType = TextureView;
};
template<typename T>
auto ToBackend(T&& common) -> decltype(ToBackendBase<NullBackendTraits>(common)) {
return ToBackendBase<NullBackendTraits>(common);
}
class Device : public DeviceBase {
public:
Device();
~Device();
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 Buffer : public BufferBase {
public:
Buffer(BufferBuilder* builder);
~Buffer();
private:
void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override;
};
class Queue : public QueueBase {
public:
Queue(QueueBuilder* builder);
~Queue();
// NXT API
void Submit(uint32_t numCommands, CommandBuffer* const * commands);
};
}
}
#endif // BACKEND_NULL_NULLBACKEND_H_