Update SwapChain to configure texture usage

Explicitly configure swap chain usage in bindings and examples

Fix missing case in switch

Make swap chain Present usage implicit

Author: Austin Eng <enga@google.com>
This commit is contained in:
Kai Ninomiya 2017-08-29 13:35:05 -07:00 committed by Kai Ninomiya
parent 08a0081c13
commit ee7b6b1b62
22 changed files with 96 additions and 26 deletions

View File

@ -49,7 +49,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450 #version 450

View File

@ -39,7 +39,7 @@ void init() {
swapchain = nxtSwapChainBuilderGetResult(builder); swapchain = nxtSwapChainBuilderGetResult(builder);
nxtSwapChainBuilderRelease(builder); nxtSwapChainBuilderRelease(builder);
} }
nxtSwapChainConfigure(swapchain, NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM, 640, 480); nxtSwapChainConfigure(swapchain, NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM, NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640, 480);
const char* vs = const char* vs =
"#version 450\n" "#version 450\n"

View File

@ -291,7 +291,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();
initRender(); initRender();

View File

@ -35,7 +35,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
struct {uint32_t a; float b;} s; struct {uint32_t a; float b;} s;
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));

View File

@ -116,7 +116,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();

View File

@ -49,7 +49,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();

View File

@ -52,7 +52,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();

View File

@ -83,7 +83,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();
initTextures(); initTextures();

View File

@ -33,7 +33,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450 #version 450

View File

@ -43,7 +43,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();

View File

@ -167,7 +167,7 @@ void init() {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers(); initBuffers();
initTextures(); initTextures();

View File

@ -464,7 +464,7 @@ namespace {
queue = device.CreateQueueBuilder().GetResult(); queue = device.CreateQueueBuilder().GetResult();
swapchain = GetSwapChain(device); swapchain = GetSwapChain(device);
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, nxt::TextureUsageBit::OutputAttachment, 640, 480);
renderpass = CreateDefaultRenderPass(device); renderpass = CreateDefaultRenderPass(device);
depthStencilView = CreateDefaultDepthStencilView(device); depthStencilView = CreateDefaultDepthStencilView(device);

View File

@ -1050,6 +1050,8 @@
"name": "configure", "name": "configure",
"args": [ "args": [
{"name": "format", "type": "texture format"}, {"name": "format", "type": "texture format"},
{"name": "allowedUsage", "type": "texture usage bit"},
{"name": "initialUsage", "type": "texture usage bit"},
{"name": "width", "type": "uint32_t"}, {"name": "width", "type": "uint32_t"},
{"name": "height", "type": "uint32_t"} {"name": "height", "type": "uint32_t"}
] ]

View File

@ -34,17 +34,24 @@ namespace backend {
return device; return device;
} }
void SwapChainBase::Configure(nxt::TextureFormat format, uint32_t width, uint32_t height) { void SwapChainBase::Configure(nxt::TextureFormat format, nxt::TextureUsageBit allowedUsage, nxt::TextureUsageBit initialUsage, uint32_t width, uint32_t height) {
if (width == 0 || height == 0) { if (width == 0 || height == 0) {
device->HandleError("Swap chain cannot be configured to zero size"); device->HandleError("Swap chain cannot be configured to zero size");
return; return;
} }
allowedUsage |= nxt::TextureUsageBit::Present;
if (!(HasZeroOrOneBits(initialUsage) && (initialUsage & allowedUsage))) {
device->HandleError("Swap chain configured with invalid texture usage");
return;
}
this->format = format; this->format = format;
this->allowedUsage = allowedUsage;
this->initialUsage = initialUsage;
this->width = width; this->width = width;
this->height = height; this->height = height;
implementation.Configure(implementation.userData, implementation.Configure(implementation.userData,
static_cast<nxtTextureFormat>(format), width, height); static_cast<nxtTextureFormat>(format), static_cast<nxtTextureUsageBit>(allowedUsage), static_cast<nxtTextureUsageBit>(initialUsage), width, height);
} }
TextureBase* SwapChainBase::GetNextTexture() { TextureBase* SwapChainBase::GetNextTexture() {
@ -59,8 +66,8 @@ namespace backend {
builder->SetExtent(width, height, 1); builder->SetExtent(width, height, 1);
builder->SetFormat(format); builder->SetFormat(format);
builder->SetMipLevels(1); builder->SetMipLevels(1);
builder->SetAllowedUsage(nxt::TextureUsageBit::OutputAttachment | nxt::TextureUsageBit::Present); builder->SetAllowedUsage(allowedUsage);
builder->SetInitialUsage(nxt::TextureUsageBit::OutputAttachment); builder->SetInitialUsage(initialUsage);
auto* texture = GetNextTextureImpl(builder); auto* texture = GetNextTextureImpl(builder);
lastNextTexture = texture; lastNextTexture = texture;

View File

@ -32,7 +32,7 @@ namespace backend {
DeviceBase* GetDevice(); DeviceBase* GetDevice();
// NXT API // NXT API
void Configure(nxt::TextureFormat format, uint32_t width, uint32_t height); void Configure(nxt::TextureFormat format, nxt::TextureUsageBit allowedUsage, nxt::TextureUsageBit initialUsage, uint32_t width, uint32_t height);
TextureBase* GetNextTexture(); TextureBase* GetNextTexture();
void Present(TextureBase* texture); void Present(TextureBase* texture);
@ -44,6 +44,8 @@ namespace backend {
DeviceBase* device = nullptr; DeviceBase* device = nullptr;
nxtSwapChainImplementation implementation = {}; nxtSwapChainImplementation implementation = {};
nxt::TextureFormat format = {}; nxt::TextureFormat format = {};
nxt::TextureUsageBit allowedUsage;
nxt::TextureUsageBit initialUsage;
uint32_t width = 0; uint32_t width = 0;
uint32_t height = 0; uint32_t height = 0;
TextureBase* lastNextTexture = nullptr; TextureBase* lastNextTexture = nullptr;

View File

@ -58,6 +58,7 @@ namespace backend {
bool TextureFormatHasDepthOrStencil(nxt::TextureFormat format) { bool TextureFormatHasDepthOrStencil(nxt::TextureFormat format) {
switch (format) { switch (format) {
case nxt::TextureFormat::R8G8B8A8Unorm: case nxt::TextureFormat::R8G8B8A8Unorm:
case nxt::TextureFormat::R8G8B8A8Uint:
return false; return false;
case nxt::TextureFormat::D32FloatS8Uint: case nxt::TextureFormat::D32FloatS8Uint:
return true; return true;

View File

@ -37,6 +37,8 @@ namespace opengl {
switch (format) { switch (format) {
case nxt::TextureFormat::R8G8B8A8Unorm: case nxt::TextureFormat::R8G8B8A8Unorm:
return {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE}; return {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE};
case nxt::TextureFormat::R8G8B8A8Uint:
return { GL_RGBA8UI, GL_RGBA, GL_UNSIGNED_INT };
case nxt::TextureFormat::D32FloatS8Uint: case nxt::TextureFormat::D32FloatS8Uint:
return {GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV}; return {GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV};
default: default:

View File

@ -35,7 +35,7 @@ typedef struct {
void (*Destroy)(void* userData); void (*Destroy)(void* userData);
/// Configure/reconfigure the swap chain. /// Configure/reconfigure the swap chain.
nxtSwapChainError (*Configure)(void* userData, nxtTextureFormat format, uint32_t width, uint32_t height); nxtSwapChainError (*Configure)(void* userData, nxtTextureFormat format, nxtTextureUsageBit allowedUsage, nxtTextureUsageBit initialUsage, uint32_t width, uint32_t height);
/// Acquire the next texture from the swap chain. /// Acquire the next texture from the swap chain.
nxtSwapChainError (*GetNextTexture)(void* userData, nxtSwapChainNextTexture* nextTexture); nxtSwapChainError (*GetNextTexture)(void* userData, nxtSwapChainNextTexture* nextTexture);

View File

@ -77,6 +77,41 @@ namespace utils {
return factory; return factory;
} }
DXGI_USAGE D3D12SwapChainBufferUsage(nxtTextureUsageBit allowedUsages) {
DXGI_USAGE usage = DXGI_CPU_ACCESS_NONE;
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_SAMPLED) {
usage |= DXGI_USAGE_SHADER_INPUT;
}
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_STORAGE) {
usage |= DXGI_USAGE_UNORDERED_ACCESS;
}
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT) {
usage |= DXGI_USAGE_RENDER_TARGET_OUTPUT;
}
return usage;
}
D3D12_RESOURCE_STATES D3D12ResourceState(nxtTextureUsageBit usage) {
D3D12_RESOURCE_STATES resourceState = D3D12_RESOURCE_STATE_COMMON;
if (usage & NXT_TEXTURE_USAGE_BIT_TRANSFER_SRC) {
resourceState |= D3D12_RESOURCE_STATE_COPY_SOURCE;
}
if (usage & NXT_TEXTURE_USAGE_BIT_TRANSFER_DST) {
resourceState |= D3D12_RESOURCE_STATE_COPY_DEST;
}
if (usage & NXT_TEXTURE_USAGE_BIT_SAMPLED) {
resourceState |= (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
}
if (usage & NXT_TEXTURE_USAGE_BIT_STORAGE) {
resourceState |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
if (usage & NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT) {
resourceState |= D3D12_RESOURCE_STATE_RENDER_TARGET;
}
return resourceState;
}
} }
class SwapChainImplD3D12 : SwapChainImpl { class SwapChainImplD3D12 : SwapChainImpl {
@ -104,6 +139,8 @@ namespace utils {
uint32_t previousRenderTargetIndex = 0; uint32_t previousRenderTargetIndex = 0;
uint64_t lastSerialRenderTargetWasUsed[kFrameCount] = {}; uint64_t lastSerialRenderTargetWasUsed[kFrameCount] = {};
D3D12_RESOURCE_STATES renderTargetResourceState;
SwapChainImplD3D12(HWND window, nxtProcTable procs) SwapChainImplD3D12(HWND window, nxtProcTable procs)
: window(window), procs(procs), factory(CreateFactory()) { : window(window), procs(procs), factory(CreateFactory()) {
} }
@ -119,7 +156,7 @@ namespace utils {
commandQueue = backend::d3d12::GetCommandQueue(backendDevice); commandQueue = backend::d3d12::GetCommandQueue(backendDevice);
} }
nxtSwapChainError Configure(nxtTextureFormat format, nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit allowedUsage, nxtTextureUsageBit initialUsage,
uint32_t width, uint32_t height) { uint32_t width, uint32_t height) {
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) { if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
return "unsupported format"; return "unsupported format";
@ -131,7 +168,7 @@ namespace utils {
swapChainDesc.Width = width; swapChainDesc.Width = width;
swapChainDesc.Height = height; swapChainDesc.Height = height;
swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.BufferUsage = D3D12SwapChainBufferUsage(allowedUsage);
swapChainDesc.BufferCount = kFrameCount; swapChainDesc.BufferCount = kFrameCount;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.SampleDesc.Count = 1; swapChainDesc.SampleDesc.Count = 1;
@ -162,6 +199,25 @@ namespace utils {
lastSerialRenderTargetWasUsed[n] = initialSerial; lastSerialRenderTargetWasUsed[n] = initialSerial;
} }
renderTargetResourceState = D3D12ResourceState(initialUsage);
// Transition the first frame. Resources are initially created in PRESENT state
if (renderTargetResourceState != D3D12_RESOURCE_STATE_PRESENT) {
ComPtr<ID3D12GraphicsCommandList> commandList = {};
backend::d3d12::OpenCommandList(backendDevice, &commandList);
D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = renderTargetResources[renderTargetIndex].Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.StateAfter = renderTargetResourceState;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &resourceBarrier);
ASSERT_SUCCESS(commandList->Close());
backend::d3d12::ExecuteCommandLists(backendDevice, { commandList.Get() });
}
return NXT_SWAP_CHAIN_NO_ERROR; return NXT_SWAP_CHAIN_NO_ERROR;
} }
@ -178,14 +234,14 @@ namespace utils {
ASSERT_SUCCESS(swapChain->Present(1, 0)); ASSERT_SUCCESS(swapChain->Present(1, 0));
// Transition last frame's render target back to being a render target // Transition last frame's render target back to being a render target
{ if (renderTargetResourceState != D3D12_RESOURCE_STATE_PRESENT) {
ComPtr<ID3D12GraphicsCommandList> commandList = {}; ComPtr<ID3D12GraphicsCommandList> commandList = {};
backend::d3d12::OpenCommandList(backendDevice, &commandList); backend::d3d12::OpenCommandList(backendDevice, &commandList);
D3D12_RESOURCE_BARRIER resourceBarrier; D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = renderTargetResources[previousRenderTargetIndex].Get(); resourceBarrier.Transition.pResource = renderTargetResources[previousRenderTargetIndex].Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT; resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET; resourceBarrier.Transition.StateAfter = renderTargetResourceState;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;

View File

@ -68,7 +68,7 @@ namespace utils {
commandQueue = [mtlDevice newCommandQueue]; commandQueue = [mtlDevice newCommandQueue];
} }
nxtSwapChainError Configure(nxtTextureFormat format, nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit, nxtTextureUsageBit,
uint32_t width, uint32_t height) { uint32_t width, uint32_t height) {
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) { if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
return "unsupported format"; return "unsupported format";

View File

@ -69,7 +69,7 @@ namespace utils {
GL_TEXTURE_2D, backTexture, 0); GL_TEXTURE_2D, backTexture, 0);
} }
nxtSwapChainError Configure(nxtTextureFormat format, nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit, nxtTextureUsageBit,
uint32_t width, uint32_t height) { uint32_t width, uint32_t height) {
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) { if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
return "unsupported format"; return "unsupported format";

View File

@ -28,9 +28,9 @@ namespace utils {
impl.Destroy = [](void* userData) { impl.Destroy = [](void* userData) {
delete reinterpret_cast<TImpl*>(userData); delete reinterpret_cast<TImpl*>(userData);
}; };
impl.Configure = [](void* userData, nxtTextureFormat format, uint32_t width, uint32_t height) { impl.Configure = [](void* userData, nxtTextureFormat format, nxtTextureUsageBit allowedUsage, nxtTextureUsageBit initialUsage, uint32_t width, uint32_t height) {
return reinterpret_cast<TImpl*>(userData)->Configure( return reinterpret_cast<TImpl*>(userData)->Configure(
format, width, height); format, allowedUsage, initialUsage, width, height);
}; };
impl.GetNextTexture = [](void* userData, nxtSwapChainNextTexture* nextTexture) { impl.GetNextTexture = [](void* userData, nxtSwapChainNextTexture* nextTexture) {
return reinterpret_cast<TImpl*>(userData)->GetNextTexture( return reinterpret_cast<TImpl*>(userData)->GetNextTexture(