1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/command_stream/scratch_space_controller_base.h"
9 #include "shared/test/common/fixtures/device_fixture.h"
10 #include "shared/test/common/mocks/mock_command_stream_receiver.h"
11 #include "shared/test/common/test_macros/test.h"
12 
13 using namespace NEO;
14 
15 class MockScratchSpaceControllerBase : public ScratchSpaceControllerBase {
16   public:
MockScratchSpaceControllerBase(uint32_t rootDeviceIndex,ExecutionEnvironment & environment,InternalAllocationStorage & allocationStorage)17     MockScratchSpaceControllerBase(uint32_t rootDeviceIndex,
18                                    ExecutionEnvironment &environment,
19                                    InternalAllocationStorage &allocationStorage) : ScratchSpaceControllerBase(rootDeviceIndex, environment, allocationStorage) {}
20 
programHeaps(HeapContainer & heapContainer,uint32_t offset,uint32_t requiredPerThreadScratchSize,uint32_t requiredPerThreadPrivateScratchSize,uint32_t currentTaskCount,OsContext & osContext,bool & stateBaseAddressDirty,bool & vfeStateDirty)21     void programHeaps(HeapContainer &heapContainer,
22                       uint32_t offset,
23                       uint32_t requiredPerThreadScratchSize,
24                       uint32_t requiredPerThreadPrivateScratchSize,
25                       uint32_t currentTaskCount,
26                       OsContext &osContext,
27                       bool &stateBaseAddressDirty,
28                       bool &vfeStateDirty) override {
29         ScratchSpaceControllerBase::programHeaps(heapContainer, offset, requiredPerThreadScratchSize, requiredPerThreadPrivateScratchSize, currentTaskCount, osContext, stateBaseAddressDirty, vfeStateDirty);
30         programHeapsCalled = true;
31     }
programBindlessSurfaceStateForScratch(BindlessHeapsHelper * heapsHelper,uint32_t requiredPerThreadScratchSize,uint32_t requiredPerThreadPrivateScratchSize,uint32_t currentTaskCount,OsContext & osContext,bool & stateBaseAddressDirty,bool & vfeStateDirty,NEO::CommandStreamReceiver * csr)32     void programBindlessSurfaceStateForScratch(BindlessHeapsHelper *heapsHelper,
33                                                uint32_t requiredPerThreadScratchSize,
34                                                uint32_t requiredPerThreadPrivateScratchSize,
35                                                uint32_t currentTaskCount,
36                                                OsContext &osContext,
37                                                bool &stateBaseAddressDirty,
38                                                bool &vfeStateDirty,
39                                                NEO::CommandStreamReceiver *csr) override {
40         ScratchSpaceControllerBase::programBindlessSurfaceStateForScratch(heapsHelper, requiredPerThreadScratchSize, requiredPerThreadPrivateScratchSize, currentTaskCount, osContext, stateBaseAddressDirty, vfeStateDirty, csr);
41         programBindlessSurfaceStateForScratchCalled = true;
42     }
43     ResidencyContainer residencyContainer;
44     bool programHeapsCalled = false;
45     bool programBindlessSurfaceStateForScratchCalled = false;
46 };
47 
48 using ScratchComtrolerTests = Test<DeviceFixture>;
49 
HWTEST_F(ScratchComtrolerTests,givenCommandQueueWhenProgramHeapsCalledThenThenProgramHeapsCalled)50 HWTEST_F(ScratchComtrolerTests, givenCommandQueueWhenProgramHeapsCalledThenThenProgramHeapsCalled) {
51     MockCsrHw2<FamilyType> csr(*pDevice->getExecutionEnvironment(), 0, pDevice->getDeviceBitfield());
52     csr.initializeTagAllocation();
53     csr.setupContext(*pDevice->getDefaultEngine().osContext);
54 
55     ExecutionEnvironment *execEnv = static_cast<ExecutionEnvironment *>(pDevice->getExecutionEnvironment());
56     std::unique_ptr<ScratchSpaceController> scratchController = std::make_unique<MockScratchSpaceControllerBase>(pDevice->getRootDeviceIndex(),
57                                                                                                                  *execEnv,
58                                                                                                                  *csr.getInternalAllocationStorage());
59 
60     bool gsbaStateDirty = false;
61     bool frontEndStateDirty = false;
62     HeapContainer heapContainer;
63     scratchController->programHeaps(heapContainer, 0, 0, 0, 0, *pDevice->getDefaultEngine().osContext, gsbaStateDirty, frontEndStateDirty);
64 
65     EXPECT_TRUE(static_cast<MockScratchSpaceControllerBase *>(scratchController.get())->programHeapsCalled);
66 }
67 
HWTEST_F(ScratchComtrolerTests,givenCommandQueueWhenProgramHeapBindlessCalledThenThenProgramBindlessSurfaceStateForScratchCalled)68 HWTEST_F(ScratchComtrolerTests, givenCommandQueueWhenProgramHeapBindlessCalledThenThenProgramBindlessSurfaceStateForScratchCalled) {
69     MockCsrHw2<FamilyType> csr(*pDevice->getExecutionEnvironment(), 0, pDevice->getDeviceBitfield());
70     csr.initializeTagAllocation();
71     csr.setupContext(*pDevice->getDefaultEngine().osContext);
72 
73     ExecutionEnvironment *execEnv = static_cast<ExecutionEnvironment *>(pDevice->getExecutionEnvironment());
74     std::unique_ptr<MockScratchSpaceControllerBase> scratchController = std::make_unique<MockScratchSpaceControllerBase>(pDevice->getRootDeviceIndex(),
75                                                                                                                          *execEnv,
76                                                                                                                          *csr.getInternalAllocationStorage());
77 
78     bool gsbaStateDirty = false;
79     bool frontEndStateDirty = false;
80     HeapContainer heapContainer;
81     scratchController->programBindlessSurfaceStateForScratch(nullptr, 0, 0, 0, *pDevice->getDefaultEngine().osContext, gsbaStateDirty, frontEndStateDirty, &csr);
82 
83     EXPECT_TRUE(static_cast<MockScratchSpaceControllerBase *>(scratchController.get())->programBindlessSurfaceStateForScratchCalled);
84 }