Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "JSInterface_Renderer.h" 21 : 22 : #include "graphics/TextureManager.h" 23 : #include "ps/CStr.h" 24 : #include "ps/CStrIntern.h" 25 : #include "renderer/Renderer.h" 26 : #include "renderer/RenderingOptions.h" 27 : #include "scriptinterface/FunctionWrapper.h" 28 : #include "scriptinterface/Object.h" 29 : 30 : #include <fmt/format.h> 31 : #include <string> 32 : 33 : namespace JSI_Renderer 34 : { 35 : #define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \ 36 : bool Get##NAME##Enabled() \ 37 : { \ 38 : return g_RenderingOptions.Get##NAME(); \ 39 : } \ 40 : \ 41 : void Set##NAME##Enabled(bool enabled) \ 42 : { \ 43 : g_RenderingOptions.Set##NAME(enabled); \ 44 : } 45 : 46 0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(CutsceneMode); 47 0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(DisplayFrustum); 48 0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(DisplayShadowsFrustum); 49 : 50 : #undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING 51 : 52 0 : std::string GetRenderDebugMode() 53 : { 54 0 : return RenderDebugModeEnum::ToString(g_RenderingOptions.GetRenderDebugMode()).c_str(); 55 : } 56 : 57 0 : void SetRenderDebugMode(const std::string& mode) 58 : { 59 0 : g_RenderingOptions.SetRenderDebugMode(RenderDebugModeEnum::FromString(mode)); 60 0 : } 61 : 62 0 : bool TextureExists(const std::wstring& filename) 63 : { 64 0 : return g_Renderer.GetTextureManager().TextureExists(filename); 65 : } 66 : 67 0 : float GetPBRBrightness() 68 : { 69 0 : return g_RenderingOptions.GetPBRBrightness(); 70 : } 71 : 72 0 : void SetPBRBrightness(const float value) 73 : { 74 0 : g_RenderingOptions.SetPBRBrightness(value); 75 0 : } 76 : 77 0 : JS::Value GetRendererCapabilities(const Script::Request& rq) 78 : { 79 0 : if (!CRenderer::IsInitialised()) 80 0 : return JS::UndefinedValue(); 81 : 82 0 : JS::RootedValue val(rq.cx); 83 0 : Script::CreateObject(rq, &val); 84 : 85 0 : const auto& capabilities{g_Renderer.GetCapabilities()}; 86 : 87 0 : for (const uint32_t sampleCount : {2, 4, 8, 16}) 88 : { 89 0 : Script::SetProperty( 90 0 : rq, val, fmt::format("msaa{}", sampleCount).c_str(), 91 0 : capabilities.multisampling && capabilities.maxSampleCount >= sampleCount, true); 92 : } 93 : 94 0 : for (const uint32_t maxAnisotropy : {2, 4, 8, 16}) 95 : { 96 0 : Script::SetProperty( 97 0 : rq, val, fmt::format("anisotropy{}", maxAnisotropy).c_str(), 98 0 : capabilities.anisotropicFiltering && capabilities.maxAnisotropy >= static_cast<float>(maxAnisotropy), true); 99 : } 100 : 101 0 : return val; 102 0 : } 103 : 104 : #define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \ 105 : Script::Function::Register<&Get##NAME##Enabled>(rq, "Renderer_Get" #NAME "Enabled"); \ 106 : Script::Function::Register<&Set##NAME##Enabled>(rq, "Renderer_Set" #NAME "Enabled"); 107 : 108 26 : void RegisterScriptFunctions(const Script::Request& rq) 109 : { 110 26 : Script::Function::Register<&TextureExists>(rq, "TextureExists"); 111 26 : Script::Function::Register<&GetRenderDebugMode>(rq, "Renderer_GetRenderDebugMode"); 112 26 : Script::Function::Register<&SetRenderDebugMode>(rq, "Renderer_SetRenderDebugMode"); 113 26 : Script::Function::Register<&GetPBRBrightness>(rq, "Renderer_GetPBRBrightness"); 114 26 : Script::Function::Register<&SetPBRBrightness>(rq, "Renderer_SetPBRBrightness"); 115 26 : Script::Function::Register<&GetRendererCapabilities>(rq, "Renderer_GetCapabilities"); 116 26 : REGISTER_BOOLEAN_SCRIPT_SETTING(CutsceneMode); 117 26 : REGISTER_BOOLEAN_SCRIPT_SETTING(DisplayFrustum); 118 26 : REGISTER_BOOLEAN_SCRIPT_SETTING(DisplayShadowsFrustum); 119 26 : } 120 : 121 : #undef REGISTER_BOOLEAN_SCRIPT_SETTING 122 : }