From 61aa08ff61cb828014ddf7a03044f33524ea4b43 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 7 Aug 2024 10:29:31 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20iniciais?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/CMakeLists.txt | 2 +- framework/pipeline/writer.cpp | 133 ++++++++++++++++++++++++ inc/xna/csharp/stream.hpp | 6 ++ inc/xna/default.hpp | 25 ++++- inc/xna/pipeline/compiler.hpp | 12 +++ inc/xna/pipeline/pipeline-enums.hpp | 11 ++ inc/xna/pipeline/writer.hpp | 150 ++++++++++++++++++++++++++++ inc/xna/xna.hpp | 2 + samples/CMakeLists.txt | 2 +- 9 files changed, 337 insertions(+), 6 deletions(-) create mode 100644 framework/pipeline/writer.cpp create mode 100644 inc/xna/pipeline/compiler.hpp create mode 100644 inc/xna/pipeline/pipeline-enums.hpp create mode 100644 inc/xna/pipeline/writer.hpp diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 0c82e8a..a2f677a 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -40,7 +40,7 @@ add_library (Xn65 STATIC "platform-dx/audioengine.cpp" "graphics/gresource.cpp" "platform-dx/effect.cpp" - "exception.cpp" "platform-dx/screen.cpp" ) + "exception.cpp" "platform-dx/screen.cpp" "pipeline/writer.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET Xn65 PROPERTY CXX_STANDARD 20) diff --git a/framework/pipeline/writer.cpp b/framework/pipeline/writer.cpp new file mode 100644 index 0000000..0664347 --- /dev/null +++ b/framework/pipeline/writer.cpp @@ -0,0 +1,133 @@ +#include "xna/pipeline/writer.hpp" + +namespace xna { + ContentWriter::ContentWriter( + P_ContentCompiler& compiler, + P_Stream const& output, + TargetPlatform targetPlatform, + GraphicsProfile targetProfile, + bool compressContent, + String const& rootDirectory, + String const& referenceRelocationPath + ) : compiler(compiler), + targetPlatform(targetPlatform), + targetProfile(targetProfile), + compressContent(compressContent), + rootDirectory(rootDirectory), + referenceRelocationPath(referenceRelocationPath), + finalOutput(output) + { + headerData = snew(); + contentData = snew(); + OutStream = reinterpret_pointer_cast(contentData); + } + + void ContentWriter::WriteSharedResources() { + while (sharedResources.size() > 0) { + sharedResources.pop(); + auto& res = sharedResources.front(); + WriteObject(res); + } + } + + void ContentWriter::WriteHeader() { + OutStream = reinterpret_pointer_cast(headerData); + Write7BitEncodedInt(static_cast(typeWriters.size())); + + for (size_t i = 0; i < typeWriters.size(); ++i) { + auto& typeWriter = typeWriters[i]; + + Write(typeWriter->GetRuntimeReader(targetPlatform)); + Write(typeWriter->TypeVersion()); + } + + Write7BitEncodedInt(static_cast(sharedResourceNames.size())); + } + + void ContentWriter::WriteFinalOutput() { + OutStream = finalOutput; + + Write((Byte)88); + Write((Byte)78); + Write((Byte)66); + + if (targetPlatform == TargetPlatform::Windows) + Write((Byte)119); + else + Exception::Throw(Exception::NOT_IMPLEMENTED); + + if (compressContent) + WriteCompressedOutput(); + else + WriteUncompressedOutput(); + } + + void ContentWriter::WriteUncompressedOutput() { + WriteVersionNumber((Ushort)5); + + const auto length1 = static_cast(headerData->Length()); + const auto length2 = static_cast(contentData->Length()); + + Write(10 + length1 + length2); + + OutStream->Write(headerData->_buffer, 0, length1); + OutStream->Write(contentData->_buffer, 0, length2); + } + + void ContentWriter::WriteCompressedOutput() { + Exception::Throw(Exception::NOT_IMPLEMENTED); + } + + void ContentWriter::WriteVersionNumber(Ushort version) { + version |= static_cast(static_cast(targetProfile) << 8 & 32512); + Write(version); + } + + void ContentWriter::Write(Vector2 const& value) { + Write(value.X); + Write(value.Y); + } + + void ContentWriter::Write(Vector3 const& value) { + Write(value.X); + Write(value.Y); + Write(value.Z); + } + + void ContentWriter::Write(Vector4 const& value) { + Write(value.X); + Write(value.Y); + Write(value.Z); + Write(value.W); + } + + void ContentWriter::Write(Matrix const& value) { + Write(value.M11); + Write(value.M12); + Write(value.M13); + Write(value.M14); + Write(value.M21); + Write(value.M22); + Write(value.M23); + Write(value.M24); + Write(value.M31); + Write(value.M32); + Write(value.M33); + Write(value.M34); + Write(value.M41); + Write(value.M42); + Write(value.M43); + Write(value.M44); + } + + void ContentWriter::Write(Quaternion const& value) { + Write(value.X); + Write(value.Y); + Write(value.Z); + Write(value.W); + } + + void ContentWriter::Write(Color const& value) { + Write(value.PackedValue()); + } +} \ No newline at end of file diff --git a/inc/xna/csharp/stream.hpp b/inc/xna/csharp/stream.hpp index 58c0304..957a4f6 100644 --- a/inc/xna/csharp/stream.hpp +++ b/inc/xna/csharp/stream.hpp @@ -44,6 +44,8 @@ namespace xna { //A simplified port of the System.IO.MemoryStream. class MemoryStream : public Stream { public: + constexpr MemoryStream(){} + constexpr MemoryStream(std::vector const& bytes): _buffer(bytes), _length(static_cast(bytes.size())){} @@ -83,6 +85,10 @@ namespace xna { virtual void Write(std::vector const& buffer, Int offset, Int count) override; virtual void WriteByte(Byte value) override; + virtual std::vector GetBuffer() const { + return _buffer; + } + public: std::vector _buffer; diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index a1ed6e5..37b2f00 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "enumerations.hpp" #include "exception.hpp" @@ -195,6 +196,17 @@ namespace xna { struct KeyboardState; struct MouseState; + //Pipeline + class ContentWriter; + class ContentCompiler; + class ContentTypeWriter; + + + // + // Forward Pointers + // + + //Graphics using P_BlendState = sptr; using P_DepthStencilState = sptr; using P_GraphicsAdapter = sptr; @@ -202,12 +214,17 @@ namespace xna { using P_RasterizerState = sptr; using P_PresentationParameters = sptr; using P_SamplerStateCollection = sptr; - using P_Stream = sptr; - using P_MemoryStream = sptr; - using P_FileStream = sptr; + using P_RenderTarget2D = sptr; using P_Texture = sptr; using P_Texture2D = sptr; - using P_RenderTarget2D = sptr; + //CSharp + using P_Stream = sptr; + using P_MemoryStream = sptr; + using P_FileStream = sptr; + //Pipeline + using P_ContentWriter = sptr; + using P_ContentCompiler = sptr; + using P_ContentTypeWriter = sptr; } diff --git a/inc/xna/pipeline/compiler.hpp b/inc/xna/pipeline/compiler.hpp new file mode 100644 index 0000000..bc390ca --- /dev/null +++ b/inc/xna/pipeline/compiler.hpp @@ -0,0 +1,12 @@ +#ifndef XNA_PIPELINE_COMPILER_HPP +#define XNA_PIPELINE_COMPILER_HPP + +#include "../default.hpp" + +namespace xna { + class ContentCompiler { + + }; +} + +#endif \ No newline at end of file diff --git a/inc/xna/pipeline/pipeline-enums.hpp b/inc/xna/pipeline/pipeline-enums.hpp new file mode 100644 index 0000000..205ff0d --- /dev/null +++ b/inc/xna/pipeline/pipeline-enums.hpp @@ -0,0 +1,11 @@ +#ifndef XNA_PIPELINE_ENUMS_HPP +#define XNA_PIPELINE_ENUMS_HPP + +namespace xna { + enum class TargetPlatform + { + Windows, + }; +} + +#endif \ No newline at end of file diff --git a/inc/xna/pipeline/writer.hpp b/inc/xna/pipeline/writer.hpp new file mode 100644 index 0000000..2b47151 --- /dev/null +++ b/inc/xna/pipeline/writer.hpp @@ -0,0 +1,150 @@ +#ifndef XNA_PIPELINE_WRITER_HPP +#define XNA_PIPELINE_WRITER_HPP + +#include "../csharp/binary.hpp" +#include "../default.hpp" +#include "pipeline-enums.hpp" +#include "../common/numerics.hpp" +#include "../common/color.hpp" + +namespace xna { + class ContentTypeWriter { + public: + virtual String GetRuntimeReader(TargetPlatform targetPlatform) = 0; + virtual Int TypeVersion() const { return 0; } + }; + + template + class ExternalReference { + + }; + + //Provides an implementation for many of the ContentCompiler methods including compilation, state tracking for shared resources and creation of the header type manifest. + class ContentWriter : public BinaryWriter { + public: + ContentWriter( + P_ContentCompiler& compiler, + P_Stream const& output, + TargetPlatform targetPlatform, + GraphicsProfile targetProfile, + bool compressContent, + String const& rootDirectory, + String const& referenceRelocationPath + ); + + //Gets the content build target platform. + constexpr TargetPlatform Target() const { return targetPlatform; } + //Gets or sets the target graphics profile. + constexpr GraphicsProfile TargetProfile() const { return targetProfile; } + + //Writes a single object preceded by a type identifier to the output binary. + template void WriteObject(T& value); + //Writes a single object to the output binary, using the specified type hint and writer worker. + template void WriteObject(T& value, ContentTypeWriter& writer); + //Writes a single object to the output binary as an instance of the specified type. + template void WriteRawObject(T& value); + //Writes a single object to the output binary using the specified writer worker. + template void WriteRawObject(T& value, ContentTypeWriter& typeWriter); + //Adds a shared reference to the output binary and records the object to be serialized later. + template void WriteSharedResource(T& value); + //Writes the name of an external file to the output binary. + template void WriteExternalReference(ExternalReference& reference); + + using BinaryWriter::Write; + + //Writes a Vector2 value. + void Write(Vector2 const& value); + //Writes a Vector3 value. + void Write(Vector3 const& value); + //Writes a Vector4 value. + void Write(Vector4 const& value); + //Writes a Matrix value. + void Write(Matrix const& value); + //Writes a Quaternion value. + void Write(Quaternion const& value); + //Writes a Color value. + void Write(Color const& value); + + inline void FlushOutput() { + WriteSharedResources(); + WriteHeader(); + WriteFinalOutput(); + } + + private: + template void InvokeWriter(T& value, ContentTypeWriter& writer); + sptr GetTypeWriter(Type const& type, int& typeIndex) { return nullptr; } + void WriteSharedResources(); + void WriteHeader(); + void WriteFinalOutput(); + void WriteUncompressedOutput(); + void WriteCompressedOutput(); + void WriteVersionNumber(Ushort version); + + private: + P_ContentCompiler compiler{ nullptr }; + TargetPlatform targetPlatform{ TargetPlatform::Windows }; + GraphicsProfile targetProfile{ GraphicsProfile::HiDef }; + bool compressContent{ false }; + String rootDirectory; + String referenceRelocationPath; + P_Stream finalOutput{ nullptr }; + P_MemoryStream headerData{ nullptr }; + P_MemoryStream contentData{ nullptr }; + std::vector typeWriters; + std::map sharedResourceNames; + std::queue sharedResources; + + private: + static constexpr Ushort XnbVersion = 5; + static constexpr Ushort XnbCompressedVersion = 32773; + static constexpr Ushort XnbVersionProfileMask = 32512; + static constexpr Int XnbVersionProfileShift = 8; + static constexpr Int XnbVersionOffset = 4; + static constexpr Int XnbFileSizeOffset = 6; + static constexpr Int XnbPrologueSize = 10; + }; + + // + // Generics implementations + // + + template + void ContentWriter::WriteObject(T& value) { + + } + + template + void ContentWriter::WriteObject(T& value, ContentTypeWriter& writer) { + + } + + template + void ContentWriter::WriteRawObject(T& value) { + + } + + template + void ContentWriter::WriteRawObject(T& value, ContentTypeWriter& typeWriter) { + + } + + + template + void ContentWriter::WriteSharedResource(T& value) { + + } + + + template + void ContentWriter::WriteExternalReference(ExternalReference& reference) { + + } + + template + void ContentWriter::InvokeWriter(T& value, ContentTypeWriter& writer) { + + } +} + +#endif \ No newline at end of file diff --git a/inc/xna/xna.hpp b/inc/xna/xna.hpp index fd43a6f..502df4b 100644 --- a/inc/xna/xna.hpp +++ b/inc/xna/xna.hpp @@ -53,6 +53,8 @@ #include "input/gamepad.hpp" #include "input/keyboard.hpp" #include "input/mouse.hpp" +#include "pipeline/writer.hpp" +#include "pipeline/compiler.hpp" namespace xna { //Exposes functions that must be implemented by the platform diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 45257e9..24ef843 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -add_subdirectory ("02_PlatfformerStarterKit") +#add_subdirectory ("02_PlatfformerStarterKit")