2017-06-16 22:34:35 +00:00
|
|
|
// Copyright 2017 The NXT Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "nxt/nxtcpp.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2017-07-17 13:37:08 +00:00
|
|
|
#include <memory>
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
// Getting data back from NXT is done in an async manners so all expectations are "deferred"
|
|
|
|
// until the end of the test. Also expectations use a copy to a MapRead buffer to get the data
|
2017-07-18 14:31:50 +00:00
|
|
|
// so resources should have the TransferSrc allowed usage bit if you want to add expectations on them.
|
2017-06-16 22:34:35 +00:00
|
|
|
#define EXPECT_BUFFER_U32_EQ(expected, buffer, offset) \
|
2017-07-17 13:37:08 +00:00
|
|
|
AddBufferExpectation(__FILE__, __LINE__, buffer, offset, sizeof(uint32_t), new detail::ExpectEq<uint32_t>(expected))
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2017-07-04 14:53:42 +00:00
|
|
|
#define EXPECT_BUFFER_U32_RANGE_EQ(expected, buffer, offset, count) \
|
2017-07-17 13:37:08 +00:00
|
|
|
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.
|
|
|
|
#define EXPECT_PIXEL_RGBA8_EQ(expected, texture, x, y) \
|
2017-07-17 20:16:50 +00:00
|
|
|
AddTextureExpectation(__FILE__, __LINE__, texture, x, y, 1, 1, 0, sizeof(RGBA8), new detail::ExpectEq<RGBA8>(expected))
|
|
|
|
|
|
|
|
#define EXPECT_TEXTURE_RGBA8_EQ(expected, texture, x, y, width, height, level) \
|
2017-08-14 23:32:26 +00:00
|
|
|
AddTextureExpectation(__FILE__, __LINE__, texture, x, y, width, height, level, sizeof(RGBA8), new detail::ExpectEq<RGBA8>(expected, (width) * (height)))
|
2017-06-27 04:11:16 +00:00
|
|
|
|
|
|
|
struct RGBA8 {
|
2017-07-17 20:16:50 +00:00
|
|
|
constexpr RGBA8() : RGBA8(0,0,0,0) {}
|
2017-06-27 04:11:16 +00:00
|
|
|
constexpr RGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a): r(r), g(g), b(b), a(a) {
|
|
|
|
}
|
|
|
|
bool operator==(const RGBA8& other) const;
|
|
|
|
bool operator!=(const RGBA8& other) const;
|
|
|
|
|
|
|
|
uint8_t r, g, b, a;
|
|
|
|
};
|
|
|
|
std::ostream& operator<< (std::ostream& stream, const RGBA8& color);
|
|
|
|
|
2017-06-16 22:34:35 +00:00
|
|
|
// Backend types used in the NXT_INSTANTIATE_TEST
|
|
|
|
enum BackendType {
|
|
|
|
D3D12Backend,
|
|
|
|
MetalBackend,
|
|
|
|
OpenGLBackend,
|
|
|
|
VulkanBackend,
|
|
|
|
NumBackendTypes,
|
|
|
|
};
|
2017-07-14 21:59:27 +00:00
|
|
|
std::ostream &operator<<(std::ostream& stream, BackendType backend);
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
namespace utils {
|
|
|
|
class BackendBinding;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class Expectation;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NXTTest : public ::testing::TestWithParam<BackendType> {
|
|
|
|
public:
|
|
|
|
~NXTTest();
|
|
|
|
|
|
|
|
void SetUp() override;
|
|
|
|
void TearDown() override;
|
2017-07-17 21:13:57 +00:00
|
|
|
|
2017-07-13 00:36:36 +00:00
|
|
|
bool IsD3D12() const;
|
2017-07-18 14:31:50 +00:00
|
|
|
bool IsMetal() const;
|
|
|
|
bool IsOpenGL() const;
|
|
|
|
bool IsVulkan() const;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nxt::Device device;
|
|
|
|
nxt::Queue queue;
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::SwapChain swapchain;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
// Helper methods to implement the EXPECT_ macros
|
2017-07-17 13:37:08 +00:00
|
|
|
std::ostringstream& AddBufferExpectation(const char* file, int line, const nxt::Buffer& buffer, uint32_t offset, uint32_t size, detail::Expectation* expectation);
|
2017-07-17 20:16:50 +00:00
|
|
|
std::ostringstream& AddTextureExpectation(const char* file, int line, const nxt::Texture& texture, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t level, uint32_t pixelSize, detail::Expectation* expectation);
|
2017-06-16 22:34:35 +00:00
|
|
|
|
2017-07-17 21:13:57 +00:00
|
|
|
void WaitABit();
|
2017-07-28 01:30:57 +00:00
|
|
|
|
|
|
|
void SwapBuffersForCapture();
|
2017-07-12 17:17:35 +00:00
|
|
|
|
2017-06-16 22:34:35 +00:00
|
|
|
private:
|
|
|
|
// MapRead buffers used to get data for the expectations
|
|
|
|
struct ReadbackSlot {
|
|
|
|
nxt::Buffer buffer;
|
|
|
|
uint32_t bufferSize;
|
|
|
|
const void* mappedData = nullptr;
|
|
|
|
};
|
2017-11-23 19:51:16 +00:00
|
|
|
std::vector<ReadbackSlot> mReadbackSlots;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
// Maps all the buffers and fill ReadbackSlot::mappedData
|
|
|
|
void MapSlotsSynchronously();
|
2018-03-20 23:48:54 +00:00
|
|
|
static void SlotMapReadCallback(nxtBufferMapAsyncStatus status,
|
|
|
|
const void* data,
|
|
|
|
nxtCallbackUserdata userdata);
|
2017-11-23 19:51:16 +00:00
|
|
|
size_t mNumPendingMapOperations = 0;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
// Reserve space where the data for an expectation can be copied
|
|
|
|
struct ReadbackReservation {
|
|
|
|
nxt::Buffer buffer;
|
|
|
|
size_t slot;
|
|
|
|
uint32_t offset;
|
|
|
|
};
|
|
|
|
ReadbackReservation ReserveReadback(uint32_t readbackSize);
|
|
|
|
|
|
|
|
struct DeferredExpectation {
|
|
|
|
const char* file;
|
|
|
|
int line;
|
|
|
|
size_t readbackSlot;
|
|
|
|
uint32_t readbackOffset;
|
|
|
|
uint32_t size;
|
2017-07-13 19:10:30 +00:00
|
|
|
uint32_t rowBytes;
|
|
|
|
uint32_t rowPitch;
|
2017-06-16 22:34:35 +00:00
|
|
|
detail::Expectation* expectation;
|
2017-07-17 13:37:08 +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;
|
2017-06-16 22:34:35 +00:00
|
|
|
};
|
2017-11-23 19:51:16 +00:00
|
|
|
std::vector<DeferredExpectation> mDeferredExpectations;
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
// Assuming the data is mapped, checks all expectations
|
|
|
|
void ResolveExpectations();
|
|
|
|
|
2017-11-23 19:51:16 +00:00
|
|
|
utils::BackendBinding* mBinding = nullptr;
|
2017-06-16 22:34:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Instantiate the test once for each backend provided after the first argument. Use it like this:
|
2017-11-20 20:48:58 +00:00
|
|
|
// NXT_INSTANTIATE_TEST(MyTestFixture, MetalBackend, OpenGLBackend)
|
2017-06-16 22:34:35 +00:00
|
|
|
#define NXT_INSTANTIATE_TEST(testName, firstParam, ...) \
|
|
|
|
const decltype(firstParam) testName##params[] = { firstParam, ##__VA_ARGS__ }; \
|
|
|
|
INSTANTIATE_TEST_CASE_P(, testName, \
|
|
|
|
testing::ValuesIn(::detail::FilterBackends(testName##params, sizeof(testName##params) / sizeof(firstParam))), \
|
|
|
|
testing::PrintToStringParamName());
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
// Helper functions used for NXT_INSTANTIATE_TEST
|
|
|
|
bool IsBackendAvailable(BackendType type);
|
|
|
|
std::vector<BackendType> FilterBackends(const BackendType* types, size_t numParams);
|
|
|
|
|
|
|
|
// All classes used to implement the deferred expectations should inherit from this.
|
|
|
|
class Expectation {
|
|
|
|
public:
|
|
|
|
virtual ~Expectation() = default;
|
|
|
|
|
|
|
|
// Will be called with the buffer or texture data the expectation should check.
|
|
|
|
virtual testing::AssertionResult Check(const void* data, size_t size) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Expectation that checks the data is equal to some expected values.
|
|
|
|
template<typename T>
|
|
|
|
class ExpectEq : public Expectation {
|
|
|
|
public:
|
|
|
|
ExpectEq(T singleValue);
|
2017-07-04 14:53:42 +00:00
|
|
|
ExpectEq(const T* values, const unsigned int count);
|
2017-06-16 22:34:35 +00:00
|
|
|
|
|
|
|
testing::AssertionResult Check(const void* data, size_t size) override;
|
|
|
|
|
|
|
|
private:
|
2017-11-23 19:51:16 +00:00
|
|
|
std::vector<T> mExpected;
|
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>;
|
2017-06-16 22:34:35 +00:00
|
|
|
}
|
|
|
|
|