2#include "RenderEffect.h"
4#include "Rendering/IGraphicsDevice.h"
5#include "Rendering/IContext.h"
6#include "Rendering/IBuffers.h"
7#include "Rendering/ICapabilities.h"
8#include "Rendering/VertexFormat.h"
10#include "Resources/VertexFormats.h"
11#include "Resources/Mesh.h"
12#include "Resources/Buffer.h"
14#include "Foundation/Logging/Logger.h"
34 if (stream.
buffer->isResident()) {
36 assert(handle &&
"Resident buffer without handle");
38 buffer->
unsetFlag(ResourceFlags::Resident);
42 if (doSet && !buffer->
empty()) {
48 buffers->
annotate(handle,
"RenderMesh");
50 buffer->setResident();
61Cogs::Core::RenderMesh::RenderMesh()
63 type = RenderResourceType::RenderMesh;
77 if (!renderEffect || !
HandleIsValid(renderEffect->effectHandle)) {
78 LOG_ERROR(logger,
"Trying to get a VAO for an invalid effect");
82 RenderResource::Id effectId = renderEffect->getIdentity();
83 if (
auto it = vaos.find(effectId); it != vaos.end()) {
87 std::array<const VertexFormat*, MeshStreamsLayout::maxStreams> formats;
88 for (
size_t i = 0; i < streamsLayout.numStreams; i++) {
89 formats[i] = Cogs::VertexFormats::getVertexFormat(streamsLayout.vertexFormats[i]);
93 vertexBuffers, streamsLayout.numStreams, formats.data(), vertexStrides, vertexOffsets,
94 indexBuffer, indexStride);
95 vaos.insert(std::make_pair(effectId, vao));
102 bool changed =
false;
107 streamsLayout.numStreams = 0;
110 for (
size_t i = 0; i < VertexDataType::LastVertexType; ++i) {
116 if (mesh->streamsUpdated & uint16_t(1 << i)) {
119 else if (stream.
buffer->isResident()) {
124 vertexBuffers[streamsLayout.numStreams] = handle;
125 vertexOffsets[streamsLayout.numStreams] = stream.
offset;
126 vertexStrides[streamsLayout.numStreams] = stream.
stride;
127 streamsLayout.vertexFormats[streamsLayout.numStreams] = stream.
format;
128 streamsLayout.numStreams++;
133 mesh->streamsUpdated = 0;
139 if (mesh->
hasStream(VertexDataType::Indexes)) {
141 if (!supportUint32Indices && indexStride == 4) {
142 LOG_ERROR_ONCE(logger,
"Mesh indices are uint32 which is not supported by graphics device");
147 indexOffset = stream.
offset;
148 indexStride = stream.
stride;
159 streamsLayout.updateHash();
161 for (
auto& it : vaos) {
174void Cogs::Core::RenderMesh::release(
Renderer * renderer)
178 for (
auto& it : vaos) {
Contains render resources used by the renderer.
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual ICapabilities * getCapabilities()=0
Get a pointer to the capability management interface used to query the graphics device capability fla...
virtual IBuffers * getBuffers()=0
Get a pointer to the buffer management interface.
Log implementation class.
Provides a weakly referenced view over the contents of a string.
constexpr bool empty() const noexcept
Check if the string is empty.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
ActivationResult
Defines results for resource activation.
@ Success
Resource activated successfully.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
@ None
The buffer can not be either read from or written to by the CPU after creation.
EBindFlags
Bind flags enumeration.
@ VertexBuffer
The buffer can be bound as input to the vertex shader stage as a vertex buffer.
@ IndexBuffer
The buffer can be bound as input to the vertex shader stage as an index buffer.
bool empty() const
If the buffer is empty. Note that an empty buffer may still have reserved storage.
size_t size() const
Size of the buffer in bytes.
void * data()
Get a pointer to the buffer data.
Contains a stream of data used by Mesh resources.
uint32_t offset
Byte offset from the start of the buffer.
VertexFormatHandle format
A pointer to the format describing the contents of the byte buffer.
uint32_t stride
Element stride.
Cogs::Core::ResourceBufferHandle buffer
Data buffer.
@ StreamsChanged
One or more of the data streams in the mesh changed.
@ IndexesChanged
The index data of the mesh changed.
VertexFormatHandle vertexFormats[maxStreams]
static constexpr size_t maxStreams
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
bool isIndexed() const
If the mesh uses indexed geometry.
constexpr bool isMeshFlagSet(MeshFlags::EMeshFlags flag) const
Check if the given mesh flag(s) is set.
bool hasStream(VertexDataType::EVertexDataType type) const
Check if the Mesh has a DataStream for the given type.
void unsetMeshFlag(MeshFlags::EMeshFlags flag)
Unset the given mesh flag.
DataStream & getStream(const VertexDataType::EVertexDataType dataType)
Get the stream corresponding to the given dataType.
uint32_t getCount() const
Get the vertex count of the mesh.
StringView getName() const
Get the name of the resource.
void unsetFlag(ResourceFlags flag)
Unset the given flag.
void attachResource(RenderResource *attachment)
Attach the given GPU resource to the resource.
RenderResource * getAttachedResource() const
Get the attached resource.
EVertexDataType
Contains data types.
static const Handle_t NoHandle
Represents a handle to nothing.
handle_type handle
Internal resource handle.
Provides buffer management functionality.
virtual VertexArrayObjectHandle loadVertexArrayObject(const EffectHandle effectHandle, const VertexBufferHandle *vertexBufferHandles, const size_t vertexBufferCount, const VertexFormat *const *vertexFormats=nullptr, const uint32_t *vertexBufferStrides=nullptr, const uint32_t *vertexBufferOffsets=nullptr, const IndexBufferHandle indexBufferHandle=IndexBufferHandle::NoHandle, uint32_t indexBufferStride=0)=0
Create a vertex array object that encapsulates binding of a set of vertex buffers and an optional ind...
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
virtual BufferHandle loadBuffer(const void *data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride=0)=0
Loads a new buffer using the given data to populate the buffer.
virtual void releaseBuffer(BufferHandle bufferHandle)=0
Releases the buffer with the given bufferHandle.
virtual void releaseVertexArrayObject(VertexArrayObjectHandle vertexArrayObjectHandle)=0
Releases the vertex array object with the given handle.
virtual const GraphicsDeviceCapabilities & getDeviceCapabilities() const
Gets the device capabilities in a structure.
@ Static
Buffer will be loaded once and used to render many subsequent frames without any updates.