diff --git a/CMakeLists.txt b/CMakeLists.txt index da6d844..44724eb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,42 +13,47 @@ option(PD_BUILD_TESTS "Sets if TestApp and TestBench get build" OFF) option(PD_BUILD_SHARED "Build Shared Library" OFF) option(PD_BUILD_TOOLS "Build Palladium Tools" OFF) +if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS") + add_compile_options(-Wno-psabi) +endif() + if(${PD_BUILD_TOOLS}) - add_subdirectory(tools) + add_subdirectory(tools) endif() add_subdirectory(vendor) # # Include Library Source set(PD_SOURCES - # Common - source/common.cpp + # Common + source/common.cpp - # Core - source/core/bits.cpp - source/core/color.cpp - source/core/mat.cpp - source/core/strings.cpp + # Core + source/core/bits.cpp + source/core/color.cpp + source/core/mat.cpp + source/core/strings.cpp + source/core/timer.cpp - # Drivers - source/drivers/os.cpp - source/drivers/gfx.cpp + # Drivers + source/drivers/os.cpp + source/drivers/gfx.cpp - # Lithium - source/lithium/pools.cpp + # Lithium + source/lithium/pools.cpp ) if(${PD_BUILD_SHARED}) - add_library(palladium SHARED ${PD_SOURCES}) - target_compile_definitions(palladium PRIVATE PD_BUILD_SHARED) + add_library(palladium SHARED ${PD_SOURCES}) + target_compile_definitions(palladium PRIVATE PD_BUILD_SHARED) else() - add_library(palladium STATIC ${PD_SOURCES}) - target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC) + add_library(palladium STATIC ${PD_SOURCES}) + target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC) endif() target_compile_definitions(palladium PUBLIC PD_DEBUG) target_compile_options(palladium - PUBLIC $<$: + PUBLIC $<$: -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include > @@ -57,75 +62,75 @@ target_compile_options(palladium add_library(palladium::palladium ALIAS palladium) target_include_directories(palladium - PUBLIC - $ - $ + PUBLIC + $ + $ ) target_compile_options(palladium - PRIVATE - $<$,$>:-O0 -g> - $<$,$>:-O3> + PRIVATE + $<$,$>:-O0 -g> + $<$,$>:-O3> - $<$,$>:/Od /Zi> - $<$,$>:/O2> + $<$,$>:/Od /Zi> + $<$,$>:/O2> ) install( - TARGETS palladium - EXPORT palladiumTargets - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + TARGETS palladium + EXPORT palladiumTargets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) install( - DIRECTORY include/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + DIRECTORY include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) install(EXPORT palladiumTargets - FILE palladiumTargets.cmake - NAMESPACE palladium:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium + FILE palladiumTargets.cmake + NAMESPACE palladium:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium ) include(CMakePackageConfigHelpers) configure_package_config_file( - cmake/palladiumConfig.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake - INSTALL_DESTINATION - ${CMAKE_INSTALL_LIBDIR}/cmake/palladium + cmake/palladiumConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake + INSTALL_DESTINATION + ${CMAKE_INSTALL_LIBDIR}/cmake/palladium ) write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake - VERSION - ${PROJECT_VERSION} - COMPATIBILITY - SameMajorVersion + ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake + VERSION + ${PROJECT_VERSION} + COMPATIBILITY + SameMajorVersion ) install( - FILES - ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake - ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium + FILES + ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium ) find_program(CLANG_FORMAT clang-format) file(GLOB_RECURSE PD_FMTFILES - CONFIGURE_DEPENDS - ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/backends/source/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/backends/include/*.hpp + CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/backends/source/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/backends/include/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/core/source/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/gfx/source/*.cpp ) add_custom_target(pd-clang-format - COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES} - COMMENT "Formatting Project Sources" + COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES} + COMMENT "Formatting Project Sources" ) add_subdirectory(backends) diff --git a/backends/source/gfx_citro3d.cpp b/backends/source/gfx_citro3d.cpp index 588d0d2..f4d19f7 100644 --- a/backends/source/gfx_citro3d.cpp +++ b/backends/source/gfx_citro3d.cpp @@ -55,6 +55,7 @@ struct GfxCitro3D::Impl { shaderProgram_s pShader; DVLB_s* pCode; int uLocProjection = 0; + C3D_Tex* CurrentTex = nullptr; std::vector pShaderRaw; GPU_TEXCOLOR TextureTranslateFormat(TextureFormat fmt) { @@ -83,14 +84,15 @@ struct GfxCitro3D::Impl { } } - void SetupPixelStage(GPU_TEXCOLOR clr) { + void SetupPixelStage() { shaderProgramUse(&pShader); C3D_BindProgram(&pShader); C3D_SetAttrInfo(&pAttr); C3D_DepthTest(false, GPU_GREATER, GPU_WRITE_ALL); + if (!CurrentTex) return; C3D_TexEnv* env = C3D_GetTexEnv(0); C3D_TexEnvInit(env); - switch (clr) { + switch (CurrentTex->fmt) { case GPU_A4: case GPU_A8: case GPU_L4: @@ -142,15 +144,30 @@ void GfxCitro3D::SysDeinit() { void GfxCitro3D::Submit(size_t count, size_t start) { if (!impl) return; + BindTexture(CurrentTex); + impl->SetupPixelStage(); // needs to be called after + C3D_Mtx proj; + Mtx_OrthoTilt(&proj, 0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f, false); + C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, impl->uLocProjection, &proj); + // C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, impl->uLocProjection, + // (C3D_Mtx*)&Projection); + auto buf = C3D_GetBufInfo(); + BufInfo_Init(buf); + BufInfo_Add(buf, GetVertexBufPtr(0), sizeof(Li::Vertex), 3, 0x210); + C3D_DrawElements(GPU_TRIANGLES, count, C3D_UNSIGNED_SHORT, + GetIndexBufPtr(start)); + impl->CurrentTex = nullptr; } void GfxCitro3D::BindTexture(TextureID id) { - if (!impl) return; + if (!impl || !id) return; + impl->CurrentTex = (C3D_Tex*)id; C3D_TexBind(0, (C3D_Tex*)id); } void GfxCitro3D::SysReset() { if (!impl) return; + C3D_CullFace(GPU_CULL_NONE); } Li::Texture GfxCitro3D::LoadTexture(const std::vector& pixels, int w, @@ -210,7 +227,7 @@ Li::Texture GfxCitro3D::LoadTexture(const std::vector& pixels, int w, void GfxCitro3D::DeleteTexture(const Li::Texture& tex) { if (!tex.GetID()) return; - UnRegisterTexture(tex); + UnregisterTexture(tex); C3D_Tex* t = reinterpret_cast(tex.GetID()); C3D_TexDelete(t); delete t; diff --git a/backends/source/gfx_directx9.cpp b/backends/source/gfx_directx9.cpp index d71b745..de84273 100644 --- a/backends/source/gfx_directx9.cpp +++ b/backends/source/gfx_directx9.cpp @@ -37,7 +37,7 @@ VS_OUT main(VS_IN input) { static const char* g_psCode = R"( sampler2D tex : register(s0); -bool alfa; +float alfa; struct PS_IN { float2 uv : TEXCOORD0; @@ -46,7 +46,7 @@ struct PS_IN { float4 main(PS_IN input) : COLOR0 { float4 tc = tex2D(tex, input.uv); - if (alfa) + if (alfa > 0.5) return float4(input.col.rgb, tc.a * input.col.a); else return tc * input.col; @@ -136,6 +136,7 @@ void GfxDirectX9::SysDeinit() { void GfxDirectX9::Submit(size_t count, size_t start) { if (!impl || !impl->Device || !impl->VBO || !impl->IBO) return; + BindTexture(CurrentTex); impl->Device->SetVertexShaderConstantF( 0, reinterpret_cast(&Projection), 4); @@ -186,7 +187,7 @@ void GfxDirectX9::SysReset() { Li::Texture GfxDirectX9::LoadTexture(const std::vector& pixels, int w, int h, TextureFormat type, TextureFilter filter) { - if (!impl || !impl->Device) return 0; + if (!impl || !impl->Device) return Li::Texture(); IDirect3DTexture9* tex = nullptr; D3DFORMAT fmt = D3DFMT_A8R8G8B8; if (type == TextureFormat::RGB24) @@ -196,7 +197,7 @@ Li::Texture GfxDirectX9::LoadTexture(const std::vector& pixels, int w, HRESULT hr = impl->Device->CreateTexture(w, h, 1, 0, fmt, D3DPOOL_MANAGED, &tex, nullptr); - if (FAILED(hr) || !tex) return 0; + if (FAILED(hr) || !tex) return Li::Texture(); D3DLOCKED_RECT rect; tex->LockRect(0, &rect, nullptr, 0); @@ -209,7 +210,7 @@ Li::Texture GfxDirectX9::LoadTexture(const std::vector& pixels, int w, u8 r = pixels[(y * w + x) * 3 + 0]; u8 g = pixels[(y * w + x) * 3 + 1]; u8 b = pixels[(y * w + x) * 3 + 2]; - dst[x] = (0xFF << 24) | (r << 16) | (g << 8) | b; // X8R8G8B8 + dst[x] = (0xFF << 24) | (r << 16) | (g << 8) | b; } dstRow += rect.Pitch; } @@ -222,7 +223,7 @@ Li::Texture GfxDirectX9::LoadTexture(const std::vector& pixels, int w, u8 g = pixels[(y * w + x) * 4 + 1]; u8 b = pixels[(y * w + x) * 4 + 2]; u8 a = pixels[(y * w + x) * 4 + 3]; - dst[x] = (a << 24) | (r << 16) | (g << 8) | b; // A8R8G8B8 + dst[x] = (a << 24) | (r << 16) | (g << 8) | b; } dstRow += rect.Pitch; } @@ -246,7 +247,7 @@ Li::Texture GfxDirectX9::LoadTexture(const std::vector& pixels, int w, void GfxDirectX9::DeleteTexture(const Li::Texture& tex) { if (!tex.GetID()) return; - UnRegisterTexture(tex); + UnregisterTexture(tex); IDirect3DTexture9* t = (IDirect3DTexture9*)tex.GetID(); t->Release(); } diff --git a/tests/gfx/source/main.cpp b/tests/gfx/source/main.cpp index 217b212..2696201 100644 --- a/tests/gfx/source/main.cpp +++ b/tests/gfx/source/main.cpp @@ -1,4 +1,12 @@ -#ifndef __3DS__ +#ifdef __3DS__ +#include <3ds.h> +#include +const u32 DisplayTransferFlags = + (GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) | + GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | + GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | + GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO)); +#else #include ////////////////////////// #include @@ -80,6 +88,15 @@ class App { #endif glfwSwapInterval(1); #else + gfxInitDefault(); + C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); + Top = + C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); + Bottom = + C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); + C3D_RenderTargetSetOutput(Top, GFX_TOP, GFX_LEFT, DisplayTransferFlags); + C3D_RenderTargetSetOutput(Bottom, GFX_BOTTOM, GFX_LEFT, + DisplayTransferFlags); PD::Gfx::UseDriver(); #endif PD::Gfx::Init(); @@ -97,13 +114,14 @@ class App { cmd->Reserve(4, 6); cmd->Add(0, 1, 2); cmd->Add(0, 2, 3); - cmd->Add(PD::Li::Vertex(PD::fvec2(0, 0), PD::fvec2(0, 0), 0xffffffff)); - cmd->Add(PD::Li::Vertex(PD::fvec2(pTex.GetSize().x, 0), PD::fvec2(1, 0), - 0xffffffff)); + cmd->Add( + PD::Li::Vertex(PD::fvec2(0, 0), pTex.GetUV().TopLeft(), 0xffffffff)); + cmd->Add(PD::Li::Vertex(PD::fvec2(pTex.GetSize().x, 0), + pTex.GetUV().TopRight(), 0xffffffff)); cmd->Add(PD::Li::Vertex(PD::fvec2(pTex.GetSize().x, pTex.GetSize().y), - PD::fvec2(1, 1), 0xffffffff)); - cmd->Add(PD::Li::Vertex(PD::fvec2(0, pTex.GetSize().y), PD::fvec2(0, 1), - 0xffffffff)); + pTex.GetUV().BotRight(), 0xffffffff)); + cmd->Add(PD::Li::Vertex(PD::fvec2(0, pTex.GetSize().y), + pTex.GetUV().BotLeft(), 0xffffffff)); cmd->Tex = pTex.GetID(); } ~App() { @@ -118,8 +136,13 @@ class App { void Run() { #ifdef __3DS__ while (aptMainLoop()) { + PD::Gfx::SetViewPort(400, 240); + C3D_FrameBegin(C3D_FRAME_SYNCDRAW); + C3D_FrameDrawOn(Top); + C3D_RenderTargetClear(Top, C3D_CLEAR_ALL, PD::Color(25, 25, 25, 25), 0); #else while (!glfwWindowShouldClose(window)) { + PD::Gfx::SetViewPort(1280, 720); if (pDriver == Driver::OpenGL2 || pDriver == Driver::OpenGL3) { glClearColor(0.1, 0.1, 0.1, 0.1); glClear(GL_COLOR_BUFFER_BIT); @@ -135,9 +158,10 @@ class App { } #endif PD::Gfx::Reset(); - PD::Gfx::SetViewPort(1280, 720); + PD::Gfx::Draw(pPool); #ifdef __3DS__ + C3D_FrameEnd(0); #else glfwPollEvents(); if (pDriver == Driver::DirectX9) { @@ -155,7 +179,10 @@ class App { } private: -#ifndef __3DS__ +#ifdef __3DS__ + C3D_RenderTarget* Top = nullptr; + C3D_RenderTarget* Bottom = nullptr; +#else GLFWwindow* window = nullptr; #endif PD::Pool pPool;