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": "get next texture",
"returns": "texture"
},
{
"name": "present",
"args": [
{"name": "texture", "type": "texture"}
]
}
{"name": "get current texture view", "returns": "texture view"},
{"name": "present"}
]
},
"swap chain descriptor": {

View File

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

View File

@ -125,8 +125,7 @@ void init() {
}
void frame() {
WGPUTexture backbuffer = wgpuSwapChainGetNextTexture(swapchain);
WGPUTextureView backbufferView = wgpuTextureCreateView(backbuffer, nullptr);
WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
WGPURenderPassDescriptor renderpassInfo;
renderpassInfo.nextInChain = nullptr;
renderpassInfo.label = nullptr;
@ -157,7 +156,7 @@ void frame() {
wgpuQueueSubmit(queue, 1, &commands);
wgpuCommandBufferRelease(commands);
wgpuSwapChainPresent(swapchain, backbuffer);
wgpuSwapChainPresent(swapchain);
wgpuTextureViewRelease(backbufferView);
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];
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);
pass.SetPipeline(renderPipeline);
pass.SetVertexBuffer(0, bufferDst);
@ -300,11 +300,11 @@ void init() {
}
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);
swapchain.Present(backbuffer);
swapchain.Present();
DoFlush();
pingpong = (pingpong + 1) % 2;

View File

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

View File

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

View File

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

View File

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