2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-06-16 22:34:35 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-07-18 12:28:38 +00:00
|
|
|
#include "dawn/dawncpp.h"
|
2018-09-19 00:32:52 +00:00
|
|
|
#include "dawn_native/DawnNative.h"
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2019-04-26 07:52:57 +00:00
|
|
|
|
2017-07-17 13:37:08 +00:00
|
|
|
#include <memory>
|
2019-02-21 16:29:12 +00:00
|
|
|
#include <unordered_map>
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2018-07-18 13:28:38 +00:00
|
|
|
// Getting data back from Dawn is done in an async manners so all expectations are "deferred"
|
2017-06-16 22:34:35 +00:00
|
|
|
// until the end of the test. Also expectations use a copy to a MapRead buffer to get the data
|
2018-07-18 13:18:25 +00:00
|
|
|
// so resources should have the TransferSrc allowed usage bit if you want to add expectations on
|
|
|
|
// them.
|
|
|
|
#define EXPECT_BUFFER_U32_EQ(expected, buffer, offset) \
|
|
|
|
AddBufferExpectation(__FILE__, __LINE__, buffer, offset, sizeof(uint32_t), \
|
|
|
|
new detail::ExpectEq<uint32_t>(expected))
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2018-07-18 13:18:25 +00:00
|
|
|
#define EXPECT_BUFFER_U32_RANGE_EQ(expected, buffer, offset, count) \
|
|
|
|
AddBufferExpectation(__FILE__, __LINE__, buffer, offset, sizeof(uint32_t) * count, \
|
|
|
|
new detail::ExpectEq<uint32_t>(expected, count))
|
2017-07-04 14:53:42 +00:00
|
|
|
|
2017-06-27 04:11:16 +00:00
|
|
|
// Test a pixel of the mip level 0 of a 2D texture.
|
2018-12-05 03:22:04 +00:00
|
|
|
#define EXPECT_PIXEL_RGBA8_EQ(expected, texture, x, y) \
|
|
|
|
AddTextureExpectation(__FILE__, __LINE__, texture, x, y, 1, 1, 0, 0, sizeof(RGBA8), \
|
2018-07-18 13:18:25 +00:00
|
|
|
new detail::ExpectEq<RGBA8>(expected))
|
2017-07-17 20:16:50 +00:00
|
|
|
|
2018-12-05 03:22:04 +00:00
|
|
|
#define EXPECT_TEXTURE_RGBA8_EQ(expected, texture, x, y, width, height, level, slice) \
|
|
|
|
AddTextureExpectation(__FILE__, __LINE__, texture, x, y, width, height, level, slice, \
|
|
|
|
sizeof(RGBA8), \
|
2018-07-18 13:18:25 +00:00
|
|
|
new detail::ExpectEq<RGBA8>(expected, (width) * (height)))
|
2017-06-27 04:11:16 +00:00
|
|
|
|
2019-02-28 09:45:48 +00:00
|
|
|
// Should only be used to test validation of function that can't be tested by regular validation
|
|
|
|
// tests;
|
|
|
|
#define ASSERT_DEVICE_ERROR(statement) \
|
|
|
|
StartExpectDeviceError(); \
|
|
|
|
statement; \
|
2019-05-01 12:07:37 +00:00
|
|
|
FlushWire(); \
|
2019-02-28 09:45:48 +00:00
|
|
|
ASSERT_TRUE(EndExpectDeviceError());
|
|
|
|
|
2017-06-27 04:11:16 +00:00
|
|
|
struct RGBA8 {
|
2018-07-18 13:18:25 +00:00
|
|
|
constexpr RGBA8() : RGBA8(0, 0, 0, 0) {
|
|
|
|
}
|
|
|
|
constexpr RGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : r(r), g(g), b(b), a(a) {
|
2017-06-27 04:11:16 +00:00
|
|
|
}
|
|
|
|
bool operator==(const RGBA8& other) const;
|
|
|
|
bool operator!=(const RGBA8& other) const;
|
|
|
|
|
|
|
|
uint8_t r, g, b, a;
|
|
|
|
};
|
2018-07-18 13:18:25 +00:00
|
|
|
std::ostream& operator<<(std::ostream& stream, const RGBA8& color);
|
2017-06-27 04:11:16 +00:00
|
|
|
|
2019-04-26 07:52:57 +00:00
|
|
|
struct DawnTestParam {
|
|
|
|
constexpr explicit DawnTestParam(dawn_native::BackendType backendType)
|
|
|
|
: backendType(backendType) {
|
|
|
|
}
|
|
|
|
|
|
|
|
dawn_native::BackendType backendType;
|
|
|
|
|
|
|
|
// TODO(jiawei.shao@intel.com): support enabling and disabling multiple workarounds.
|
|
|
|
const char* forceEnabledWorkaround = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-02-05 12:17:20 +00:00
|
|
|
// Shorthands for backend types used in the DAWN_INSTANTIATE_TEST
|
2019-04-26 07:52:57 +00:00
|
|
|
static constexpr DawnTestParam D3D12Backend(dawn_native::BackendType::D3D12);
|
|
|
|
static constexpr DawnTestParam MetalBackend(dawn_native::BackendType::Metal);
|
|
|
|
static constexpr DawnTestParam OpenGLBackend(dawn_native::BackendType::OpenGL);
|
|
|
|
static constexpr DawnTestParam VulkanBackend(dawn_native::BackendType::Vulkan);
|
|
|
|
|
|
|
|
DawnTestParam ForceWorkaround(const DawnTestParam& originParam, const char* workaround);
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2019-02-21 16:29:12 +00:00
|
|
|
struct GLFWwindow;
|
|
|
|
|
2017-06-16 22:34:35 +00:00
|
|
|
namespace utils {
|
|
|
|
class BackendBinding;
|
2018-07-26 13:07:57 +00:00
|
|
|
class TerribleCommandBuffer;
|
2017-06-16 22:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class Expectation;
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:07:57 +00:00
|
|
|
namespace dawn_wire {
|
2019-02-11 21:50:16 +00:00
|
|
|
class WireClient;
|
|
|
|
class WireServer;
|
2018-07-26 13:07:57 +00:00
|
|
|
} // namespace dawn_wire
|
2018-06-07 11:10:44 +00:00
|
|
|
|
2019-02-21 16:29:12 +00:00
|
|
|
void InitDawnEnd2EndTestEnvironment(int argc, char** argv);
|
|
|
|
|
|
|
|
class DawnTestEnvironment : public testing::Environment {
|
|
|
|
public:
|
|
|
|
DawnTestEnvironment(int argc, char** argv);
|
|
|
|
~DawnTestEnvironment() = default;
|
|
|
|
|
|
|
|
void SetUp() override;
|
|
|
|
|
2019-05-01 12:57:27 +00:00
|
|
|
bool UsesWire() const;
|
2019-02-21 16:29:12 +00:00
|
|
|
dawn_native::Instance* GetInstance() const;
|
|
|
|
GLFWwindow* GetWindowForBackend(dawn_native::BackendType type) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void CreateBackendWindow(dawn_native::BackendType type);
|
|
|
|
|
|
|
|
bool mUseWire = false;
|
2019-05-15 06:06:26 +00:00
|
|
|
bool mEnableBackendValidation = false;
|
2019-02-21 16:29:12 +00:00
|
|
|
std::unique_ptr<dawn_native::Instance> mInstance;
|
|
|
|
|
|
|
|
// Windows don't usually like to be bound to one API than the other, for example switching
|
|
|
|
// from Vulkan to OpenGL causes crashes on some drivers. Because of this, we lazily created
|
|
|
|
// a window for each backing API.
|
|
|
|
std::unordered_map<dawn_native::BackendType, GLFWwindow*> mWindows;
|
|
|
|
};
|
|
|
|
|
2019-04-26 07:52:57 +00:00
|
|
|
class DawnTest : public ::testing::TestWithParam<DawnTestParam> {
|
2018-06-07 11:10:44 +00:00
|
|
|
public:
|
2018-09-06 13:26:48 +00:00
|
|
|
DawnTest();
|
2018-07-18 13:18:25 +00:00
|
|
|
~DawnTest();
|
2018-06-07 11:10:44 +00:00
|
|
|
|
|
|
|
void SetUp() override;
|
|
|
|
void TearDown() override;
|
|
|
|
|
|
|
|
bool IsD3D12() const;
|
|
|
|
bool IsMetal() const;
|
|
|
|
bool IsOpenGL() const;
|
|
|
|
bool IsVulkan() const;
|
|
|
|
|
2018-09-19 00:32:52 +00:00
|
|
|
bool IsAMD() const;
|
|
|
|
bool IsARM() const;
|
|
|
|
bool IsImgTec() const;
|
|
|
|
bool IsIntel() const;
|
|
|
|
bool IsNvidia() const;
|
|
|
|
bool IsQualcomm() const;
|
|
|
|
|
|
|
|
bool IsWindows() const;
|
|
|
|
bool IsLinux() const;
|
|
|
|
bool IsMacOS() const;
|
|
|
|
|
2019-05-01 12:57:27 +00:00
|
|
|
bool UsesWire() const;
|
|
|
|
|
2019-02-28 09:45:48 +00:00
|
|
|
void StartExpectDeviceError();
|
|
|
|
bool EndExpectDeviceError();
|
|
|
|
|
2018-06-07 11:10:44 +00:00
|
|
|
protected:
|
2018-07-18 11:43:49 +00:00
|
|
|
dawn::Device device;
|
|
|
|
dawn::Queue queue;
|
|
|
|
dawn::SwapChain swapchain;
|
2018-06-07 11:10:44 +00:00
|
|
|
|
|
|
|
// Helper methods to implement the EXPECT_ macros
|
|
|
|
std::ostringstream& AddBufferExpectation(const char* file,
|
|
|
|
int line,
|
2018-07-18 11:43:49 +00:00
|
|
|
const dawn::Buffer& buffer,
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t offset,
|
|
|
|
uint64_t size,
|
2018-06-07 11:10:44 +00:00
|
|
|
detail::Expectation* expectation);
|
|
|
|
std::ostringstream& AddTextureExpectation(const char* file,
|
|
|
|
int line,
|
2018-07-18 11:43:49 +00:00
|
|
|
const dawn::Texture& texture,
|
2018-06-07 11:10:44 +00:00
|
|
|
uint32_t x,
|
|
|
|
uint32_t y,
|
|
|
|
uint32_t width,
|
|
|
|
uint32_t height,
|
|
|
|
uint32_t level,
|
2018-12-05 03:22:04 +00:00
|
|
|
uint32_t slice,
|
2018-06-07 11:10:44 +00:00
|
|
|
uint32_t pixelSize,
|
|
|
|
detail::Expectation* expectation);
|
|
|
|
|
|
|
|
void WaitABit();
|
2019-05-01 12:07:37 +00:00
|
|
|
void FlushWire();
|
2018-06-07 11:10:44 +00:00
|
|
|
|
|
|
|
void SwapBuffersForCapture();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Things used to set up testing through the Wire.
|
2019-02-11 21:50:16 +00:00
|
|
|
std::unique_ptr<dawn_wire::WireServer> mWireServer;
|
|
|
|
std::unique_ptr<dawn_wire::WireClient> mWireClient;
|
2018-09-06 13:26:48 +00:00
|
|
|
std::unique_ptr<utils::TerribleCommandBuffer> mC2sBuf;
|
|
|
|
std::unique_ptr<utils::TerribleCommandBuffer> mS2cBuf;
|
2018-06-07 11:10:44 +00:00
|
|
|
|
2019-02-28 09:45:48 +00:00
|
|
|
// Tracking for validation errors
|
2019-03-11 16:52:42 +00:00
|
|
|
static void OnDeviceError(const char* message, DawnCallbackUserdata userdata);
|
2019-02-28 09:45:48 +00:00
|
|
|
bool mExpectError = false;
|
|
|
|
bool mError = false;
|
|
|
|
|
2018-06-07 11:10:44 +00:00
|
|
|
// MapRead buffers used to get data for the expectations
|
|
|
|
struct ReadbackSlot {
|
2018-07-18 11:43:49 +00:00
|
|
|
dawn::Buffer buffer;
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t bufferSize;
|
2018-06-07 11:10:44 +00:00
|
|
|
const void* mappedData = nullptr;
|
|
|
|
};
|
|
|
|
std::vector<ReadbackSlot> mReadbackSlots;
|
|
|
|
|
|
|
|
// Maps all the buffers and fill ReadbackSlot::mappedData
|
|
|
|
void MapSlotsSynchronously();
|
2019-03-11 16:52:42 +00:00
|
|
|
static void SlotMapReadCallback(DawnBufferMapAsyncStatus status,
|
2018-06-07 11:10:44 +00:00
|
|
|
const void* data,
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t dataLength,
|
2019-03-11 16:52:42 +00:00
|
|
|
DawnCallbackUserdata userdata);
|
2018-06-07 11:10:44 +00:00
|
|
|
size_t mNumPendingMapOperations = 0;
|
|
|
|
|
|
|
|
// Reserve space where the data for an expectation can be copied
|
|
|
|
struct ReadbackReservation {
|
2018-07-18 11:43:49 +00:00
|
|
|
dawn::Buffer buffer;
|
2018-06-07 11:10:44 +00:00
|
|
|
size_t slot;
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t offset;
|
2018-06-07 11:10:44 +00:00
|
|
|
};
|
2019-04-05 20:51:29 +00:00
|
|
|
ReadbackReservation ReserveReadback(uint64_t readbackSize);
|
2018-06-07 11:10:44 +00:00
|
|
|
|
|
|
|
struct DeferredExpectation {
|
|
|
|
const char* file;
|
|
|
|
int line;
|
|
|
|
size_t readbackSlot;
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t readbackOffset;
|
|
|
|
uint64_t size;
|
2018-06-07 11:10:44 +00:00
|
|
|
uint32_t rowBytes;
|
|
|
|
uint32_t rowPitch;
|
2018-09-06 13:26:48 +00:00
|
|
|
std::unique_ptr<detail::Expectation> expectation;
|
2018-06-07 11:10:44 +00:00
|
|
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
|
|
|
|
// Use unique_ptr because of missing move/copy constructors on std::basic_ostringstream
|
|
|
|
std::unique_ptr<std::ostringstream> message;
|
|
|
|
};
|
|
|
|
std::vector<DeferredExpectation> mDeferredExpectations;
|
|
|
|
|
|
|
|
// Assuming the data is mapped, checks all expectations
|
|
|
|
void ResolveExpectations();
|
|
|
|
|
2018-09-06 13:26:48 +00:00
|
|
|
std::unique_ptr<utils::BackendBinding> mBinding;
|
2018-09-19 00:32:52 +00:00
|
|
|
|
|
|
|
dawn_native::PCIInfo mPCIInfo;
|
2017-06-16 22:34:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Instantiate the test once for each backend provided after the first argument. Use it like this:
|
2018-07-18 13:16:37 +00:00
|
|
|
// DAWN_INSTANTIATE_TEST(MyTestFixture, MetalBackend, OpenGLBackend)
|
2019-02-21 09:43:40 +00:00
|
|
|
#define DAWN_INSTANTIATE_TEST(testName, firstParam, ...) \
|
|
|
|
const decltype(firstParam) testName##params[] = {firstParam, ##__VA_ARGS__}; \
|
|
|
|
INSTANTIATE_TEST_SUITE_P( \
|
|
|
|
, testName, \
|
|
|
|
testing::ValuesIn(::detail::FilterBackends( \
|
|
|
|
testName##params, sizeof(testName##params) / sizeof(firstParam))), \
|
2019-02-21 14:57:01 +00:00
|
|
|
::detail::GetParamName)
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2018-08-30 05:10:52 +00:00
|
|
|
// Skip a test when the given condition is satisfied.
|
|
|
|
#define DAWN_SKIP_TEST_IF(condition) \
|
|
|
|
if (condition) { \
|
|
|
|
std::cout << "Test skipped: " #condition "." << std::endl; \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:34:35 +00:00
|
|
|
namespace detail {
|
2018-07-18 13:16:37 +00:00
|
|
|
// Helper functions used for DAWN_INSTANTIATE_TEST
|
2019-02-05 12:17:20 +00:00
|
|
|
bool IsBackendAvailable(dawn_native::BackendType type);
|
2019-04-26 07:52:57 +00:00
|
|
|
std::vector<DawnTestParam> FilterBackends(const DawnTestParam* params, size_t numParams);
|
|
|
|
std::string GetParamName(const testing::TestParamInfo<DawnTestParam>& info);
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
// All classes used to implement the deferred expectations should inherit from this.
|
|
|
|
class Expectation {
|
2018-07-18 13:18:25 +00:00
|
|
|
public:
|
|
|
|
virtual ~Expectation() = default;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2018-07-18 13:18:25 +00:00
|
|
|
// Will be called with the buffer or texture data the expectation should check.
|
|
|
|
virtual testing::AssertionResult Check(const void* data, size_t size) = 0;
|
2017-06-16 22:34:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Expectation that checks the data is equal to some expected values.
|
2018-07-18 13:18:25 +00:00
|
|
|
template <typename T>
|
2017-06-16 22:34:35 +00:00
|
|
|
class ExpectEq : public Expectation {
|
2018-07-18 13:18:25 +00:00
|
|
|
public:
|
|
|
|
ExpectEq(T singleValue);
|
|
|
|
ExpectEq(const T* values, const unsigned int count);
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2018-07-18 13:18:25 +00:00
|
|
|
testing::AssertionResult Check(const void* data, size_t size) override;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2018-07-18 13:18:25 +00:00
|
|
|
private:
|
|
|
|
std::vector<T> mExpected;
|
2017-06-16 22:34:35 +00:00
|
|
|
};
|
2018-02-04 16:07:02 +00:00
|
|
|
extern template class ExpectEq<uint8_t>;
|
2017-06-16 22:34:35 +00:00
|
|
|
extern template class ExpectEq<uint32_t>;
|
2017-06-27 04:11:16 +00:00
|
|
|
extern template class ExpectEq<RGBA8>;
|
2018-07-18 13:18:25 +00:00
|
|
|
} // namespace detail
|