Hash :
dc9743fb
Author :
Date :
2020-07-14T11:33:29
Add buffer serialization capability Serializes buffers' states stored on CPU. Gets buffers contents on GPU by mapping buffers to CPU's address space, then copy their data. Unmap buffers after finish. Since this feature is for tests only, it is only implemented for the Vulkan backend. Adds buffer serialization to serializeContext method so that capture replay regresssion testing now compares the states of buffers too. Bug: angleproject:4817 Change-Id: Ic9b529701014d5ba8420023a021cd5ea381bd9a1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2295950 Commit-Queue: Manh Nguyen <nguyenmh@google.com> Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// frame_capture_utils.cpp:
// ANGLE frame capture util implementation.
//
#include "libANGLE/frame_capture_utils.h"
#include "common/MemoryBuffer.h"
#include "common/angleutils.h"
#include "libANGLE/BinaryStream.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Context.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/renderer/FramebufferImpl.h"
namespace angle
{
bool IsValidColorAttachmentBinding(GLenum binding, size_t colorAttachmentsCount)
{
return binding == GL_BACK || (binding >= GL_COLOR_ATTACHMENT0 &&
(binding - GL_COLOR_ATTACHMENT0) < colorAttachmentsCount);
}
Result ReadPixelsFromAttachment(const gl::Context *context,
gl::Framebuffer *framebuffer,
const gl::FramebufferAttachment &framebufferAttachment,
ScratchBuffer *scratchBuffer,
MemoryBuffer **pixels)
{
gl::Extents extents = framebufferAttachment.getSize();
GLenum binding = framebufferAttachment.getBinding();
gl::InternalFormat format = *framebufferAttachment.getFormat().info;
if (IsValidColorAttachmentBinding(binding,
framebuffer->getState().getColorAttachments().size()))
{
format = framebuffer->getImplementation()->getImplementationColorReadFormat(context);
}
ANGLE_CHECK_GL_ALLOC(const_cast<gl::Context *>(context),
scratchBuffer->getInitialized(
format.pixelBytes * extents.width * extents.height, pixels, 0));
ANGLE_TRY(framebuffer->readPixels(context, gl::Rectangle{0, 0, extents.width, extents.height},
format.format, format.type, gl::PixelPackState{}, nullptr,
(*pixels)->data()));
return Result::Continue;
}
Result SerializeContext(gl::BinaryOutputStream *bos, const gl::Context *context)
{
ScratchBuffer scratchBuffer(1);
const gl::FramebufferManager &framebufferManager =
context->getState().getFramebufferManagerForCapture();
for (const auto &framebuffer : framebufferManager)
{
gl::Framebuffer *framebufferPtr = framebuffer.second;
ANGLE_TRY(SerializeFramebuffer(context, bos, &scratchBuffer, framebufferPtr));
}
const gl::BufferManager &bufferManager = context->getState().getBufferManagerForCapture();
for (const auto &buffer : bufferManager)
{
gl::Buffer *bufferPtr = buffer.second;
ANGLE_TRY(SerializeBuffer(context, bos, &scratchBuffer, bufferPtr));
}
scratchBuffer.clear();
return Result::Continue;
}
Result SerializeFramebuffer(const gl::Context *context,
gl::BinaryOutputStream *bos,
ScratchBuffer *scratchBuffer,
gl::Framebuffer *framebuffer)
{
return SerializeFramebufferState(context, bos, scratchBuffer, framebuffer,
framebuffer->getState());
}
Result SerializeFramebufferState(const gl::Context *context,
gl::BinaryOutputStream *bos,
ScratchBuffer *scratchBuffer,
gl::Framebuffer *framebuffer,
const gl::FramebufferState &framebufferState)
{
bos->writeInt(framebufferState.id().value);
bos->writeString(framebufferState.getLabel());
bos->writeIntVector(framebufferState.getDrawBufferStates());
bos->writeInt(framebufferState.getReadBufferState());
bos->writeInt(framebufferState.getDefaultWidth());
bos->writeInt(framebufferState.getDefaultHeight());
bos->writeInt(framebufferState.getDefaultSamples());
bos->writeInt(framebufferState.getDefaultFixedSampleLocations());
bos->writeInt(framebufferState.getDefaultLayers());
const std::vector<gl::FramebufferAttachment> &colorAttachments =
framebufferState.getColorAttachments();
for (const gl::FramebufferAttachment &colorAttachment : colorAttachments)
{
if (colorAttachment.isAttached())
{
ANGLE_TRY(SerializeFramebufferAttachment(context, bos, scratchBuffer, framebuffer,
colorAttachment));
}
}
if (framebuffer->getDepthStencilAttachment())
{
ANGLE_TRY(SerializeFramebufferAttachment(context, bos, scratchBuffer, framebuffer,
*framebuffer->getDepthStencilAttachment()));
}
else
{
if (framebuffer->getDepthAttachment())
{
ANGLE_TRY(SerializeFramebufferAttachment(context, bos, scratchBuffer, framebuffer,
*framebuffer->getDepthAttachment()));
}
if (framebuffer->getStencilAttachment())
{
ANGLE_TRY(SerializeFramebufferAttachment(context, bos, scratchBuffer, framebuffer,
*framebuffer->getStencilAttachment()));
}
}
return Result::Continue;
}
Result SerializeFramebufferAttachment(const gl::Context *context,
gl::BinaryOutputStream *bos,
ScratchBuffer *scratchBuffer,
gl::Framebuffer *framebuffer,
const gl::FramebufferAttachment &framebufferAttachment)
{
bos->writeInt(framebufferAttachment.type());
// serialize target variable
bos->writeInt(framebufferAttachment.getBinding());
if (framebufferAttachment.type() == GL_TEXTURE)
{
SerializeImageIndex(bos, framebufferAttachment.getTextureImageIndex());
}
bos->writeInt(framebufferAttachment.getNumViews());
bos->writeInt(framebufferAttachment.isMultiview());
bos->writeInt(framebufferAttachment.getBaseViewIndex());
bos->writeInt(framebufferAttachment.getRenderToTextureSamples());
GLenum prevReadBufferState = framebuffer->getReadBufferState();
GLenum binding = framebufferAttachment.getBinding();
if (IsValidColorAttachmentBinding(binding,
framebuffer->getState().getColorAttachments().size()))
{
framebuffer->setReadBuffer(framebufferAttachment.getBinding());
ANGLE_TRY(framebuffer->syncState(context, GL_FRAMEBUFFER));
}
MemoryBuffer *pixelsPtr = nullptr;
ANGLE_TRY(ReadPixelsFromAttachment(context, framebuffer, framebufferAttachment, scratchBuffer,
&pixelsPtr));
bos->writeBytes(pixelsPtr->data(), pixelsPtr->size());
// Reset framebuffer state
framebuffer->setReadBuffer(prevReadBufferState);
return Result::Continue;
}
void SerializeImageIndex(gl::BinaryOutputStream *bos, const gl::ImageIndex &imageIndex)
{
bos->writeEnum(imageIndex.getType());
bos->writeInt(imageIndex.getLevelIndex());
bos->writeInt(imageIndex.getLayerIndex());
bos->writeInt(imageIndex.getLayerCount());
}
Result SerializeBuffer(const gl::Context *context,
gl::BinaryOutputStream *bos,
ScratchBuffer *scratchBuffer,
gl::Buffer *buffer)
{
SerializeBufferState(bos, buffer->getState());
MemoryBuffer *dataPtr = nullptr;
ANGLE_CHECK_GL_ALLOC(
const_cast<gl::Context *>(context),
scratchBuffer->getInitialized(static_cast<size_t>(buffer->getSize()), &dataPtr, 0));
ANGLE_TRY(buffer->getSubData(context, 0, dataPtr->size(), dataPtr->data()));
bos->writeBytes(dataPtr->data(), dataPtr->size());
return Result::Continue;
}
void SerializeBufferState(gl::BinaryOutputStream *bos, const gl::BufferState &bufferState)
{
bos->writeString(bufferState.getLabel());
bos->writeEnum(bufferState.getUsage());
bos->writeInt(bufferState.getSize());
bos->writeInt(bufferState.getAccessFlags());
bos->writeInt(bufferState.getAccess());
bos->writeInt(bufferState.isMapped());
bos->writeInt(bufferState.getMapOffset());
bos->writeInt(bufferState.getMapLength());
}
} // namespace angle