From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Shaders/VBO.h | 308 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 Runtime/Shaders/VBO.h (limited to 'Runtime/Shaders/VBO.h') diff --git a/Runtime/Shaders/VBO.h b/Runtime/Shaders/VBO.h new file mode 100644 index 0000000..d8931ab --- /dev/null +++ b/Runtime/Shaders/VBO.h @@ -0,0 +1,308 @@ +#ifndef VBO_H +#define VBO_H + +#include "Runtime/GfxDevice/GfxDeviceTypes.h" +#include "Configuration/UnityConfigure.h" +#include "Runtime/Serialize/SerializeUtility.h" +#include "Runtime/Filters/Mesh/VertexData.h" +#include "Runtime/Utilities/LinkedList.h" +#include "Runtime/Modules/ExportModules.h" + +class ChannelAssigns; +class Matrix4x4f; + + +struct MeshPartitionInfo +{ + UInt32 submeshStart; + UInt32 partitionCount; + MeshPartitionInfo() {submeshStart=partitionCount=0;} + + DECLARE_SERIALIZE_NO_PPTR (MeshPartitionInfo); +}; + +struct MeshPartition +{ + int vertexCount; + int vertexOffset; + int indexCount; + int indexByteOffset; + MeshPartition() { vertexCount = vertexOffset = indexCount = indexByteOffset = 0; } + + DECLARE_SERIALIZE_NO_PPTR (MeshPartition); +}; + +struct VertexBufferData +{ + ChannelInfoArray channels; + StreamInfoArray streams; + UInt8* buffer; + int bufferSize; + int vertexCount; + + VertexBufferData() + { + // Channels and streams have default constructors + buffer = 0; + bufferSize = 0; + vertexCount = 0; +#if UNITY_PS3 + numBones = 0; + numInfluences = 0; + inflPerVertex = 0; + bones = NULL; + influences = NULL; +#endif + } + +#if UNITY_PS3 + UInt32 numBones; + UInt32 numInfluences; + UInt32 inflPerVertex; + Matrix4x4f* bones; + void* influences; + + UNITY_VECTOR(kMemVertexData, MeshPartitionInfo) partInfo; + UNITY_VECTOR(kMemVertexData, MeshPartition) partitions; +#endif +}; + +struct IndexBufferData +{ + void* indices; + int count; + UInt32 hasTopologies; // bitmask +}; + +struct VertexStreamData +{ + UInt8* buffer; + UInt32 channelMask; + int stride; + int vertexCount; +}; + +size_t GetChannelFormatSize (UInt8 format); + +class EXPORT_COREMODULE VBO : public ListElement +{ +public: + virtual ~VBO() { } + + virtual void UpdateVertexData( const VertexBufferData& buffer ) = 0; + virtual void UpdateIndexData (const IndexBufferData& buffer) = 0; + virtual void DrawVBO (const ChannelAssigns& channels, UInt32 firstIndexByte, UInt32 indexCount, + GfxPrimitiveType topology, UInt32 firstVertex, UInt32 vertexCount ) = 0; + #if GFX_ENABLE_DRAW_CALL_BATCHING + virtual void DrawCustomIndexed( const ChannelAssigns& channels, void* indices, UInt32 indexCount, + GfxPrimitiveType topology, UInt32 vertexRangeBegin, UInt32 vertexRangeEnd, UInt32 drawVertexCount ) = 0; + #endif + + // recreate hardware buffers + virtual void Recreate() { } + + enum { kMaxQuads = 65536/4 - 4 }; // so we fit into 16 bit indices, minus some more just in case + + // For writing directly to VBO. VBO must be filled (UpdateData) + // at least once; and vertex layout + topology from the last fill + // is used. For example, for skinned meshes you have to call + // UpdateData on start and each time layout/topology changes; + // then map,write,unmap for each skinning. + // + // In some situations a vertex buffer might become lost; then you need to do UpdateData + // again before using Map. + virtual bool MapVertexStream( VertexStreamData& outData, unsigned stream ) = 0; + virtual void UnmapVertexStream( unsigned stream ) = 0; + virtual bool IsVertexStreamMapped( unsigned stream ) const { return m_IsStreamMapped[stream]; } + + virtual bool IsVertexBufferLost() const = 0; + virtual bool IsIndexBufferLost() const { return false; } + + virtual bool IsUsingSourceVertices() const { return false; } + virtual bool IsUsingSourceIndices() const { return false; } + + // WARNING: no checks will be done. So use wuth caution. + // Actually you should only use it coupled with Mesh unloading its VertexData + virtual void UnloadSourceVertices() {} + + // Tell vertices will be mapped from render thread + virtual void SetMappedFromRenderThread( bool ) {} + + // Whats the access pattern for modifying vertices? + enum StreamMode + { + kStreamModeNoAccess, + kStreamModeWritePersist, + kStreamModeDynamic, + kStreamModeCount + }; + + virtual void SetVertexStreamMode( unsigned stream, StreamMode mode ) { m_StreamModes[stream] = mode; } + StreamMode GetVertexStreamMode( unsigned stream ) const { return StreamMode(m_StreamModes[stream]); } + int GetStreamStride( unsigned stream = 0) const { return m_Streams[stream].stride; } + + // TODO: probably unify with vertex streams mode, or extract ibo data altogether in different class + virtual void SetIndicesDynamic(bool dynamic) { m_IndicesDynamic = dynamic; } + bool AreIndicesDynamic() const { return m_IndicesDynamic; } + + static int GetDefaultChannelByteSize (int channelNum); + static int GetDefaultChannelFormat(int channelNum); + static int GetDefaultChannelDimension(int channelNum); + + virtual int GetRuntimeMemorySize() const = 0; + + bool GetHideFromRuntimeStats() const { return m_HideFromRuntimeStats; } + void SetHideFromRuntimeStats( bool flag ) { m_HideFromRuntimeStats = flag; } + +#if GFX_SUPPORTS_D3D9 + virtual void ResetDynamicVB() {} +#endif + + // TODO: that is actually how it would/should work in the feature + // or at least what i understood speaking with kaspar + // for now lets limit to gles2 where it is really needed +#if GFX_SUPPORTS_OPENGLES20 + virtual void MarkBuffersLost() {}; +#endif + + virtual void UseAsStreamOutput() { } + +#if UNITY_XENON + virtual void AddExtraUvChannels( const UInt8* data, UInt32 size, int extraUvCount ) = 0; + virtual void CopyExtraUvChannels( VBO* source ) = 0; +#endif + +protected: + VBO() + { + for (int i=0; i