1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tests/Test.h"
9
10 #include "include/gpu/graphite/Context.h"
11 #include "include/gpu/graphite/Recorder.h"
12 #include "include/gpu/graphite/Recording.h"
13 #include "src/gpu/graphite/Buffer.h"
14 #include "src/gpu/graphite/BufferManager.h"
15 #include "src/gpu/graphite/Caps.h"
16 #include "src/gpu/graphite/ContextPriv.h"
17 #include "src/gpu/graphite/RecorderPriv.h"
18 #include "src/gpu/graphite/RecordingPriv.h"
19
20 namespace skgpu::graphite {
21 namespace {
22
is_offset_aligned(size_t offset,size_t alignment)23 static bool is_offset_aligned(size_t offset, size_t alignment) {
24 return 0 == (offset & (alignment - 1));
25 }
26
27 } // namespace
28
DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(BufferManagerGpuOnlyBufferTest,reporter,context)29 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(BufferManagerGpuOnlyBufferTest, reporter, context) {
30 std::unique_ptr<Recorder> recorder = context->makeRecorder();
31 DrawBufferManager* mgr = recorder->priv().drawBufferManager();
32
33 // Allocate a series of GPU-only buffers. These buffers should not be mapped before and after
34 // they get transferred to the recording.
35 auto ssbo = mgr->getStorage(10);
36 auto vertex = mgr->getVertexStorage(10);
37 auto index = mgr->getIndexStorage(10);
38 auto indirect = mgr->getIndirectStorage(10);
39
40 REPORTER_ASSERT(reporter, !ssbo.fBuffer->isMapped());
41 REPORTER_ASSERT(reporter, !vertex.fBuffer->isMapped());
42 REPORTER_ASSERT(reporter, !index.fBuffer->isMapped());
43 REPORTER_ASSERT(reporter, !indirect.fBuffer->isMapped());
44
45 // Ensure that the buffers' starting alignment matches the required storage buffer alignment.
46 size_t requiredAlignment = context->priv().caps()->requiredStorageBufferAlignment();
47 REPORTER_ASSERT(reporter, is_offset_aligned(ssbo.fOffset, requiredAlignment));
48 REPORTER_ASSERT(reporter, is_offset_aligned(vertex.fOffset, requiredAlignment));
49 REPORTER_ASSERT(reporter, is_offset_aligned(index.fOffset, requiredAlignment));
50 REPORTER_ASSERT(reporter, is_offset_aligned(indirect.fOffset, requiredAlignment));
51
52 // Transfers the ownership of used buffers to a Recording.
53 auto recording = recorder->snap();
54
55 // Ensure that the buffers are still unmapped.
56 REPORTER_ASSERT(reporter, !ssbo.fBuffer->isMapped());
57 REPORTER_ASSERT(reporter, !vertex.fBuffer->isMapped());
58 REPORTER_ASSERT(reporter, !index.fBuffer->isMapped());
59 REPORTER_ASSERT(reporter, !indirect.fBuffer->isMapped());
60
61 // Since these buffers never need their contents to be host-visible, no buffer transfer/copy
62 // tasks should have been created for them.
63 REPORTER_ASSERT(reporter, !recording->priv().hasTasks());
64
65 // Request a mapped ssbo followed by an unmapped one. The two buffers should be distinct.
66 auto [ssboPtr, mappedSsbo] = mgr->getMappedStorage(10);
67 ssbo = mgr->getStorage(10);
68 REPORTER_ASSERT(reporter, !ssbo.fBuffer->isMapped());
69 REPORTER_ASSERT(reporter, ssbo.fBuffer != mappedSsbo.fBuffer);
70 }
71
72 } // namespace skgpu::graphite
73