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
|
/*
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/command_container/cmdcontainer.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/non_copyable_or_moveable.h"
#include "shared/source/helpers/ptr_math.h"
#include <cstdint>
namespace NEO {
class GraphicsAllocation;
class LinearStream : NEO::NonCopyableAndNonMovableClass {
public:
virtual ~LinearStream() = default;
LinearStream() = default;
LinearStream(void *buffer, size_t bufferSize);
LinearStream(GraphicsAllocation *gfxAllocation);
LinearStream(GraphicsAllocation *gfxAllocation, void *buffer, size_t bufferSize);
LinearStream(void *buffer, size_t bufferSize, CommandContainer *cmdContainer, size_t batchBufferEndSize);
void *getCpuBase() const;
void *getSpace(size_t size);
size_t getMaxAvailableSpace() const;
size_t getAvailableSpace() const;
size_t getUsed() const;
void *getCmdContainer() const { return cmdContainer; }
uint64_t getGpuBase() const;
void setGpuBase(uint64_t gpuAddress);
uint64_t getCurrentGpuAddressPosition() const;
void overrideMaxSize(size_t newMaxSize);
void replaceBuffer(void *buffer, size_t bufferSize);
GraphicsAllocation *getGraphicsAllocation() const;
void replaceGraphicsAllocation(GraphicsAllocation *gfxAllocation);
template <typename Cmd>
Cmd *getSpaceForCmd() {
auto ptr = getSpace(sizeof(Cmd));
return reinterpret_cast<Cmd *>(ptr);
}
void ensureContinuousSpace(size_t size);
protected:
size_t sizeUsed = 0;
size_t maxAvailableSpace{0};
void *buffer{nullptr};
GraphicsAllocation *graphicsAllocation{nullptr};
CommandContainer *cmdContainer{nullptr};
size_t batchBufferEndSize{0};
uint64_t gpuBase{0};
};
inline void *LinearStream::getCpuBase() const {
return buffer;
}
inline void LinearStream::setGpuBase(uint64_t gpuAddress) {
gpuBase = gpuAddress;
}
inline void LinearStream::ensureContinuousSpace(size_t size) {
if (cmdContainer && (getAvailableSpace() < (batchBufferEndSize + size))) {
UNRECOVERABLE_IF(sizeUsed + batchBufferEndSize > maxAvailableSpace);
cmdContainer->closeAndAllocateNextCommandBuffer();
}
}
inline void *LinearStream::getSpace(size_t size) {
if (size == 0u) {
return ptrOffset(buffer, sizeUsed);
}
ensureContinuousSpace(size);
UNRECOVERABLE_IF(sizeUsed + size > maxAvailableSpace);
UNRECOVERABLE_IF(reinterpret_cast<int64_t>(buffer) == 0);
auto memory = ptrOffset(buffer, sizeUsed);
sizeUsed += size;
return memory;
}
inline size_t LinearStream::getMaxAvailableSpace() const {
return maxAvailableSpace;
}
inline size_t LinearStream::getAvailableSpace() const {
DEBUG_BREAK_IF(sizeUsed > maxAvailableSpace);
return maxAvailableSpace - sizeUsed;
}
inline size_t LinearStream::getUsed() const {
return sizeUsed;
}
inline void LinearStream::overrideMaxSize(size_t newMaxSize) {
maxAvailableSpace = newMaxSize;
}
inline void LinearStream::replaceBuffer(void *buffer, size_t bufferSize) {
this->buffer = buffer;
maxAvailableSpace = bufferSize;
sizeUsed = 0;
}
inline GraphicsAllocation *LinearStream::getGraphicsAllocation() const {
return graphicsAllocation;
}
inline void LinearStream::replaceGraphicsAllocation(GraphicsAllocation *gfxAllocation) {
graphicsAllocation = gfxAllocation;
}
inline uint64_t LinearStream::getCurrentGpuAddressPosition() const {
return (getGpuBase() + getUsed());
}
static_assert(NEO::NonCopyableAndNonMovable<LinearStream>);
} // namespace NEO
|