Make the SwapChain interface match webgpu.h

This changes the methods of Dawn's SwapChain to match webgpu.h, namely
Present() now doesn't take arguments, and GetNextTexture() is replaced
with GetCurrentTextureView().

BUG=dawn:269

Change-Id: Ia0debefb170caf799c3310b1dad5535c4c5f59ca
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/13441
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Corentin Wallez 2019-11-12 18:30:11 +00:00 committed by Commit Bot service account
parent 700cfe7664
commit 604072bc2e
8 changed files with 53 additions and 47 deletions

View File

@ -1188,16 +1188,8 @@
{"name": "height", "type": "uint32_t"} {"name": "height", "type": "uint32_t"}
] ]
}, },
{ {"name": "get current texture view", "returns": "texture view"},
"name": "get next texture", {"name": "present"}
"returns": "texture"
},
{
"name": "present",
"args": [
{"name": "texture", "type": "texture"}
]
}
] ]
}, },
"swap chain descriptor": { "swap chain descriptor": {

View File

@ -143,7 +143,7 @@ void init() {
} }
void frame() { void frame() {
wgpu::Texture backbuffer = swapchain.GetNextTexture(); wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
static int f = 0; static int f = 0;
f++; f++;
@ -152,7 +152,7 @@ void frame() {
} }
ubo.SetSubData(0, kNumTriangles * sizeof(ShaderData), shaderData.data()); ubo.SetSubData(0, kNumTriangles * sizeof(ShaderData), shaderData.data());
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}); utils::ComboRenderPassDescriptor renderPass({backbufferView});
wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{ {
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
@ -169,7 +169,7 @@ void frame() {
wgpu::CommandBuffer commands = encoder.Finish(); wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands); queue.Submit(1, &commands);
swapchain.Present(backbuffer); swapchain.Present();
DoFlush(); DoFlush();
fprintf(stderr, "frame %i\n", f); fprintf(stderr, "frame %i\n", f);
} }

View File

@ -125,8 +125,7 @@ void init() {
} }
void frame() { void frame() {
WGPUTexture backbuffer = wgpuSwapChainGetNextTexture(swapchain); WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
WGPUTextureView backbufferView = wgpuTextureCreateView(backbuffer, nullptr);
WGPURenderPassDescriptor renderpassInfo; WGPURenderPassDescriptor renderpassInfo;
renderpassInfo.nextInChain = nullptr; renderpassInfo.nextInChain = nullptr;
renderpassInfo.label = nullptr; renderpassInfo.label = nullptr;
@ -157,7 +156,7 @@ void frame() {
wgpuQueueSubmit(queue, 1, &commands); wgpuQueueSubmit(queue, 1, &commands);
wgpuCommandBufferRelease(commands); wgpuCommandBufferRelease(commands);
wgpuSwapChainPresent(swapchain, backbuffer); wgpuSwapChainPresent(swapchain);
wgpuTextureViewRelease(backbufferView); wgpuTextureViewRelease(backbufferView);
DoFlush(); DoFlush();

View File

@ -261,7 +261,7 @@ void initSim() {
} }
} }
wgpu::CommandBuffer createCommandBuffer(const wgpu::Texture backbuffer, size_t i) { wgpu::CommandBuffer createCommandBuffer(const wgpu::TextureView backbufferView, size_t i) {
auto& bufferDst = particleBuffers[(i + 1) % 2]; auto& bufferDst = particleBuffers[(i + 1) % 2];
wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
@ -274,7 +274,7 @@ wgpu::CommandBuffer createCommandBuffer(const wgpu::Texture backbuffer, size_t i
} }
{ {
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView); utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(renderPipeline); pass.SetPipeline(renderPipeline);
pass.SetVertexBuffer(0, bufferDst); pass.SetVertexBuffer(0, bufferDst);
@ -300,11 +300,11 @@ void init() {
} }
void frame() { void frame() {
wgpu::Texture backbuffer = swapchain.GetNextTexture(); wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbuffer, pingpong); wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbufferView, pingpong);
queue.Submit(1, &commandBuffer); queue.Submit(1, &commandBuffer);
swapchain.Present(backbuffer); swapchain.Present();
DoFlush(); DoFlush();
pingpong = (pingpong + 1) % 2; pingpong = (pingpong + 1) % 2;

View File

@ -153,8 +153,8 @@ void frame() {
s.b += 0.02f; s.b += 0.02f;
if (s.b >= 1.0f) {s.b = 0.0f;} if (s.b >= 1.0f) {s.b = 0.0f;}
wgpu::Texture backbuffer = swapchain.GetNextTexture(); wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView); utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{ {
@ -169,7 +169,7 @@ void frame() {
wgpu::CommandBuffer commands = encoder.Finish(); wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands); queue.Submit(1, &commands);
swapchain.Present(backbuffer); swapchain.Present();
DoFlush(); DoFlush();
} }

View File

@ -264,8 +264,8 @@ void frame() {
cameraBuffer.SetSubData(0, sizeof(CameraData), &cameraData); cameraBuffer.SetSubData(0, sizeof(CameraData), &cameraData);
wgpu::Texture backbuffer = swapchain.GetNextTexture(); wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView); utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{ {
@ -292,7 +292,7 @@ void frame() {
wgpu::CommandBuffer commands = encoder.Finish(); wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands); queue.Submit(1, &commands);
swapchain.Present(backbuffer); swapchain.Present();
DoFlush(); DoFlush();
} }

View File

@ -99,12 +99,21 @@ namespace dawn_native {
static_cast<WGPUTextureUsage>(allowedUsage), width, height); static_cast<WGPUTextureUsage>(allowedUsage), width, height);
} }
TextureBase* SwapChainBase::GetNextTexture() { TextureViewBase* SwapChainBase::GetCurrentTextureView() {
if (GetDevice()->ConsumedError(ValidateGetNextTexture())) { if (GetDevice()->ConsumedError(ValidateGetCurrentTextureView())) {
return TextureBase::MakeError(GetDevice()); return TextureViewBase::MakeError(GetDevice());
} }
ASSERT(!IsError()); ASSERT(!IsError());
// Return the same current texture view until Present is called.
if (mCurrentTextureView.Get() != nullptr) {
// Calling GetCurrentTextureView always returns a new reference so add it even when
// reuse the existing texture view.
mCurrentTextureView->Reference();
return mCurrentTextureView.Get();
}
// Create the backing texture and the view.
TextureDescriptor descriptor; TextureDescriptor descriptor;
descriptor.dimension = wgpu::TextureDimension::e2D; descriptor.dimension = wgpu::TextureDimension::e2D;
descriptor.size.width = mWidth; descriptor.size.width = mWidth;
@ -116,21 +125,28 @@ namespace dawn_native {
descriptor.mipLevelCount = 1; descriptor.mipLevelCount = 1;
descriptor.usage = mAllowedUsage; descriptor.usage = mAllowedUsage;
auto* texture = GetNextTextureImpl(&descriptor); // Get the texture but remove the external refcount because it is never passed outside
mLastNextTexture = texture; // of dawn_native
return texture; mCurrentTexture = AcquireRef(GetNextTextureImpl(&descriptor));
mCurrentTextureView = mCurrentTexture->CreateView(nullptr);
return mCurrentTextureView.Get();
} }
void SwapChainBase::Present(TextureBase* texture) { void SwapChainBase::Present() {
if (GetDevice()->ConsumedError(ValidatePresent(texture))) { if (GetDevice()->ConsumedError(ValidatePresent())) {
return; return;
} }
ASSERT(!IsError()); ASSERT(!IsError());
if (GetDevice()->ConsumedError(OnBeforePresent(texture))) if (GetDevice()->ConsumedError(OnBeforePresent(mCurrentTexture.Get()))) {
return; return;
}
mImplementation.Present(mImplementation.userData); mImplementation.Present(mImplementation.userData);
mCurrentTexture = nullptr;
mCurrentTextureView = nullptr;
} }
const DawnSwapChainImplementation& SwapChainBase::GetImplementation() { const DawnSwapChainImplementation& SwapChainBase::GetImplementation() {
@ -154,7 +170,7 @@ namespace dawn_native {
return {}; return {};
} }
MaybeError SwapChainBase::ValidateGetNextTexture() const { MaybeError SwapChainBase::ValidateGetCurrentTextureView() const {
DAWN_TRY(GetDevice()->ValidateObject(this)); DAWN_TRY(GetDevice()->ValidateObject(this));
if (mWidth == 0) { if (mWidth == 0) {
@ -165,14 +181,12 @@ namespace dawn_native {
return {}; return {};
} }
MaybeError SwapChainBase::ValidatePresent(TextureBase* texture) const { MaybeError SwapChainBase::ValidatePresent() const {
DAWN_TRY(GetDevice()->ValidateObject(this)); DAWN_TRY(GetDevice()->ValidateObject(this));
DAWN_TRY(GetDevice()->ValidateObject(texture));
// This also checks that the texture is valid since mLastNextTexture is always valid. if (mCurrentTextureView.Get() == nullptr) {
if (texture != mLastNextTexture) {
return DAWN_VALIDATION_ERROR( return DAWN_VALIDATION_ERROR(
"Tried to present something other than the last NextTexture"); "Cannot call present without a GetCurrentTextureView call for this frame");
} }
return {}; return {};

View File

@ -39,8 +39,8 @@ namespace dawn_native {
wgpu::TextureUsage allowedUsage, wgpu::TextureUsage allowedUsage,
uint32_t width, uint32_t width,
uint32_t height); uint32_t height);
TextureBase* GetNextTexture(); TextureViewBase* GetCurrentTextureView();
void Present(TextureBase* texture); void Present();
protected: protected:
SwapChainBase(DeviceBase* device, ObjectBase::ErrorTag tag); SwapChainBase(DeviceBase* device, ObjectBase::ErrorTag tag);
@ -54,15 +54,16 @@ namespace dawn_native {
wgpu::TextureUsage allowedUsage, wgpu::TextureUsage allowedUsage,
uint32_t width, uint32_t width,
uint32_t height) const; uint32_t height) const;
MaybeError ValidateGetNextTexture() const; MaybeError ValidateGetCurrentTextureView() const;
MaybeError ValidatePresent(TextureBase* texture) const; MaybeError ValidatePresent() const;
DawnSwapChainImplementation mImplementation = {}; DawnSwapChainImplementation mImplementation = {};
wgpu::TextureFormat mFormat = {}; wgpu::TextureFormat mFormat = {};
wgpu::TextureUsage mAllowedUsage; wgpu::TextureUsage mAllowedUsage;
uint32_t mWidth = 0; uint32_t mWidth = 0;
uint32_t mHeight = 0; uint32_t mHeight = 0;
TextureBase* mLastNextTexture = nullptr; Ref<TextureBase> mCurrentTexture;
Ref<TextureViewBase> mCurrentTextureView;
}; };
} // namespace dawn_native } // namespace dawn_native