-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBufferTests.cpp
More file actions
155 lines (135 loc) · 6.92 KB
/
BufferTests.cpp
File metadata and controls
155 lines (135 loc) · 6.92 KB
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
/***************************************************************************
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************/
#include "Testing/UnitTest.h"
namespace Falcor
{
namespace
{
const uint32_t kIterations = 4;
enum class Type
{
ByteAddressBuffer = 0,
TypedBuffer = 1,
StructuredBuffer = 2,
};
// static_assert must depend on template parameter, otherwise it will be evaluated before template instantiation (and always evaluate false)
template <Type...> inline constexpr bool alwaysFalse = false;
template <Type type>
void testBuffer(GPUUnitTestContext& ctx, uint32_t numElems, uint32_t index = 0, uint32_t count = 0)
{
numElems = div_round_up(numElems, 256u) * 256u; // Make sure we run full thread groups.
// Create a data blob for the test.
// We fill it some numbers that don't overlap with the test buffers values.
std::vector<uint32_t> blob;
if (count > 0)
{
blob.resize(count);
for (uint32_t i = 0; i < blob.size(); i++) blob[i] = (count - i) * 3;
}
// Create clear program.
Program::DefineList defines = { { "TYPE", std::to_string((uint32_t)type) } };
ctx.createProgram("Tests/Core/BufferTests.cs.slang", "clearBuffer", defines);
// Create test buffer.
Buffer::SharedPtr pBuffer;
if constexpr (type == Type::ByteAddressBuffer) pBuffer = Buffer::create(numElems * sizeof(uint32_t), ResourceBindFlags::UnorderedAccess, Buffer::CpuAccess::None);
else if constexpr (type == Type::TypedBuffer) pBuffer = Buffer::createTyped<uint32_t>(numElems, ResourceBindFlags::UnorderedAccess);
else if constexpr (type == Type::StructuredBuffer) pBuffer = Buffer::createStructured(ctx.getProgram(), "buffer", numElems, ResourceBindFlags::UnorderedAccess);
else static_assert(alwaysFalse);
ctx["buffer"] = pBuffer;
// Run kernel to clear the buffer.
// We clear explicitly instead of using clearUAV() as the latter is not compatible with RWStructuredBuffer.
ctx.runProgram(numElems, 1, 1);
// Create update program.
ctx.createProgram("Tests/Core/BufferTests.cs.slang", "updateBuffer", defines);
ctx["buffer"] = pBuffer;
// Run kernel N times to update the buffer (RMW).
for (uint32_t i = 0; i < kIterations; i++) ctx.runProgram(numElems, 1, 1);
// Use setBlob() to update part of the buffer from the CPU.
if (count > 0)
{
assert(index + blob.size() <= numElems);
pBuffer->setBlob(blob.data(), index * sizeof(uint32_t), blob.size() * sizeof(uint32_t));
}
// Run kernel N times to update the buffer (RMW).
for (uint32_t i = 0; i < kIterations; i++) ctx.runProgram(numElems, 1, 1);
// Run kernel to read values in the buffer.
ctx.createProgram("Tests/Core/BufferTests.cs.slang", "readBuffer", defines);
ctx.allocateStructuredBuffer("result", numElems);
ctx["buffer"] = pBuffer;
ctx.runProgram(numElems, 1, 1);
// Verify results.
const uint32_t* pResult = ctx.mapBuffer<const uint32_t>("result");
for (uint32_t i = 0; i < numElems; i++)
{
// Each RMW pass adds i+1 to the element at index i.
// We run kIterations passes, then replace part of the buffer, followed by kIterations more passes.
uint32_t expected = (i + 1) * kIterations * 2;
if (i >= index && i < index + blob.size()) expected = blob[i - index] + (i + 1) * kIterations;
uint32_t result = pResult[i];
EXPECT_EQ(result, expected) << "i = " << i << " (numElems = " << numElems << " index = " << index << " count = " << count << ")";
}
ctx.unmapBuffer("result");
}
}
GPU_TEST(RawBuffer)
{
auto testFunc = testBuffer<Type::ByteAddressBuffer>;
for (uint32_t numElems = 1u << 8; numElems <= (1u << 16); numElems <<= 4)
{
testFunc(ctx, numElems, 0, 0);
testFunc(ctx, numElems, 0, 1);
testFunc(ctx, numElems, 0, numElems / 2);
testFunc(ctx, numElems, 1, 1);
testFunc(ctx, numElems, numElems / 2 + 3, numElems / 4 - 1);
}
}
GPU_TEST(TypedBuffer)
{
auto testFunc = testBuffer<Type::TypedBuffer>;
for (uint32_t numElems = 1u << 8; numElems <= (1u << 16); numElems <<= 4)
{
testFunc(ctx, numElems, 0, 0);
testFunc(ctx, numElems, 0, 1);
testFunc(ctx, numElems, 0, numElems / 2);
testFunc(ctx, numElems, 1, 1);
testFunc(ctx, numElems, numElems / 2 + 3, numElems / 4 - 1);
}
}
GPU_TEST(StructuredBuffer)
{
auto testFunc = testBuffer<Type::StructuredBuffer>;
for (uint32_t numElems = 1u << 8; numElems <= (1u << 16); numElems <<= 4)
{
testFunc(ctx, numElems, 0, 0);
testFunc(ctx, numElems, 0, 1);
testFunc(ctx, numElems, 0, numElems / 2);
testFunc(ctx, numElems, 1, 1);
testFunc(ctx, numElems, numElems / 2 + 3, numElems / 4 - 1);
}
}
}