Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Main.h"
- #include "SkyeCuillin/ImguiSubsystem.h"
- #include "SkyeCuillin/RenderPassGUI.h"
- #include "Game/Loader/DevIL.h"
- #include "Game/Loader/Sctex.h"
- #include "Graphics/Common/Texture/TextureCompressor.h"
- #include "Core/Math/Numeric/Mat3.h"
- class Main : public Engine::System
- {
- private:
- SkyeCuillin::ImguiSubsystem* mImgui;
- SkyeCuillin::RenderPassGUI* mTexturesPass;
- Engine::Log* mLog;
- Engine::Window* mWindow;
- Engine::Constants* mOptions;
- Engine::D3DRenderer* mRenderer;
- Engine::D3DSwapChain* mSwapChain;
- Engine::Texture* mUncompressed;
- Engine::Texture* mCompressed;
- float mUncompressedMiplevel = 0.0f;
- float mNumUncompressedMiplevels = 1.0f;
- float mCompressedMiplevel = 0.0f;
- float mNumCompressedMiplevels = 1.0f;
- float mCurrentMiplevel = 0.0f;
- Engine::mat3 mTransform;
- private:
- Engine::LoaderDevIL::Image* mImage = nullptr;
- size_t* mCompressedWidth;
- size_t* mCompressedHeight;
- size_t mCompressedLevels;
- unsigned char** mCompressedData;
- Engine::Graphics::Format mCompressedFormat;
- void LoadTexture(const std::string& filename)
- {
- if (filename.find(".sctex") != std::string::npos)
- {
- Engine::LoaderSctex* loader = new Engine::LoaderSctex(mLog);
- if (mCompressed != nullptr)
- {
- delete mCompressed;
- }
- mCompressed = loader->Load(filename, mRenderer, "Compressed");
- if (mImage != nullptr)
- {
- delete mImage;
- mImage = nullptr;
- }
- // Now, load the same image - but only into helper data, these must be de-compressed
- // and setup as original image (and therefore also uncompressed image.
- Engine::LoaderSctex::HelperImageData* imageData = loader->Load(filename);
- mImage = new Engine::LoaderDevIL::Image();
- mImage->mUseAlphaMipmap = true;
- mImage->mChannels = 4;
- mImage->mBpp = 4;
- mImage->mWidth = imageData->mImageRecord[0].mWidth;
- mImage->mHeight = imageData->mImageRecord[0].mHeight;
- mImage->mData = new unsigned char[imageData->mImageRecord[0].mWidth * imageData->mImageRecord[0].mHeight * 4];
- mImage->mMipLevels = imageData->mHeader.mNumImages;
- mImage->mMipmaps = new unsigned char*[mImage->mMipLevels];
- mImage->mMipmaps[0] = mImage->mData;
- Engine::TextureCompressor::Decompress(imageData->mImageRecord[0].mWidth, imageData->mImageRecord[0].mHeight, mImage->mData, imageData->mImageData[0].mData, (Engine::TextureCompressor::Format)imageData->mHeader.mFormat);
- for (size_t i = 1; i < mImage->mMipLevels; i++)
- {
- mImage->mMipmaps[i] = new unsigned char[imageData->mImageRecord[i].mWidth * imageData->mImageRecord[i].mHeight * 4];
- Engine::TextureCompressor::Decompress(imageData->mImageRecord[i].mWidth, imageData->mImageRecord[i].mHeight, mImage->mMipmaps[i], imageData->mImageData[i].mData, (Engine::TextureCompressor::Format)imageData->mHeader.mFormat);
- }
- // Check for BC5 format - those hold normal map. Add blue channel.
- if ((Engine::TextureCompressor::Format)imageData->mHeader.mFormat == Engine::TextureCompressor::Format::BC5)
- {
- for (size_t i = 0; i < mImage->mMipLevels; i++)
- {
- unsigned char* ptr = mImage->mMipmaps[i];
- for (size_t j = 0; j < imageData->mImageRecord[i].mWidth * imageData->mImageRecord[i].mHeight; j++)
- {
- float x = (float)ptr[0] / 255.0f * 2.0f - 1.0f;
- float y = (float)ptr[1] / 255.0f * 2.0f - 1.0f;
- float z = 1.0f - x * x - y * y;
- if (z < 0.0f)
- {
- z = 0.0f;
- }
- else
- {
- z = sqrtf(z);
- }
- ptr[0] = (unsigned char)((x * 0.5f + 0.5f) * 255.0f);
- ptr[1] = (unsigned char)((y * 0.5f + 0.5f) * 255.0f);
- ptr[2] = (unsigned char)((z * 0.5f + 0.5f) * 255.0f);
- ptr[3] = 255;
- ptr += 4;
- }
- }
- }
- if (mImage != nullptr)
- {
- mNumUncompressedMiplevels = (float)mImage->mMipLevels;
- if (mUncompressed != nullptr)
- {
- delete mUncompressed;
- }
- mUncompressed = new Engine::Texture("Uncompressed");
- mUncompressed->InitMipmaps(mRenderer, mImage->mWidth, mImage->mHeight, 1, mImage->mMipLevels, Engine::Graphics::RGBA8, (const void**)mImage->mMipmaps);
- mUncompressed->SetName("Uncompressed");
- }
- delete imageData;
- delete loader;
- }
- else
- {
- Engine::LoaderDevIL* loader = new Engine::LoaderDevIL(mLog);
- if (mImage != nullptr)
- {
- delete mImage;
- mImage = nullptr;
- }
- mImage = loader->Load(filename, true, true);
- if (mImage != nullptr)
- {
- loader->GenerateMipmaps(mImage);
- mNumUncompressedMiplevels = (float)mImage->mMipLevels;
- if (mUncompressed != nullptr)
- {
- delete mUncompressed;
- }
- mUncompressed = new Engine::Texture("Uncompressed");
- mUncompressed->InitMipmaps(mRenderer, mImage->mWidth, mImage->mHeight, 1, mImage->mMipLevels, mImage->mBpp <= 4 ? Engine::Graphics::RGBA8 : Engine::Graphics::RGBA32F, (const void**)mImage->mMipmaps);
- mUncompressed->SetName("Uncompressed");
- delete mCompressed;
- mCompressed = nullptr;
- }
- delete loader;
- }
- }
- void CompressTexture(Engine::TextureCompressor::Format format, bool dithering, bool generateMipmaps, bool alphaMipmap, bool normalMipmap, float exposure)
- {
- if (mImage != nullptr)
- {
- switch (format)
- {
- case Engine::TextureCompressor::Format::BC1:
- mCompressedFormat = Engine::Graphics::BC1;
- break;
- case Engine::TextureCompressor::Format::BC2:
- mCompressedFormat = Engine::Graphics::BC2;
- break;
- case Engine::TextureCompressor::Format::BC3:
- mCompressedFormat = Engine::Graphics::BC3;
- break;
- case Engine::TextureCompressor::Format::BC4:
- mCompressedFormat = Engine::Graphics::BC4;
- break;
- case Engine::TextureCompressor::Format::BC5:
- mCompressedFormat = Engine::Graphics::BC5;
- break;
- case Engine::TextureCompressor::Format::BC6H:
- mCompressedFormat = Engine::Graphics::BC6H;
- break;
- }
- Engine::LoaderDevIL* loader = new Engine::LoaderDevIL(mLog);
- mImage->mUseAlphaMipmap = alphaMipmap;
- if (normalMipmap)
- {
- loader->GenerateMipmapsNormal(mImage);
- }
- else
- {
- loader->GenerateMipmaps(mImage);
- }
- if (mUncompressed != nullptr)
- {
- delete mUncompressed;
- }
- mUncompressed = new Engine::Texture("Uncompressed");
- mUncompressed->InitMipmaps(mRenderer, mImage->mWidth, mImage->mHeight, 1, mImage->mMipLevels, mImage->mBpp <= 4 ? Engine::Graphics::RGBA8 : Engine::Graphics::RGBA32F, (const void**)mImage->mMipmaps);
- mUncompressed->SetName("Uncompressed");
- delete loader;
- if (mCompressed != nullptr)
- {
- delete mCompressed;
- }
- mCompressed = new Engine::Texture("Compressed");
- if (mCompressedData)
- {
- for (int i = 0; i < mCompressedLevels; i++)
- {
- delete mCompressedData[i];
- }
- delete[] mCompressedData;
- }
- if (mCompressedWidth)
- {
- delete[] mCompressedWidth;
- }
- if (mCompressedHeight)
- {
- delete[] mCompressedHeight;
- }
- if (generateMipmaps)
- {
- size_t width = mImage->mWidth;
- size_t height = mImage->mHeight;
- mCompressedLevels = 1;
- while (width > 4 && height > 4)
- {
- width /= 2;
- height /= 2;
- if ((width % 4 == 0) && (height % 4 == 0))
- {
- mCompressedLevels++;
- }
- else
- {
- break;
- }
- }
- mCompressedWidth = new size_t[mCompressedLevels];
- mCompressedHeight = new size_t[mCompressedLevels];
- width = mImage->mWidth;
- height = mImage->mHeight;
- mCompressedData = new unsigned char*[mCompressedLevels];
- for (int i = 0; i < mCompressedLevels; i++)
- {
- mCompressedWidth[i] = width;
- mCompressedHeight[i] = height;
- mCompressedData[i] = new unsigned char[mCompressedWidth[i] * mCompressedHeight[i] * Engine::Graphics::GetFormatSize(mCompressedFormat) / (Engine::Graphics::GetBlockSize(mCompressedFormat) * Engine::Graphics::GetBlockSize(mCompressedFormat))];
- Engine::TextureCompressor::Compress(mCompressedWidth[i], mCompressedHeight[i], mCompressedData[i], mImage->mMipmaps[i], format, 4, true, dithering, mImage->mBpp == 16 ? true : false, exposure);
- width /= 2;
- height /= 2;
- }
- mNumCompressedMiplevels = (float)mCompressedLevels;
- mCompressed->InitMipmaps(mRenderer, mCompressedWidth[0], mCompressedHeight[0], 1, mCompressedLevels, mCompressedFormat, (const void**)mCompressedData);
- }
- else
- {
- mCompressedLevels = 1;
- mCompressedWidth = new size_t[mCompressedLevels];
- mCompressedHeight = new size_t[mCompressedLevels];
- mCompressedWidth[0] = mImage->mWidth;
- mCompressedHeight[0] = mImage->mHeight;
- mCompressedData = new unsigned char*[mCompressedLevels];
- mCompressedData[0] = new unsigned char[mCompressedWidth[0] * mCompressedHeight[0] * Engine::Graphics::GetFormatSize(mCompressedFormat) / (Engine::Graphics::GetBlockSize(mCompressedFormat) * Engine::Graphics::GetBlockSize(mCompressedFormat))];
- Engine::TextureCompressor::Compress(mCompressedWidth[0], mCompressedHeight[0], mCompressedData[0], mImage->mData, format, 4, true, dithering, mImage->mBpp == 16 ? true : false, exposure);
- mNumCompressedMiplevels = 1.0f;
- mCompressed->Init(mRenderer, mCompressedWidth[0], mCompressedHeight[0], 1, mCompressedFormat, (const void*)mCompressedData[0]);
- }
- mCompressed->SetName("Compressed");
- }
- }
- public:
- /// <summary>
- /// Headless compression of input image into output image based on input parameters
- ///
- /// <para>Format types:</para>
- /// <para>0 - BC1</para>
- /// <para>1 - BC2</para>
- /// <para>2 - BC3</para>
- /// <para>3 - BC4</para>
- /// <para>4 - BC5</para>
- /// </summary>
- /// <param name="log">Log to write errors/info into</param>
- /// <param name="input">Input image filename</param>
- /// <param name="output">Output image filename</param>
- /// <param name="format">Format type</param>
- /// <param name="dithering">Use dithering?</param>
- /// <param name="generateMipmaps">Generate mipmaps?</param>
- /// <param name="alphaMipmap">Use alpha conservation when generating mipmaps?</param>
- /// <param name="normalMipmap">Generate normal-map mipmaps (renormalize each level)?</param>
- /// <param name="exposure">Exposure value to pre-multiply original image with</param>
- static void CompressHeadless(Engine::Log* log, const std::string& input, const std::string& output, int format, bool dithering, bool generateMipmaps, bool alphaMipmap, bool normalMipmap, float exposure)
- {
- Engine::LoaderDevIL* loader = new Engine::LoaderDevIL(log);
- Engine::LoaderDevIL::Image* image = loader->Load(input, true, true);
- if (image != nullptr)
- {
- Engine::TextureCompressor::Format compressFormat = (Engine::TextureCompressor::Format)format;
- Engine::Graphics::Format compressedFormat;
- switch (compressFormat)
- {
- case Engine::TextureCompressor::Format::BC1:
- compressedFormat = Engine::Graphics::BC1;
- break;
- case Engine::TextureCompressor::Format::BC2:
- compressedFormat = Engine::Graphics::BC2;
- break;
- case Engine::TextureCompressor::Format::BC3:
- compressedFormat = Engine::Graphics::BC3;
- break;
- case Engine::TextureCompressor::Format::BC4:
- compressedFormat = Engine::Graphics::BC4;
- break;
- case Engine::TextureCompressor::Format::BC5:
- compressedFormat = Engine::Graphics::BC5;
- break;
- case Engine::TextureCompressor::Format::BC6H:
- compressedFormat = Engine::Graphics::BC6H;
- break;
- }
- image->mUseAlphaMipmap = alphaMipmap;
- if (normalMipmap)
- {
- loader->GenerateMipmapsNormal(image);
- }
- else
- {
- loader->GenerateMipmaps(image);
- }
- size_t compressedLevels = 0;
- size_t* compressedWidth = nullptr;
- size_t* compressedHeight = nullptr;
- unsigned char** compressedData = nullptr;
- if (generateMipmaps)
- {
- size_t width = image->mWidth;
- size_t height = image->mHeight;
- compressedLevels = 1;
- while (width > 4 && height > 4)
- {
- width /= 2;
- height /= 2;
- if ((width % 4 == 0) && (height % 4 == 0))
- {
- compressedLevels++;
- }
- else
- {
- break;
- }
- }
- compressedWidth = new size_t[compressedLevels];
- compressedHeight = new size_t[compressedLevels];
- width = image->mWidth;
- height = image->mHeight;
- compressedData = new unsigned char* [compressedLevels];
- for (int i = 0; i < compressedLevels; i++)
- {
- compressedWidth[i] = width;
- compressedHeight[i] = height;
- compressedData[i] = new unsigned char[compressedWidth[i] * compressedHeight[i] * Engine::Graphics::GetFormatSize(compressedFormat) / (Engine::Graphics::GetBlockSize(compressedFormat) * Engine::Graphics::GetBlockSize(compressedFormat))];
- Engine::TextureCompressor::Compress(compressedWidth[i], compressedHeight[i], compressedData[i], image->mMipmaps[i], compressFormat, 4, true, dithering, image->mBpp == 16 ? true : false, exposure);
- width /= 2;
- height /= 2;
- }
- }
- else
- {
- compressedLevels = 1;
- compressedWidth = new size_t[compressedLevels];
- compressedHeight = new size_t[compressedLevels];
- compressedWidth[0] = image->mWidth;
- compressedHeight[0] = image->mHeight;
- compressedData = new unsigned char* [compressedLevels];
- compressedData[0] = new unsigned char[compressedWidth[0] * compressedHeight[0] * Engine::Graphics::GetFormatSize(compressedFormat) / (Engine::Graphics::GetBlockSize(compressedFormat) * Engine::Graphics::GetBlockSize(compressedFormat))];
- Engine::TextureCompressor::Compress(compressedWidth[0], compressedHeight[0],compressedData[0], image->mData, compressFormat, 4, true, dithering, image->mBpp == 16 ? true : false, exposure);
- }
- // Save the image
- Engine::LoaderSctex* sctexLoader = new Engine::LoaderSctex(log);
- std::vector<Engine::LoaderSctex::Image> data;
- for (size_t i = 0; i < compressedLevels; i++)
- {
- Engine::LoaderSctex::Image image;
- image.mWidth = compressedWidth[i];
- image.mHeight = compressedHeight[i];
- image.mData = compressedData[i];
- data.push_back(image);
- }
- sctexLoader->Save(output, compressedFormat, data);
- delete sctexLoader;
- for (size_t i = 0; i < compressedLevels; i++)
- {
- delete[] compressedData[i];
- }
- delete compressedWidth;
- delete compressedHeight;
- delete compressedData;
- delete image;
- }
- delete loader;
- }
- Main(Engine::Log* log, Engine::Constants* options) : Engine::System("Main")
- {
- EnableUpdater();
- mLog = log;
- mOptions = options;
- mWindow = new Engine::Window(log, options);
- mWindow->Create();
- mRenderer = new Engine::D3DRenderer(mWindow, log, options);
- mSwapChain = new Engine::D3DSwapChain(mRenderer, mWindow, log, options);
- static Engine::EventChannel chan;
- chan.Add<Engine::Keyboard::KeyPressed>(*this);
- chan.Add<Engine::Keyboard::KeyReleased>(*this);
- chan.Add<Engine::Keyboard::KeyChar>(*this);
- chan.Add<Engine::Mouse::MouseMove>(*this);
- chan.Add<Engine::Mouse::ButtonPressed>(*this);
- chan.Add<Engine::Mouse::ButtonReleased>(*this);
- chan.Add<Engine::Mouse::MouseWheel>(*this);
- chan.Add<Engine::Window::Resize>(*this);
- }
- virtual ~Main()
- {
- for (int i = 0; i < mCompressedLevels; i++)
- {
- delete mCompressedData[i];
- }
- delete[] mCompressedData;
- delete[] mCompressedWidth;
- delete[] mCompressedHeight;
- delete mImage;
- delete mCompressed;
- delete mUncompressed;
- delete mSwapChain;
- delete mRenderer;
- delete mWindow;
- }
- virtual bool Init()
- {
- int w;
- int h;
- this->mWindow->GetSize(&w, &h);
- mTexturesPass = new SkyeCuillin::RenderPassGUI(mRenderer, w, h);
- mImgui = new SkyeCuillin::ImguiSubsystem(mRenderer, w, h);
- ImGui::GetStyle().WindowBorderSize = 0;
- LoadTexture("../Data/Shared/Test.png");
- CompressTexture(Engine::TextureCompressor::Format::BC1, true, true, false, false, 1.0f);
- return true;
- }
- virtual void Shutdown()
- {
- mRenderer->Flush();
- delete mImgui;
- delete mTexturesPass;
- mWindow->Destroy();
- }
- virtual void Update()
- {
- static bool compressionShow = false;
- int w;
- int h;
- mWindow->GetSize(&w, &h);
- mWindow->Process();
- Engine::GraphicsContext* context = mRenderer->GetGraphicsContext();
- context->Begin();
- context->SetDescriptorHeap(Engine::DescriptorHeap::CBV_SRV_UAV, mRenderer->Heap());
- context->TransitionResource(mSwapChain->GetBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, true);
- context->SetRenderTargets(mSwapChain->GetBackBuffer(), mSwapChain->GetDepthBuffer());
- context->SetViewport(0.0f, 0.0f, (float)w, (float)h);
- context->SetScissorRect(0.0f, 0.0f, (float)w, (float)h);
- context->ClearColor(mSwapChain->GetBackBuffer(), 0.0f, 0.0f, 0.0f, 1.0f);
- context->ClearDepth(mSwapChain->GetDepthBuffer(), 1.0f, 0);
- static bool alphaMask = false;
- static float exposure = 1.0f;
- static int mode = 0;
- Engine::float3 mUV[2] = {
- mTransform * Engine::float3(-1.0f, -1.0f, 1.0f),
- mTransform * Engine::float3(1.0f, 1.0f, 1.0f)
- };
- mTexturesPass->Clear();
- if (mUncompressed != nullptr)
- {
- mUncompressedMiplevel = mCurrentMiplevel;
- if (mCurrentMiplevel >= mNumUncompressedMiplevels)
- {
- mUncompressedMiplevel = mNumUncompressedMiplevels - 1.0f;
- }
- mTexturesPass->AddSprite(Engine::float2(0.0f, 0.0f),
- Engine::float2(0.5f, 1.0f),
- Engine::float2(mUV[0].x, mUV[0].y) * 0.5f + 0.5f,
- Engine::float2(mUV[1].x, mUV[1].y) * 0.5f + 0.5f,
- Engine::float4(exposure, exposure, exposure, 1.0f),
- mUncompressed,
- mUncompressedMiplevel,
- alphaMask ? mode + 5 : mode);
- }
- if (mCompressed != nullptr)
- {
- mCompressedMiplevel = mCurrentMiplevel;
- if (mCurrentMiplevel >= mNumCompressedMiplevels)
- {
- mCompressedMiplevel = mNumCompressedMiplevels - 1.0f;
- }
- mTexturesPass->AddSprite(Engine::float2(0.5f, 0.0f),
- Engine::float2(0.5f, 1.0f),
- Engine::float2(mUV[0].x, mUV[0].y) * 0.5f + 0.5f,
- Engine::float2(mUV[1].x, mUV[1].y) * 0.5f + 0.5f,
- Engine::float4(exposure, exposure, exposure, 1.0f),
- mCompressed,
- mCompressedMiplevel,
- alphaMask ? mode + 5 : mode);
- }
- mTexturesPass->Render(mRenderer->Heap(), context);
- bool loadTexture = false;
- std::string textureFilename;
- mImgui->Update(context);
- mImgui->NewFrame();
- ImGui::SetNextWindowSize(ImVec2((float)w, 20.0f), ImGuiCond_Always);
- ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- if (ImGui::Begin("Menu", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_MenuBar))
- {
- if (ImGui::BeginMenuBar())
- {
- if (ImGui::BeginMenu("File"))
- {
- if (ImGui::MenuItem("Open", "CTRL+O", nullptr))
- {
- std::string filename;
- bool open = Engine::FileDialog::Show(mLog, "Open Image", "Image Files\0*.jpg;*.jpeg;*.png;*.bmp;*.tga;*.dds;*.tif;*.gif;*.sctex\0All Files\0*.*\0", Engine::FileDialog::Type::OPEN_FILE_DIALOG, filename);
- if (open)
- {
- textureFilename = filename;
- loadTexture = true;
- // Reset miplevel, view and exposure when loading a new image
- mCurrentMiplevel = 0.0f;
- mTransform = Engine::mat3();
- exposure = 1.0f;
- }
- }
- if (ImGui::MenuItem("Save", "CTRL+S", nullptr))
- {
- std::string filename;
- bool save = Engine::FileDialog::Show(mLog, "Save Compressed Image", "Skye Cuillin Texture\0*.sctex\0All Files\0*.*\0", Engine::FileDialog::Type::SAVE_FILE_DIALOG, filename);
- if (save)
- {
- Engine::LoaderSctex* sctexLoader = new Engine::LoaderSctex(mLog);
- std::vector<Engine::LoaderSctex::Image> data;
- for (size_t i = 0; i < mCompressedLevels; i++)
- {
- Engine::LoaderSctex::Image image;
- image.mWidth = mCompressedWidth[i];
- image.mHeight = mCompressedHeight[i];
- image.mData = mCompressedData[i];
- data.push_back(image);
- }
- sctexLoader->Save(filename, mCompressedFormat, data);
- delete sctexLoader;
- }
- }
- if (ImGui::MenuItem("Exit"))
- {
- static Engine::EventChannel chan;
- chan.Broadcast(Engine::Scheduler::StopEvent());
- }
- ImGui::EndMenu();
- }
- if (ImGui::BeginMenu("Edit"))
- {
- if (ImGui::MenuItem("Compress"))
- {
- compressionShow = true;
- }
- ImGui::EndMenu();
- }
- if (ImGui::BeginMenu("View"))
- {
- if (ImGui::MenuItem("Mip Level Up"))
- {
- mCurrentMiplevel = mCurrentMiplevel - 1.0f;
- if (mCurrentMiplevel < 0.0f)
- {
- mCurrentMiplevel = 0.0f;
- }
- }
- if (ImGui::MenuItem("Mip Level Down"))
- {
- mCurrentMiplevel = mCurrentMiplevel + 1.0f;
- float maxLevel = Engine::Math::Max(mNumCompressedMiplevels, mNumUncompressedMiplevels);
- if (mCurrentMiplevel >= maxLevel)
- {
- mCurrentMiplevel = maxLevel - 1.0f;
- }
- }
- if (ImGui::MenuItem("Reset View"))
- {
- mTransform = Engine::mat3();
- }
- if (ImGui::MenuItem("Reset Exposure"))
- {
- exposure = 1.0f;
- }
- ImGui::EndMenu();
- }
- ImGui::RadioButton("C", &mode, 0);
- ImGui::RadioButton("R", &mode, 1);
- ImGui::RadioButton("G", &mode, 2);
- ImGui::RadioButton("B", &mode, 3);
- ImGui::RadioButton("A", &mode, 4);
- ImGui::Checkbox("Alpha Mask", &alphaMask);
- ImGui::SliderFloat("Exposure", &exposure, 0.0f, 10.0f);
- ImGui::EndMenuBar();
- }
- }
- ImGui::End();
- ImGui::PopStyleVar();
- if (compressionShow)
- {
- if (ImGui::Begin("Compress", nullptr, ImGuiWindowFlags_NoCollapse))
- {
- const char* compression[] = { "BC1", "BC2", "BC3", "BC4", "BC5", "BC6H"};
- static int compressionSelected = 0;
- ImGui::Combo("Compression Type", &compressionSelected, compression, IM_ARRAYSIZE(compression));
- static bool dithering = false;
- ImGui::Checkbox("Use Dithering", &dithering);
- static bool generateMipmaps = false;
- ImGui::Checkbox("Generate Mipmaps", &generateMipmaps);
- static bool alphaMipmaps = false;
- ImGui::Checkbox("Alpha Mipmaps", &alphaMipmaps);
- static bool normalMipmaps = false;
- ImGui::Checkbox("Normal Mipmaps", &normalMipmaps);
- if (ImGui::Button("Compress"))
- {
- CompressTexture((Engine::TextureCompressor::Format)compressionSelected, dithering, generateMipmaps, alphaMipmaps, normalMipmaps, exposure);
- compressionShow = false;
- }
- ImGui::SameLine();
- if (ImGui::Button("Cancel"))
- {
- compressionShow = false;
- }
- }
- ImGui::End();
- }
- ImGui::SetNextWindowSize(ImVec2((float)w, 24.0f), ImGuiCond_Always);
- ImGui::SetNextWindowPos(ImVec2(0.0f, (float)h - 24.0f));
- ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
- if (ImGui::Begin("StatusBar", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse))
- {
- ImGui::Text("Mip Level %2f Position (%4f, %4f)", mCurrentMiplevel, (mUV[0].x + mUV[1].x) * 0.5f, (mUV[1].y + mUV[1].y) * 0.5f);
- }
- ImGui::End();
- ImGui::PopStyleVar();
- mImgui->Render();
- context->TransitionResource(mSwapChain->GetBackBuffer(), D3D12_RESOURCE_STATE_PRESENT, true);
- uint64_t fence = context->Finish();
- mSwapChain->SwapBuffers();
- if (loadTexture)
- {
- LoadTexture(textureFilename);
- }
- }
- void Handle(const Engine::Keyboard::KeyChar& kc)
- {
- ImGuiIO& io = ImGui::GetIO();
- if (kc.mKey < 256)
- {
- io.AddInputCharacter((ImWchar)kc.mKey);
- }
- }
- void Handle(const Engine::Keyboard::KeyPressed& kp)
- {
- ImGuiIO& io = ImGui::GetIO();
- io.KeysDown[kp.mKey] = 1;
- Engine::Input* input = (Engine::Input*)Engine::Core::Instance()->Get("Input");
- /*if (input->GetKeyboard()->GetKeyState(Engine::Keyboard::KEY_CONTROL_L))
- {
- io.AddInputCharacter((ImWchar)kp.mKey);
- }*/
- switch (kp.mKey)
- {
- case Engine::Keyboard::KEY_CONTROL_L:
- io.KeyCtrl = true;
- break;
- case Engine::Keyboard::KEY_ALT_L:
- io.KeyAlt = true;
- break;
- case Engine::Keyboard::KEY_SHIFT_L:
- io.KeyShift = true;
- break;
- }
- switch (kp.mKey)
- {
- case Engine::Keyboard::KEY_PAGE_UP:
- mCurrentMiplevel = mCurrentMiplevel - 1.0f;
- if (mCurrentMiplevel < 0.0f)
- {
- mCurrentMiplevel = 0.0f;
- }
- break;
- case Engine::Keyboard::KEY_PAGE_DOWN:
- mCurrentMiplevel = mCurrentMiplevel + 1.0f;
- float maxLevel = Engine::Math::Max(mNumCompressedMiplevels, mNumUncompressedMiplevels);
- if (mCurrentMiplevel >= maxLevel)
- {
- mCurrentMiplevel = maxLevel - 1.0f;
- }
- break;
- }
- }
- void Handle(const Engine::Keyboard::KeyReleased& kp)
- {
- ImGuiIO& io = ImGui::GetIO();
- io.KeysDown[kp.mKey] = 0;
- switch (kp.mKey)
- {
- case Engine::Keyboard::KEY_CONTROL_L:
- io.KeyCtrl = false;
- break;
- case Engine::Keyboard::KEY_ALT_L:
- io.KeyAlt = false;
- break;
- case Engine::Keyboard::KEY_SHIFT_L:
- io.KeyShift = false;
- break;
- }
- }
- bool mMousing = false;
- float mPrevMouse[2];
- void Handle(const Engine::Mouse::MouseMove& m)
- {
- ImGuiIO& io = ImGui::GetIO();
- io.MousePos.x = (float)m.mX;
- io.MousePos.y = (float)m.mY;
- // Check whether we're moving with mouse and whether mouse isn't over ImGUI panels/windows - if both apply, update viewport transform
- if (mMousing && !io.WantCaptureMouse)
- {
- float mDeltaMouse[2];
- mDeltaMouse[0] = mPrevMouse[0] - (float)m.mX;
- mDeltaMouse[1] = mPrevMouse[1] - (float)m.mY;
- int w = 0;
- int h = 0;
- mWindow->GetSize(&w, &h);
- Engine::float3 transform = Engine::float3(mDeltaMouse[0] * 1.0f / (float)w, mDeltaMouse[1] * 1.0f / (float)h, 1.0f);
- mTransform = mTransform * Engine::mat3(transform);
- }
- mPrevMouse[0] = (float)m.mX;
- mPrevMouse[1] = (float)m.mY;
- }
- void Handle(const Engine::Mouse::MouseWheel& m)
- {
- ImGuiIO& io = ImGui::GetIO();
- io.MouseWheel = (float)m.mDelta / 120.0f;
- // Check whether mouse isn't over ImGUI panels/windows - if it isn't, zoom in/out viewport
- if (!io.WantCaptureMouse)
- {
- if (m.mDelta < 0)
- {
- mTransform = mTransform * Engine::mat3(1.1f, 0.0f, 0.0f, 0.0f, 1.1f, 0.0f, 0.0f, 0.0f, 1.0f);
- }
- else if (m.mDelta > 0)
- {
- mTransform = mTransform * Engine::mat3(0.9f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 0.0f, 0.0f, 1.0f);
- }
- }
- }
- void Handle(const Engine::Mouse::ButtonPressed& bp)
- {
- ImGuiIO& io = ImGui::GetIO();
- io.MouseDown[bp.mButton] = true;
- if (bp.mButton == 0)
- {
- mMousing = true;
- }
- }
- void Handle(const Engine::Mouse::ButtonReleased& br)
- {
- ImGuiIO& io = ImGui::GetIO();
- io.MouseDown[br.mButton] = false;
- if (br.mButton == 0)
- {
- mMousing = false;
- }
- }
- void Handle(const Engine::Window::Resize& r)
- {
- mSwapChain->Resize(r.mWidth, r.mHeight);
- mImgui->Resize(r.mWidth, r.mHeight);
- mTexturesPass->Resize(r.mWidth, r.mHeight);
- }
- };
- int main(int argc, char** argv)
- {
- Engine::Log* log = new Engine::Log();
- log->AddOutput(new std::ofstream("Output.log"), Engine::Log::LOG_DEFAULT);
- if (argc > 1)
- {
- int inputIndex = -1;
- std::string input;
- int outputIndex = -1;
- std::string output;
- int formatIndex = -1;
- int format = 0;
- bool dithering = false;
- bool mipmaps = false;
- bool alphaMipmaps = false;
- bool normalMipmaps = false;
- bool useExposure = false;
- int exposureIndex = -1;
- float exposure = 1.0f;
- for (int i = 0; i < argc; i++)
- {
- std::string arg = std::string(argv[i]);
- if ((arg == "--input" || arg == "-i") && argc >(i + 1) && inputIndex == -1)
- {
- inputIndex = i + 1;
- }
- else if ((arg == "--output" || arg == "-o") && argc > (i + 1) && outputIndex == -1)
- {
- outputIndex = i + 1;
- }
- else if ((arg == "--format" || arg == "-f") && argc > (i + 1) && formatIndex == -1)
- {
- formatIndex = i + 1;
- }
- else if (arg == "--dithering" || arg == "-d")
- {
- dithering = true;
- }
- else if (arg == "--mipmaps" || arg == "-m")
- {
- mipmaps = true;
- }
- else if (arg == "--alpha" || arg == "-a")
- {
- alphaMipmaps = true;
- }
- else if (arg == "--normals" || arg == "-n")
- {
- normalMipmaps = true;
- }
- else if ((arg == "--exposure" || arg == "-e") && argc > (i + 1) && exposureIndex == -1)
- {
- useExposure = true;
- exposureIndex = i + 1;
- }
- }
- input = argv[inputIndex];
- if (input[0] == '\"')
- {
- input = input.substr(1, input.length() - 2);
- }
- output = argv[outputIndex];
- if (output[0] == '\"')
- {
- output = output.substr(1, output.length() - 2);
- }
- format = std::atoi(argv[formatIndex]);
- if (exposureIndex > -1)
- {
- exposure = std::atof(argv[exposureIndex]);
- }
- if (inputIndex == -1 || outputIndex == -1 || formatIndex == -1 || input.length() == 0 || output.length() == 0 || format < 0 || format > 4)
- {
- std::cout << "Error: Invalid arguments, required arguments are:" << std::endl << std::endl;
- std::cout << "--input, -i" << std::endl;
- std::cout << "\tPath to input image file (*.*)" << std::endl;
- std::cout << "--output, -o" << std::endl;
- std::cout << "\tPath to output image file (*.sctex)" << std::endl;
- std::cout << "--format, -f" << std::endl;
- std::cout << "\tFormat to use for compression (0 - BC1, 1 - BC2, 2 - BC3, 3 - BC4, 4 - BC5)" << std::endl;
- std::cout << "--dithering, -d" << std::endl;
- std::cout << "\tUse dithering to improve" << std::endl;
- std::cout << "--mipmaps, -m" << std::endl;
- std::cout << "\tPath to input image file" << std::endl;
- std::cout << "--alpha, -a" << std::endl;
- std::cout << "\tPath to input image file" << std::endl;
- std::cout << "--normals, -n" << std::endl;
- std::cout << "\tPath to input image file" << std::endl;
- std::cout << "--exposure, -e" << std::endl;
- std::cout << "\tExposure value - decimal number, default is 1.0" << std::endl;
- }
- else
- {
- Main::CompressHeadless(log, input, output, format, dithering, mipmaps, alphaMipmaps, normalMipmaps, exposure);
- }
- }
- else
- {
- Engine::Constants* options = new Engine::Constants(log, "../Data/Config/ConfigTexture.conf");
- Engine::Input* input = new Engine::Input();
- Main* m = new Main(log, options);
- Engine::Core::Instance()->Add(input);
- Engine::Core::Instance()->Add(m);
- Engine::Core::Instance()->SetLog(log);
- Engine::Core::Instance()->SetOptions(options);
- Engine::Core::Instance()->Run();
- Engine::Core::Instance()->Dispose();
- delete m;
- delete input;
- delete options;
- }
- delete log;
- Engine::MemoryDump(std::cout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement