diff --git a/core/e2core/FileSystem.cpp b/core/e2core/FileSystem.cpp index e4cc96e80..82163284e 100644 --- a/core/e2core/FileSystem.cpp +++ b/core/e2core/FileSystem.cpp @@ -419,6 +419,12 @@ bool CFileSystem::Init(bool editorMode) AddPackage(packageName, type, mountPath); } + // source control is for development builds only +#if !defined(_RETAIL) && !defined(_PROFILE) + // p4 finds its client config (P4CONFIG) from the working directory + m_sourceControl = ISourceControl::Create(g_eqCore->GetConfig(), *fsConfig, GetAbsoluteFullPath(SP_ROOT, ".")); +#endif + m_isInit = true; return true; } @@ -426,6 +432,7 @@ bool CFileSystem::Init(bool editorMode) void CFileSystem::Shutdown() { m_isInit = false; + m_sourceControl = nullptr; m_fsPackages.clear(true); m_findDatas.clear(true); m_directories.clear(true); @@ -486,9 +493,22 @@ IFileStreamPtr CFileSystem::Open(const char* filename, int openFlags, int search if (isWrite && !spInfo.writePath) return false; + // source control checks out the file before writing and adds it once it is created + EqString scFullPath; + bool scFileCreated = false; + if (isWrite && m_sourceControl) + { + scFullPath = GetAbsoluteFullPath(SP_DIRECT, filePath); + scFileCreated = fast_stat(scFullPath, nullptr) != 0; + m_sourceControl->PrepareFileWrite(scFullPath, SC_FILETYPE_AUTO); + } + COSFile osFile; if (osFile.Open(filePath, osModeFlags)) { + if (scFileCreated) + m_sourceControl->FinishFileWrite(scFullPath, SC_FILETYPE_AUTO); + fileHandle = IFileStreamPtr(CRefPtr_new(COSFileStream, filename, std::move(osFile))); return true; } @@ -750,7 +770,10 @@ EqStringRef CFileSystem::GetAbsoluteFullPath(ESearchPath search, const char* dir bool CFileSystem::FileRemove(const char* filename, ESearchPath search ) const { - const EqStringRef path = GetAbsolutePath(search, filename); + const EqString path = GetAbsolutePath(search, filename); + + if (m_sourceControl && m_sourceControl->RemoveFile(GetAbsoluteFullPath(SP_DIRECT, path))) + return true; return remove(path) == 0; } @@ -780,10 +803,84 @@ bool CFileSystem::RemoveDir(const char* dirname, ESearchPath search ) const bool CFileSystem::Rename(const char* oldNameOrPath, const char* newNameOrPath, ESearchPath search) const { - const EqStringRef oldPath = GetAbsolutePath(search, oldNameOrPath); - const EqStringRef newPath = GetAbsolutePath(search, newNameOrPath); + const EqString oldPath = GetAbsolutePath(search, oldNameOrPath); + const EqString newPath = GetAbsolutePath(search, newNameOrPath); + + if (!m_sourceControl) + return rename(oldPath, newPath) == 0; + + // directories are renamed without source control + const EqString oldFullPath = GetAbsoluteFullPath(SP_DIRECT, oldPath); + if (DirExist(oldFullPath, SP_DIRECT)) + return rename(oldPath, newPath) == 0; + + const EqString newFullPath = GetAbsoluteFullPath(SP_DIRECT, newPath); + if (m_sourceControl->RenameFile(oldFullPath, newFullPath)) + return true; + + if (rename(oldPath, newPath) != 0) + return false; + + // renamed file which is not in the depot is added as a new one + if (fast_stat(newFullPath, nullptr) == 0) + m_sourceControl->FinishFileWrite(newFullPath, SC_FILETYPE_AUTO); + + return true; +} + +void CFileSystem::PushSourceControlChangelist(const char* description) +{ + if (m_sourceControl) + m_sourceControl->PushChangelist(description); +} + +void CFileSystem::PopSourceControlChangelist() +{ + if (m_sourceControl) + m_sourceControl->PopChangelist(); +} + +bool CFileSystem::IsSourceControlEnabled() const +{ + return m_sourceControl && m_sourceControl->IsAvailable(); +} + +ESourceControlStatus CFileSystem::GetSourceControlStatus() const +{ + return m_sourceControl ? m_sourceControl->GetStatus() : SC_STATUS_NONE; +} + +ESourceControlStatus CFileSystem::SetSourceControlOnline(bool online) +{ + return m_sourceControl ? m_sourceControl->SetOnline(online) : SC_STATUS_NONE; +} + +void CFileSystem::SetSourceControlStatusCallback(const SourceControlStatusFunc& func) +{ + if (m_sourceControl) + m_sourceControl->SetStatusCallback(func); +} + +void CFileSystem::CheckoutSourceControlFiles(const char* fileOrDirName, ESearchPath search, ESourceControlFileType fileType) +{ + if (!m_sourceControl) + return; + + const EqString fullPath = GetAbsoluteFullPath(SP_DIRECT, GetAbsolutePath(search, fileOrDirName)); + if (DirExist(fullPath, SP_DIRECT)) + m_sourceControl->CheckoutDir(fullPath); + else + m_sourceControl->PrepareFileWrite(fullPath, fileType); +} + +void CFileSystem::AddSourceControlFile(const char* fileName, ESearchPath search, ESourceControlFileType fileType) +{ + if (!m_sourceControl) + return; - return rename(oldPath, newPath) == 0; + const EqString fullPath = GetAbsoluteFullPath(SP_DIRECT, GetAbsolutePath(search, fileName)); + if (fast_stat(fullPath, nullptr) == 0) + m_sourceControl->FinishFileWrite(fullPath, fileType); } bool CFileSystem::WalkOverSearchPaths(int searchFlags, const char* fileName, const SPWalkFunc& func) const diff --git a/core/e2core/FileSystem.h b/core/e2core/FileSystem.h index 13762fbd6..c799dc103 100644 --- a/core/e2core/FileSystem.h +++ b/core/e2core/FileSystem.h @@ -8,6 +8,7 @@ #pragma once #include "core/IFileSystem.h" #include "core/platform/OSFile.h" +#include "SourceControl.h" class CBasePackageReader; struct FSSearchPathInfo; @@ -124,7 +125,22 @@ class CFileSystem : public IFileSystem void FindClose(FSFindData* findData); bool FindIsDirectory(FSFindData* findData) const; int FindGetDirIndex(FSFindData* findData) const; - + + //------------------------------------------------------------ + // Source control + //------------------------------------------------------------ + + void PushSourceControlChangelist(const char* description); + void PopSourceControlChangelist(); + bool IsSourceControlEnabled() const; + ESourceControlStatus GetSourceControlStatus() const; + ESourceControlStatus SetSourceControlOnline(bool online); + void SetSourceControlStatusCallback(const SourceControlStatusFunc& func); + void CheckoutSourceControlFiles(const char* fileOrDirName, ESearchPath search, ESourceControlFileType fileType); + void AddSourceControlFile(const char* fileName, ESearchPath search, ESourceControlFileType fileType); + + ISourceControl* GetSourceControl() const { return m_sourceControl.Ptr(); } + //------------------------- bool IsInitialized() const { return m_isInit; } @@ -147,7 +163,9 @@ class CFileSystem : public IFileSystem Array m_fsPackages{ PP_SL }; // package serving as FS layers Array m_findDatas{ PP_SL }; - + + ISourceControlPtr m_sourceControl; + bool m_isInit{ false }; }; diff --git a/core/e2core/SourceControl.cpp b/core/e2core/SourceControl.cpp new file mode 100644 index 000000000..713224901 --- /dev/null +++ b/core/e2core/SourceControl.cpp @@ -0,0 +1,288 @@ +////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) Inspiration Byte +// 2009-2025 +////////////////////////////////////////////////////////////////////////////////// +// Description: File system source control provider selection and changelist scopes +////////////////////////////////////////////////////////////////////////////////// + +#include "core/core_common.h" +#include "core/IDkCore.h" +#include "core/ICommandLine.h" +#include "core/ConCommand.h" +#include "utils/KeyValues.h" +#include "FileSystem.h" +#include "SourceControlP4.h" +#include "SourceControl.h" + +DECLARE_CMD(fs_sc_status, "Prints file system source control status", CV_UNREGISTERED) +{ + CFileSystem* fileSystem = static_cast(g_fileSystem.GetInstancePtr()); + ISourceControl* sourceControl = fileSystem->GetSourceControl(); + if (!sourceControl) + { + Msg("Source control is not enabled\n"); + return; + } + sourceControl->PrintStatus(); +} + +DECLARE_CMD(fs_sc_reset, "Drops cached source control file states", CV_UNREGISTERED) +{ + CFileSystem* fileSystem = static_cast(g_fileSystem.GetInstancePtr()); + ISourceControl* sourceControl = fileSystem->GetSourceControl(); + if (sourceControl) + sourceControl->ResetCache(); +} + +//------------------------------------------------------------------------------------------------- + +struct SCChangelistScope +{ + EqString desc; + Array pendingAdds{ PP_SL }; // SC_FILETYPE_AUTO + Array pendingBinaryAdds{ PP_SL }; // SC_FILETYPE_BINARY + Array pendingRemoves{ PP_SL }; + Array pendingReverts{ PP_SL }; + int mergedDepth{ 0 }; // scopes with the same description pushed into this one + + Array& GetPendingAdds(ESourceControlFileType fileType) + { + return (fileType == SC_FILETYPE_BINARY) ? pendingBinaryAdds : pendingAdds; + } +}; + +// changelist scopes are per thread so that loader threads do not write into a scope of another thread +static thread_local Array s_changelistScopes{ PP_SL }; + +void ISourceControl::PushChangelist(EqStringRef description) +{ + EqString desc = ExpandDesc(description); + + // work split into parts that open the same scope inside an outer one is sent by the outer scope + if (s_changelistScopes.numElem() && !s_changelistScopes.back().desc.CompareCaseIns(desc)) + { + ++s_changelistScopes.back().mergedDepth; + return; + } + + SCChangelistScope& scope = s_changelistScopes.append(); + scope.desc = std::move(desc); +} + +void ISourceControl::PopChangelist() +{ + ASSERT_MSG(s_changelistScopes.numElem(), "ISourceControl::PopChangelist - no changelist scope"); + if (!s_changelistScopes.numElem()) + return; + + SCChangelistScope& scope = s_changelistScopes.back(); + if (scope.mergedDepth > 0) + { + --scope.mergedDepth; + return; + } + + // the scope stays current while its files are added + if (scope.pendingAdds.numElem()) + AddFiles(scope.pendingAdds, SC_FILETYPE_AUTO); + + if (scope.pendingBinaryAdds.numElem()) + AddFiles(scope.pendingBinaryAdds, SC_FILETYPE_BINARY); + + if (scope.pendingRemoves.numElem()) + RemoveFiles(scope.pendingRemoves); + + if (scope.pendingReverts.numElem()) + RevertUnchangedFiles(scope.pendingReverts); + + s_changelistScopes.popBack(); + + // release memory so thread exit does not leave allocations behind + if (!s_changelistScopes.numElem()) + s_changelistScopes.clear(true); +} + +EqStringRef ISourceControl::GetScopeChangelistDesc() +{ + if (!s_changelistScopes.numElem()) + return EqStringRef(); + return s_changelistScopes.back().desc; +} + +static void SCAppendUniquePath(Array& paths, EqStringRef fullPath) +{ + for (const EqString& path : paths) + { + if (!path.CompareCaseIns(fullPath)) + return; + } + paths.append(fullPath); +} + +static void SCRemovePath(Array& paths, EqStringRef fullPath) +{ + for (int i = 0; i < paths.numElem(); ++i) + { + if (paths[i].CompareCaseIns(fullPath)) + continue; + + paths.removeIndex(i); + break; + } +} + +bool ISourceControl::DeferScopeAdd(EqStringRef fullPath, ESourceControlFileType fileType) +{ + if (!s_changelistScopes.numElem()) + return false; + + // the last requested file type wins + SCChangelistScope& scope = s_changelistScopes.back(); + SCRemovePath(scope.pendingAdds, fullPath); + SCRemovePath(scope.pendingBinaryAdds, fullPath); + SCAppendUniquePath(scope.GetPendingAdds(fileType), fullPath); + return true; +} + +void ISourceControl::DeferScopeRevertUnchanged(EqStringRef fullPath) +{ + if (!s_changelistScopes.numElem()) + return; + + SCAppendUniquePath(s_changelistScopes.back().pendingReverts, fullPath); +} + +bool ISourceControl::DeferScopeRemove(EqStringRef fullPath) +{ + if (!s_changelistScopes.numElem()) + return false; + + SCAppendUniquePath(s_changelistScopes.back().pendingRemoves, fullPath); + return true; +} + +void ISourceControl::CancelScopeFile(EqStringRef fullPath) +{ + if (!s_changelistScopes.numElem()) + return; + + SCChangelistScope& scope = s_changelistScopes.back(); + SCRemovePath(scope.pendingAdds, fullPath); + SCRemovePath(scope.pendingBinaryAdds, fullPath); + SCRemovePath(scope.pendingRemoves, fullPath); + SCRemovePath(scope.pendingReverts, fullPath); +} + +void ISourceControl::CancelScopeRemove(EqStringRef fullPath) +{ + if (!s_changelistScopes.numElem()) + return; + + SCRemovePath(s_changelistScopes.back().pendingRemoves, fullPath); +} + +void ISourceControl::SetStatus(ESourceControlStatus status, const char* error) +{ + if (Atomic::Exchange(m_status, static_cast(status)) == status && !error) + return; + + if (m_statusCallback) + m_statusCallback(status, error); +} + +EqString ISourceControl::ExpandDesc(EqStringRef description) +{ + EqString desc(description); + + int found = 0; + do { + found = desc.ReplaceSubstr("$APP_NAME$", g_eqCore->GetApplicationName(), false, found); + } while (found != -1); + + return desc; +} + +//------------------------------------------------------------------------------------------------- + +static bool IsSourceControlApplication(const KVSection* scConfig) +{ + const KVSection* appsSec = scConfig ? scConfig->FindSection("Applications") : nullptr; + + // enabled for every application unless the list is given + if (!appsSec) + return true; + + const EqStringRef appName = g_eqCore->GetApplicationName(); + for (EqStringRef name : appsSec->Values()) + { + if (!name.CompareCaseIns(appName)) + return true; + } + + return false; +} + +ISourceControlPtr ISourceControl::Create(const KVSection& coreConfig, const KVSection& fsConfig, EqStringRef workDir) +{ + EqStringRef providerName; + if (!fsConfig.Get("UseSourceControl").GetValues(providerName) || !providerName.Length()) + return nullptr; + + if (g_cmdLine->Find("-nosourcecontrol") != -1) + { + MsgInfo("* Source control: disabled by -nosourcecontrol\n"); + return nullptr; + } + + // 'SourceControl { ... }' section of the provider + const KVSection* scConfig = nullptr; + for (const KVSection& section : coreConfig.Keys("SourceControl")) + { + EqStringRef name; + section.GetValues(name); + if (!name.CompareCaseIns(providerName)) + { + scConfig = §ion; + break; + } + } + + if (!IsSourceControlApplication(scConfig)) + return nullptr; + + ISourceControlPtr sourceControl; + if (!providerName.CompareCaseIns("p4")) + { + EqStringRef changelistDesc = "+auto $APP_NAME$"; + EqStringRef executable; + bool addNewFiles = true; + int connectTimeout = 5; + int commandTimeout = 15; + if (scConfig) + { + scConfig->Get("DefaultChangelistDesc").GetValues(changelistDesc); + scConfig->Get("AddNewFiles").GetValues(addNewFiles); + scConfig->Get("Executable").GetValues(executable); + scConfig->Get("ConnectTimeout").GetValues(connectTimeout); + scConfig->Get("CommandTimeout").GetValues(commandTimeout); + } + + sourceControl = ISourceControlPtr(CRefPtr_new(CSourceControlP4, workDir, executable, ExpandDesc(changelistDesc), addNewFiles, + max(connectTimeout, 1), max(commandTimeout, 1))); + } + else + { + MsgError("Unknown source control provider '%s'\n", providerName.ToCString()); + return nullptr; + } + + MsgInfo("* Source control: %s\n", sourceControl->GetName()); + + if (!fs_sc_status.IsRegistered()) + ConCommandBase::Register(&fs_sc_status); + + if (!fs_sc_reset.IsRegistered()) + ConCommandBase::Register(&fs_sc_reset); + + return sourceControl; +} diff --git a/core/e2core/SourceControl.h b/core/e2core/SourceControl.h new file mode 100644 index 000000000..e0bc2c71f --- /dev/null +++ b/core/e2core/SourceControl.h @@ -0,0 +1,101 @@ +////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) Inspiration Byte +// 2009-2025 +////////////////////////////////////////////////////////////////////////////////// +// Description: File system source control provider interface +////////////////////////////////////////////////////////////////////////////////// + +#pragma once +#include "core/IFileSystem.h" + +struct KVSection; + +class ISourceControl; +using ISourceControlPtr = CRefPtr; + +// Source control provider used by CFileSystem to check out, add, delete and move files +// it writes. All paths are absolute full paths with platform separators. +class ISourceControl : public RefCountedObject +{ +public: + // Creates the provider selected by 'UseSourceControl' of the FileSystem config section. + // Returns nullptr when source control is not configured or not enabled for this application. + static ISourceControlPtr Create(const KVSection& coreConfig, const KVSection& fsConfig, EqStringRef workDir); + + // replaces '$APP_NAME$' with the application name + static EqString ExpandDesc(EqStringRef description); + + // Changelist scopes of the calling thread (see SourceControlScope in IFileSystem.h). + // Files added or removed inside a scope are added or deleted by one command when the scope is + // popped, checked out files written inside a scope are reverted then if their contents did not + // change. A scope pushed with the description of the innermost scope is merged into it. + void PushChangelist(EqStringRef description); + void PopChangelist(); + + ESourceControlStatus GetStatus() const { return static_cast(Atomic::Load(m_status)); } + void SetStatusCallback(const SourceControlStatusFunc& func) { m_statusCallback = func; } + + virtual const char* GetName() const = 0; + + // connects to the server on the first call, false while the provider is offline + virtual bool IsAvailable() = 0; + + // see IFileSystem::SetSourceControlOnline + virtual ESourceControlStatus SetOnline(bool online) = 0; + + // checks out all depot files of the directory recursively and caches the states of its files, + // so that writing many files there does not query the server per file + virtual void CheckoutDir(EqStringRef fullPath) = 0; + + // called before a file is opened for writing: checks out the file if it is in the depot. + // The file type is changed unless it is SC_FILETYPE_AUTO. + virtual void PrepareFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) = 0; + + // called after a file was created: adds the file with the file type if it is not in the depot + virtual void FinishFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) = 0; + + // returns true when the provider removed the file, false to remove it without source control + virtual bool RemoveFile(EqStringRef fullPath) = 0; + + // returns true when the provider renamed the file, false to rename it without source control + virtual bool RenameFile(EqStringRef oldFullPath, EqStringRef newFullPath) = 0; + + // drops cached file states (after changes made outside the application) + virtual void ResetCache() = 0; + virtual void PrintStatus() const = 0; + +protected: + // description of the innermost changelist scope of the calling thread, empty when there is none + static EqStringRef GetScopeChangelistDesc(); + + // queues the file to be added when the innermost scope is popped, false when there is no scope + static bool DeferScopeAdd(EqStringRef fullPath, ESourceControlFileType fileType); + + // queues the checked out file to be reverted if unchanged when the innermost scope is popped + static void DeferScopeRevertUnchanged(EqStringRef fullPath); + + // queues the file to be deleted when the innermost scope is popped, false when there is no scope + static bool DeferScopeRemove(EqStringRef fullPath); + + // removes the file from the add, remove and revert queues of the innermost scope + static void CancelScopeFile(EqStringRef fullPath); + + // removes the file from the remove queue of the innermost scope (file written again) + static void CancelScopeRemove(EqStringRef fullPath); + + // stores the status and calls the status callback + void SetStatus(ESourceControlStatus status, const char* error); + + // adds files queued by a scope with the file type, called before the scope is popped + virtual void AddFiles(ArrayCRef fullPaths, ESourceControlFileType fileType) = 0; + + // reverts files queued by a scope whose contents match the depot, called before the scope is popped + virtual void RevertUnchangedFiles(ArrayCRef fullPaths) = 0; + + // deletes files queued by a scope, the workspace files are already removed + virtual void RemoveFiles(ArrayCRef fullPaths) = 0; + +private: + SourceControlStatusFunc m_statusCallback; + volatile int m_status{ SC_STATUS_NOT_CONNECTED }; +}; diff --git a/core/e2core/SourceControlP4.cpp b/core/e2core/SourceControlP4.cpp new file mode 100644 index 000000000..278752be7 --- /dev/null +++ b/core/e2core/SourceControlP4.cpp @@ -0,0 +1,1557 @@ +////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) Inspiration Byte +// 2009-2025 +////////////////////////////////////////////////////////////////////////////////// +// Description: Perforce source control provider (p4 command line client) +////////////////////////////////////////////////////////////////////////////////// + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#else +#include +#include +#include +#endif + +#include + +#include "core/core_common.h" +#include "SourceControlP4.h" + +using namespace Threading; + +// global options of every p4 call: script output, tagged fields; RunCommand adds the network wait limit +static constexpr const char* P4_GLOBAL_OPTIONS = "-s -ztag -zprog=e2core"; + +// 'add', 'edit' or 'reopen' with the file type option, SC_FILETYPE_AUTO leaves the type to the server +static EqString P4TypedCommand(const char* command, ESourceControlFileType fileType) +{ + if (fileType == SC_FILETYPE_BINARY) + return EqString::Format("%s -t binary", command); + return command; +} + +//------------------------------------------------------------------------------------------------- +// Process runner +//------------------------------------------------------------------------------------------------- + +#ifdef _WIN32 + +// runs a command line, writes 'input' to its stdin and collects stdout and stderr +static bool P4RunProcess(const char* cmdLine, const char* workDir, EqStringRef input, EqString& output, int& exitCode) +{ + SECURITY_ATTRIBUTES secAttrs{}; + secAttrs.nLength = sizeof(secAttrs); + secAttrs.bInheritHandle = TRUE; + + HANDLE outRead = nullptr; + HANDLE outWrite = nullptr; + if (!CreatePipe(&outRead, &outWrite, &secAttrs, 0)) + return false; + + HANDLE inRead = nullptr; + HANDLE inWrite = nullptr; + if (!CreatePipe(&inRead, &inWrite, &secAttrs, 0)) + { + CloseHandle(outRead); + CloseHandle(outWrite); + return false; + } + + // only the child ends are inherited + SetHandleInformation(outRead, HANDLE_FLAG_INHERIT, 0); + SetHandleInformation(inWrite, HANDLE_FLAG_INHERIT, 0); + + STARTUPINFOW startInfo{}; + startInfo.cb = sizeof(startInfo); + startInfo.dwFlags = STARTF_USESTDHANDLES; + startInfo.hStdInput = inRead; + startInfo.hStdOutput = outWrite; + startInfo.hStdError = outWrite; + + EqWString cmdLineW; + AnsiUnicodeConverter(cmdLineW, cmdLine); + + EqWString workDirW; + if (workDir && *workDir) + AnsiUnicodeConverter(workDirW, workDir); + + // CreateProcess may modify the command line buffer + PROCESS_INFORMATION procInfo{}; + const BOOL started = CreateProcessW(nullptr, cmdLineW.GetData(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, + workDirW.Length() ? workDirW.ToCString() : nullptr, &startInfo, &procInfo); + const DWORD startError = started ? 0 : GetLastError(); + + // read returns when all write ends are closed, so close ours + CloseHandle(outWrite); + CloseHandle(inRead); + + if (!started) + { + MsgWarning("p4: can not run '%s' (error %u)\n", cmdLine, startError); + CloseHandle(outRead); + CloseHandle(inWrite); + return false; + } + + // input is small (changelist spec), the child reads it before producing output + if (input.Length()) + { + DWORD bytesWritten = 0; + WriteFile(inWrite, input.ToCString(), static_cast(input.Length()), &bytesWritten, nullptr); + } + + // closed stdin also makes password prompts fail instead of waiting + CloseHandle(inWrite); + + char readBuffer[1024]; + DWORD bytesRead = 0; + while (ReadFile(outRead, readBuffer, sizeof(readBuffer), &bytesRead, nullptr) && bytesRead > 0) + output.Append(readBuffer, static_cast(bytesRead)); + + WaitForSingleObject(procInfo.hProcess, INFINITE); + + DWORD processExitCode = 0; + GetExitCodeProcess(procInfo.hProcess, &processExitCode); + exitCode = static_cast(processExitCode); + + CloseHandle(procInfo.hProcess); + CloseHandle(procInfo.hThread); + CloseHandle(outRead); + + return true; +} + +static EqString P4QuoteArg(EqStringRef arg) +{ + // file paths can not contain quotes on Windows + return EqString::Format("\"%s\"", arg.ToCString()); +} + +static int P4GetFileAccess(EqStringRef fullPath) +{ + EqWString pathW; + AnsiUnicodeConverter(pathW, fullPath); + + const DWORD attributes = GetFileAttributesW(pathW.ToCString()); + if (attributes == INVALID_FILE_ATTRIBUTES) + return -1; + + return (attributes & FILE_ATTRIBUTE_READONLY) ? 0 : 1; +} + +// clears the read-only attribute p4 sets on files that are not opened +static bool P4MakeWritable(EqStringRef fullPath) +{ + EqWString pathW; + AnsiUnicodeConverter(pathW, fullPath); + + const DWORD attributes = GetFileAttributesW(pathW.ToCString()); + if (attributes == INVALID_FILE_ATTRIBUTES) + return false; + + return SetFileAttributesW(pathW.ToCString(), attributes & ~FILE_ATTRIBUTE_READONLY) != FALSE; +} + +// p4.exe from PATH, otherwise from the Perforce installation directory (P4INSTROOT is set by the +// installer). Debugging from Visual Studio replaces PATH with the binaries directory. +static EqString P4FindExecutable() +{ + wchar_t pathW[MAX_PATH]{ 0 }; + if (SearchPathW(nullptr, L"p4.exe", nullptr, MAX_PATH, pathW, nullptr)) + { + EqString path; + AnsiUnicodeConverter(path, pathW); + return path; + } + + static const HKEY s_rootKeys[] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }; + for (HKEY rootKey : s_rootKeys) + { + DWORD size = sizeof(pathW); + if (RegGetValueW(rootKey, L"SOFTWARE\\Perforce\\Environment", L"P4INSTROOT", RRF_RT_REG_SZ, nullptr, pathW, &size) != ERROR_SUCCESS) + continue; + + EqString installDir; + AnsiUnicodeConverter(installDir, pathW); + + const EqString path = fnmPathCombine(installDir, "p4.exe"); + if (P4GetFileAccess(path) != -1) + return path; + } + + return "p4"; +} + +#else + +static bool P4RunProcess(const char* cmdLine, const char* workDir, EqStringRef input, EqString& output, int& exitCode) +{ + int outPipe[2]; + if (pipe(outPipe) != 0) + return false; + + int inPipe[2]; + if (pipe(inPipe) != 0) + { + close(outPipe[0]); + close(outPipe[1]); + return false; + } + + const pid_t pid = fork(); + if (pid == 0) + { + dup2(inPipe[0], STDIN_FILENO); + dup2(outPipe[1], STDOUT_FILENO); + dup2(outPipe[1], STDERR_FILENO); + close(inPipe[0]); + close(inPipe[1]); + close(outPipe[0]); + close(outPipe[1]); + + if (workDir && *workDir && chdir(workDir) != 0) + _exit(127); + + execl("/bin/sh", "sh", "-c", cmdLine, static_cast(nullptr)); + _exit(127); + } + + close(inPipe[0]); + close(outPipe[1]); + + if (pid < 0) + { + close(inPipe[1]); + close(outPipe[0]); + return false; + } + + if (input.Length()) + { + const ssize_t written = write(inPipe[1], input.ToCString(), input.Length()); + (void)written; + } + close(inPipe[1]); + + char readBuffer[1024]; + ssize_t bytesRead = 0; + while ((bytesRead = read(outPipe[0], readBuffer, sizeof(readBuffer))) > 0) + output.Append(readBuffer, static_cast(bytesRead)); + close(outPipe[0]); + + int status = 0; + waitpid(pid, &status, 0); + exitCode = WIFEXITED(status) ? WEXITSTATUS(status) : -1; + + return true; +} + +static EqString P4QuoteArg(EqStringRef arg) +{ + EqString quoted("'"); + for (int i = 0; i < arg.Length(); ++i) + { + if (arg[i] == '\'') + quoted.Append("'\\''"); + else + quoted.Append(arg[i]); + } + quoted.Append('\''); + return quoted; +} + +static int P4GetFileAccess(EqStringRef fullPath) +{ + if (access(fullPath, F_OK) != 0) + return -1; + + return access(fullPath, W_OK) == 0 ? 1 : 0; +} + +static bool P4MakeWritable(EqStringRef fullPath) +{ + struct stat fileStat; + if (stat(fullPath, &fileStat) != 0) + return false; + + return chmod(fullPath, fileStat.st_mode | S_IWUSR) == 0; +} + +// the shell finds p4 in PATH +static EqString P4FindExecutable() +{ + return "p4"; +} + +#endif // _WIN32 + +//------------------------------------------------------------------------------------------------- +// Output parsing +//------------------------------------------------------------------------------------------------- + +static int P4PathHash(EqStringRef path) +{ + constexpr int MULTIPLIER = 37; + + int hash = 0; + for (int i = 0; i < path.Length(); ++i) + { + char chr = path[i]; + if (chr == '/' || chr == '\\') + chr = CORRECT_PATH_SEPARATOR; + hash = MULTIPLIER * hash + static_cast(CType::LowerChar(chr)); + } + + return hash; +} + +static void P4ParseOutput(EqStringRef output, P4Result& result) +{ + int lineStart = 0; + while (lineStart < output.Length()) + { + int lineEnd = output.Find('\n', true, lineStart); + if (lineEnd == -1) + lineEnd = output.Length(); + + const EqStringRef line = output.Mid(lineStart, lineEnd - lineStart).TrimChar('\r', false, true); + lineStart = lineEnd + 1; + + const int prefixEnd = line.Find(':', true); + if (prefixEnd == -1) + { + if (line.Length()) + result.messages.append(line); + continue; + } + + const EqStringRef prefix = line.Left(prefixEnd); + const EqStringRef text = line.Mid(prefixEnd + 1, line.Length() - prefixEnd - 1).TrimSpaces(true, false); + + // tagged fields are 'info1: key value', nested levels are 'info2:'... + if (prefix.Length() > 4 && !prefix.Left(4).Compare("info") && CType::IsDigit(prefix[4])) + { + const int keyEnd = text.Find(' ', true); + + P4Field& field = result.fields.append(); + if (keyEnd == -1) + { + field.key = text; + } + else + { + field.key = text.Left(keyEnd); + field.value = text.Mid(keyEnd + 1, text.Length() - keyEnd - 1); + } + continue; + } + + if (!prefix.Compare("exit")) + { + result.exitCode = atoi(text); + continue; + } + + result.messages.append(line); + } +} + +const P4Field* P4Result::FindField(EqStringRef key) const +{ + for (const P4Field& field : fields) + { + if (!field.key.Compare(key)) + return &field; + } + return nullptr; +} + +bool P4Result::HasMessage(EqStringRef text) const +{ + for (const EqString& message : messages) + { + if (message.Find(text, false) != -1) + return true; + } + return false; +} + +bool P4Result::HasError() const +{ + for (const EqString& message : messages) + { + if (!message.Left(6).Compare("error:")) + return true; + } + return false; +} + +static EP4FileState P4StateFromOpenAction(EqStringRef action) +{ + if (!action.Compare("add") || !action.Compare("move/add") || !action.Compare("branch")) + return P4_FILE_OPENED_ADD; + + if (!action.Compare("delete") || !action.Compare("move/delete")) + return P4_FILE_OPENED_DELETE; + + return P4_FILE_OPENED_EDIT; +} + +static int P4ChangeFromField(const P4Field* field) +{ + if (!field) + return -1; + if (!field->value.Compare("default")) + return 0; + return atoi(field->value); +} + +// state of an fstat record +static P4FileInfo P4FileInfoFromRecord(const P4Field* action, const P4Field* change, const P4Field* headAction) +{ + P4FileInfo info; + if (action) + { + info.state = P4StateFromOpenAction(action->value); + info.change = P4ChangeFromField(change); + } + else if (headAction && (!headAction->value.Compare("delete") || !headAction->value.Compare("move/delete"))) + { + // deleted at head revision, can be added again + info.state = P4_FILE_NOT_IN_DEPOT; + } + else + { + info.state = P4_FILE_SYNCED; + } + return info; +} + +static void P4PrintMessages(const P4Result& result) +{ + for (const EqString& message : result.messages) + MsgWarning("p4: %s\n", message.ToCString()); +} + +//------------------------------------------------------------------------------------------------- +// Provider +//------------------------------------------------------------------------------------------------- + +CSourceControlP4::CSourceControlP4(EqStringRef workDir, EqStringRef executable, EqStringRef defaultChangelistDesc, bool addNewFiles, int connectTimeout, int commandTimeout) + : m_workDir(workDir) + , m_executable(executable) + , m_defaultChangelistDesc(defaultChangelistDesc) + , m_connectTimeout(connectTimeout) + , m_commandTimeout(commandTimeout) + , m_addNewFiles(addNewFiles) +{ +} + +CSourceControlP4::~CSourceControlP4() +{ + if (m_offlineFiles.size()) + MsgWarning("Source control (p4): %d files were written offline, run 'p4 reconcile' for them\n", m_offlineFiles.size()); +} + +// maxWait is the network wait limit in seconds, 0 for the command timeout +bool CSourceControlP4::RunCommand(EqStringRef args, P4Result& result, EqStringRef input, int maxWait) +{ + if (!m_executable.Length()) + m_executable = P4FindExecutable(); + + EqString cmdLine(P4QuoteArg(m_executable)); + cmdLine.AppendFmt(" %s -v net.maxwait=%d ", P4_GLOBAL_OPTIONS, maxWait > 0 ? maxWait : m_commandTimeout); + cmdLine.Append(args); + + DevMsg(DEVMSG_FS, "%s\n", cmdLine.ToCString()); + + EqString output; + result.started = P4RunProcess(cmdLine, m_workDir, input, output, result.exitCode); + if (!result.started) + return false; + + P4ParseOutput(output, result); + return result.Succeeded(); +} + +// returns true and sets the provider offline when the server can not be used at all +bool CSourceControlP4::CheckConnectionError(const P4Result& result) +{ + if (!result.started) + { + GoOffline(EqString::Format("can not run '%s', set 'Executable' in the 'SourceControl p4' section of E2.CONFIG", m_executable.ToCString())); + return true; + } + + static constexpr const char* s_connectionErrors[] = { + "Connect to server failed", + "P4PASSWD", + "session has expired", + "please login", + "Perforce password", + }; + + for (const char* errorText : s_connectionErrors) + { + if (!result.HasMessage(errorText)) + continue; + + P4PrintMessages(result); + GoOffline("can not connect to the server, check P4PORT or run 'p4 login'"); + return true; + } + + return false; +} + +void CSourceControlP4::GoOffline(EqStringRef reason) +{ + if (m_offline) + return; + + m_offline = true; + m_connected = false; + MsgWarning("Source control (p4) is offline: %s\n", reason.ToCString()); + SetStatus(SC_STATUS_OFFLINE, reason); +} + +// remembers a file written, removed or renamed while offline for ReconcileOfflineFiles +void CSourceControlP4::TrackOfflineFile(EqStringRef fullPath) +{ + if (!IsUnderClientRoot(fullPath)) + return; + + P4OfflineFile& file = m_offlineFiles[P4PathHash(fullPath)]; + file.path = fullPath; + file.changelistDesc = GetCurrentChangelistDesc(); +} + +// a file written while offline: p4 sets read-only files that are not opened, the write would fail +void CSourceControlP4::PrepareOfflineFileWrite(EqStringRef fullPath) +{ + if (!IsUnderClientRoot(fullPath)) + return; + + if (P4GetFileAccess(fullPath) == 0) + P4MakeWritable(fullPath); + + TrackOfflineFile(fullPath); +} + +// opens the files written while offline in the changelists they were written for +void CSourceControlP4::ReconcileOfflineFiles() +{ + if (!m_offlineFiles.size()) + return; + + Array changelistDescs{ PP_SL }; + for (auto it = m_offlineFiles.begin(); !it.atEnd(); ++it) + { + bool found = false; + for (const EqString& desc : changelistDescs) + { + if (!desc.CompareCaseIns(it->changelistDesc)) + { + found = true; + break; + } + } + + if (!found) + changelistDescs.append(it->changelistDesc); + } + + // edited, new and removed files, file names are read from stdin + const char* command = m_addNewFiles ? "-x - reconcile" : "-x - reconcile -e -d"; + + int numOpened = 0; + for (const EqString& desc : changelistDescs) + { + EqString fileList; + for (auto it = m_offlineFiles.begin(); !it.atEnd(); ++it) + { + if (it->changelistDesc.CompareCaseIns(desc)) + continue; + + fileList.Append(it->path); + fileList.Append('\n'); + } + + P4Result result; + RunChangelistCommand(desc, command, "", result, fileList); + + // offline again, the files are reconciled by the next attempt + if (m_offline) + return; + + for (const P4Field& field : result.fields) + { + if (!field.key.Compare("clientFile")) + ++numOpened; + } + + // warnings are unchanged files + for (const EqString& message : result.messages) + { + if (!message.Left(6).Compare("error:")) + MsgWarning("p4: %s\n", message.ToCString()); + } + } + + MsgInfo("* Source control: reconciled %d files written offline, %d opened\n", m_offlineFiles.size(), numOpened); + m_offlineFiles.clear(); +} + +bool CSourceControlP4::EnsureConnected() +{ + if (m_offline) + return false; + + if (m_connected) + return true; + + SetStatus(SC_STATUS_CONNECTING, nullptr); + + // a server that can not be reached fails by the short connection timeout + P4Result infoResult; + RunCommand("info", infoResult, nullptr, m_connectTimeout); + if (CheckConnectionError(infoResult)) + return false; + + const P4Field* clientName = infoResult.FindField("clientName"); + const P4Field* clientRoot = infoResult.FindField("clientRoot"); + const P4Field* userName = infoResult.FindField("userName"); + if (!clientName || !userName || !clientName->value.Compare("*unknown*")) + { + P4PrintMessages(infoResult); + GoOffline("no p4 client workspace for the working directory"); + return false; + } + + // 'p4 info' does not require a login, check the ticket explicitly + P4Result loginResult; + RunCommand("login -s", loginResult, nullptr, m_connectTimeout); + if (CheckConnectionError(loginResult)) + return false; + + if (!loginResult.Succeeded()) + { + P4PrintMessages(loginResult); + GoOffline("not logged in, run 'p4 login'"); + return false; + } + + m_clientName = clientName->value; + m_userName = userName->value; + + // 'null' root is used by clients with alternate roots only + if (clientRoot && clientRoot->value.CompareCaseIns("null")) + { + EqString clientRootPath(clientRoot->value); + fnmPathFixSeparators(clientRootPath); + m_clientRoot = clientRootPath.TrimChar(CORRECT_PATH_SEPARATOR, false, true); + } + + m_connected = true; + MsgInfo("* Source control: p4 client '%s', user '%s'\n", m_clientName.ToCString(), m_userName.ToCString()); + SetStatus(SC_STATUS_ONLINE, nullptr); + + return true; +} + +EqStringRef CSourceControlP4::GetCurrentChangelistDesc() const +{ + const EqStringRef scopeDesc = GetScopeChangelistDesc(); + if (scopeDesc.Length()) + return scopeDesc; + return m_defaultChangelistDesc; +} + +// finds a pending changelist with the description or creates a new one +int CSourceControlP4::EnsureChangelist(EqStringRef desc) +{ + const int descHash = P4PathHash(desc); + auto it = m_changelists.find(descHash); + if (!it.atEnd()) + return *it; + + // trimmed copy, temp strings are recycled while parsing long outputs + const EqString wantedDesc(desc.TrimSpaces()); + + P4Result changesResult; + RunCommand(EqString::Format("changes -s pending -l -c %s -u %s", P4QuoteArg(m_clientName).ToCString(), P4QuoteArg(m_userName).ToCString()), changesResult); + if (CheckConnectionError(changesResult)) + return -1; + + // records start with 'change', the first description line comes as 'desc' + int change = -1; + int foundChange = -1; + for (const P4Field& field : changesResult.fields) + { + if (!field.key.Compare("change")) + { + change = atoi(field.value); + continue; + } + + if (field.key.Compare("desc") || change <= 0) + continue; + + if (!field.value.TrimSpaces().CompareCaseIns(wantedDesc)) + { + foundChange = change; + break; + } + } + + if (foundChange <= 0) + { + EqString spec; + spec.AppendFmt("Change:\tnew\n\nClient:\t%s\n\nUser:\t%s\n\nStatus:\tnew\n\nDescription:\n\t%s\n", + m_clientName.ToCString(), m_userName.ToCString(), wantedDesc.ToCString()); + + P4Result createResult; + RunCommand("change -i", createResult, spec); + if (CheckConnectionError(createResult)) + return -1; + + // 'info: Change 1234 created.' + for (const EqString& message : createResult.messages) + { + const int changePos = message.Find("Change ", true); + if (changePos != -1 && message.Find("created", true) != -1) + { + foundChange = atoi(message.ToCString() + changePos + 7); + break; + } + } + + if (foundChange <= 0) + { + P4PrintMessages(createResult); + MsgWarning("p4: can not create changelist '%s'\n", wantedDesc.ToCString()); + return -1; + } + + MsgInfo("p4: created changelist %d '%s'\n", foundChange, wantedDesc.ToCString()); + } + + m_changelists.insert(descHash, foundChange); + return foundChange; +} + +// runs ' -c ' for the current changelist +bool CSourceControlP4::RunChangelistCommand(EqStringRef command, EqStringRef fileArgs, P4Result& result, EqStringRef input) +{ + const EqString desc(GetCurrentChangelistDesc()); + return RunChangelistCommand(desc, command, fileArgs, result, input); +} + +// runs ' -c ' for the changelist with the description +bool CSourceControlP4::RunChangelistCommand(EqStringRef desc, EqStringRef command, EqStringRef fileArgs, P4Result& result, EqStringRef input) +{ + // a cached changelist may be submitted or deleted meanwhile, then look it up again once + for (int attempt = 0; attempt < 2; ++attempt) + { + const int change = EnsureChangelist(desc); + if (change <= 0) + return false; + + result.fields.clear(); + result.messages.clear(); + result.exitCode = -1; + result.started = false; + + RunCommand(EqString::Format("%s -c %d %s", command.ToCString(), change, fileArgs.ToCString()), result, input); + if (CheckConnectionError(result)) + return false; + + const bool staleChange = result.HasMessage(EqString::Format("Change %d", change)) && result.HasError(); + if (!staleChange) + break; + + auto staleIt = m_changelists.find(P4PathHash(desc)); + if (!staleIt.atEnd()) + m_changelists.remove(staleIt); + } + + return result.Succeeded(); +} + +bool CSourceControlP4::IsUnderClientRoot(EqStringRef fullPath) const +{ + if (!m_clientRoot.Length()) + return true; + + if (fullPath.Length() <= m_clientRoot.Length()) + return false; + + if (fullPath[m_clientRoot.Length()] != CORRECT_PATH_SEPARATOR) + return false; + + return !fullPath.Left(m_clientRoot.Length()).CompareCaseIns(m_clientRoot); +} + +const P4KnownDir* CSourceControlP4::FindKnownDir(EqStringRef fullPath) const +{ + for (const P4KnownDir& dir : m_knownDirs) + { + if (fullPath.Length() <= dir.path.Length() || fullPath[dir.path.Length()] != CORRECT_PATH_SEPARATOR) + continue; + + if (!fullPath.Left(dir.path.Length()).CompareCaseIns(dir.path)) + return &dir; + } + return nullptr; +} + +P4FileInfo CSourceControlP4::GetCachedFileInfo(EqStringRef fullPath) const +{ + auto it = m_fileInfos.find(P4PathHash(fullPath)); + + // a checked out directory has all its depot files cached, other files are not in the depot + const P4KnownDir* knownDir = FindKnownDir(fullPath); + if (knownDir && (it.atEnd() || it->generation < knownDir->generation)) + { + P4FileInfo info; + info.state = P4_FILE_NOT_IN_DEPOT; + return info; + } + + if (it.atEnd()) + return P4FileInfo(); + return *it; +} + +void CSourceControlP4::SetFileInfo(EqStringRef fullPath, EP4FileState state, int change) +{ + P4FileInfo info; + info.state = state; + info.change = change; + info.generation = m_cacheGeneration; + m_fileInfos[P4PathHash(fullPath)] = info; +} + +// cached state of a file in a checked out directory, otherwise asks the server +P4FileInfo CSourceControlP4::GetFileInfo(EqStringRef fullPath) +{ + if (FindKnownDir(fullPath)) + return GetCachedFileInfo(fullPath); + return QueryFileInfo(fullPath); +} + +P4FileInfo CSourceControlP4::QueryFileInfo(EqStringRef fullPath) +{ + P4FileInfo info; + + P4Result result; + RunCommand(EqString::Format("fstat %s", P4QuoteArg(fullPath).ToCString()), result); + if (CheckConnectionError(result)) + return info; + + if (result.FindField("depotFile")) + { + info = P4FileInfoFromRecord(result.FindField("action"), result.FindField("change"), result.FindField("headAction")); + } + else if (result.HasMessage("no such file")) + { + info.state = P4_FILE_NOT_IN_DEPOT; + } + else + { + // not under the client root or not in the client view + info.state = P4_FILE_IGNORED; + } + + SetFileInfo(fullPath, info.state, info.change); + return info; +} + +bool CSourceControlP4::OpenForEdit(EqStringRef fullPath, const P4FileInfo& info, ESourceControlFileType fileType) +{ + if (info.state == P4_FILE_OPENED_DELETE && !RevertKeepFile(fullPath)) + return false; + + P4Result result; + if (!RunChangelistCommand(P4TypedCommand("edit", fileType), P4QuoteArg(fullPath), result)) + { + P4PrintMessages(result); + MsgWarning("p4: can not check out '%s'\n", fullPath.ToCString()); + return false; + } + + SetFileInfo(fullPath, P4_FILE_OPENED_EDIT, EnsureChangelist(GetCurrentChangelistDesc())); + DevMsg(DEVMSG_FS, "p4: checked out '%s'\n", fullPath.ToCString()); + + return true; +} + +// adds the file now, or when the changelist scope of the calling thread is popped +void CSourceControlP4::AddNewFile(EqStringRef fullPath, ESourceControlFileType fileType) +{ + if (!m_addNewFiles) + return; + + if (DeferScopeAdd(fullPath, fileType)) + return; + + AddFile(fullPath, fileType); +} + +bool CSourceControlP4::AddFile(EqStringRef fullPath, ESourceControlFileType fileType) +{ + P4Result result; + if (!RunChangelistCommand(P4TypedCommand("add", fileType), P4QuoteArg(fullPath), result)) + { + if (result.HasMessage("ignored file") || result.HasMessage("not in client view")) + { + SetFileInfo(fullPath, P4_FILE_IGNORED, -1); + DevMsg(DEVMSG_FS, "p4: '%s' is ignored\n", fullPath.ToCString()); + return false; + } + + P4PrintMessages(result); + MsgWarning("p4: can not add '%s'\n", fullPath.ToCString()); + return false; + } + + SetFileInfo(fullPath, P4_FILE_OPENED_ADD, EnsureChangelist(GetCurrentChangelistDesc())); + DevMsg(DEVMSG_FS, "p4: added '%s'\n", fullPath.ToCString()); + + return true; +} + +// inside a changelist scope moves an opened file into the scope changelist, setting the file type +bool CSourceControlP4::ReopenToCurrentChangelist(EqStringRef fullPath, const P4FileInfo& info, ESourceControlFileType fileType) +{ + if (!GetScopeChangelistDesc().Length()) + return true; + + const int change = EnsureChangelist(GetCurrentChangelistDesc()); + if (change <= 0 || info.change == change) + return true; + + P4Result result; + if (!RunChangelistCommand(P4TypedCommand("reopen", fileType), P4QuoteArg(fullPath), result)) + { + P4PrintMessages(result); + return false; + } + + SetFileInfo(fullPath, info.state, change); + return true; +} + +bool CSourceControlP4::RevertKeepFile(EqStringRef fullPath) +{ + P4Result result; + RunCommand(EqString::Format("revert -k %s", P4QuoteArg(fullPath).ToCString()), result); + if (CheckConnectionError(result)) + return false; + + if (!result.Succeeded()) + { + P4PrintMessages(result); + return false; + } + + auto it = m_fileInfos.find(P4PathHash(fullPath)); + if (!it.atEnd()) + m_fileInfos.remove(it); + + return true; +} + +bool CSourceControlP4::IsAvailable() +{ + CScopedMutex m(m_mutex); + return EnsureConnected(); +} + +ESourceControlStatus CSourceControlP4::SetOnline(bool online) +{ + CScopedMutex m(m_mutex); + if (!online) + { + if (!m_offline) + { + m_offline = true; + m_connected = false; + MsgInfo("* Source control: p4 is offline\n"); + SetStatus(SC_STATUS_OFFLINE, nullptr); + } + return SC_STATUS_OFFLINE; + } + + if (m_connected) + return SC_STATUS_ONLINE; + + // files may be opened, submitted or reverted outside the application meanwhile + m_offline = false; + m_fileInfos.clear(); + m_changelists.clear(); + m_knownDirs.clear(); + + if (!EnsureConnected()) + return SC_STATUS_OFFLINE; + + ReconcileOfflineFiles(); + return m_offline ? SC_STATUS_OFFLINE : SC_STATUS_ONLINE; +} + +void CSourceControlP4::CheckoutDir(EqStringRef fullPath) +{ + CScopedMutex m(m_mutex); + if (!EnsureConnected() || !IsUnderClientRoot(fullPath)) + return; + + const EqString dirPath(fullPath.TrimChar(CORRECT_PATH_SEPARATOR, false, true)); + const EqString dirWildcard(P4QuoteArg(EqString::Format("%s%c...", dirPath.ToCString(), CORRECT_PATH_SEPARATOR))); + + P4Result fstatResult; + RunCommand(EqString::Format("fstat %s", dirWildcard.ToCString()), fstatResult); + if (CheckConnectionError(fstatResult)) + return; + + const int change = EnsureChangelist(GetCurrentChangelistDesc()); + if (change <= 0) + return; + + struct DirFile + { + EqString path; + P4FileInfo info; + }; + Array dirFiles{ PP_SL }; + + // records start with 'depotFile' + const P4Field* clientFile = nullptr; + const P4Field* action = nullptr; + const P4Field* actionChange = nullptr; + const P4Field* headAction = nullptr; + auto flushRecord = [&]() { + if (clientFile) + { + DirFile& file = dirFiles.append(); + file.path = clientFile->value; + file.info = P4FileInfoFromRecord(action, actionChange, headAction); + } + clientFile = action = actionChange = headAction = nullptr; + }; + + for (const P4Field& field : fstatResult.fields) + { + if (!field.key.Compare("depotFile")) + flushRecord(); + else if (!field.key.Compare("clientFile")) + clientFile = &field; + else if (!field.key.Compare("action")) + action = &field; + else if (!field.key.Compare("change")) + actionChange = &field; + else if (!field.key.Compare("headAction")) + headAction = &field; + } + flushRecord(); + + bool needEdit = false; + bool needReopen = false; + for (const DirFile& file : dirFiles) + { + if (file.info.state == P4_FILE_SYNCED) + needEdit = true; + else if (file.info.state >= P4_FILE_OPENED_EDIT && file.info.change != change) + needReopen = true; + } + + bool edited = false; + if (needEdit) + { + P4Result editResult; + edited = RunChangelistCommand("edit", dirWildcard, editResult); + if (!edited) + P4PrintMessages(editResult); + } + + bool reopened = false; + if (needReopen) + { + P4Result reopenResult; + reopened = RunChangelistCommand("reopen", dirWildcard, reopenResult); + if (!reopened) + P4PrintMessages(reopenResult); + } + + // states stored before are outdated, files missing in the listing are not in the depot + ++m_cacheGeneration; + + int numCheckedOut = 0; + for (const DirFile& file : dirFiles) + { + P4FileInfo info = file.info; + if (info.state == P4_FILE_SYNCED && edited) + { + info.state = P4_FILE_OPENED_EDIT; + info.change = change; + ++numCheckedOut; + } + else if (info.state >= P4_FILE_OPENED_EDIT && reopened) + { + info.change = change; + } + + // a file failed to check out is asked again when it is written + if (info.state == P4_FILE_SYNCED) + continue; + + SetFileInfo(file.path, info.state, info.change); + } + + P4KnownDir* knownDir = nullptr; + for (P4KnownDir& dir : m_knownDirs) + { + if (!dir.path.CompareCaseIns(dirPath)) + { + knownDir = &dir; + break; + } + } + + if (!knownDir) + { + knownDir = &m_knownDirs.append(); + knownDir->path = dirPath; + } + knownDir->generation = m_cacheGeneration; + + DevMsg(DEVMSG_FS, "p4: checked out %d of %d depot files in '%s'\n", numCheckedOut, dirFiles.numElem(), dirPath.ToCString()); +} + +// adds files queued by a changelist scope with one command +void CSourceControlP4::AddFiles(ArrayCRef fullPaths, ESourceControlFileType fileType) +{ + CScopedMutex m(m_mutex); + if (!fullPaths.numElem()) + return; + + EqString fileList; + for (const EqString& fullPath : fullPaths) + { + fileList.Append(fullPath); + fileList.Append('\n'); + } + + // file names are read from stdin + P4Result result; + if (EnsureConnected()) + RunChangelistCommand(P4TypedCommand("-x - add", fileType), "", result, fileList); + + if (m_offline) + { + for (const EqString& fullPath : fullPaths) + TrackOfflineFile(fullPath); + return; + } + + const int change = EnsureChangelist(GetCurrentChangelistDesc()); + + // files not reported are asked again when they are written + for (const EqString& fullPath : fullPaths) + { + auto it = m_fileInfos.find(P4PathHash(fullPath)); + if (!it.atEnd()) + m_fileInfos.remove(it); + } + + int numAdded = 0; + for (const P4Field& field : result.fields) + { + if (field.key.Compare("clientFile")) + continue; + + SetFileInfo(field.value, P4_FILE_OPENED_ADD, change); + ++numAdded; + } + + // 'info: - ignored file can't be added.' + for (const EqString& message : result.messages) + { + const int ignoredPos = message.Find(" - ignored file", true); + const int pathStart = message.Find(": ", true); + if (ignoredPos != -1 && pathStart != -1 && pathStart < ignoredPos) + { + SetFileInfo(message.Mid(pathStart + 2, ignoredPos - pathStart - 2), P4_FILE_IGNORED, -1); + continue; + } + + if (!message.Left(6).Compare("error:") || !message.Left(8).Compare("warning:")) + MsgWarning("p4: %s\n", message.ToCString()); + } + + DevMsg(DEVMSG_FS, "p4: added %d of %d files\n", numAdded, fullPaths.numElem()); +} + +// reverts files of a changelist scope that were checked out and written without changes +void CSourceControlP4::RevertUnchangedFiles(ArrayCRef fullPaths) +{ + CScopedMutex m(m_mutex); + if (!fullPaths.numElem() || !EnsureConnected()) + return; + + EqString fileList; + for (const EqString& fullPath : fullPaths) + { + fileList.Append(fullPath); + fileList.Append('\n'); + } + + // changed files are left opened and not reported + P4Result result; + RunCommand("-x - revert -a", result, fileList); + if (CheckConnectionError(result)) + return; + + // records of reverted files: depotFile, clientFile (local path), oldAction, action 'reverted' + int numReverted = 0; + for (const P4Field& field : result.fields) + { + if (field.key.Compare("clientFile")) + continue; + + SetFileInfo(field.value, P4_FILE_SYNCED, -1); + ++numReverted; + } + + // warnings are files reverted outside the application meanwhile + for (const EqString& message : result.messages) + { + if (!message.Left(6).Compare("error:")) + MsgWarning("p4: %s\n", message.ToCString()); + } + + DevMsg(DEVMSG_FS, "p4: reverted %d unchanged of %d checked out files\n", numReverted, fullPaths.numElem()); +} + +// deletes files removed inside a changelist scope, their workspace files are already removed +void CSourceControlP4::RemoveFiles(ArrayCRef fullPaths) +{ + CScopedMutex m(m_mutex); + if (!fullPaths.numElem()) + return; + + if (!EnsureConnected()) + { + for (const EqString& fullPath : fullPaths) + TrackOfflineFile(fullPath); + return; + } + + // opened files are reverted first, files opened for add are not in the depot + EqString revertList; + EqString deleteList; + for (const EqString& fullPath : fullPaths) + { + const EP4FileState state = GetCachedFileInfo(fullPath).state; + if (state == P4_FILE_OPENED_ADD || state == P4_FILE_OPENED_EDIT) + { + revertList.Append(fullPath); + revertList.Append('\n'); + } + + if (state == P4_FILE_SYNCED || state == P4_FILE_OPENED_EDIT) + { + deleteList.Append(fullPath); + deleteList.Append('\n'); + } + } + + P4Result revertResult; + if (revertList.Length()) + RunCommand("-x - revert -k", revertResult, revertList); + + P4Result deleteResult; + if (!m_offline && deleteList.Length()) + RunChangelistCommand("-x - delete", "", deleteResult, deleteList); + + if (m_offline) + { + for (const EqString& fullPath : fullPaths) + TrackOfflineFile(fullPath); + return; + } + + // files not reported are asked again when they are written + for (const EqString& fullPath : fullPaths) + { + auto it = m_fileInfos.find(P4PathHash(fullPath)); + if (!it.atEnd()) + m_fileInfos.remove(it); + } + + const int change = EnsureChangelist(GetCurrentChangelistDesc()); + + int numDeleted = 0; + for (const P4Field& field : deleteResult.fields) + { + if (field.key.Compare("clientFile")) + continue; + + SetFileInfo(field.value, P4_FILE_OPENED_DELETE, change); + ++numDeleted; + } + + P4PrintMessages(revertResult); + for (const EqString& message : deleteResult.messages) + { + if (!message.Left(6).Compare("error:") || !message.Left(8).Compare("warning:")) + MsgWarning("p4: %s\n", message.ToCString()); + } + + DevMsg(DEVMSG_FS, "p4: deleted %d of %d removed files\n", numDeleted, fullPaths.numElem()); +} + +void CSourceControlP4::PrepareFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) +{ + CScopedMutex m(m_mutex); + if (EnsureConnected()) + PrepareConnectedFileWrite(fullPath, fileType); + + // the server went away while the file was checked out + if (m_offline) + PrepareOfflineFileWrite(fullPath); +} + +void CSourceControlP4::PrepareConnectedFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) +{ + if (!IsUnderClientRoot(fullPath)) + return; + + const int fileAccess = P4GetFileAccess(fullPath); + + // missing files are added or checked out by FinishFileWrite + if (fileAccess == -1) + return; + + P4FileInfo info = GetCachedFileInfo(fullPath); + + // a read-only file can be reverted outside the application, never trust the cache for it + const bool trustCache = (fileAccess == 1) && info.state != P4_FILE_UNKNOWN && info.state != P4_FILE_SYNCED; + if (!trustCache) + info = QueryFileInfo(fullPath); + + switch (info.state) + { + case P4_FILE_SYNCED: + case P4_FILE_OPENED_DELETE: + OpenForEdit(fullPath, info, fileType); + break; + case P4_FILE_OPENED_EDIT: + case P4_FILE_OPENED_ADD: + ReopenToCurrentChangelist(fullPath, info, fileType); + break; + case P4_FILE_NOT_IN_DEPOT: + // existing file the application writes, ignored files are cached once 'p4 add' rejects them + AddNewFile(fullPath, fileType); + break; + default: + break; + } + + // the file is written before the scope is popped, an unchanged one leaves the changelist + if (GetCachedFileInfo(fullPath).state == P4_FILE_OPENED_EDIT) + DeferScopeRevertUnchanged(fullPath); +} + +void CSourceControlP4::FinishFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) +{ + CScopedMutex m(m_mutex); + if (EnsureConnected()) + FinishConnectedFileWrite(fullPath, fileType); + + if (m_offline) + TrackOfflineFile(fullPath); +} + +void CSourceControlP4::FinishConnectedFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) +{ + if (!IsUnderClientRoot(fullPath)) + return; + + // a file removed in this scope and written again stays in the depot + CancelScopeRemove(fullPath); + + // the file did not exist before, a cached state is outdated unless its directory is checked out + const P4FileInfo info = GetFileInfo(fullPath); + switch (info.state) + { + case P4_FILE_NOT_IN_DEPOT: + AddNewFile(fullPath, fileType); + break; + case P4_FILE_SYNCED: + case P4_FILE_OPENED_DELETE: + OpenForEdit(fullPath, info, fileType); + break; + case P4_FILE_OPENED_EDIT: + case P4_FILE_OPENED_ADD: + ReopenToCurrentChangelist(fullPath, info, fileType); + break; + default: + break; + } + + if (GetCachedFileInfo(fullPath).state == P4_FILE_OPENED_EDIT) + DeferScopeRevertUnchanged(fullPath); +} + +bool CSourceControlP4::RemoveFile(EqStringRef fullPath) +{ + CScopedMutex m(m_mutex); + if (EnsureConnected()) + { + if (RemoveConnectedFile(fullPath)) + return true; + } + + // removed by the caller, a read-only file can not be removed on Windows + if (m_offline && IsUnderClientRoot(fullPath)) + { + P4MakeWritable(fullPath); + TrackOfflineFile(fullPath); + } + + return false; +} + +bool CSourceControlP4::RemoveConnectedFile(EqStringRef fullPath) +{ + if (!IsUnderClientRoot(fullPath)) + return false; + + // a file removed before its scope is popped must not be added or reverted + CancelScopeFile(fullPath); + + const P4FileInfo info = GetFileInfo(fullPath); + + // inside a scope the caller removes the file, it is deleted with the other files of the scope + const bool inDepotOrOpened = info.state == P4_FILE_SYNCED || info.state == P4_FILE_OPENED_EDIT || info.state == P4_FILE_OPENED_ADD; + if (inDepotOrOpened && DeferScopeRemove(fullPath)) + { + P4MakeWritable(fullPath); + return false; + } + + switch (info.state) + { + case P4_FILE_OPENED_ADD: + // not in depot yet, file is removed without source control + RevertKeepFile(fullPath); + return false; + case P4_FILE_OPENED_EDIT: + if (!RevertKeepFile(fullPath)) + return false; + break; + case P4_FILE_SYNCED: + break; + default: + return false; + } + + P4Result result; + if (!RunChangelistCommand("delete", P4QuoteArg(fullPath), result)) + { + P4PrintMessages(result); + MsgWarning("p4: can not delete '%s'\n", fullPath.ToCString()); + return false; + } + + SetFileInfo(fullPath, P4_FILE_OPENED_DELETE, EnsureChangelist(GetCurrentChangelistDesc())); + DevMsg(DEVMSG_FS, "p4: deleted '%s'\n", fullPath.ToCString()); + + // 'p4 delete' removes the workspace file, let the caller remove it otherwise + return P4GetFileAccess(fullPath) == -1; +} + +bool CSourceControlP4::RenameFile(EqStringRef oldFullPath, EqStringRef newFullPath) +{ + CScopedMutex m(m_mutex); + if (EnsureConnected()) + { + if (RenameConnectedFile(oldFullPath, newFullPath)) + return true; + } + + // renamed by the caller, the new file is tracked by FinishFileWrite + if (m_offline) + TrackOfflineFile(oldFullPath); + + return false; +} + +bool CSourceControlP4::RenameConnectedFile(EqStringRef oldFullPath, EqStringRef newFullPath) +{ + if (!IsUnderClientRoot(oldFullPath) || !IsUnderClientRoot(newFullPath)) + return false; + + P4FileInfo info = GetFileInfo(oldFullPath); + switch (info.state) + { + case P4_FILE_SYNCED: + // 'p4 move' needs the file opened for edit + if (!OpenForEdit(oldFullPath, info, SC_FILETYPE_AUTO)) + return false; + break; + case P4_FILE_OPENED_EDIT: + case P4_FILE_OPENED_ADD: + break; + default: + // renamed without source control, the new file is added by the caller + CancelScopeFile(oldFullPath); + return false; + } + + P4Result result; + const EqString fileArgs = EqString::Format("%s %s", P4QuoteArg(oldFullPath).ToCString(), P4QuoteArg(newFullPath).ToCString()); + if (!RunChangelistCommand("move", fileArgs, result)) + { + P4PrintMessages(result); + MsgWarning("p4: can not move '%s' to '%s'\n", oldFullPath.ToCString(), newFullPath.ToCString()); + return false; + } + + const int change = EnsureChangelist(GetCurrentChangelistDesc()); + SetFileInfo(oldFullPath, P4_FILE_OPENED_DELETE, change); + SetFileInfo(newFullPath, P4_FILE_OPENED_ADD, change); + DevMsg(DEVMSG_FS, "p4: moved '%s' to '%s'\n", oldFullPath.ToCString(), newFullPath.ToCString()); + + return true; +} + +void CSourceControlP4::ResetCache() +{ + CScopedMutex m(m_mutex); + m_fileInfos.clear(); + m_changelists.clear(); + m_knownDirs.clear(); +} + +void CSourceControlP4::PrintStatus() const +{ + CScopedMutex m(m_mutex); + + Msg("Source control: p4%s\n", m_offline ? " (offline)" : (m_connected ? "" : " (not connected yet)")); + if (m_connected) + { + Msg(" client: %s, user: %s\n", m_clientName.ToCString(), m_userName.ToCString()); + Msg(" client root: %s\n", m_clientRoot.Length() ? m_clientRoot.ToCString() : "(any)"); + } + Msg(" default changelist: '%s'\n", m_defaultChangelistDesc.ToCString()); + Msg(" executable: %s\n", m_executable.Length() ? m_executable.ToCString() : "(not resolved yet)"); + Msg(" add new files: %s\n", m_addNewFiles ? "yes" : "no"); + Msg(" timeouts: connect %d s, commands %d s\n", m_connectTimeout, m_commandTimeout); + Msg(" files written offline: %d\n", m_offlineFiles.size()); + Msg(" cached files: %d, changelists: %d, checked out directories: %d\n", m_fileInfos.size(), m_changelists.size(), m_knownDirs.numElem()); + for (const P4KnownDir& dir : m_knownDirs) + Msg(" %s\n", dir.path.ToCString()); +} diff --git a/core/e2core/SourceControlP4.h b/core/e2core/SourceControlP4.h new file mode 100644 index 000000000..b1852f3d9 --- /dev/null +++ b/core/e2core/SourceControlP4.h @@ -0,0 +1,142 @@ +////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) Inspiration Byte +// 2009-2025 +////////////////////////////////////////////////////////////////////////////////// +// Description: Perforce source control provider (p4 command line client) +////////////////////////////////////////////////////////////////////////////////// + +#pragma once +#include "SourceControl.h" + +enum EP4FileState : int +{ + P4_FILE_UNKNOWN = 0, + P4_FILE_IGNORED, // outside the client root or view, or ignored by P4IGNORE + P4_FILE_NOT_IN_DEPOT, // can be added + P4_FILE_SYNCED, // in depot, not opened + P4_FILE_OPENED_EDIT, + P4_FILE_OPENED_ADD, + P4_FILE_OPENED_DELETE, +}; + +struct P4FileInfo +{ + EP4FileState state{ P4_FILE_UNKNOWN }; + int change{ -1 }; // changelist the file is opened in, 0 is the default changelist + int generation{ 0 }; // cache generation the state was stored in +}; + +// directory checked out by CheckoutDir, its depot files are cached +struct P4KnownDir +{ + EqString path; + int generation{ 0 }; // states stored before this generation are outdated +}; + +// file written, removed or renamed while offline, reconciled when the provider is online again +struct P4OfflineFile +{ + EqString path; + EqString changelistDesc; +}; + +struct P4Field +{ + EqString key; + EqString value; +}; + +// parsed output of 'p4 -s -ztag' +struct P4Result +{ + Array fields{ PP_SL }; // tagged 'infoN: key value' lines + Array messages{ PP_SL }; // 'info:', 'warning:' and 'error:' lines + int exitCode{ -1 }; + bool started{ false }; + + const P4Field* FindField(EqStringRef key) const; + bool HasMessage(EqStringRef text) const; + bool HasError() const; + bool Succeeded() const { return started && exitCode == 0 && !HasError(); } +}; + +class CSourceControlP4 : public ISourceControl +{ +public: + CSourceControlP4(EqStringRef workDir, EqStringRef executable, EqStringRef defaultChangelistDesc, bool addNewFiles, int connectTimeout, int commandTimeout); + ~CSourceControlP4(); + + const char* GetName() const override { return "p4"; } + bool IsAvailable() override; + ESourceControlStatus SetOnline(bool online) override; + + void CheckoutDir(EqStringRef fullPath) override; + void PrepareFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) override; + void FinishFileWrite(EqStringRef fullPath, ESourceControlFileType fileType) override; + bool RemoveFile(EqStringRef fullPath) override; + bool RenameFile(EqStringRef oldFullPath, EqStringRef newFullPath) override; + + void ResetCache() override; + void PrintStatus() const override; + +protected: + void AddFiles(ArrayCRef fullPaths, ESourceControlFileType fileType) override; + void RevertUnchangedFiles(ArrayCRef fullPaths) override; + void RemoveFiles(ArrayCRef fullPaths) override; + +private: + bool RunCommand(EqStringRef args, P4Result& result, EqStringRef input = nullptr, int maxWait = 0); + bool RunChangelistCommand(EqStringRef command, EqStringRef fileArgs, P4Result& result, EqStringRef input = nullptr); + bool RunChangelistCommand(EqStringRef desc, EqStringRef command, EqStringRef fileArgs, P4Result& result, EqStringRef input); + bool CheckConnectionError(const P4Result& result); + + bool EnsureConnected(); + EqStringRef GetCurrentChangelistDesc() const; + int EnsureChangelist(EqStringRef desc); + bool IsUnderClientRoot(EqStringRef fullPath) const; + + const P4KnownDir* FindKnownDir(EqStringRef fullPath) const; + P4FileInfo GetCachedFileInfo(EqStringRef fullPath) const; + void SetFileInfo(EqStringRef fullPath, EP4FileState state, int change); + P4FileInfo QueryFileInfo(EqStringRef fullPath); + P4FileInfo GetFileInfo(EqStringRef fullPath); + + bool OpenForEdit(EqStringRef fullPath, const P4FileInfo& info, ESourceControlFileType fileType); + bool AddFile(EqStringRef fullPath, ESourceControlFileType fileType); + void AddNewFile(EqStringRef fullPath, ESourceControlFileType fileType); + bool ReopenToCurrentChangelist(EqStringRef fullPath, const P4FileInfo& info, ESourceControlFileType fileType); + bool RevertKeepFile(EqStringRef fullPath); + + // stops running commands until SetOnline(true), reason is passed to the status callback + void GoOffline(EqStringRef reason); + void TrackOfflineFile(EqStringRef fullPath); + void PrepareOfflineFileWrite(EqStringRef fullPath); + + // operations while connected, the public ones handle the offline state after them + void PrepareConnectedFileWrite(EqStringRef fullPath, ESourceControlFileType fileType); + void FinishConnectedFileWrite(EqStringRef fullPath, ESourceControlFileType fileType); + bool RemoveConnectedFile(EqStringRef fullPath); + bool RenameConnectedFile(EqStringRef oldFullPath, EqStringRef newFullPath); + void ReconcileOfflineFiles(); + + mutable Threading::CEqMutex m_mutex; + Map m_fileInfos{ PP_SL }; + Map m_changelists{ PP_SL }; // description hash to changelist number + Array m_knownDirs{ PP_SL }; + Map m_offlineFiles{ PP_SL }; // path hash to file + int m_cacheGeneration{ 0 }; + + EqString m_workDir; + EqString m_executable; // found by the first command when not configured + EqString m_defaultChangelistDesc; + EqString m_clientName; + EqString m_clientRoot; + EqString m_userName; + + int m_connectTimeout{ 5 }; // net.maxwait seconds of the connection check + int m_commandTimeout{ 15 }; // net.maxwait seconds of other commands + + bool m_connected{ false }; + bool m_offline{ false }; + bool m_addNewFiles{ true }; +}; diff --git a/core/materialsystem1/MaterialSystem.cpp b/core/materialsystem1/MaterialSystem.cpp index 281da8a10..b5df235c6 100644 --- a/core/materialsystem1/MaterialSystem.cpp +++ b/core/materialsystem1/MaterialSystem.cpp @@ -1556,6 +1556,20 @@ GPUBufferView CMaterialSystem::GetTransientUniformBuffer(const void* data, int64 TransientBufferCollection& collection = m_transientUniformBuffers; + // Buffers are switched only when the current one is full. Views handed out earlier stay valid + // until the ring wraps around, even when their draw is recorded into a command buffer that is + // submitted after many internal buffer commits. + if (data && size > 0) + { + ASSERT(size <= maxTransientBufferSize); + const int64 alignedOffset = AlignBufferSize(collection.bufferOffsets[collection.bufferIdx], bufferAlignment); + if (alignedOffset + size > maxTransientBufferSize) + { + collection.bufferIdx = (collection.bufferIdx + 1) % elementsOf(collection.buffers); + collection.bufferOffsets[collection.bufferIdx] = 0; + } + } + const int bufferIndex = collection.bufferIdx; IGPUBufferPtr& buffer = collection.buffers[bufferIndex]; if (!buffer) @@ -1599,16 +1613,12 @@ void CMaterialSystem::QueueCommitInternalBuffers() buffers.append(submitBuf); } + // transient buffers are written from job threads + CScopedMutex m(s_matSystemBufferMutex); if (m_bufferCmdRecorder) { buffers.append(m_bufferCmdRecorder->End()); m_bufferCmdRecorder = nullptr; - - { - TransientBufferCollection& collection = m_transientUniformBuffers; - collection.bufferOffsets[collection.bufferIdx] = 0; - collection.bufferIdx = (collection.bufferIdx + 1) % elementsOf(collection.buffers); - } m_cameraChangeId += 8192; } } diff --git a/core/materialsystem1/Renderers/Empty/ShaderAPIEmpty.h b/core/materialsystem1/Renderers/Empty/ShaderAPIEmpty.h index 147775251..730826d71 100644 --- a/core/materialsystem1/Renderers/Empty/ShaderAPIEmpty.h +++ b/core/materialsystem1/Renderers/Empty/ShaderAPIEmpty.h @@ -162,6 +162,7 @@ class ShaderAPIEmpty : public ShaderAPI_Base // Synchronization void Flush() {} + void WaitForDeviceIdle() {} //------------------------------------------------------------- // Shaders diff --git a/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.cpp b/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.cpp index 89c723a82..7e37f60c0 100644 --- a/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.cpp +++ b/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.cpp @@ -53,6 +53,8 @@ DECLARE_CVAR_F(nvrhi_validation); static Threading::CEqMutex s_transientHeapsMutex; static Threading::CEqMutex s_cmdListMutex; +// shader modules are created lazily from parallel job threads (async pipeline creation) +static Threading::CEqMutex s_shaderModuleMutex; CNVRHIRenderAPI CNVRHIRenderAPI::Instance; ShaderAPI_Base& ShaderAPI_Base::Instance = CNVRHIRenderAPI::Instance; @@ -101,7 +103,8 @@ void CNVRHIRenderAPI::Shutdown() ASSERT_MSG(m_rhiCommandLists.numElem() == m_rhiFreeCommandLists.numElem(), "Found command lists were not executed. Tell programmer to fix that"); - m_shaderCache.clear(true); + // release the RHI shader modules, plain clear would leak them + ClearShaderPackages(); m_rhiTransientTextureHeaps.clear(true); m_rhiFreeTransientTextureHeaps.clear(true); m_rhiTransientBufferHeaps.clear(true); @@ -661,6 +664,9 @@ IGPUBindGroupPtr CNVRHIRenderAPI::CreateBindGroup(const IGPUComputePipeline* com const ShaderInfo::Module& CNVRHIRenderAPI::GetOrLoadShaderModule(const ShaderInfo& shaderInfo, int shaderModuleIdx, const char* dbgName) const { + // guard against parallel pipeline creation jobs loading the same module twice + CScopedMutex m(s_shaderModuleMutex); + ShaderInfo::Module& mod = const_cast(shaderInfo.modules[shaderModuleIdx]); if (mod.rhiModule) return mod; @@ -1391,3 +1397,14 @@ void CNVRHIRenderAPI::Flush() { g_renderWorker.SignalWork(); } + +void CNVRHIRenderAPI::WaitForDeviceIdle() +{ + if (!m_rhiDevice) + return; + + g_renderWorker.WaitForExecute(__func__, [this]() { + m_rhiDevice->waitForIdle(); + return 0; + }); +} diff --git a/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.h b/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.h index f1c73e0e5..5a2cc07cf 100644 --- a/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.h +++ b/core/materialsystem1/Renderers/NVRHI/NVRHIRenderAPI.h @@ -49,6 +49,7 @@ class CNVRHIRenderAPI : public ShaderAPI_Base // Synchronization void Flush(); + void WaitForDeviceIdle(); //------------------------------------------------------------- // Shaders diff --git a/core/materialsystem1/Renderers/NVRHI/NVRHIStates.cpp b/core/materialsystem1/Renderers/NVRHI/NVRHIStates.cpp index 9e1d2b2ee..47160bbb9 100644 --- a/core/materialsystem1/Renderers/NVRHI/NVRHIStates.cpp +++ b/core/materialsystem1/Renderers/NVRHI/NVRHIStates.cpp @@ -12,8 +12,9 @@ void nvrhiFillSamplerDesc(const SamplerStateParams& samplerParams, nvrhi::Sample ASSERT(samplerParams.maxAnisotropy > 0); rhiSamplerDesc + // comparison is disabled for a standard sampler, but the compare op must still be a valid enum + .setComparisonFunc(samplerParams.compareFunc == COMPFUNC_NONE ? nvrhi::ComparisonFunc::Never : g_nvrhiCompareFunc[samplerParams.compareFunc]) .setReductionType(samplerParams.compareFunc == COMPFUNC_NONE ? nvrhi::SamplerReductionType::Standard : nvrhi::SamplerReductionType::Comparison) - .setComparisonFunc(g_nvrhiCompareFunc[samplerParams.compareFunc]) .setAddressU(g_nvrhiAddressMode[samplerParams.addressU]) .setAddressV(g_nvrhiAddressMode[samplerParams.addressV]) .setAddressW(g_nvrhiAddressMode[samplerParams.addressW]) diff --git a/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.cpp b/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.cpp index 8050318d1..6c09b2119 100644 --- a/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.cpp +++ b/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.cpp @@ -47,8 +47,21 @@ CNVRHISwapChainVK::~CNVRHISwapChainVK() vk::Device vkDevice = CNVRHIRenderLibVK::Instance->m_vkDevice; vk::Instance vkInstance = CNVRHIRenderLibVK::Instance->m_vkInstance; + // the swapchain must be destroyed before the surface it was created from + if (m_vkSwapChain) + { + vkDevice.waitIdle(); + vkDevice.destroySwapchainKHR(m_vkSwapChain); + m_vkSwapChain = nullptr; + } + for(vk::Semaphore& vkImgSemaphore : m_vkPresentSemaphores) vkDevice.destroySemaphore(vkImgSemaphore); + m_vkPresentSemaphores.clear(); + + for(vk::Semaphore& vkImgSemaphore : m_vkAcquireSemaphores) + vkDevice.destroySemaphore(vkImgSemaphore); + m_vkAcquireSemaphores.clear(); m_vkCurrentPresentSemaphore = vk::Semaphore{}; if (m_vkWindowSurface) @@ -73,12 +86,20 @@ void CNVRHISwapChainVK::UpdateBackbufferView() nvrhi::vulkan::DeviceHandle nvrhiDevice = CNVRHIRenderLibVK::Instance->m_nvrhiDevice; vk::Device vkDevice = CNVRHIRenderLibVK::Instance->m_vkDevice; + + // rotate the acquire semaphore: it stays signaled until the next graphics queue submit consumes it + vk::Semaphore& vkAcquireSemaphore = m_vkAcquireSemaphores[m_vkAcquireSemaphoreIndex]; + m_vkAcquireSemaphoreIndex = (m_vkAcquireSemaphoreIndex + 1) % m_vkAcquireSemaphores.numElem(); + const vk::Result res = vkDevice.acquireNextImageKHR(m_vkSwapChain, std::numeric_limits::max(), - m_vkCurrentPresentSemaphore, + vkAcquireSemaphore, vk::Fence(), &m_swapChainBufferIndex); + // consume the acquire semaphore on the next graphics queue submit even if this frame is skipped + nvrhiDevice->queueWaitForSemaphore(nvrhi::CommandQueue::Graphics, vkAcquireSemaphore, 0); + // not sure if this is fine if (res != vk::Result::eNotReady) { @@ -92,8 +113,10 @@ void CNVRHISwapChainVK::UpdateBackbufferView() m_textureRef->m_rhiTexture = m_rhiSwapChainTextures[m_swapChainBufferIndex]; } + // the semaphore is selected by the acquired image index, so it cannot be reused + // while the presented image has not been re-acquired + m_vkCurrentPresentSemaphore = m_vkPresentSemaphores[m_swapChainBufferIndex]; nvrhiDevice->queueSignalSemaphore(nvrhi::CommandQueue::Graphics, m_vkCurrentPresentSemaphore, 0); - nvrhiDevice->queueWaitForSemaphore(nvrhi::CommandQueue::Graphics, m_vkCurrentPresentSemaphore, 0); } void CNVRHISwapChainVK::GetBackbufferSize(int& wide, int& tall) const @@ -162,6 +185,8 @@ bool CNVRHISwapChainVK::UpdateResize() const bool enableSwapChainSharing = queues.numElem() > 1; + const vk::SwapchainKHR vkOldSwapChain = m_vkSwapChain; + auto desc = vk::SwapchainCreateInfoKHR() .setSurface(m_vkWindowSurface) .setMinImageCount(CNVRHIRenderLibVK::Instance->m_swapChainBufferCount) @@ -177,17 +202,20 @@ bool CNVRHISwapChainVK::UpdateResize() .setCompositeAlpha(vk::CompositeAlphaFlagBitsKHR::eOpaque) .setPresentMode(presentMode) .setClipped(true) - .setOldSwapchain(m_vkSwapChain); + .setOldSwapchain(vkOldSwapChain); m_rhiSwapChainTextures.clear(); m_vkSwapChainBuffers.clear(); - + for (vk::Semaphore& vkImgSemaphore : m_vkPresentSemaphores) vkDevice.destroySemaphore(vkImgSemaphore); m_vkPresentSemaphores.clear(); - m_vkCurrentPresentSemaphore = vk::Semaphore{}; - m_vkSwapChain = nullptr; + for (vk::Semaphore& vkImgSemaphore : m_vkAcquireSemaphores) + vkDevice.destroySemaphore(vkImgSemaphore); + m_vkAcquireSemaphores.clear(); + m_vkCurrentPresentSemaphore = vk::Semaphore{}; + m_vkAcquireSemaphoreIndex = 0; m_swapChainBufferIndex = 0; const vk::Result res = vkDevice.createSwapchainKHR(&desc, nullptr, &m_vkSwapChain); @@ -217,8 +245,12 @@ bool CNVRHISwapChainVK::UpdateResize() // Give each swapchain image its own semaphore in case of overlap (e.g.MoltenVK async queue submit) m_vkPresentSemaphores.append(vkDevice.createSemaphore(vk::SemaphoreCreateInfo())); + m_vkAcquireSemaphores.append(vkDevice.createSemaphore(vk::SemaphoreCreateInfo())); } - m_vkCurrentPresentSemaphore = m_vkPresentSemaphores.front(); + + // the old swapchain was retired by the new one and must be destroyed + if (vkOldSwapChain) + vkDevice.destroySwapchainKHR(vkOldSwapChain); m_textureRef->m_rhiTexture = nullptr; @@ -248,10 +280,8 @@ bool CNVRHISwapChainVK::SwapBuffers() const vk::Result res = CNVRHIRenderLibVK::Instance->m_vkPresentQueue.presentKHR(&info); ASSERT_MSG(res == vk::Result::eSuccess || res == vk::Result::eErrorOutOfDateKHR || res == vk::Result::eSuboptimalKHR, "Present failure"); - // cycle the semaphore queue and setup m_PresentSemaphore for the next swapchain image - m_vkPresentSemaphores.popFront(); - m_vkPresentSemaphores.append(m_vkCurrentPresentSemaphore); - m_vkCurrentPresentSemaphore = m_vkPresentSemaphores.front(); + // no semaphore rotation here: m_vkCurrentPresentSemaphore is selected + // by the acquired image index in UpdateBackbufferView return true; } \ No newline at end of file diff --git a/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.h b/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.h index 3a7964c10..09f86a1ad 100644 --- a/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.h +++ b/core/materialsystem1/Renderers/NVRHI/NVRHISwapChainVK.h @@ -46,10 +46,14 @@ class CNVRHISwapChainVK : public ISwapChain Array m_rhiSwapChainTextures{ PP_SL }; Array m_vkSwapChainBuffers{ PP_SL }; + // one signal semaphore per swapchain image, selected by the acquired image index Array m_vkPresentSemaphores{ PP_SL }; + // acquire semaphores are consumed by the next graphics queue submit, so they rotate + Array m_vkAcquireSemaphores{ PP_SL }; vk::Semaphore m_vkCurrentPresentSemaphore; uint m_swapChainBufferIndex{ COM_UINT_MAX }; + uint m_vkAcquireSemaphoreIndex{ 0 }; nvrhi::Format m_swapChainFormat{ nvrhi::Format::RGBA8_UNORM }; CRefPtr m_textureRef; diff --git a/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.cpp b/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.cpp index b936fc373..5c1bf2a25 100644 --- a/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.cpp +++ b/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.cpp @@ -1398,6 +1398,21 @@ void CWGPURenderAPI::Flush() WGPU_INSTANCE_SPIN; } +void CWGPURenderAPI::WaitForDeviceIdle() +{ + g_renderWorker.WaitForExecute(__func__, [this]() { + // wait for all submitted work on the queue + WGPUQueueWorkDoneCallbackInfo rhiCbInfo{}; + rhiCbInfo.mode = WGPUCallbackMode_WaitAnyOnly; + WGPUFuture rhiWorkDoneFuture = wgpuQueueOnSubmittedWorkDone(m_rhiQueue, rhiCbInfo); + + WGPUFutureWaitInfo rhiWaitInfo{}; + rhiWaitInfo.future = rhiWorkDoneFuture; + wgpuInstanceWaitAny(m_rhiInstance, 1, &rhiWaitInfo, UINT64_MAX); + return 0; + }); +} + static void CreateQuerySet() { WGPUQuerySetDescriptor rhiQuerySetDesc = {}; diff --git a/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.h b/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.h index 83185fc2d..a4b37df8f 100644 --- a/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.h +++ b/core/materialsystem1/Renderers/WGPU/WGPURenderAPI.h @@ -44,6 +44,7 @@ class CWGPURenderAPI : public ShaderAPI_Base // Synchronization void Flush(); + void WaitForDeviceIdle(); //------------------------------------------------------------- // Shaders diff --git a/core/materialsystem1/Shaders/Base/SDFFont.cpp b/core/materialsystem1/Shaders/Base/SDFFont.cpp index 098aaedad..af40b9a92 100644 --- a/core/materialsystem1/Shaders/Base/SDFFont.cpp +++ b/core/materialsystem1/Shaders/Base/SDFFont.cpp @@ -51,6 +51,7 @@ BEGIN_SHADER_CLASS( m_shadowParamsVar = GetMaterialVar("ShadowParams", "[0.94 0.95 0, 1]"); m_shadowColor = GetMaterialVar("ShadowColor", "[0 0 0 1]"); m_shadowOffset = GetMaterialVar("ShadowOffset", "[0 0]"); + m_msdfParams = GetMaterialVar("MsdfParams", "[0 0 0 0]"); } SHADER_INIT_RESOURCES() @@ -174,6 +175,7 @@ BEGIN_SHADER_CLASS( bufferData.append(m_shadowColor.Get()); bufferData.append(m_shadowParamsVar.Get()); bufferData.append(Vector4D(m_shadowOffset.Get(), 0.0f)); + bufferData.append(m_msdfParams.Get()); GPUBufferView cameraParamsBuffer; for (const RenderBufferInfo& rendBuffer : setupParams.uniformBuffers) @@ -220,4 +222,5 @@ BEGIN_SHADER_CLASS( MatVec4Proxy m_shadowColor; MatVec4Proxy m_shadowParamsVar; MatVec2Proxy m_shadowOffset; + MatVec4Proxy m_msdfParams; END_SHADER_CLASS \ No newline at end of file diff --git a/docs/architecture.md b/docs/architecture.md index 0c3894723..fa8922502 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -80,7 +80,7 @@ Public interfaces are declared in `public/` and implemented in `core/` or `share | `public/{audio,font,input,physics,render,egf}/` | Public headers of the corresponding `shared/` libraries | | `shared/dpk/` | DPK package and ZIP readers/writer | | `shared/font/` | Font loading, caching and text layout | -| `shared/render/` | Render utilities: debug overlay, particles, decals, sprites, render lists, blur/sort compute | +| `shared/render/` | Render utilities: debug overlay, particles, decals, sprites, render lists, blur/sort compute, full-screen FXAA / SMAA | | `shared/grim/` | GRIM — GPU-driven rendering instance manager | | `shared/studiofile/`, `shared/studio/` | EGF model loading and studio geometry cache | | `shared/egf/` | EGF/motion package generator and source model loaders (ESM, FBX, OBJ) — tools only | @@ -202,7 +202,7 @@ All are static libraries using `public` and `shared` include paths. |---|---|---| | `dpkLib` | `dpk/**` | (lz4, zlib) | | `fontLib` | `font/**` | — | -| `renderUtilLib` | `render/**` | — | +| `renderUtilLib` | `render/**` | (smaa) | | `grimLib` | `grim/**` | renderUtilLib (imgui) | | `studioFileLib` | `studiofile/**` | (bullet2, zlib) | | `studioLib` | `studio/**` | studioFileLib | diff --git a/docs/codestyle.md b/docs/codestyle.md index f08cb3580..c3ef8342e 100644 --- a/docs/codestyle.md +++ b/docs/codestyle.md @@ -112,6 +112,9 @@ The reference shows the general layout; where it disagrees with this document, t ones in the narrowest scope (class or function). - Use `const` for locals that are not modified after initialization. - Initialize members in the class declaration with brace initializers: `int m_fileIdx{ -1 };`. +- In size computations take `sizeof` of the variable or element rather than of its type, when + possible: `arr.numElem() * sizeof(arr[0])`, not `arr.numElem() * sizeof(ushort)`. The size then + follows a change of the element type. ## Engine facilities diff --git a/docs/framework_math.md b/docs/framework_math.md index ad5d35388..5b0ad4140 100644 --- a/docs/framework_math.md +++ b/docs/framework_math.md @@ -145,6 +145,11 @@ Implemented in `Utility.cpp`: - `IsPointInCone`. - 2D: `LineIntersectsLine2D`, `LineSegIntersectsLineSeg2D`, `LineSegIntersectsCircle2D`, `DistancePointPoly2D`, `ConvexHull2D`, `RectangleRectangleSeparation` (negative when overlapping). +- 2D polygons: `PolygonSignedArea2D` (positive counter-clockwise with Y up, as the `ConvexHull2D` + hull), `PolygonConvexPartition2D` (a simple polygon of any winding split into convex parts of + limited size, as point indices: ear clipping, then Hertel-Mehlhorn merging; false for a + self-crossing or degenerate polygon), `PolylineMiterOffsets2D` (per-point offsets of a unit half + width strip along a polyline, mitered at the bends). - 3D distances: `DistanceLineLine`, `DistanceLineSegLineSeg` (optional closest-point outputs). - Angles (degrees unless the name says `Rad`/`PI`): `ConstrainAngle180`, `ConstrainAnglePI`, `NormalizeAngles180`, `AngleDiff`, `AngleDiffRad`, `AnglesDiff`, `VecAngleDiff`. diff --git a/docs/public_filesystem.md b/docs/public_filesystem.md index b85043d84..0ce1c2ab2 100644 --- a/docs/public_filesystem.md +++ b/docs/public_filesystem.md @@ -70,3 +70,116 @@ while (fsFind.Next()) `GetPath()` returns the name of the current entry, `GetDirIndex()` the search path index it came from. + +## Source control + +In tools and the editor the file system can keep written files under source control. Only +Perforce (`p4`) is supported; it runs the `p4` command line client found in `PATH`, in the +Perforce installation directory (`P4INSTROOT`, Windows) or set by `Executable`. Profile and Retail +builds do not create the provider. +The implementation is in `core/e2core/SourceControl.cpp` and `core/e2core/SourceControlP4.cpp`. + +Configuration in `E2.CONFIG`: + +``` +FileSystem +{ + ... + UseSourceControl p4; // provider name, selects the section below +} + +SourceControl p4 +{ + Applications "LevelEditor" "egfMan" "texcooker"; // optional, all applications when missing + DefaultChangelistDesc "+auto $APP_NAME$"; // pending changelist for written files + AddNewFiles 1; // optional, 'p4 add' new files (default 1) + Executable "C:/Program Files/Perforce/p4.exe"; // optional, path to p4 + ConnectTimeout 5; // optional, net.maxwait seconds of the connection check (default 5) + CommandTimeout 15; // optional, net.maxwait seconds of other commands (default 15) +} +``` + +- The provider is created in `Init` (`ISourceControl::Create`) when `UseSourceControl` is set and the application name is in + `Applications`; `-nosourcecontrol` on the command line disables it. The `editorMode` argument of + `Init` is not used for this. +- `p4` runs in the base directory, so the client, port and user come from `P4CONFIG`, `p4 set` or + the environment. The connection is checked on the first write or `IsSourceControlEnabled` + (`p4 info`, `p4 login -s` with `ConnectTimeout`); when the server is not reachable, the user is + not logged in or a command loses the connection, a warning is printed and the provider goes + offline (see [Online and offline](#online-and-offline)). +- Only files under the client root are handled. Files outside the client view or matched by + `P4IGNORE` are written as usual. + +| Operation | Perforce action | +|---|---| +| `Open` with `FS_OPEN_WRITE` / `FS_OPEN_APPEND`, file in depot | `p4 edit` before the file is opened | +| `Open` for writing creates a file | `p4 add` (or `p4 edit` if the file is in depot) | +| `FileCopy` | goes through `Open` | +| `FileRemove`, file in depot | `p4 delete`; a file opened for add is reverted and removed; inside a scope the file is removed at once and deleted when the scope ends | +| `Rename`, file in depot | `p4 edit` + `p4 move`; other files are renamed and added, directories are renamed without source control | +| `CheckoutSourceControlFiles`, file | `p4 edit`, for writers that do not use `Open` (`CDPKFileWriter::Begin`) | +| `CheckoutSourceControlFiles`, directory | `p4 fstat`, `p4 edit` and `p4 reopen` of `/...`: all depot files of the directory | +| `AddSourceControlFile` | `p4 add` of a file created without `Open` (`p4 edit` if it is in depot) | + +`CheckoutSourceControlFiles` (for a file) and `AddSourceControlFile` take an +`ESourceControlFileType`. With `SC_FILETYPE_BINARY` the file is added, checked out and reopened +with `-t binary`, which also retypes a depot file stored as text. Files written by `Open` use +`SC_FILETYPE_AUTO`: the depot file keeps its type and the server detects the type of a new file +by its contents, so it can take a binary file for text and convert its line endings on sync. +`CDPKFileWriter` passes `SC_FILETYPE_BINARY` for packages (`.epk`, `.shd`). + +`IsSourceControlEnabled` connects on the first call and tells whether written files are handled +(online). `GetSourceControlStatus` returns the status without connecting. + +A directory checked out with `CheckoutSourceControlFiles` keeps the states of all its depot files +in the cache, so writing files there does not query the server per file; a file missing in its +listing is treated as not in depot. Checking out the directory again refreshes the listing. + +Files are opened in a pending changelist with the `DefaultChangelistDesc` description +(`$APP_NAME$` is replaced with the application name). An existing pending changelist of the client +and user with the same description is reused, otherwise it is created on the first file. + +`SourceControlScope` sends the files of the calling thread to another changelist while it exists. +Scopes nest; a file already opened in another changelist is moved to the scope changelist with +`p4 reopen`. A scope pushed with the description of the innermost scope of the thread is merged +into it and sends nothing when it ends: + +```cpp +SourceControlScope scScope("+export %s", levelName.ToCString()); +SaveLevelFiles(levelName); // files go to the '+export ' changelist +``` + +New files written inside a scope are added by one `p4 -x - add` per file type when the scope ends; outside a +scope each file is added at once. Files removed inside a scope are removed from the disk at once +(made writable first) and deleted by one `p4 -x - revert -k` of the opened ones and one +`p4 -x - delete` when the scope ends; a removed file written again in the scope stays in the depot. +Checked out files written inside a scope are passed to one `p4 -x - revert -a` when the scope ends, +so files written without changes leave the changelist; outside a scope they stay opened. A +changelist left without files is not deleted. Work split across frames or jobs opens the scope again +in each part; the changelist is found by its description. To send all parts with one command each, +keep an outer scope with the same description open on the thread that runs the parts +(`PushSourceControlChangelist` before the first part, `PopSourceControlChangelist` after the last); +the level editor save does this. + +File states are cached. `fs_sc_status` prints the provider state, `fs_sc_reset` drops the cache +after changes made in P4V or the command line (a read-only file is always queried again). + +### Online and offline + +`ESourceControlStatus` is `SC_STATUS_NONE` without a provider, `SC_STATUS_NOT_CONNECTED` until the +first connection, then `SC_STATUS_CONNECTING`, `SC_STATUS_ONLINE` or `SC_STATUS_OFFLINE`. + +- A failure (no server within `ConnectTimeout` or `CommandTimeout`, no login, no client workspace, + `p4` not found) sets the provider offline. Only the first failing command waits for the timeout, + later writes run no commands. +- `SetSourceControlOnline(false)` goes offline without a failure; `SetSourceControlOnline(true)` + drops the cached states and changelists, connects (blocking up to `ConnectTimeout`) and runs + `p4 -x - reconcile` (`-e -d` without `AddNewFiles`) of the files written offline, grouped by the + changelist description they were written with. +- While offline a written file that exists and is read-only is made writable, so the write does not + fail, and every written, removed or renamed file under the client root is remembered for the + reconcile. Files remembered at shutdown are reported with a warning. +- `SetSourceControlStatusCallback` receives every status change; `error` is the reason when the + provider went offline because of a failure and `nullptr` otherwise. It is called on the thread + that ran the command with the provider locked, so it must only store the state (the level editor + opens a prompt on the next frame). diff --git a/docs/public_rendering_overview.md b/docs/public_rendering_overview.md index 2db6bebf9..60bf94d0b 100644 --- a/docs/public_rendering_overview.md +++ b/docs/public_rendering_overview.md @@ -70,6 +70,11 @@ Main responsibilities: `SetupOrtho`, `GetViewProjection`, `GetCameraParams`; `SetFogInfo`. - **Helpers:** `GetDynamicMesh()`, `GetTransientUniformBuffer`, `GetWhiteTexture`, `GetErrorCheckerboardTexture`, `GetShaderAPI()` (RHI access). +- **Transient uniform data:** `GetTransientUniformBuffer(data, size)` is thread safe and returns a + view into a ring of eight 512 KB buffers. The data is written by a command buffer that is queued + before the next submitted command buffers, so the view is valid in any command buffer submitted + later; the ring moves to the next buffer only when the current one is full, so a view stays + valid until about 4 MB more transient data is written. - **Device loss:** `AddDestroyLostCallbacks` / `RemoveLostRestoreCallbacks`. ## Materials — `IMaterial.h`, `IMaterialVar.h` @@ -285,6 +290,51 @@ const float width = font->GetStringWidth(L"Some text", params); `TEXT_STYLE_USE_TAGS`, `TEXT_STYLE_SCISSOR`, `TEXT_STYLE_UPPERCASE`, `TEXT_STYLE_LOWERCASE`. - Tags (with `TEXT_STYLE_USE_TAGS`): `&#RRGGBB;text&;` — colored text; `&;` closes the tag. Only color is supported. +- Kerning pairs of `eqfont2` fonts are applied by `GetStringWidth` and text drawing. - Custom layouts: implement `ITextLayoutBuilder` (`OnNewLine`, `LayoutChar`) and set it in `params.layoutBuilder`; `CRectangleTextLayoutBuilder` (`shared/font/FontLayoutBuilders.h`) wraps text inside a rectangle. + +### Font file formats + +`CFont::LoadFont` detects the format by the root section of the `.fnt` file: + +| Root section | Source | Rendering | +|---|---|---| +| `Font` | fixed grid bitmap font | legacy SDF | +| `eqfont` | customized BMFont (`isSDF 1` for its outer-distance SDF) | bitmap or legacy SDF | +| `eqfont2` | msdf-atlas-gen + `tools/fontconv/msdf_to_eqfont.py` ([tools_overview.md](tools_overview.md)) | MSDF / MTSDF / SDF | + +All of them are drawn with the `SDFFont` shader (`_sdfRegular` material of the font cache); +`FontParams.w` selects the legacy or the `eqfont2` path. + +### eqfont2 (MSDF) fonts + +``` +eqfont2 +{ + type "mtsdf"; // msdf, mtsdf, sdf or psdf + texture "ui/fonts/roboto"; // atlas texture, materials path + size 48; // atlas pixels per em + distanceRange 8; // distance range in atlas pixels + lineheight 1.171875; // metrics are in ems + ascender 0.927734; + descender 0.244141; + // unicode advance [plane left top right bottom] [atlas left top right bottom] + glyph 32 0.249023; + glyph 33 0.271973 -0.027018 -0.802083 0.306315 0.09375 1899.5 530.5 1915.5 573.5; + kern 65 86 -0.05; // first second advance (ems) +} +``` + +- Plane bounds use a Y-down axis with the origin at the baseline; atlas bounds are in atlas + pixels from the top-left corner. Glyphs without bounds (spaces) only advance. +- `FontStyleParam::scale` is the em size in screen pixels; line height, baseline offset, advances + and kerning are scaled by it. +- The edge is antialiased over one screen pixel at any scale (the shader derives the screen-space + distance range from `distanceRange` and texture coordinate derivatives), so one atlas covers all + text sizes. +- `textWeight` moves the edge outwards (bolder) in normalized distance, `0.5` is the whole outer + half of the range. `shadowWeight` expands and softens the shadow by the same amount; + `shadowOffset` is in screen pixels. MTSDF fonts draw the shadow from the true SDF in alpha. +- `r_font_sdfStart` / `r_font_sdfRange` affect only legacy `eqfont` SDF fonts. diff --git a/docs/rendering_matsystem_shaders_overview.md b/docs/rendering_matsystem_shaders_overview.md index f998cc9c9..585e69361 100644 --- a/docs/rendering_matsystem_shaders_overview.md +++ b/docs/rendering_matsystem_shaders_overview.md @@ -198,6 +198,11 @@ shader BaseUnlit - `VertexLayouts` — vertex formats to compile for; each must have a Slang layout in `VertexLayouts/.h`. The name must equal the C++ vertex format name: the pipeline uses `StringId24` of the format name as `shaderVertexLayoutId`. + A layout without a `VertexLayouts/.h` file only selects a shader variant + (`CURRENT_VERTEX_ID == VID_`): ShaderCooker does not add the `IVertex` / `VertexLayout` + modules for it, so such a shader must not `import VertexLayout` (e.g. the SMAA passes in + `SMAA.def`, picked by `RenderPipelineDesc::ShaderVertexLayoutId`). Compute shaders never get + these modules. - Every nested section with `define` adds a switchable define; ShaderCooker compiles all combinations of defines for every vertex layout (`SkipCombo` excludes combinations). Section names (`Pass`, `MatVar`, `BlendMode`) only group and document the defines. diff --git a/docs/sys_application_overview.md b/docs/sys_application_overview.md index fe9be39bf..237d30e5a 100644 --- a/docs/sys_application_overview.md +++ b/docs/sys_application_overview.md @@ -148,6 +148,37 @@ frame the loop sleeps `sys_sleep` ms (default 1). 7. `EndScene`: `g_matSystem->EndFrame()`. 8. Pending fullscreen toggle, `g_onEndFrame`. +### Full-screen anti-aliasing — `shared/render/EdgeAA.cpp` + +`CRenderFullScreenEdgeAA` (`g_fullScreenEdgeAA`) anti-aliases the back buffer after the scene and +EqUI, before the debug overlay, console and ImGui. It is skipped while a state viewport is visible +(the viewport target is not a storage texture). `r_aa` selects the mode: + +| `r_aa` | Mode | Passes | +|---|---|---| +| 0 | Off | — | +| 1 | FXAA 3.11 | `EdgeAA` compute: back buffer → `_rt_AAFramebuffer` with luma in alpha; `EdgeAA` material draws into the back buffer (`r_fxaaQualitySubpix`, `r_fxaaQualityEdgeThreshold`, `r_fxaaQualityEdgeThresholdMin`) | +| 2 (default) | SMAA 1x | Same back buffer copy, then `SMAA` shader passes `EdgeDetection` (luma edges → `_rt_SMAAEdges`, RG8), `BlendingWeight` (→ `_rt_SMAABlend`, RGBA8), `NeighborhoodBlending` into the back buffer | + +- `r_aa` is declared in `sys_host.cpp`: `CGameHost::InitSystems` passes it to `Init(mode)`, and its + change callback calls `SetMode(mode)`, which re-creates the resources (`Shutdown` + `Init`) once + initialized. `Init` creates only the resources of the mode (none for 0, FXAA ones for 1, FXAA and + SMAA ones for 2). +- `PreRender(cmdRecorder)` records the copy and, for SMAA, the edge and weight passes; + `Render(rendPassRecorder)` draws the final pass inside the host's back buffer render pass. +- SMAA tuning: `r_smaaThreshold` (0.1), `r_smaaMaxSearchSteps` (16), `r_smaaMaxSearchStepsDiag` (8), + `r_smaaCornerRounding` (25) — the defaults are the SMAA `HIGH` preset, `0.05` / `32` / `16` / `25` + is `ULTRA`. They are written into the `SMAAParams` uniform buffer every frame. +- The SMAA shader is E2Base `shadersSRC/SMAA.def` / `source_SMAA.fx`; the pass is a vertex layout + without a layout file (`EdgeDetection`, `BlendingWeight`, `NeighborhoodBlending`), selected by + `ShaderVertexLayoutId` of the render pipeline. `common/smaa.h` is the unmodified reference + `SMAA.hlsl` (iryoku/smaa, MIT), set up for Slang at the top of `source_SMAA.fx`. Every texture is + sampled with one linear clamp sampler at level 0, because WGSL forbids implicit-derivative sampling + after the `discard` of the edge detection pass. The C++ `SMAAParams` must match the shader struct. +- The area and search lookup textures come from `src_dependency/smaa` (`AreaTex.h`, `SearchTex.h`) + and are created at `Init` as procedural textures `_smaaAreaTex` (RG8) and `_smaaSearchTex` (R8). +- If the SMAA pipelines cannot be created, `r_aa 2` falls back to FXAA. + The ImGui renderer backend (`imgui_impl_matsystem.cpp`) draws through material system dynamic meshes, which hold up to 32767 vertices and indices. It sets `ImGuiBackendFlags_RendererHasVtxOffset`, so ImGui can put more than 65535 vertices into one draw list with 16-bit indices, and splits draw diff --git a/docs/tools_overview.md b/docs/tools_overview.md index 2228994b3..c66c393e7 100644 --- a/docs/tools_overview.md +++ b/docs/tools_overview.md @@ -16,8 +16,9 @@ commands and ConVars can be passed with `+name value` on the command line. | `egfMan2` | GUI (sysLib + ImGui, Windows) | `.egf`, `.mop`, `.pod` | — (viewer) | | `fcompress` | console | `PackageCooker.CONFIG` | `.epk` packages | | `shadercooker` | console | `ShaderCooker.CONFIG` + Slang shader sources | Shader packages for RHI backends (`.shd`, DPK format) | -| `texcooker` | console | `TextureCooker.CONFIG` + materials + source images + `.atl` atlas descriptions | Batch-converted textures (`.dds`) via external converters, texture atlases (`.atlas` + material) | +| `texcooker` | console | `TextureCooker.CONFIG` + materials + source images + `.atl` atlas descriptions + `.font` font descriptions | Batch-converted textures (`.dds`) via external converters, texture atlases (`.atlas` + material), `eqfont2` fonts | | `localization_tool` | GUI (C#, WinForms) | `resources/text_/*.txt` | Edited localization files | +| `fontconv/msdf_to_eqfont.py` | Python 3 script | msdf-atlas-gen JSON layout + PNG atlas | `eqfont2` `.fnt` files, uncompressed RGBA8 `.dds` atlas | ## egfCA — EGF compiler (`tools/egfca`) @@ -149,6 +150,78 @@ The tool writes the packed source image, a material (`.mat`) and an `.atlas` fil rectangles (`atlasgroup`); the packed image is then converted like any other texture. Materials with an `.atlas` file expose the rectangles through `IMaterial::GetAtlas()`. +### Font atlases + +Font description files (`.font`) in the source materials folder are processed in the same pass +as `.atl` files (`FontAtlasGenerator.cpp`). Glyph outlines and kerning (GPOS or `kern` table) are +read with `stb_truetype`, distance fields are generated with msdfgen (`src_dependency/msdfgen`, +core only) and glyphs are packed with `stb_rect_pack` into the smallest power of two atlas. + +``` +texture "ui/roboto_msdf"; // atlas TGA in the source materials folder, no extension +type mtsdf; // mtsdf (default), msdf, sdf, psdf +size 48; // atlas pixels per em +pxrange 8; // distance range in atlas pixels +padding 2; // extra pixels around each glyph box +charset "charset.txt" // charset files relative to the .font file (optional) +{ + chars "№€"; // inline entries (optional), same keys as in charset files +} +font "roboto_msdf" "ttf/Roboto-Regular.ttf"; // +font "roboto_msdf_b" "ttf/Roboto-Bold.ttf"; // all fonts share one atlas +``` + +- The charset is key-values: each value of the `charset` key is a charset file (key-values, UTF-8, + BOM allowed) relative to the `.font` file, and keys of the `charset` section are added inline. + Charset files and the section use the same keys; order and duplicates do not matter: + + ``` + chars " !\"#$%&'()*+,-./0123456789:;<=>?@"; // every character of the strings + range "A" "Z"; // first and last: a single character or a number + range 0x400 0x45F; + code 0x2116 8364; // code points, decimal or 0x hex + ``` + + A one-character `range` bound is the character itself, so `"0" "9"` are digits. Inside a string + `\"` is a quote and `\n`, `\r`, `\t` are control characters; a backslash cannot end a string + (add it with `code 0x5C`). +- The tool writes the RGBA8 atlas TGA to `/.tga` and one `eqfont2` + file per `font` entry to `/resources/fonts/.fnt` (format: + [public_rendering_overview.md](public_rendering_overview.md#eqfont2-msdf-fonts)). +- Each atlas texture is added to `/_fonts.mat` with `usage:fonts` (existing + entries are kept; the file is written only when an entry is added or changed), so the batch + conversion pass converts the atlas to DDS. The `fonts` usage must keep the texture uncompressed + and without mipmaps (`-f BGRA -m 1` for texconv): block compression breaks the distance channels. +- Glyph quads include `pxrange / 2 + padding` pixels around the outline, which leaves room for + `textWeight` and the shadow offset. + +## MSDF font converter (`tools/fontconv`) + +`msdf_to_eqfont.py` converts the output of +[msdf-atlas-gen](https://github.com/Chlumsky/msdf-atlas-gen) (v1.4) into `eqfont2` font files +(format: [public_rendering_overview.md](public_rendering_overview.md#eqfont2-msdf-fonts)). + +``` +msdf-atlas-gen -font Roboto-Regular.ttf -charset charset.txt -fontname Roboto-Regular + -and -font Roboto-Bold.ttf -charset charset.txt -fontname Roboto-Bold + -type mtsdf -size 48 -pxrange 8 -outerpxpadding 2 -yorigin top -potr + -format png -imageout roboto.png -json roboto.json +python msdf_to_eqfont.py roboto.json roboto.png -texture ui/fonts/roboto + -fntout /resources/fonts -ddsout /materials/ui/fonts/roboto.dds +``` + +- `-type mtsdf` is preferred: RGB holds the MSDF for sharp edges, alpha holds a true SDF used for + shadows. `msdf`, `sdf` and `psdf` atlases are accepted as well. +- Several fonts joined with `-and` share one atlas; the script writes one `.fnt` per font, named + by its `-fontname`. With a single font the name comes from `-name`, `-fontname` or the JSON file + name. +- `-pxrange` limits how far `textWeight` / `shadowWeight` can move the edge (half the range on + each side); `-outerpxpadding` keeps shadow samples (offset by `shadowOffset`) from reaching + neighbor glyphs. +- The atlas is written as uncompressed RGBA8 DDS: block compression breaks the distance channels. +- msdf-atlas-gen reads kerning only from the legacy `kern` table, so fonts with GPOS-only kerning + (for example Roboto) get no kerning pairs. + ## Localization tool (`tools/localization_tool`) C# WinForms application (`EqLocalizationTool.sln`, built separately from premake) for editing diff --git a/framework/ds/Array.h b/framework/ds/Array.h index 8066b642f..3c843b4e0 100644 --- a/framework/ds/Array.h +++ b/framework/ds/Array.h @@ -262,7 +262,7 @@ class BufferedArrayStorage : FixedArrayStorage T* getData() { return m_dynamicStorage.getData() ? m_dynamicStorage.getData() : FixedStorage::getData(); } const T* getData() const { return m_dynamicStorage.getData() ? m_dynamicStorage.getData() : FixedStorage::getData(); } - const PPSourceLine getSL() const { return m_dynamicStorage.m_sl; } + const PPSourceLine getSL() const { return m_dynamicStorage.getSL(); } DynamicArrayStorage& getDynamicStorage() { return m_dynamicStorage; } const DynamicArrayStorage& getDynamicStorage() const { return m_dynamicStorage; } diff --git a/framework/math/Utility.cpp b/framework/math/Utility.cpp index 8bae27d0f..6898d80df 100644 --- a/framework/math/Utility.cpp +++ b/framework/math/Utility.cpp @@ -535,6 +535,230 @@ void ConvexHull2D(Array& points, Array& hull) hull.setNum(k - 1); } +float PolygonSignedArea2D(ArrayCRef points) +{ + const int numPoints = points.numElem(); + if (numPoints < 3) + return 0.0f; + + // shoelace formula + float doubleArea = 0.0f; + for (int i = 0, j = numPoints - 1; i < numPoints; j = i++) + doubleArea += points[j].x * points[i].y - points[i].x * points[j].y; + + return doubleArea * 0.5f; +} + +bool IsPointInPolygon2D(ArrayCRef points, const Vector2D& point) +{ + const int numPoints = points.numElem(); + if (numPoints < 3) + return false; + + // crossings of the ray from the point along +X + bool inside = false; + for (int i = 0, j = numPoints - 1; i < numPoints; j = i++) + { + const Vector2D& a = points[i]; + const Vector2D& b = points[j]; + if ((a.y > point.y) != (b.y > point.y) && point.x < (b.x - a.x) * (point.y - a.y) / (b.y - a.y) + a.x) + inside = !inside; + } + return inside; +} + +bool PolygonConvexPartition2D(ArrayCRef points, int maxPartPoints, Array& outPartIndices, Array& outPartSizes) +{ + outPartIndices.clear(); + outPartSizes.clear(); + + const int numPoints = points.numElem(); + const float signedArea = PolygonSignedArea2D(points); + if (numPoints < 3 || maxPartPoints < 3 || fabsf(signedArea) < F_EPS) + return false; + + // the turns are measured relative to the polygon size + AARectangle bounds; + for (const Vector2D& point : points) + bounds.AddVertex(point); + const float eps = max(lengthSqr(bounds.GetSize()), 1.0f) * 1e-6f; + + // working in the positive winding + Array remaining(PP_SL); + remaining.reserve(numPoints); + for (int i = 0; i < numPoints; ++i) + remaining.append(signedArea > 0.0f ? i : numPoints - 1 - i); + + auto isInsideTriangle = [&](int a, int b, int c, int pointIdx) { + const Vector2D& p = points[pointIdx]; + if (distanceSqr(p, points[a]) < eps || distanceSqr(p, points[b]) < eps || distanceSqr(p, points[c]) < eps) + return false; + + return ConvexOrient2D(points[a], points[b], p) >= -eps + && ConvexOrient2D(points[b], points[c], p) >= -eps + && ConvexOrient2D(points[c], points[a], p) >= -eps; + }; + + // ear clipping, a collinear point is dropped when there is no ear + using Part = FixedArray; + Array parts(PP_SL); + while (remaining.numElem() > 3) + { + const int count = remaining.numElem(); + int earIdx = -1; + int collinearIdx = -1; + for (int i = 0; i < count && earIdx == -1; ++i) + { + const int a = remaining[(i + count - 1) % count]; + const int b = remaining[i]; + const int c = remaining[(i + 1) % count]; + + const float turn = ConvexOrient2D(points[a], points[b], points[c]); + if (turn <= eps) + { + if (turn >= -eps && collinearIdx == -1) + collinearIdx = i; + continue; + } + + bool isEar = true; + for (int j = 0; j < count && isEar; ++j) + { + const int other = remaining[j]; + if (other != a && other != b && other != c && isInsideTriangle(a, b, c, other)) + isEar = false; + } + + if (isEar) + earIdx = i; + } + + if (earIdx == -1 && collinearIdx == -1) + return false; + + if (earIdx != -1) + { + Part& triangle = parts.append(); + triangle.append(remaining[(earIdx + count - 1) % count]); + triangle.append(remaining[earIdx]); + triangle.append(remaining[(earIdx + 1) % count]); + remaining.removeIndex(earIdx); + } + else + { + remaining.removeIndex(collinearIdx); + } + } + + if (ConvexOrient2D(points[remaining[0]], points[remaining[1]], points[remaining[2]]) > eps) + { + Part& triangle = parts.append(); + triangle.append(remaining[0]); + triangle.append(remaining[1]); + triangle.append(remaining[2]); + } + + if (!parts.numElem()) + return false; + + auto isConvex = [&](const Part& part) { + const int count = part.numElem(); + for (int i = 0; i < count; ++i) + { + if (ConvexOrient2D(points[part[i]], points[part[(i + 1) % count]], points[part[(i + 2) % count]]) < -eps) + return false; + } + return true; + }; + + // merging the parts over their shared diagonals while they stay convex + const int maxPoints = min(maxPartPoints, Part().numAllocated()); + bool merged = true; + while (merged) + { + merged = false; + for (int p = 0; p < parts.numElem() && !merged; ++p) + { + for (int q = p + 1; q < parts.numElem() && !merged; ++q) + { + const Part& partP = parts[p]; + const Part& partQ = parts[q]; + const int countP = partP.numElem(); + const int countQ = partQ.numElem(); + if (countP + countQ - 2 > maxPoints) + continue; + + // the diagonal goes u -> v in P and v -> u in Q + for (int i = 0; i < countP && !merged; ++i) + { + const int u = partP[i]; + const int v = partP[(i + 1) % countP]; + for (int j = 0; j < countQ && !merged; ++j) + { + if (partQ[j] != v || partQ[(j + 1) % countQ] != u) + continue; + + // P from v around to u, then Q after u up to v + Part mergedPart; + for (int k = 0; k < countP; ++k) + mergedPart.append(partP[(i + 1 + k) % countP]); + for (int k = 2; k < countQ; ++k) + mergedPart.append(partQ[(j + k) % countQ]); + + if (!isConvex(mergedPart)) + break; + + parts[p] = mergedPart; + parts.fastRemoveIndex(q); + merged = true; + } + } + } + } + } + + for (const Part& part : parts) + { + outPartIndices.append(part.ptr(), part.numElem()); + outPartSizes.append(part.numElem()); + } + return true; +} + +void PolylineMiterOffsets2D(ArrayCRef points, Array& outOffsets) +{ + const int numPoints = points.numElem(); + outOffsets.setNum(numPoints); + if (numPoints < 2) + { + for (Vector2D& offset : outOffsets) + offset = vec2_zero; + return; + } + + auto getSegmentDir = [&](int segmentIdx) { + return normalize(points[segmentIdx + 1] - points[segmentIdx]); + }; + + for (int i = 0; i < numPoints; ++i) + { + Vector2D dir; + float miterScale = 1.0f; + if (i == 0) + dir = getSegmentDir(0); + else if (i == numPoints - 1) + dir = getSegmentDir(numPoints - 2); + else + { + const Vector2D segmentDir = getSegmentDir(i); + dir = normalize(getSegmentDir(i - 1) + segmentDir); + miterScale = 1.0f / max(dot(dir, segmentDir), 0.5f); + } + + outOffsets[i] = Vector2D(dir.y, -dir.x) * miterScale; + } +} + float RectangleAlongAxisSeparation( const Vector2D& pos, const Vector2D& axis, float extent, const Vector2D& otherDir, const Vector2D& otherPerpDir, const Vector2D& otherExtents) diff --git a/framework/math/Utility.h b/framework/math/Utility.h index 9ec7efea6..9ed21503c 100644 --- a/framework/math/Utility.h +++ b/framework/math/Utility.h @@ -59,6 +59,25 @@ float DistanceLineSegLineSeg(const Vector3D& L0, const Vector3D& L1, const Vecto void ConvexHull2D(Array& points, Array& out); +// signed area of the closed polygon: positive when its points go counter-clockwise with X to the right +// and Y up (clockwise when Y goes down, as on screen), the hull of ConvexHull2D is positive +float PolygonSignedArea2D(ArrayCRef points); + +// the point is inside the closed polygon of any winding (even-odd rule), false for less than 3 points +bool IsPointInPolygon2D(ArrayCRef points, const Vector2D& point); + +// splits the simple polygon (any winding, no self crossings) into convex parts of at most maxPartPoints +// points: ear clipping, then the diagonals are removed while the merged parts stay convex (Hertel-Mehlhorn). +// outPartIndices holds the polygon point indices of the parts one after another, outPartSizes their +// counts; the parts have a positive PolygonSignedArea2D, collinear points may be dropped. +// False when the polygon is degenerate or crosses itself, the outputs are empty then +bool PolygonConvexPartition2D(ArrayCRef points, int maxPartPoints, Array& outPartIndices, Array& outPartSizes); + +// offsets of the polyline points across it for a strip of unit half width: the normal (dir.y, -dir.x) of +// the segment direction at the ends, along the bisector at a bend and longer there to keep the strip +// width (the scale is limited to 2 at sharp bends); zero offsets for a polyline of less than 2 points +void PolylineMiterOffsets2D(ArrayCRef points, Array& outOffsets); + //--------------------------------------------------------------------------------- float RectangleAlongAxisSeparation(const Vector2D& pos, const Vector2D& axis, float extent, diff --git a/framework/math/Vector.inl b/framework/math/Vector.inl index 38f41b4d3..69008fb72 100644 --- a/framework/math/Vector.inl +++ b/framework/math/Vector.inl @@ -189,7 +189,7 @@ inline bool operator == (const TVec2D &u, const TVec2D &v) template inline bool operator != (const TVec2D& u, const TVec2D& v) { - return (u.x != v.x && u.y != v.y); + return (u.x != v.x || u.y != v.y); } /* --------------------------------------------------------------------------------- */ @@ -360,7 +360,7 @@ inline bool operator == (const TVec3D &u, const TVec3D &v) template inline bool operator != (const TVec3D& u, const TVec3D& v) { - return (u.x != v.x && u.y != v.y && u.z != v.z); + return (u.x != v.x || u.y != v.y || u.z != v.z); } template @@ -563,7 +563,7 @@ inline bool operator == (const TVec4D &u, const TVec4D &v) template inline bool operator != (const TVec4D& u, const TVec4D& v) { - return (u.x != v.x && u.y != v.y && u.z != v.z && u.w != v.w); + return (u.x != v.x || u.y != v.y || u.z != v.z || u.w != v.w); } /* --------------------------------------------------------------------------------- */ diff --git a/framework/math/math_common.h b/framework/math/math_common.h index 24d661b70..66b14abe9 100644 --- a/framework/math/math_common.h +++ b/framework/math/math_common.h @@ -157,17 +157,14 @@ inline float RemapVal(float val, float A, float B, float C, float D) } // Remap a value in the range [A,B] to [C,D] and clamp result to [C,D] +// Either range may be descending (B < A or D < C). An empty range A == B gives C below A, D otherwise. inline float RemapValClamp(float val, float A, float B, float C, float D) { - if (D < C) - { - QuickSwap(A, B); - QuickSwap(C, D); - } + if (A == B) + return val < A ? C : D; - if (val < A) return C; - if (val > B) return D; - return RemapVal(val, A, B, C, D); + const float t = min(max((val - A) / (B - A), 0.0f), 1.0f); + return C + (D - C) * t; } template diff --git a/public/core/IFileSystem.h b/public/core/IFileSystem.h index fed86d031..ec0854819 100644 --- a/public/core/IFileSystem.h +++ b/public/core/IFileSystem.h @@ -22,6 +22,30 @@ enum ESearchPath : int SP_IGNORE_PACKAGE = (1 << 4), // ignore files from packages }; +// file type a file is added or checked out with by the source control +enum ESourceControlFileType : int +{ + SC_FILETYPE_AUTO = 0, // type of the depot file, new files are detected by the server + SC_FILETYPE_BINARY, // binary, no line ending conversion on sync + + SC_FILETYPE_COUNT +}; + +// state of the file system source control +enum ESourceControlStatus : int +{ + SC_STATUS_NONE = 0, // not configured for the application + SC_STATUS_NOT_CONNECTED, // configured, connects on the first write or IsSourceControlEnabled + SC_STATUS_CONNECTING, + SC_STATUS_ONLINE, + SC_STATUS_OFFLINE, // server not reachable or not logged in, or set offline by the application +}; + +// Called when the source control status changes. 'error' is the reason when the provider went +// offline because of a failure, nullptr otherwise. Called on the thread that ran the command +// with the provider locked: the callback must only store the state and must not use the file system. +using SourceControlStatusFunc = EqFunction; + struct FSFindData; //------------------------------------------------------------------------------ @@ -32,7 +56,7 @@ class IFileSystem : public IEqCoreModule { friend class CFileSystemFind; public: - CORE_INTERFACE("E2_Filesystem_011") + CORE_INTERFACE("E2_Filesystem_012") // Initialization of filesystem virtual bool Init(bool bEditorMode) = 0; @@ -100,6 +124,38 @@ class IFileSystem : public IEqCoreModule // opens package for further reading. Does not add package as FS layer. virtual IPackFileReaderPtr OpenPackage(const char* packageName, int searchFlags = -1) = 0; + //------------------------------------------------------------ + // Source control + //------------------------------------------------------------ + + // files written, removed or renamed by the calling thread go to the pending changelist + // with this description until the scope is popped. Use SourceControlScope. + virtual void PushSourceControlChangelist(const char* description) = 0; + virtual void PopSourceControlChangelist() = 0; + + // true when written files are checked out and added, connects to the server on the first call + virtual bool IsSourceControlEnabled() const = 0; + + // current status, does not connect + virtual ESourceControlStatus GetSourceControlStatus() const = 0; + + // Online connects to the server (blocks up to the connect timeout) and reconciles files written + // while offline. Offline runs no commands: written read-only files are made writable and + // remembered for the reconcile. Returns the resulting status. + virtual ESourceControlStatus SetSourceControlOnline(bool online) = 0; + + // set before other threads write files, nullptr removes the callback + virtual void SetSourceControlStatusCallback(const SourceControlStatusFunc& func) = 0; + + // checks out a depot file, or all depot files of a directory recursively, into the current + // changelist. Writing many files of a checked out directory does not query the server per file. + // Also used by writers which do not open files with Open. + // A checked out file gets the file type unless it is SC_FILETYPE_AUTO, directories ignore it. + virtual void CheckoutSourceControlFiles(const char* fileOrDirName, ESearchPath search, ESourceControlFileType fileType = SC_FILETYPE_AUTO) = 0; + + // adds a file created without Open with the file type, or checks it out when it is in the depot + virtual void AddSourceControlFile(const char* fileName, ESearchPath search, ESourceControlFileType fileType = SC_FILETYPE_AUTO) = 0; + protected: //------------------------------------------------------------ // Locator @@ -180,3 +236,37 @@ inline bool CFileSystemFind::Next() return (m_fd && m_curPath); } + +//----------------------------------------------------------------------------------------- +// Source control changelist scope +//----------------------------------------------------------------------------------------- + +// Files written by the calling thread while the scope exists go to the pending changelist +// with the given description, it is created on the first file and reused when it exists. +// New and removed files are added and deleted by one command when the scope ends. +// A scope with the description of the innermost scope of the thread is merged into it, so work +// split into parts that open the scope again sends its files when the outer scope ends. +// Does nothing when source control is not enabled. +// +// SourceControlScope scScope("+export %s", levelName.ToCString()); +class SourceControlScope +{ +public: + SourceControlScope(const char* descFormat, ...) + { + va_list argList; + va_start(argList, descFormat); + const EqString description(EqString::FormatV(descFormat, argList)); + va_end(argList); + + g_fileSystem->PushSourceControlChangelist(description); + } + + ~SourceControlScope() + { + g_fileSystem->PopSourceControlChangelist(); + } + + SourceControlScope(const SourceControlScope&) = delete; + SourceControlScope& operator=(const SourceControlScope&) = delete; +}; diff --git a/public/materialsystem1/renderers/IGPUBuffer.h b/public/materialsystem1/renderers/IGPUBuffer.h index 1fbfbeace..1ef4bc795 100644 --- a/public/materialsystem1/renderers/IGPUBuffer.h +++ b/public/materialsystem1/renderers/IGPUBuffer.h @@ -60,15 +60,15 @@ template static T NextBufferOffset(T writeSize, T& offset, T bufferSize, int alignment = 4) { ASSERT(writeSize < bufferSize); - if (offset + writeSize > bufferSize) + + // aligned offset must fit, not the previous end + const T writeOffset = (offset + (alignment - 1)) & ~(alignment - 1); + if (writeOffset + writeSize > bufferSize) { - ASSERT_FAIL("Exceeded buffer size %d, needed %d", bufferSize, offset + writeSize); + ASSERT_FAIL("Exceeded buffer size %d, needed %d", bufferSize, writeOffset + writeSize); return 0; } - offset = (offset + (alignment - 1)) & ~(alignment - 1); - - const T writeOffset = offset; - offset += (writeSize + (alignment - 1)) & ~(alignment - 1); + offset = writeOffset + ((writeSize + (alignment - 1)) & ~(alignment - 1)); return writeOffset; } \ No newline at end of file diff --git a/public/materialsystem1/renderers/IShaderAPI.h b/public/materialsystem1/renderers/IShaderAPI.h index d09b8b167..2c187cd27 100644 --- a/public/materialsystem1/renderers/IShaderAPI.h +++ b/public/materialsystem1/renderers/IShaderAPI.h @@ -110,6 +110,9 @@ class IShaderAPI //------------------------------------------------------------- virtual void Flush() = 0; + // waits until all queued GPU work has been completed. Use before freeing resources + // that can still be referenced by the queued command buffers + virtual void WaitForDeviceIdle() = 0; //------------------------------------------------------------- // Buffer management diff --git a/shared/dpk/DPKFileWriter.cpp b/shared/dpk/DPKFileWriter.cpp index e3bcb4752..ba7fca359 100644 --- a/shared/dpk/DPKFileWriter.cpp +++ b/shared/dpk/DPKFileWriter.cpp @@ -57,9 +57,14 @@ bool CDPKFileWriter::Begin(const char* fileName, ESearchPath searchPath) if (m_output.IsOpen()) return false; + // package is written without IFileSystem::Open, check it out and add it explicitly as binary + g_fileSystem->CheckoutSourceControlFiles(fileName, searchPath, SC_FILETYPE_BINARY); + if (!m_output.Open(g_fileSystem->GetAbsolutePath(searchPath, fileName), COSFile::WRITE)) return false; + g_fileSystem->AddSourceControlFile(fileName, searchPath, SC_FILETYPE_BINARY); + memset(&m_header, 0, sizeof(m_header)); m_header.version = DPK_VERSION; m_header.signature = DPK_SIGNATURE; diff --git a/shared/font/Font.cpp b/shared/font/Font.cpp index ed087dc34..f8faeb5c2 100644 --- a/shared/font/Font.cpp +++ b/shared/font/Font.cpp @@ -28,10 +28,24 @@ DECLARE_CVAR(r_font_sdfRange, "0.06", nullptr, CV_CHEAT); static bool IsVisibleChar( int ch ) { - return (ch != '\n') && + return (ch != '\n') && (ch != '\r'); } +static int ApplyCharCase( int chrId, const FontStyleParam& params ) +{ + if (params.styleFlag & TEXT_STYLE_UPPERCASE) + return CType::UpperChar(chrId); + else if (params.styleFlag & TEXT_STYLE_LOWERCASE) + return CType::LowerChar(chrId); + return chrId; +} + +static uint KerningPairKey( int firstChrId, int secondChrId ) +{ + return (static_cast(firstChrId & 0xFFFF) << 16) | static_cast(secondChrId & 0xFFFF); +} + //----------------------------------------------------------------------------------------- class CPlainTextLayoutBuilder : public ITextLayoutBuilder @@ -104,7 +118,6 @@ static CPlainTextLayoutBuilder s_defaultTextLayout; //----------------------------------------------------------------------------------------- CFont::CFont() { - memset(&m_flags, 1, sizeof(m_flags)); } float CFont::GetStringWidth( const char* str, const FontStyleParam& params, int charCount, int breakOnChar) const @@ -125,7 +138,7 @@ float CFont::GetStringWidth( const wchar_t* str, const FontStyleParam& params, i float CFont::GetLineHeight( const FontStyleParam& params ) const { - if(m_flags.sdf) // only scale SDF characters + if(IsDistanceField()) // only scale SDF characters return m_lineHeight * params.scale.y; return m_lineHeight; @@ -133,12 +146,25 @@ float CFont::GetLineHeight( const FontStyleParam& params ) const float CFont::GetBaselineOffs( const FontStyleParam& params ) const { - if(m_flags.sdf) // only scale SDF characters + if(IsDistanceField()) // only scale SDF characters return m_baseline * params.scale.y; return m_baseline; } +float CFont::GetKerning( int firstChrId, int secondChrId, const FontStyleParam& params ) const +{ + if (m_kerning.isEmpty() || firstChrId == 0) + return 0.0f; + + auto it = m_kerning.find(KerningPairKey(ApplyCharCase(firstChrId, params), ApplyCharCase(secondChrId, params))); + if (it.atEnd()) + return 0.0f; + + // kerning is stored in ems, same as advance + return *it * params.scale.x; +} + enum class ECharMode { NORMAL = 0, @@ -244,6 +270,7 @@ float CFont::_GetStringWidth( const CHAR_T* str, const FontStyleParam& params, i float totalWidth = 0.0f; int count = 0; + int prevChr = 0; auto AddCharWidth = [&](const CHAR_T* str, const FontStyleParam& fontStyle) -> bool { if (count >= charCount) return false; @@ -252,7 +279,13 @@ float CFont::_GetStringWidth( const CHAR_T* str, const FontStyleParam& params, i return false; if (!IsVisibleChar(*str)) + { + prevChr = 0; return true; + } + + totalWidth += GetKerning(prevChr, *str, fontStyle); + prevChr = *str; FontChar chr; GetScaledCharacter(chr, *str, fontStyle); @@ -421,6 +454,7 @@ void CFont::BuildCharVertexBuffer(CMeshBuilder& builder, const CHAR_T* str, cons Vector2D curStartPos = textPos; bool hasNewLine = true; int lineNumber = 0; + int prevChr = 0; auto DrawFontCharacter = [&](const CHAR_T* str, const FontStyleParam& fontStyle) -> bool { // @@ -436,12 +470,16 @@ void CFont::BuildCharVertexBuffer(CMeshBuilder& builder, const CHAR_T* str, cons { lineNumber++; hasNewLine = true; + prevChr = 0; return true; } if (!IsVisibleChar(*str)) return true; + curStartPos.x += GetKerning(prevChr, *str, fontStyle); + prevChr = *str; + // // Render part - text filling // @@ -453,7 +491,7 @@ void CFont::BuildCharVertexBuffer(CMeshBuilder& builder, const CHAR_T* str, cons Vector2D cPos(curStartPos.x + chr.ofsX, curStartPos.y - baseLine + chr.ofsY); Vector2D cSize(chr.x1 - chr.x0, chr.y1 - chr.y0); - if (m_flags.sdf) // only scale SDF characters + if (IsDistanceField()) // only scale SDF characters cSize *= m_scale * fontStyle.scale; if (!layoutBuilder->LayoutChar(fontStyle, (void*)str, isWideChar, chr, curStartPos, cPos, cSize)) @@ -574,16 +612,50 @@ void CFont::SetupDrawTextMeshBuffer(RenderDrawCmd& drawCmd, const FontStyleParam MatVec4Proxy& shadowColor = fontCache->m_shadowColor; MatVec4Proxy& shadowSdfRange = fontCache->m_shadowParams; MatVec2Proxy& shadowOffset = fontCache->m_shadowOffset; + MatVec4Proxy& msdfParams = fontCache->m_msdfParams; baseColor.Set(color_white); + if (IsMSDF()) + { + // FontParams.w >= 2 selects the eqfont2 distance field path in the shader (3 - use alpha SDF). + // Weights are offsets of the edge in normalized distance (0.5 is the glyph edge) + sdfRange.Set(Vector4D(params.textWeight, 0.0f, 1.0f, 2.0f)); + msdfParams.Set(Vector4D(m_distanceRange * m_invTexSize, 0.0f, 0.0f)); + + if ((params.styleFlag & TEXT_STYLE_SHADOW) && params.shadowAlpha > 0.0f) + { + // shadow offset is in screen pixels, convert it to atlas texture coordinates + const Vector2D screenPixelsPerAtlasPixel = m_scale * params.scale; + const Vector2D atlasPixelsPerScreenPixel(1.0f / max(screenPixelsPerAtlasPixel.x, F_EPS), 1.0f / max(screenPixelsPerAtlasPixel.y, F_EPS)); + shadowColor.Set(ColorRGBA(params.shadowColor, params.shadowAlpha)); + shadowOffset.Set(params.shadowOffset * atlasPixelsPerScreenPixel * m_invTexSize); + + // shadow weight expands and softens the shadow like legacy SDF fonts do. + // MTSDF keeps a true SDF in alpha which has no corner artifacts when expanded + shadowSdfRange.Set(Vector4D(params.shadowWeight, params.shadowWeight, 0.0f, (m_type == FONT_TYPE_MTSDF) ? 3.0f : 2.0f)); + } + else + shadowColor.Set(vec4_zero); + + MatSysDefaultRenderPass defaultRenderPass; + defaultRenderPass.blendMode = SHADER_BLEND_TRANSLUCENT; + defaultRenderPass.texture = m_fontTexture; + + RenderPassContext defaultPassContext(rendPassRecorder, &defaultRenderPass); + g_matSystem->SetupDrawCommand(drawCmd, defaultPassContext); + return; + } + + msdfParams.Set(vec4_zero); + // draw shadow if ((params.styleFlag & TEXT_STYLE_SHADOW) && params.shadowAlpha > 0.0f) { shadowColor.Set(ColorRGBA(params.shadowColor, params.shadowAlpha)); shadowOffset.Set(params.shadowOffset / m_fontTexture->GetSize().xy()); - if (m_flags.sdf) + if (IsDistanceField()) { // shadow width const float sdfEndClamped = clamp(r_font_sdfRange.GetFloat() + params.shadowWeight, 0.0f, 1.0f - r_font_sdfStart.GetFloat()); @@ -595,7 +667,7 @@ void CFont::SetupDrawTextMeshBuffer(RenderDrawCmd& drawCmd, const FontStyleParam else shadowColor.Set(vec4_zero); - if (m_flags.sdf) + if (IsDistanceField()) { const float sdfEndClamped = clamp(r_font_sdfRange.GetFloat() + params.textWeight, 0.0f, 1.0f - r_font_sdfStart.GetFloat()); sdfRange.Set(Vector4D(r_font_sdfStart.GetFloat() - params.textWeight, sdfEndClamped, 1.0f, 0.0f)); @@ -639,7 +711,7 @@ void CFont::GetScaledCharacter( FontChar& chr, const int chrId, const FontStyleP chr = GetFontCharById(charId); - if(m_flags.sdf) // only scale SDF characters + if(IsDistanceField()) // only scale SDF characters { chr.advX = chr.advX * params.scale.x; chr.ofsX = chr.ofsX * params.scale.x; @@ -706,131 +778,221 @@ bool CFont::LoadFont( const char* filenamePrefix ) return false; } - const KVSection* fontSec = kvs.FindSection("Font", KV_FLAG_SECTION); - if(fontSec) + if (const KVSection* fontSec = kvs.FindSection("Font", KV_FLAG_SECTION)) + return LoadFontGrid(fontSec, finalFileName); + + if (const KVSection* fontSec = kvs.FindSection("eqFont")) + return LoadFontV1(fontSec, finalFileName); + + if (const KVSection* fontSec = kvs.FindSection("eqfont2", KV_FLAG_SECTION)) + return LoadFontV2(fontSec, finalFileName); + + MsgError("ERROR: '%s' is not a valid font file\n", finalFileName.ToCString()); + + return false; +} + +// loads the texture from the 'texture' key (font name by default) +bool CFont::LoadFontTexture(const KVSection* fontSec, const SamplerStateParams& samplerParams, const char* finalFileName) +{ + const EqString textureName = KV_GetValueString(fontSec->FindSection("texture"), 0, m_name.ToCString()); + m_fontTexture = g_texLoader->LoadTextureFromFileSync(textureName, samplerParams, TEXFLAG_IGNORE_QUALITY, finalFileName); + + if (m_fontTexture == nullptr) { - const int charWide = KV_GetValueInt(fontSec->FindSection("charWidth"), 0, 8); - const int charTall = KV_GetValueInt(fontSec->FindSection("charHeight"), 0, 8); - const int charsPerLine = KV_GetValueInt(fontSec->FindSection("charsPerLine"), 0, 16); - const float interval = KV_GetValueFloat(fontSec->FindSection("interval"), 0, 0.75); + MsgError("ERROR: no texture for font '%s'\n", finalFileName); + return false; + } - { - const EqString textureName = KV_GetValueString(fontSec->FindSection("texture"), 0, filenamePrefix); - const bool filterTexture = KV_GetValueBool(fontSec->FindSection("filterFontTexture")); - SamplerStateParams samplerParams(filterTexture ? TEXFILTER_LINEAR : TEXFILTER_NEAREST, TEXADDRESS_WRAP); - m_fontTexture = g_texLoader->LoadTextureFromFileSync(textureName, samplerParams, TEXFLAG_IGNORE_QUALITY, finalFileName); - } + m_invTexSize = 1.0f / m_fontTexture->GetSize().xy(); + return true; +} - if (m_fontTexture == nullptr) +// 'Font' - fixed size glyph grid of 256 characters +bool CFont::LoadFontGrid(const KVSection* fontSec, const char* finalFileName) +{ + const int charWide = KV_GetValueInt(fontSec->FindSection("charWidth"), 0, 8); + const int charTall = KV_GetValueInt(fontSec->FindSection("charHeight"), 0, 8); + const int charsPerLine = KV_GetValueInt(fontSec->FindSection("charsPerLine"), 0, 16); + const float interval = KV_GetValueFloat(fontSec->FindSection("interval"), 0, 0.75); + + // grid fonts are scaled by FontStyleParam::scale and shaded like legacy SDF fonts + m_type = FONT_TYPE_SDF; + + const bool filterTexture = KV_GetValueBool(fontSec->FindSection("filterFontTexture")); + if (!LoadFontTexture(fontSec, SamplerStateParams(filterTexture ? TEXFILTER_LINEAR : TEXFILTER_NEAREST, TEXADDRESS_WRAP), finalFileName)) + return false; + + m_spacing = KV_GetValueFloat(fontSec->FindSection("spacing")); + m_baseline = charTall; + m_lineHeight = charTall + 4; + + int line = 0; + int lChars = 0; + for (int i = 0; i < 256; i++) + { + if (lChars == charsPerLine) { - MsgError("ERROR: no texture for font '%s'\n", finalFileName.GetData()); - return false; + line++; + lChars = 0; } - m_invTexSize = 1.0f / m_fontTexture->GetSize().xy(); + FontChar& chr = m_charMap[i]; - m_spacing = KV_GetValueFloat(fontSec->FindSection("spacing")); - m_baseline = charTall; - m_lineHeight = charTall + 4; + const float CurCharPos_x = lChars * charTall; + const float CurCharPos_y = line * charTall; - int line = 0; - int lChars = 0; - for (int i = 0; i < 256; i++) - { - if (lChars == charsPerLine) - { - line++; - lChars = 0; - } + chr.x0 = CurCharPos_x + interval; // LineCharCount * interval + chr.x1 = CurCharPos_x + interval + charWide; + chr.y0 = CurCharPos_y + interval; // Line Count * height + chr.y1 = CurCharPos_y + interval + charTall; + + chr.advX = (chr.x1 - chr.x0) + m_spacing; + chr.ofsX = 0; + chr.ofsY = 0; + + lChars++; + } - FontChar& chr = m_charMap[i]; + return true; +} - const float CurCharPos_x = lChars * charTall; - const float CurCharPos_y = line * charTall; +// 'eqFont' - BMFont export, bitmap or legacy SDF +bool CFont::LoadFontV1(const KVSection* fontSec, const char* finalFileName) +{ + m_type = KV_GetValueBool(fontSec->FindSection("isSDF")) ? FONT_TYPE_SDF : FONT_TYPE_BITMAP; - chr.x0 = CurCharPos_x + interval; // LineCharCount * interval - chr.x1 = CurCharPos_x + interval + charWide; - chr.y0 = CurCharPos_y + interval; // Line Count * height - chr.y1 = CurCharPos_y + interval + charTall; + const bool filterTexture = KV_GetValueBool(fontSec->FindSection("filter")) || IsDistanceField(); + if (!LoadFontTexture(fontSec, SamplerStateParams(filterTexture ? TEXFILTER_LINEAR : TEXFILTER_NEAREST, TEXADDRESS_WRAP), finalFileName)) + return false; - chr.advX = (chr.x1 - chr.x0) + m_spacing; - chr.ofsX = 0; - chr.ofsY = 0; + m_spacing = 0.0f; - lChars++; + if (IsDistanceField()) + m_scale = KV_GetVector2D(fontSec->FindSection("scale")); + + m_baseline = KV_GetValueFloat(fontSec->FindSection("baseline")) * m_scale.y; + m_lineHeight = KV_GetValueFloat(fontSec->FindSection("lineheight")) * m_scale.y; + + for (const KVSection& charSec : fontSec->Keys()) + { + // x y w h ox oy advanceX + // 0 1 2 3 4 5 6 + + FontChar fontChar; + const int valueCount = charSec.GetValues( + fontChar.x0, + fontChar.y0, + fontChar.x1, + fontChar.y1, + fontChar.ofsX, + fontChar.ofsY, + fontChar.advX + ); + + if(valueCount != 7) + { + if(valueCount > 2) + { + ASSERT_FAIL("Invalid font file %s", m_name.ToCString()); + } + continue; } - return true; + fontChar.x1 += fontChar.x0; + fontChar.y1 += fontChar.y0; + + fontChar.ofsX *= m_scale.x; + fontChar.ofsY *= m_scale.y; + fontChar.advX *= m_scale.x; + + const int charIdx = atoi(charSec.GetName()); + m_charMap.insert(charIdx, fontChar); } - fontSec = kvs.FindSection("eqFont"); - if (fontSec) + return true; +} + +// 'eqfont2' is produced from msdf-atlas-gen output by tools/fontconv/msdf_to_eqfont.py. +// Metrics are in ems with Y axis down and origin at the baseline, atlas bounds are in pixels. +bool CFont::LoadFontV2(const KVSection* fontSec, const char* finalFileName) +{ + const EqStringRef atlasType = KV_GetValueString(fontSec->FindSection("type"), 0, "mtsdf"); + if (atlasType == "mtsdf") + m_type = FONT_TYPE_MTSDF; + else if (atlasType == "msdf" || atlasType == "sdf" || atlasType == "psdf") + m_type = FONT_TYPE_MSDF; + else { - m_flags.sdf = KV_GetValueBool(fontSec->FindSection("isSDF")); - m_flags.bold = KV_GetValueBool(fontSec->FindSection("bold")); + MsgError("ERROR: font '%s' has unsupported atlas type '%s'\n", finalFileName, atlasType.ToCString()); + return false; + } - { - const EqString textureName = KV_GetValueString(fontSec->FindSection("texture"), 0, filenamePrefix); - const bool filterTexture = KV_GetValueBool(fontSec->FindSection("filter")) || m_flags.sdf; - SamplerStateParams samplerParams(filterTexture ? TEXFILTER_LINEAR : TEXFILTER_NEAREST, TEXADDRESS_WRAP); - m_fontTexture = g_texLoader->LoadTextureFromFileSync(textureName, samplerParams, TEXFLAG_IGNORE_QUALITY, finalFileName); - } + const float atlasEmSize = KV_GetValueFloat(fontSec->FindSection("size"), 0, 0.0f); + if (atlasEmSize <= 0.0f) + { + MsgError("ERROR: font '%s' has invalid atlas size\n", finalFileName); + return false; + } - if (m_fontTexture == nullptr) - { - MsgError("ERROR: no texture for font '%s'\n", finalFileName.GetData()); - return false; - } + if (!LoadFontTexture(fontSec, SamplerStateParams(TEXFILTER_LINEAR, TEXADDRESS_CLAMP), finalFileName)) + return false; - m_invTexSize = 1.0f / m_fontTexture->GetSize().xy(); - m_spacing = 0.0f; + m_distanceRange = KV_GetValueFloat(fontSec->FindSection("distanceRange"), 0, 2.0f); + m_spacing = 0.0f; - if (m_flags.sdf) - m_scale = KV_GetVector2D(fontSec->FindSection("scale")); + // glyph quads are sized from atlas pixels, FontStyleParam::scale is the em size in pixels + m_scale = Vector2D(1.0f / atlasEmSize); - m_baseline = KV_GetValueFloat(fontSec->FindSection("baseline")) * m_scale.y; - m_lineHeight = KV_GetValueFloat(fontSec->FindSection("lineheight")) * m_scale.y; + const float ascender = KV_GetValueFloat(fontSec->FindSection("ascender")); + m_baseline = ascender; + m_lineHeight = KV_GetValueFloat(fontSec->FindSection("lineheight"), 0, 1.0f); - for (const KVSection& charSec : fontSec->Keys()) + for (const KVSection& keySec : fontSec->Keys()) + { + if (!CString::CompareCaseIns(keySec.GetName(), "glyph")) { - // x y w h ox oy advanceX - // 0 1 2 3 4 5 6 + // unicode advance [planeLeft planeTop planeRight planeBottom atlasLeft atlasTop atlasRight atlasBottom] + int charIdx = 0; + float advance = 0.0f; + float planeLeft = 0.0f, planeTop = 0.0f, planeRight = 0.0f, planeBottom = 0.0f; + float atlasLeft = 0.0f, atlasTop = 0.0f, atlasRight = 0.0f, atlasBottom = 0.0f; - FontChar fontChar; - const int valueCount = charSec.GetValues( - fontChar.x0, - fontChar.y0, - fontChar.x1, - fontChar.y1, - fontChar.ofsX, - fontChar.ofsY, - fontChar.advX - ); - - if(valueCount != 7) + const int valueCount = keySec.GetValues(charIdx, advance, + planeLeft, planeTop, planeRight, planeBottom, + atlasLeft, atlasTop, atlasRight, atlasBottom); + + if (valueCount != 2 && valueCount != 10) { - if(valueCount > 2) - { - ASSERT_FAIL("Invalid font file %s", filenamePrefix); - } + MsgError("ERROR: font '%s' has invalid glyph entry\n", finalFileName); continue; } - fontChar.x1 += fontChar.x0; - fontChar.y1 += fontChar.y0; - - fontChar.ofsX *= m_scale.x; - fontChar.ofsY *= m_scale.y; - fontChar.advX *= m_scale.x; + FontChar fontChar; + fontChar.advX = advance; + if (valueCount == 10) + { + fontChar.x0 = atlasLeft; + fontChar.y0 = atlasTop; + fontChar.x1 = atlasRight; + fontChar.y1 = atlasBottom; + + // text drawing subtracts the baseline offset, compensate it here + fontChar.ofsX = planeLeft; + fontChar.ofsY = planeTop + ascender; + } - const int charIdx = atoi(charSec.GetName()); m_charMap.insert(charIdx, fontChar); } - - return true; + else if (!CString::CompareCaseIns(keySec.GetName(), "kern")) + { + int firstChrId = 0; + int secondChrId = 0; + float kerning = 0.0f; + if (keySec.GetValues(firstChrId, secondChrId, kerning) == 3) + m_kerning.insert(KerningPairKey(firstChrId, secondChrId), kerning); + } } - MsgError("ERROR: '%s' is not a valid font file\n", finalFileName.ToCString()); - - return false; + return true; } \ No newline at end of file diff --git a/shared/font/Font.h b/shared/font/Font.h index e7c976bf0..1ca235caf 100644 --- a/shared/font/Font.h +++ b/shared/font/Font.h @@ -10,11 +10,21 @@ #include "font/IFont.h" class CMeshBuilder; +struct KVSection; struct RenderDrawCmd; struct RenderPassContext; +struct SamplerStateParams; class ITexture; using ITexturePtr = CRefPtr; +enum EFontType : int +{ + FONT_TYPE_BITMAP = 0, // bitmap glyphs, not scaled by FontStyleParam::scale + FONT_TYPE_SDF, // legacy BMFont SDF in alpha, edge set by r_font_sdfStart / r_font_sdfRange + FONT_TYPE_MSDF, // eqfont2 msdf / sdf / psdf atlas, distance is the median of RGB + FONT_TYPE_MTSDF, // eqfont2 mtsdf atlas, MSDF in RGB and true SDF in alpha +}; + class CFont : public IEqFont { friend class CEqConsoleInput; @@ -25,6 +35,9 @@ class CFont : public IEqFont bool LoadFont( const char* filenamePrefix ); + // returns kerning between two characters in pixels + float GetKerning( int firstChrId, int secondChrId, const FontStyleParam& params ) const; + // returns string width in pixels float GetStringWidth( const wchar_t* str, const FontStyleParam& params, int charCount = -1, int breakOnChar = -1) const; float GetStringWidth( const char* str, const FontStyleParam& params, int charCount = -1, int breakOnChar = -1) const; @@ -44,6 +57,18 @@ class CFont : public IEqFont protected: using FontCharMap = Map; + using FontKerningMap = Map; + + bool LoadFontGrid( const KVSection* fontSec, const char* finalFileName ); + bool LoadFontV1( const KVSection* fontSec, const char* finalFileName ); + bool LoadFontV2( const KVSection* fontSec, const char* finalFileName ); + bool LoadFontTexture( const KVSection* fontSec, const SamplerStateParams& samplerParams, const char* finalFileName ); + + // glyphs are scaled by FontStyleParam::scale and drawn with distance field shading + bool IsDistanceField() const { return m_type != FONT_TYPE_BITMAP; } + + // eqfont2 distance field atlas + bool IsMSDF() const { return m_type == FONT_TYPE_MSDF || m_type == FONT_TYPE_MTSDF; } void SetupDrawTextMeshBuffer(RenderDrawCmd& drawCmd, const FontStyleParam& params, IGPURenderPassRecorder* rendPassRecorder); @@ -69,20 +94,18 @@ class CFont : public IEqFont // map of chars FontCharMap m_charMap{ PP_SL }; + FontKerningMap m_kerning{ PP_SL }; EqString m_name; float m_spacing{ 0.0f }; float m_baseline{ 0.0f }; float m_lineHeight{ 0.0f }; + float m_distanceRange{ 0.0f }; // eqfont2 distance range in atlas pixels Vector2D m_scale{ 1.0f }; Vector2D m_invTexSize{ 1.0f }; ITexturePtr m_fontTexture; - struct - { - bool sdf : 1; - bool bold : 2; - } m_flags; + EFontType m_type{ FONT_TYPE_BITMAP }; }; \ No newline at end of file diff --git a/shared/font/FontCache.cpp b/shared/font/FontCache.cpp index beda250fb..735e04917 100644 --- a/shared/font/FontCache.cpp +++ b/shared/font/FontCache.cpp @@ -178,6 +178,7 @@ bool CEqFontCache::Init() m_shadowParams = m_sdfMaterial->GetMaterialVar("ShadowParams", "[0.94 0.95 0, 1]"); m_shadowColor = m_sdfMaterial->GetMaterialVar("ShadowColor", "[0 0 0 1]"); m_shadowOffset = m_sdfMaterial->GetMaterialVar("ShadowOffset", "[0 0]"); + m_msdfParams = m_sdfMaterial->GetMaterialVar("MsdfParams", "[0 0 0 0]"); m_sdfMaterial->LoadShaderAndTextures(); diff --git a/shared/font/FontCache.h b/shared/font/FontCache.h index 439a3d931..3eb05406c 100644 --- a/shared/font/FontCache.h +++ b/shared/font/FontCache.h @@ -68,4 +68,5 @@ class CEqFontCache : public IEqFontCache MatVec4Proxy m_shadowColor; MatVec4Proxy m_shadowParams; MatVec2Proxy m_shadowOffset; + MatVec4Proxy m_msdfParams; }; diff --git a/shared/grim/GrimBaseRenderer.cpp b/shared/grim/GrimBaseRenderer.cpp index 2898b7a44..17300bee0 100644 --- a/shared/grim/GrimBaseRenderer.cpp +++ b/shared/grim/GrimBaseRenderer.cpp @@ -1117,7 +1117,10 @@ void GRIMBaseRenderer::UpdateIndirectInstances_Compute(IntermediateState& interm computeRecorder->Complete(); // copy draw invocations to readback buffer - intermediate.cmdRecorder->CopyBufferToBuffer(rendState.drawInvocationsBuffer, 0, rendState.drawInvocationsReadbackBuffer, 0, rendState.drawInvocationsBuffer->GetSize()); + // buffer does not shrink when draw infos are freed, copy only used commands + const int drawInvocationsSize = m_drawInfos.numElem() * static_cast(sizeof(GPUDrawIndexedIndirectCmd)); + if (drawInvocationsSize > 0) + intermediate.cmdRecorder->CopyBufferToBuffer(rendState.drawInvocationsBuffer, 0, rendState.drawInvocationsReadbackBuffer, 0, drawInvocationsSize); } void GRIMBaseRenderer::UpdateIndirectInstances_Software(IntermediateState& intermediate) @@ -1279,7 +1282,8 @@ bool GRIMBaseRenderer::IsSoftwareMode() const void GRIMBaseRenderer::PostPrepareDraw(GRIMRenderState& renderState) { IGPUBufferPtr& drawInvocationsReadbackBuffer = renderState.drawInvocationsReadbackBuffer; - if (IsSoftwareMode() || renderState.drawFrame != m_drawFrame || !drawInvocationsReadbackBuffer) + const int drawInvocationsSize = min(m_drawInfos.numElem() * static_cast(sizeof(GPUDrawIndexedIndirectCmd)), drawInvocationsReadbackBuffer ? static_cast(drawInvocationsReadbackBuffer->GetSize()) : 0); + if (IsSoftwareMode() || renderState.drawFrame != m_drawFrame || drawInvocationsSize <= 0) { renderState.drawInvocationsReadbackFuture = nullptr; return; @@ -1288,7 +1292,7 @@ void GRIMBaseRenderer::PostPrepareDraw(GRIMRenderState& renderState) BitArray& activeDrawInvocations = renderState.activeDrawInvocations; // read draw invocations - renderState.drawInvocationsReadbackFuture = drawInvocationsReadbackBuffer->Lock(0, drawInvocationsReadbackBuffer->GetSize(), 0); + renderState.drawInvocationsReadbackFuture = drawInvocationsReadbackBuffer->Lock(0, drawInvocationsSize, 0); renderState.drawInvocationsReadbackFuture.AddCallback([this, &activeDrawInvocations, drawInvocationsReadbackBuffer](const FutureResult& result) { if (result.IsError()) { @@ -1297,7 +1301,7 @@ void GRIMBaseRenderer::PostPrepareDraw(GRIMRenderState& renderState) } ASSERT(result->data); - const int numIndirectCmds = result->size / sizeof(GPUDrawIndexedIndirectCmd); + const int numIndirectCmds = min(static_cast(result->size / sizeof(GPUDrawIndexedIndirectCmd)), activeDrawInvocations.numBits()); GPUDrawIndexedIndirectCmd* drawIndexedCmd = reinterpret_cast(result->data); int drawnInstances = 0; diff --git a/shared/premake5.lua b/shared/premake5.lua index 4ddc91fd4..073028033 100644 --- a/shared/premake5.lua +++ b/shared/premake5.lua @@ -27,7 +27,7 @@ project "fontLib" -- Render Utility and Debug Drawing project "renderUtilLib" properties { "staticlib", "unitybuild" } - uses { "public", "shared" } + uses { "public", "shared", "smaa" } files { "render/**", PUBLIC_DIR.."/render/**" diff --git a/shared/render/EdgeAA.cpp b/shared/render/EdgeAA.cpp index cc347bf93..f07cba84d 100644 --- a/shared/render/EdgeAA.cpp +++ b/shared/render/EdgeAA.cpp @@ -2,41 +2,110 @@ // Copyright (C) Inspiration Byte // 2009-2021 ////////////////////////////////////////////////////////////////////////////////// -// Description: DrvSyn post-processing effects +// Description: Full screen anti-aliasing (FXAA, SMAA 1x) ////////////////////////////////////////////////////////////////////////////////// +#include +#include + #include "core/core_common.h" #include "core/ConVar.h" #include "utils/KeyValues.h" #include "EdgeAA.h" #include "materialsystem1/IMaterialSystem.h" +#include "materialsystem1/IMatSysShader.h" #include "materialsystem1/MeshBuilder.h" +DEFINE_SHADER_NOFACTORY(SMAA) -DECLARE_CVAR(r_fxaa, "1", "Fast Approximate Anti-aliasing", CV_ARCHIVE); DECLARE_CVAR_CLAMP(r_fxaaQualitySubpix, "0.25", 0.25f, 1.0f, nullptr, CV_ARCHIVE); DECLARE_CVAR_CLAMP(r_fxaaQualityEdgeThreshold, "0.11", 0.063f, 0.5f, nullptr, CV_ARCHIVE); DECLARE_CVAR_CLAMP(r_fxaaQualityEdgeThresholdMin, "0.063", 0.0f, 1.0f, nullptr, CV_ARCHIVE); +DECLARE_CVAR_CLAMP(r_smaaThreshold, "0.1", 0.01f, 0.5f, "SMAA edge detection luma threshold, lower is more edges", CV_ARCHIVE); +DECLARE_CVAR_CLAMP(r_smaaMaxSearchSteps, "16", 1, 112, "SMAA horizontal / vertical edge search distance (pixels / 2)", CV_ARCHIVE); +DECLARE_CVAR_CLAMP(r_smaaMaxSearchStepsDiag, "8", 0, 20, "SMAA diagonal edge search distance", CV_ARCHIVE); +DECLARE_CVAR_CLAMP(r_smaaCornerRounding, "25", 0, 100, "SMAA corner rounding percent", CV_ARCHIVE); + +// must match SMAAParams in shadersSRC/source_SMAA.fx +struct SMAAParams +{ + Vector4D rtMetrics; + float threshold; + float maxSearchSteps; + float maxSearchStepsDiag; + float cornerRounding; +}; + static ETextureFormat fsaaGetTextureFormat() { return FORMAT_RGBA8; } -bool CRenderFullScreenEdgeAA::IsEnabled() const +// passLayoutId is the SMAA shader vertex layout that selects the pass, see shadersSRC/SMAA.def +static IGPURenderPipelinePtr smaaCreatePipeline(int passLayoutId, ETextureFormat targetFormat) { - if (!r_fxaa.GetBool()) - return false; + return g_renderAPI->CreateRenderPipeline( + Builder() + .ShaderName("SMAA") + .ShaderVertexLayoutId(passLayoutId) + .FragmentState( + Builder() + .ColorTarget("Target", targetFormat) + .End() + ) + .PrimitiveState(Builder() + .Topology(PRIM_TRIANGLES) + .Cull(CULL_NONE) + .End() + ) + .End() + ); +} + +//------------------------------------------------------------------------------------------------- +EEdgeAAMode CRenderFullScreenEdgeAA::GetMode() const +{ if (!m_edgeAALumaPipeline) - return false; + return EDGEAA_NONE; - return true; + if (m_mode == EDGEAA_SMAA) + { + if (m_smaaEdgesPipeline && m_smaaWeightsPipeline) + return EDGEAA_SMAA; + + // SMAA shaders are not available + return EDGEAA_FXAA; + } + + return m_mode; +} + +bool CRenderFullScreenEdgeAA::IsEnabled() const +{ + return GetMode() != EDGEAA_NONE; +} + +void CRenderFullScreenEdgeAA::SetMode(EEdgeAAMode mode) +{ + if (!m_initialized || mode == m_mode) + return; + + Shutdown(); + Init(mode); } -bool CRenderFullScreenEdgeAA::Init() +// creates only the resources of the mode, SetMode re-initializes +bool CRenderFullScreenEdgeAA::Init(EEdgeAAMode mode) { + m_initialized = true; + m_mode = mode; + + if (mode == EDGEAA_NONE) + return true; + ITexture* backBufferTex = g_matSystem->GetDefaultDepthBuffer(); const IVector2D screenSize = backBufferTex->GetSize().xy(); @@ -48,7 +117,7 @@ bool CRenderFullScreenEdgeAA::Init() .Flags(TEXFLAG_STORAGE) .End() ); - + m_edgeAALumaPipeline = g_renderAPI->CreateComputePipeline(Builder() .ShaderName("EdgeAA") .End() @@ -64,21 +133,109 @@ bool CRenderFullScreenEdgeAA::Init() m_edgeAASettings = m_edgeAAMat->GetMaterialVar("Settings"); } + + if (mode == EDGEAA_SMAA && !InitSMAA(screenSize)) + MsgWarning("SMAA is not available, FXAA will be used instead\n"); + + return true; +} + +bool CRenderFullScreenEdgeAA::InitSMAA(const IVector2D& screenSize) +{ + m_smaaEdgesTarget = g_renderAPI->CreateRenderTarget( + Builder() + .Name("_rt_SMAAEdges") + .Format(FORMAT_RG8) + .Size(screenSize.x, screenSize.y) + .End() + ); + + m_smaaBlendTarget = g_renderAPI->CreateRenderTarget( + Builder() + .Name("_rt_SMAABlend") + .Format(FORMAT_RGBA8) + .Size(screenSize.x, screenSize.y) + .End() + ); + + m_smaaAreaTex = g_renderAPI->CreateProceduralTexture( + Builder() + .Name("_smaaAreaTex") + .Format(FORMAT_RG8) + .Size(AREATEX_WIDTH, AREATEX_HEIGHT) + .Sampler(SamplerStateParams(TEXFILTER_LINEAR, TEXADDRESS_CLAMP)) + .End(), + areaTexBytes, sizeof(areaTexBytes) + ); + + m_smaaSearchTex = g_renderAPI->CreateProceduralTexture( + Builder() + .Name("_smaaSearchTex") + .Format(FORMAT_R8) + .Size(SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT) + .Sampler(SamplerStateParams(TEXFILTER_LINEAR, TEXADDRESS_CLAMP)) + .End(), + searchTexBytes, sizeof(searchTexBytes) + ); + + m_smaaParamsBuffer = g_renderAPI->CreateBuffer(BufferInfo(sizeof(SMAAParams), 1), BUFFERUSAGE_UNIFORM | BUFFERUSAGE_COPY_DST, "SMAAParams"); + + m_smaaEdgesPipeline = smaaCreatePipeline(StringIdConst24("EdgeDetection"), FORMAT_RG8); + m_smaaWeightsPipeline = smaaCreatePipeline(StringIdConst24("BlendingWeight"), FORMAT_RGBA8); + + // neighborhood blending pipeline is created by the back buffer format in RenderSMAA + m_smaaBlendPipeline = nullptr; + m_smaaBlendTargetFormat = FORMAT_NONE; + + if (!m_smaaEdgesTarget || !m_smaaBlendTarget || !m_smaaAreaTex || !m_smaaSearchTex || !m_smaaParamsBuffer + || !m_smaaEdgesPipeline || !m_smaaWeightsPipeline) + { + m_smaaEdgesPipeline = nullptr; + m_smaaWeightsPipeline = nullptr; + return false; + } + return true; } void CRenderFullScreenEdgeAA::Shutdown() { + m_initialized = false; + m_mode = EDGEAA_NONE; + m_lumaFramebuffer = nullptr; m_edgeAALumaPipeline = nullptr; m_edgeAAMat = nullptr; + + m_smaaEdgesTarget = nullptr; + m_smaaBlendTarget = nullptr; + m_smaaAreaTex = nullptr; + m_smaaSearchTex = nullptr; + m_smaaParamsBuffer = nullptr; + m_smaaEdgesPipeline = nullptr; + m_smaaWeightsPipeline = nullptr; + m_smaaBlendPipeline = nullptr; + m_smaaBlendTargetFormat = FORMAT_NONE; } void CRenderFullScreenEdgeAA::PreRender(IGPUCommandRecorder* cmdRecorder) { - if (!m_edgeAALumaPipeline) - return; + switch (GetMode()) + { + case EDGEAA_FXAA: + PreRenderFXAA(cmdRecorder); + break; + case EDGEAA_SMAA: + PreRenderSMAA(cmdRecorder); + break; + default: + break; + } +} +// copies back buffer into _rt_AAFramebuffer (with luma in alpha for FXAA), returns its size +IVector2D CRenderFullScreenEdgeAA::CopyBackbuffer(IGPUCommandRecorder* cmdRecorder) +{ // NOTE: see shader source for workgroups size constexpr const int workgroupSize = 32; constexpr const int blocksPerWorkgroup = workgroupSize * 4; @@ -107,9 +264,96 @@ void CRenderFullScreenEdgeAA::PreRender(IGPUCommandRecorder* cmdRecorder) computePassRecorder->Complete(); cmdRecorder->DbgPopGroup(); + + return framebufferSize; +} + +void CRenderFullScreenEdgeAA::PreRenderFXAA(IGPUCommandRecorder* cmdRecorder) +{ + CopyBackbuffer(cmdRecorder); +} + +void CRenderFullScreenEdgeAA::PreRenderSMAA(IGPUCommandRecorder* cmdRecorder) +{ + const IVector2D framebufferSize = CopyBackbuffer(cmdRecorder); + + g_renderAPI->ResizeRenderTarget(m_smaaEdgesTarget, { framebufferSize.x, framebufferSize.y, 1 }); + g_renderAPI->ResizeRenderTarget(m_smaaBlendTarget, { framebufferSize.x, framebufferSize.y, 1 }); + + SMAAParams params; + params.rtMetrics = Vector4D(1.0f / framebufferSize.x, 1.0f / framebufferSize.y, framebufferSize.x, framebufferSize.y); + params.threshold = r_smaaThreshold.GetFloat(); + params.maxSearchSteps = r_smaaMaxSearchSteps.GetInt(); + params.maxSearchStepsDiag = r_smaaMaxSearchStepsDiag.GetInt(); + params.cornerRounding = r_smaaCornerRounding.GetInt(); + cmdRecorder->WriteBuffer(m_smaaParamsBuffer, ¶ms, sizeof(params), 0); + + const SamplerStateParams linearClampSampler(TEXFILTER_LINEAR, TEXADDRESS_CLAMP); + + // edges and weights passes discard or skip pixels, so the targets are cleared to zero + const MColor clearColor(0.0f, 0.0f, 0.0f, 0.0f); + + cmdRecorder->DbgPushGroup("SMAAEdgeDetection"); + { + IGPURenderPassRecorderPtr rendPassRecorder = cmdRecorder->BeginRenderPass( + Builder() + .Name("SMAAEdgeDetection") + .ColorTarget(m_smaaEdgesTarget, true, clearColor) + .End() + ); + rendPassRecorder->SetPipeline(m_smaaEdgesPipeline); + rendPassRecorder->SetBindGroup(0, g_renderAPI->CreateBindGroup(m_smaaEdgesPipeline, + Builder().GroupIndex(0) + .Sampler(StringIdConst24("LinearSampler"), linearClampSampler) + .Buffer(StringIdConst24("smaaParams"), m_smaaParamsBuffer) + .Texture(StringIdConst24("ColorTexture"), m_lumaFramebuffer) + .End()) + ); + rendPassRecorder->Draw(3, 0, 1); + rendPassRecorder->Complete(); + } + cmdRecorder->DbgPopGroup(); + + cmdRecorder->DbgPushGroup("SMAABlendingWeight"); + { + IGPURenderPassRecorderPtr rendPassRecorder = cmdRecorder->BeginRenderPass( + Builder() + .Name("SMAABlendingWeight") + .ColorTarget(m_smaaBlendTarget, true, clearColor) + .End() + ); + rendPassRecorder->SetPipeline(m_smaaWeightsPipeline); + rendPassRecorder->SetBindGroup(0, g_renderAPI->CreateBindGroup(m_smaaWeightsPipeline, + Builder().GroupIndex(0) + .Sampler(StringIdConst24("LinearSampler"), linearClampSampler) + .Buffer(StringIdConst24("smaaParams"), m_smaaParamsBuffer) + .Texture(StringIdConst24("EdgesTexture"), m_smaaEdgesTarget) + .Texture(StringIdConst24("AreaTexture"), m_smaaAreaTex) + .Texture(StringIdConst24("SearchTexture"), m_smaaSearchTex) + .End()) + ); + rendPassRecorder->Draw(3, 0, 1); + rendPassRecorder->Complete(); + } + cmdRecorder->DbgPopGroup(); } void CRenderFullScreenEdgeAA::Render(IGPURenderPassRecorder* rendPassRecorder) +{ + switch (GetMode()) + { + case EDGEAA_FXAA: + RenderFXAA(rendPassRecorder); + break; + case EDGEAA_SMAA: + RenderSMAA(rendPassRecorder); + break; + default: + break; + } +} + +void CRenderFullScreenEdgeAA::RenderFXAA(IGPURenderPassRecorder* rendPassRecorder) { Vector4D settings; settings.x = r_fxaaQualitySubpix.GetFloat(); @@ -123,3 +367,27 @@ void CRenderFullScreenEdgeAA::Render(IGPURenderPassRecorder* rendPassRecorder) drawCmd.SetDrawNonIndexed(PRIM_TRIANGLES, 3, 0); g_matSystem->SetupDrawCommand(drawCmd, RenderPassContext(rendPassRecorder, nullptr)); } + +void CRenderFullScreenEdgeAA::RenderSMAA(IGPURenderPassRecorder* rendPassRecorder) +{ + const ETextureFormat targetFormat = rendPassRecorder->GetRenderTargetFormats()[0]; + if (!m_smaaBlendPipeline || m_smaaBlendTargetFormat != targetFormat) + { + m_smaaBlendTargetFormat = targetFormat; + m_smaaBlendPipeline = smaaCreatePipeline(StringIdConst24("NeighborhoodBlending"), targetFormat); + } + + if (!m_smaaBlendPipeline) + return; + + rendPassRecorder->SetPipeline(m_smaaBlendPipeline); + rendPassRecorder->SetBindGroup(0, g_renderAPI->CreateBindGroup(m_smaaBlendPipeline, + Builder().GroupIndex(0) + .Sampler(StringIdConst24("LinearSampler"), SamplerStateParams(TEXFILTER_LINEAR, TEXADDRESS_CLAMP)) + .Buffer(StringIdConst24("smaaParams"), m_smaaParamsBuffer) + .Texture(StringIdConst24("ColorTexture"), m_lumaFramebuffer) + .Texture(StringIdConst24("BlendTexture"), m_smaaBlendTarget) + .End()) + ); + rendPassRecorder->Draw(3, 0, 1); +} diff --git a/shared/render/EdgeAA.h b/shared/render/EdgeAA.h index 6a951b610..ad359e952 100644 --- a/shared/render/EdgeAA.h +++ b/shared/render/EdgeAA.h @@ -2,7 +2,7 @@ // Copyright (C) Inspiration Byte // 2009-2021 ////////////////////////////////////////////////////////////////////////////////// -// Description: DrvSyn post-processing effects +// Description: Full screen anti-aliasing (FXAA, SMAA 1x) ////////////////////////////////////////////////////////////////////////////////// #pragma once @@ -11,21 +11,60 @@ class ConVar; class IGPURenderPassRecorder; class IGPUComputePipeline; +class IGPURenderPipeline; +class IGPUBuffer; using IGPUComputePipelinePtr = CRefPtr; +using IGPURenderPipelinePtr = CRefPtr; +using IGPUBufferPtr = CRefPtr; + +enum EEdgeAAMode : int +{ + EDGEAA_NONE = 0, + EDGEAA_FXAA, + EDGEAA_SMAA, +}; class CRenderFullScreenEdgeAA { public: - bool Init(); + bool Init(EEdgeAAMode mode); void Shutdown(); + // re-creates resources for the new mode if initialized + void SetMode(EEdgeAAMode mode); bool IsEnabled() const; void PreRender(IGPUCommandRecorder* cmdRecorder); void Render(IGPURenderPassRecorder* rendPassRecorder); + protected: + EEdgeAAMode GetMode() const; + + IVector2D CopyBackbuffer(IGPUCommandRecorder* cmdRecorder); + + void PreRenderFXAA(IGPUCommandRecorder* cmdRecorder); + void RenderFXAA(IGPURenderPassRecorder* rendPassRecorder); + + bool InitSMAA(const IVector2D& screenSize); + void PreRenderSMAA(IGPUCommandRecorder* cmdRecorder); + void RenderSMAA(IGPURenderPassRecorder* rendPassRecorder); + + EEdgeAAMode m_mode{ EDGEAA_NONE }; + bool m_initialized{ false }; + IMaterialPtr m_edgeAAMat; MatVec4Proxy m_edgeAASettings; ITexturePtr m_lumaFramebuffer; IGPUComputePipelinePtr m_edgeAALumaPipeline; + + // SMAA 1x + ITexturePtr m_smaaEdgesTarget; + ITexturePtr m_smaaBlendTarget; + ITexturePtr m_smaaAreaTex; + ITexturePtr m_smaaSearchTex; + IGPUBufferPtr m_smaaParamsBuffer; + IGPURenderPipelinePtr m_smaaEdgesPipeline; + IGPURenderPipelinePtr m_smaaWeightsPipeline; + IGPURenderPipelinePtr m_smaaBlendPipeline; + ETextureFormat m_smaaBlendTargetFormat{ FORMAT_NONE }; }; diff --git a/shared/sys/sys_host.cpp b/shared/sys/sys_host.cpp index 6ab61338d..60b53d682 100644 --- a/shared/sys/sys_host.cpp +++ b/shared/sys/sys_host.cpp @@ -225,6 +225,13 @@ static EQCURSOR s_defaultCursor[20]; CStaticAutoPtr g_pHost; CStaticAutoPtr g_fullScreenEdgeAA; +static void r_aa_changed(ConVar* cvar, const char* oldValue) +{ + g_fullScreenEdgeAA->SetMode(static_cast(cvar->GetInt())); +} + +DECLARE_CVAR_CLAMP_CHANGE(r_aa, "2", r_aa_changed, 0, 2, "Anti-aliasing mode. 0 - off, 1 - FXAA, 2 - SMAA", CV_ARCHIVE); + static OSModule* g_matsysmodule = nullptr; IMaterialSystem* g_matSystem = nullptr; IShaderAPI* g_renderAPI = nullptr; @@ -644,7 +651,7 @@ bool CGameHost::InitSystems() g_consoleInput->Init(); REGISTER_INTERNAL_SHADERS(); - g_fullScreenEdgeAA->Init(); + g_fullScreenEdgeAA->Init(static_cast(r_aa.GetInt())); ApplyWindowTitle(); diff --git a/src_dependency/msdfgen/LICENSE.txt b/src_dependency/msdfgen/LICENSE.txt new file mode 100644 index 000000000..fbea359b2 --- /dev/null +++ b/src_dependency/msdfgen/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2014 - 2025 Viktor Chlumsky + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src_dependency/msdfgen/msdfgen/core/Bitmap.h b/src_dependency/msdfgen/msdfgen/core/Bitmap.h new file mode 100644 index 000000000..05c305d64 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/Bitmap.h @@ -0,0 +1,60 @@ + +#pragma once + +#include "YAxisOrientation.h" +#include "BitmapRef.hpp" + +namespace msdfgen { + +/// A 2D image bitmap with N channels of type T. Pixel memory is managed by the class. +template +class Bitmap { + +public: + Bitmap(); + Bitmap(int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION); + explicit Bitmap(const BitmapConstRef &orig); + explicit Bitmap(const BitmapConstSection &orig); + Bitmap(const Bitmap &orig); +#ifdef MSDFGEN_USE_CPP11 + Bitmap(Bitmap &&orig); +#endif + ~Bitmap(); + Bitmap &operator=(const BitmapConstRef &orig); + Bitmap &operator=(const BitmapConstSection &orig); + Bitmap &operator=(const Bitmap &orig); +#ifdef MSDFGEN_USE_CPP11 + Bitmap &operator=(Bitmap &&orig); +#endif + /// Bitmap width in pixels. + int width() const; + /// Bitmap height in pixels. + int height() const; + T *operator()(int x, int y); + const T *operator()(int x, int y) const; +#ifdef MSDFGEN_USE_CPP11 + explicit operator T *(); + explicit operator const T *() const; +#else + operator T *(); + operator const T *() const; +#endif + operator BitmapRef(); + operator BitmapConstRef() const; + operator BitmapSection(); + operator BitmapConstSection() const; + /// Returns a reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax). + BitmapSection getSection(int xMin, int yMin, int xMax, int yMax); + /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax). + BitmapConstSection getConstSection(int xMin, int yMin, int xMax, int yMax) const; + +private: + T *pixels; + int w, h; + YAxisOrientation yOrientation; + +}; + +} + +#include "Bitmap.hpp" diff --git a/src_dependency/msdfgen/msdfgen/core/Bitmap.hpp b/src_dependency/msdfgen/msdfgen/core/Bitmap.hpp new file mode 100644 index 000000000..afb53942f --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/Bitmap.hpp @@ -0,0 +1,172 @@ + +#include "Bitmap.h" + +#include +#include + +namespace msdfgen { + +template +Bitmap::Bitmap() : pixels(NULL), w(0), h(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { } + +template +Bitmap::Bitmap(int width, int height, YAxisOrientation yOrientation) : w(width), h(height), yOrientation(yOrientation) { + pixels = new T[N*w*h]; +} + +template +Bitmap::Bitmap(const BitmapConstRef &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) { + pixels = new T[N*w*h]; + memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); +} + +template +Bitmap::Bitmap(const BitmapConstSection &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) { + pixels = new T[N*w*h]; + T *dst = pixels; + const T *src = orig.pixels; + int rowLength = N*w; + for (int y = 0; y < h; ++y) { + memcpy(dst, src, sizeof(T)*rowLength); + dst += rowLength; + src += orig.rowStride; + } +} + +template +Bitmap::Bitmap(const Bitmap &orig) : w(orig.w), h(orig.h), yOrientation(orig.yOrientation) { + pixels = new T[N*w*h]; + memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); +} + +#ifdef MSDFGEN_USE_CPP11 +template +Bitmap::Bitmap(Bitmap &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h), yOrientation(orig.yOrientation) { + orig.pixels = NULL; + orig.w = 0, orig.h = 0; +} +#endif + +template +Bitmap::~Bitmap() { + delete[] pixels; +} + +template +Bitmap &Bitmap::operator=(const BitmapConstRef &orig) { + if (pixels != orig.pixels) { + delete[] pixels; + w = orig.width, h = orig.height; + yOrientation = orig.yOrientation; + pixels = new T[N*w*h]; + memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); + } + return *this; +} + +template +Bitmap &Bitmap::operator=(const BitmapConstSection &orig) { + if (orig.pixels && orig.pixels >= pixels && orig.pixels < pixels+N*w*h) + return *this = Bitmap(orig); + delete[] pixels; + w = orig.width, h = orig.height; + yOrientation = orig.yOrientation; + pixels = new T[N*w*h]; + T *dst = pixels; + const T *src = orig.pixels; + int rowLength = N*w; + for (int y = 0; y < h; ++y) { + memcpy(dst, src, sizeof(T)*rowLength); + dst += rowLength; + src += orig.rowStride; + } + return *this; +} + +template +Bitmap &Bitmap::operator=(const Bitmap &orig) { + if (this != &orig) { + delete[] pixels; + w = orig.w, h = orig.h; + yOrientation = orig.yOrientation; + pixels = new T[N*w*h]; + memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); + } + return *this; +} + +#ifdef MSDFGEN_USE_CPP11 +template +Bitmap &Bitmap::operator=(Bitmap &&orig) { + if (this != &orig) { + delete[] pixels; + pixels = orig.pixels; + w = orig.w, h = orig.h; + yOrientation = orig.yOrientation; + orig.pixels = NULL; + } + return *this; +} +#endif + +template +int Bitmap::width() const { + return w; +} + +template +int Bitmap::height() const { + return h; +} + +template +T *Bitmap::operator()(int x, int y) { + return pixels+N*(w*y+x); +} + +template +const T *Bitmap::operator()(int x, int y) const { + return pixels+N*(w*y+x); +} + +template +Bitmap::operator T *() { + return pixels; +} + +template +Bitmap::operator const T *() const { + return pixels; +} + +template +Bitmap::operator BitmapRef() { + return BitmapRef(pixels, w, h, yOrientation); +} + +template +Bitmap::operator BitmapConstRef() const { + return BitmapConstRef(pixels, w, h, yOrientation); +} + +template +Bitmap::operator BitmapSection() { + return BitmapSection(pixels, w, h, yOrientation); +} + +template +Bitmap::operator BitmapConstSection() const { + return BitmapConstSection(pixels, w, h, yOrientation); +} + +template +BitmapSection Bitmap::getSection(int xMin, int yMin, int xMax, int yMax) { + return BitmapSection(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation); +} + +template +BitmapConstSection Bitmap::getConstSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapConstSection(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation); +} + +} diff --git a/src_dependency/msdfgen/msdfgen/core/BitmapRef.hpp b/src_dependency/msdfgen/msdfgen/core/BitmapRef.hpp new file mode 100644 index 000000000..6e93fe983 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/BitmapRef.hpp @@ -0,0 +1,154 @@ + +#pragma once + +#include "YAxisOrientation.h" + +namespace msdfgen { + +/// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object. +template +struct BitmapRef; +/// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object. +template +struct BitmapConstRef; +/// Reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative). +template +struct BitmapSection; +/// Constant reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative). +template +struct BitmapConstSection; + +template +struct BitmapRef { + + T *pixels; + int width, height; + YAxisOrientation yOrientation; + + inline BitmapRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { } + inline BitmapRef(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { } + + inline T *operator()(int x, int y) const { + return pixels+N*(width*y+x); + } + + /// Returns a reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapSection getSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapSection(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation); + } + + /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapConstSection getConstSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapConstSection(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation); + } + +}; + +template +struct BitmapConstRef { + + const T *pixels; + int width, height; + YAxisOrientation yOrientation; + + inline BitmapConstRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { } + inline BitmapConstRef(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { } + inline BitmapConstRef(const BitmapRef &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), yOrientation(orig.yOrientation) { } + + inline const T *operator()(int x, int y) const { + return pixels+N*(width*y+x); + } + + /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapConstSection getSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapConstSection(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation); + } + + /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapConstSection getConstSection(int xMin, int yMin, int xMax, int yMax) const { + return getSection(xMin, yMin, xMax, yMax); + } + +}; + +template +struct BitmapSection { + + T *pixels; + int width, height; + /// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative. + int rowStride; + YAxisOrientation yOrientation; + + inline BitmapSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { } + inline BitmapSection(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { } + inline BitmapSection(T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { } + inline BitmapSection(const BitmapRef &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { } + + inline T *operator()(int x, int y) const { + return pixels+rowStride*y+N*x; + } + + /// Returns a reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapSection getSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapSection(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation); + } + + /// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapConstSection getConstSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapConstSection(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation); + } + + /// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows. + inline void reorient(YAxisOrientation newYAxisOrientation) { + if (yOrientation != newYAxisOrientation) { + pixels += rowStride*(height-1); + rowStride = -rowStride; + yOrientation = newYAxisOrientation; + } + } + +}; + +template +struct BitmapConstSection { + + const T *pixels; + int width, height; + /// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative. + int rowStride; + YAxisOrientation yOrientation; + + inline BitmapConstSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { } + inline BitmapConstSection(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { } + inline BitmapConstSection(const T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { } + inline BitmapConstSection(const BitmapRef &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { } + inline BitmapConstSection(const BitmapConstRef &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { } + inline BitmapConstSection(const BitmapSection &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(orig.rowStride), yOrientation(orig.yOrientation) { } + + inline const T *operator()(int x, int y) const { + return pixels+rowStride*y+N*x; + } + + /// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapConstSection getSection(int xMin, int yMin, int xMax, int yMax) const { + return BitmapConstSection(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation); + } + + /// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax). + inline BitmapConstSection getConstSection(int xMin, int yMin, int xMax, int yMax) const { + return getSection(xMin, yMin, xMax, yMax); + } + + /// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows. + inline void reorient(YAxisOrientation newYAxisOrientation) { + if (yOrientation != newYAxisOrientation) { + pixels += rowStride*(height-1); + rowStride = -rowStride; + yOrientation = newYAxisOrientation; + } + } + +}; + +} diff --git a/src_dependency/msdfgen/msdfgen/core/Contour.cpp b/src_dependency/msdfgen/msdfgen/core/Contour.cpp new file mode 100644 index 000000000..aade65e59 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/Contour.cpp @@ -0,0 +1,90 @@ + +#include "Contour.h" + +#include "arithmetics.hpp" + +namespace msdfgen { + +static double shoelace(const Point2 &a, const Point2 &b) { + return (b.x-a.x)*(a.y+b.y); +} + +void Contour::addEdge(const EdgeHolder &edge) { + edges.push_back(edge); +} + +#ifdef MSDFGEN_USE_CPP11 +void Contour::addEdge(EdgeHolder &&edge) { + edges.push_back((EdgeHolder &&) edge); +} +#endif + +EdgeHolder &Contour::addEdge() { + edges.resize(edges.size()+1); + return edges.back(); +} + +static void boundPoint(double &xMin, double &yMin, double &xMax, double &yMax, Point2 p) { + if (p.x < xMin) xMin = p.x; + if (p.y < yMin) yMin = p.y; + if (p.x > xMax) xMax = p.x; + if (p.y > yMax) yMax = p.y; +} + +void Contour::bound(double &xMin, double &yMin, double &xMax, double &yMax) const { + for (std::vector::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) + (*edge)->bound(xMin, yMin, xMax, yMax); +} + +void Contour::boundMiters(double &xMin, double &yMin, double &xMax, double &yMax, double border, double miterLimit, int polarity) const { + if (edges.empty()) + return; + Vector2 prevDir = edges.back()->direction(1).normalize(true); + for (std::vector::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) { + Vector2 dir = -(*edge)->direction(0).normalize(true); + if (polarity*crossProduct(prevDir, dir) >= 0) { + double miterLength = miterLimit; + double q = .5*(1-dotProduct(prevDir, dir)); + if (q > 0) + miterLength = min(1/sqrt(q), miterLimit); + Point2 miter = (*edge)->point(0)+border*miterLength*(prevDir+dir).normalize(true); + boundPoint(xMin, yMin, xMax, yMax, miter); + } + prevDir = (*edge)->direction(1).normalize(true); + } +} + +int Contour::winding() const { + if (edges.empty()) + return 0; + double total = 0; + if (edges.size() == 1) { + Point2 a = edges[0]->point(0), b = edges[0]->point(1/3.), c = edges[0]->point(2/3.); + total += shoelace(a, b); + total += shoelace(b, c); + total += shoelace(c, a); + } else if (edges.size() == 2) { + Point2 a = edges[0]->point(0), b = edges[0]->point(.5), c = edges[1]->point(0), d = edges[1]->point(.5); + total += shoelace(a, b); + total += shoelace(b, c); + total += shoelace(c, d); + total += shoelace(d, a); + } else { + Point2 prev = edges.back()->point(0); + for (std::vector::const_iterator edge = edges.begin(); edge != edges.end(); ++edge) { + Point2 cur = (*edge)->point(0); + total += shoelace(prev, cur); + prev = cur; + } + } + return sign(total); +} + +void Contour::reverse() { + for (int i = (int) edges.size()/2; i > 0; --i) + EdgeHolder::swap(edges[i-1], edges[edges.size()-i]); + for (std::vector::iterator edge = edges.begin(); edge != edges.end(); ++edge) + (*edge)->reverse(); +} + +} diff --git a/src_dependency/msdfgen/msdfgen/core/Contour.h b/src_dependency/msdfgen/msdfgen/core/Contour.h new file mode 100644 index 000000000..53ceb8eda --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/Contour.h @@ -0,0 +1,34 @@ + +#pragma once + +#include +#include "EdgeHolder.h" + +namespace msdfgen { + +/// A single closed contour of a shape. +class Contour { + +public: + /// The sequence of edges that make up the contour. + std::vector edges; + + /// Adds an edge to the contour. + void addEdge(const EdgeHolder &edge); +#ifdef MSDFGEN_USE_CPP11 + void addEdge(EdgeHolder &&edge); +#endif + /// Creates a new edge in the contour and returns its reference. + EdgeHolder &addEdge(); + /// Adjusts the bounding box to fit the contour. + void bound(double &xMin, double &yMin, double &xMax, double &yMax) const; + /// Adjusts the bounding box to fit the contour border's mitered corners. + void boundMiters(double &xMin, double &yMin, double &xMax, double &yMax, double border, double miterLimit, int polarity) const; + /// Computes the winding of the contour. Returns 1 if positive, -1 if negative. + int winding() const; + /// Reverses the sequence of edges on the contour. + void reverse(); + +}; + +} diff --git a/src_dependency/msdfgen/msdfgen/core/DistanceMapping.cpp b/src_dependency/msdfgen/msdfgen/core/DistanceMapping.cpp new file mode 100644 index 000000000..526e12905 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/DistanceMapping.cpp @@ -0,0 +1,27 @@ + +#include "DistanceMapping.h" + +namespace msdfgen { + +DistanceMapping DistanceMapping::inverse(Range range) { + double rangeWidth = range.upper-range.lower; + return DistanceMapping(rangeWidth, range.lower/(rangeWidth ? rangeWidth : 1)); +} + +DistanceMapping::DistanceMapping() : scale(1), translate(0) { } + +DistanceMapping::DistanceMapping(Range range) : scale(1/(range.upper-range.lower)), translate(-range.lower) { } + +double DistanceMapping::operator()(double d) const { + return scale*(d+translate); +} + +double DistanceMapping::operator()(Delta d) const { + return scale*d.value; +} + +DistanceMapping DistanceMapping::inverse() const { + return DistanceMapping(1/scale, -scale*translate); +} + +} diff --git a/src_dependency/msdfgen/msdfgen/core/DistanceMapping.h b/src_dependency/msdfgen/msdfgen/core/DistanceMapping.h new file mode 100644 index 000000000..fadbefa54 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/DistanceMapping.h @@ -0,0 +1,36 @@ + +#pragma once + +#include "Range.hpp" + +namespace msdfgen { + +/// Linear transformation of signed distance values. +class DistanceMapping { + +public: + /// Explicitly designates value as distance delta rather than an absolute distance. + class Delta { + public: + double value; + inline explicit Delta(double distanceDelta) : value(distanceDelta) { } + inline operator double() const { return value; } + }; + + static DistanceMapping inverse(Range range); + + DistanceMapping(); + DistanceMapping(Range range); + double operator()(double d) const; + double operator()(Delta d) const; + DistanceMapping inverse() const; + +private: + double scale; + double translate; + + inline DistanceMapping(double scale, double translate) : scale(scale), translate(translate) { } + +}; + +} diff --git a/src_dependency/msdfgen/msdfgen/core/EdgeColor.h b/src_dependency/msdfgen/msdfgen/core/EdgeColor.h new file mode 100644 index 000000000..5d3730c9a --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/EdgeColor.h @@ -0,0 +1,20 @@ + +#pragma once + +#include "base.h" + +namespace msdfgen { + +/// Edge color specifies which color channels an edge belongs to. +enum EdgeColor { + BLACK = 0, + RED = 1, + GREEN = 2, + YELLOW = 3, + BLUE = 4, + MAGENTA = 5, + CYAN = 6, + WHITE = 7 +}; + +} diff --git a/src_dependency/msdfgen/msdfgen/core/EdgeHolder.cpp b/src_dependency/msdfgen/msdfgen/core/EdgeHolder.cpp new file mode 100644 index 000000000..f552e98fa --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/EdgeHolder.cpp @@ -0,0 +1,67 @@ + +#include "EdgeHolder.h" + +namespace msdfgen { + +void EdgeHolder::swap(EdgeHolder &a, EdgeHolder &b) { + EdgeSegment *tmp = a.edgeSegment; + a.edgeSegment = b.edgeSegment; + b.edgeSegment = tmp; +} + +EdgeHolder::EdgeHolder(const EdgeHolder &orig) : edgeSegment(orig.edgeSegment ? orig.edgeSegment->clone() : NULL) { } + +#ifdef MSDFGEN_USE_CPP11 +EdgeHolder::EdgeHolder(EdgeHolder &&orig) : edgeSegment(orig.edgeSegment) { + orig.edgeSegment = NULL; +} +#endif + +EdgeHolder::~EdgeHolder() { + delete edgeSegment; +} + +EdgeHolder &EdgeHolder::operator=(const EdgeHolder &orig) { + if (this != &orig) { + delete edgeSegment; + edgeSegment = orig.edgeSegment ? orig.edgeSegment->clone() : NULL; + } + return *this; +} + +#ifdef MSDFGEN_USE_CPP11 +EdgeHolder &EdgeHolder::operator=(EdgeHolder &&orig) { + if (this != &orig) { + delete edgeSegment; + edgeSegment = orig.edgeSegment; + orig.edgeSegment = NULL; + } + return *this; +} +#endif + +EdgeSegment &EdgeHolder::operator*() { + return *edgeSegment; +} + +const EdgeSegment &EdgeHolder::operator*() const { + return *edgeSegment; +} + +EdgeSegment *EdgeHolder::operator->() { + return edgeSegment; +} + +const EdgeSegment *EdgeHolder::operator->() const { + return edgeSegment; +} + +EdgeHolder::operator EdgeSegment *() { + return edgeSegment; +} + +EdgeHolder::operator const EdgeSegment *() const { + return edgeSegment; +} + +} diff --git a/src_dependency/msdfgen/msdfgen/core/EdgeHolder.h b/src_dependency/msdfgen/msdfgen/core/EdgeHolder.h new file mode 100644 index 000000000..5ae2dc231 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/EdgeHolder.h @@ -0,0 +1,41 @@ + +#pragma once + +#include "edge-segments.h" + +namespace msdfgen { + +/// Container for a single edge of dynamic type. +class EdgeHolder { + +public: + /// Swaps the edges held by a and b. + static void swap(EdgeHolder &a, EdgeHolder &b); + + inline EdgeHolder() : edgeSegment() { } + inline EdgeHolder(EdgeSegment *segment) : edgeSegment(segment) { } + inline EdgeHolder(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE) : edgeSegment(EdgeSegment::create(p0, p1, edgeColor)) { } + inline EdgeHolder(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE) : edgeSegment(EdgeSegment::create(p0, p1, p2, edgeColor)) { } + inline EdgeHolder(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE) : edgeSegment(EdgeSegment::create(p0, p1, p2, p3, edgeColor)) { } + EdgeHolder(const EdgeHolder &orig); +#ifdef MSDFGEN_USE_CPP11 + EdgeHolder(EdgeHolder &&orig); +#endif + ~EdgeHolder(); + EdgeHolder &operator=(const EdgeHolder &orig); +#ifdef MSDFGEN_USE_CPP11 + EdgeHolder &operator=(EdgeHolder &&orig); +#endif + EdgeSegment &operator*(); + const EdgeSegment &operator*() const; + EdgeSegment *operator->(); + const EdgeSegment *operator->() const; + operator EdgeSegment *(); + operator const EdgeSegment *() const; + +private: + EdgeSegment *edgeSegment; + +}; + +} diff --git a/src_dependency/msdfgen/msdfgen/core/MSDFErrorCorrection.cpp b/src_dependency/msdfgen/msdfgen/core/MSDFErrorCorrection.cpp new file mode 100644 index 000000000..940db3d49 --- /dev/null +++ b/src_dependency/msdfgen/msdfgen/core/MSDFErrorCorrection.cpp @@ -0,0 +1,496 @@ + +#include "MSDFErrorCorrection.h" + +#include +#include "arithmetics.hpp" +#include "equation-solver.h" +#include "EdgeColor.h" +#include "bitmap-interpolation.hpp" +#include "edge-selectors.h" +#include "contour-combiners.h" +#include "ShapeDistanceFinder.h" +#include "generator-config.h" + +namespace msdfgen { + +#define ARTIFACT_T_EPSILON .01 +#define PROTECTION_RADIUS_TOLERANCE 1.001 + +#define CLASSIFIER_FLAG_CANDIDATE 0x01 +#define CLASSIFIER_FLAG_ARTIFACT 0x02 + +MSDFGEN_PUBLIC const double ErrorCorrectionConfig::defaultMinDeviationRatio = 1.11111111111111111; +MSDFGEN_PUBLIC const double ErrorCorrectionConfig::defaultMinImproveRatio = 1.11111111111111111; + +/// The base artifact classifier recognizes artifacts based on the contents of the SDF alone. +class BaseArtifactClassifier { +public: + inline BaseArtifactClassifier(double span, bool protectedFlag) : span(span), protectedFlag(protectedFlag) { } + /// Evaluates if the median value xm interpolated at xt in the range between am at at and bm at bt indicates an artifact. + inline int rangeTest(double at, double bt, double xt, float am, float bm, float xm) const { + // For protected texels, only consider inversion artifacts (interpolated median has different sign than boundaries). For the rest, it is sufficient that the interpolated median is outside its boundaries. + if ((am > .5f && bm > .5f && xm <= .5f) || (am < .5f && bm < .5f && xm >= .5f) || (!protectedFlag && median(am, bm, xm) != xm)) { + double axSpan = (xt-at)*span, bxSpan = (bt-xt)*span; + // Check if the interpolated median's value is in the expected range based on its distance (span) from boundaries a, b. + if (!(xm >= am-axSpan && xm <= am+axSpan && xm >= bm-bxSpan && xm <= bm+bxSpan)) + return CLASSIFIER_FLAG_CANDIDATE|CLASSIFIER_FLAG_ARTIFACT; + return CLASSIFIER_FLAG_CANDIDATE; + } + return 0; + } + /// Returns true if the combined results of the tests performed on the median value m interpolated at t indicate an artifact. + inline bool evaluate(double t, float m, int flags) const { + return (flags&2) != 0; + } +private: + double span; + bool protectedFlag; +}; + +/// The shape distance checker evaluates the exact shape distance to find additional artifacts at a significant performance cost. +template