Skip to content

Commit

Permalink
Normalized imgui and added texture filter (HarbourMasters#271)
Browse files Browse the repository at this point in the history
* Normalized imgui and added texture filter and fixed develop

* Fixed incorrect separator title
  • Loading branch information
KiritoDv authored and Kenix3 committed Oct 18, 2022
1 parent c3d029a commit 537455a
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 163 deletions.
41 changes: 10 additions & 31 deletions libultraship/GameSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
#include "Cvar.h"
#include "GlobalCtx2.h"
#include "SohImGuiImpl.h"
#include "stox.h"
#include "../../soh/include/z64audio.h"
#include <string>
#include "SohHooks.h"
#include "../../soh/soh/Enhancements/debugconsole.h"

#include "Window.h"
#include "Lib/Fast3D/gfx_rendering_api.h"

#define ABS(var) var < 0 ? -(var) : var

Expand All @@ -25,14 +24,6 @@ using namespace Ship;
namespace Game {

bool DeSyncAudio = false;
SoHConfigType Settings;
const std::string ConfSection = DEBUG_SECTION;
const std::string AudioSection = AUDIO_SECTION;
const std::string ControllerSection = CONTROLLER_SECTION;
const std::string EnhancementSection = ENHANCEMENTS_SECTION;
const std::string CosmeticsSection = COSMETICS_SECTION;
const std::string CheatSection = CHEATS_SECTION;
const std::string LanguagesSection = LANGUAGES_SECTION;

void UpdateAudio() {
Audio_SetGameVolume(SEQ_BGM_MAIN, CVar_GetFloat("gMainMusicVolume", 1));
Expand All @@ -41,18 +32,6 @@ namespace Game {
Audio_SetGameVolume(SEQ_SFX, CVar_GetFloat("gFanfareVolume", 1));
}

void LoadSettings() {
const std::shared_ptr<ConfigFile> pConf = GlobalCtx2::GetInstance()->GetConfig();
ConfigFile& Conf = *pConf;

// Debug
SohImGui::console->opened = stob(Conf[ConfSection]["console"]);
Settings.debug.menu_bar = stob(Conf[ConfSection]["menu_bar"]);
Settings.debug.soh = stob(Conf[ConfSection]["soh_debug"]);

UpdateAudio();
}

void LoadPadSettings() {
const std::shared_ptr<ConfigFile> pConf = GlobalCtx2::GetInstance()->GetConfig();
ConfigFile& Conf = *pConf;
Expand All @@ -65,23 +44,23 @@ namespace Game {
}
}

void SaveSettings() {
const std::shared_ptr<ConfigFile> pConf = GlobalCtx2::GetInstance()->GetConfig();
ConfigFile& Conf = *pConf;

// Debug
Conf[ConfSection]["console"] = std::to_string(SohImGui::console->opened);
Conf[ConfSection]["menu_bar"] = std::to_string(Settings.debug.menu_bar);
Conf[ConfSection]["soh_debug"] = std::to_string(Settings.debug.soh);
void LoadSettings() {
DebugConsole_LoadCVars();
}

Conf.Save();
void SaveSettings() {
DebugConsole_SaveCVars();
}

void InitSettings() {
ModInternal::registerHookListener({ AUDIO_INIT, [](HookEvent ev) {
UpdateAudio();
}});
ModInternal::registerHookListener({ GFX_INIT, [](HookEvent ev) {
gfx_get_current_rendering_api()->set_texture_filter((FilteringMode) CVar_GetS32("gTextureFilter", THREE_POINT));
SohImGui::console->opened = CVar_GetS32("gConsoleEnabled", 0);
UpdateAudio();
}});
}

void SetSeqPlayerVolume(SeqPlayers playerId, float volume) {
Expand Down
23 changes: 0 additions & 23 deletions libultraship/GameSettings.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
#pragma once

struct SoHConfigType {
// Debug
struct {
bool soh = false;
bool menu_bar = false;
bool soh_sink = true;
} debug;

// Graphics
struct {
bool show = false;
} graphics;
};

enum SeqPlayers {
/* 0 */ SEQ_BGM_MAIN,
/* 1 */ SEQ_FANFARE,
Expand All @@ -22,16 +8,7 @@ enum SeqPlayers {
/* 4 */ SEQ_MAX
};

#define DEBUG_SECTION "DEBUG SETTINGS"
#define AUDIO_SECTION "AUDIO SETTINGS"
#define CONTROLLER_SECTION "CONTROLLER SECTION"
#define ENHANCEMENTS_SECTION "ENHANCEMENT SETTINGS"
#define COSMETICS_SECTION "COSMETIC SETTINGS"
#define CHEATS_SECTION "CHEATS SETTINGS"
#define LANGUAGES_SECTION "LANGUAGES SETTINGS"

namespace Game {
extern SoHConfigType Settings;
void InitSettings();
void LoadSettings();
void LoadPadSettings();
Expand Down
43 changes: 26 additions & 17 deletions libultraship/Lib/Fast3D/gfx_direct3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
#ifndef _LANGUAGE_C
#define _LANGUAGE_C
#endif
#include <PR/ultra64/gbi.h>
#include "PR/ultra64/gbi.h"

#include "gfx_cc.h"
#include "gfx_window_manager_api.h"
#include "gfx_rendering_api.h"
#include "gfx_direct3d_common.h"

#define DECLARE_GFX_DXGI_FUNCTIONS
Expand All @@ -28,7 +26,9 @@
#include "gfx_screen_config.h"
#include "../../SohImGuiImpl.h"

#define THREE_POINT_FILTERING 0
#include "gfx_cc.h"
#include "gfx_rendering_api.h"
#include "gfx_pc.h"
#define DEBUG_D3D 0

using namespace Microsoft::WRL; // For ComPtr
Expand Down Expand Up @@ -135,6 +135,7 @@ static struct {
//uint32_t current_width, current_height;
uint32_t render_target_height;
int current_framebuffer;
FilteringMode current_filter_mode = NONE;

int8_t depth_test;
int8_t depth_mask;
Expand Down Expand Up @@ -397,7 +398,7 @@ static struct ShaderProgram *gfx_d3d11_create_and_load_new_shader(uint64_t shade
char buf[4096];
size_t len, num_floats;

gfx_direct3d_common_build_shader(buf, len, num_floats, cc_features, false, THREE_POINT_FILTERING);
gfx_direct3d_common_build_shader(buf, len, num_floats, cc_features, false, d3d.current_filter_mode == THREE_POINT);

ComPtr<ID3DBlob> vs, ps;
ComPtr<ID3DBlob> error_blob;
Expand Down Expand Up @@ -564,11 +565,8 @@ static void gfx_d3d11_set_sampler_parameters(int tile, bool linear_filter, uint3
D3D11_SAMPLER_DESC sampler_desc;
ZeroMemory(&sampler_desc, sizeof(D3D11_SAMPLER_DESC));

#if THREE_POINT_FILTERING
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
#else
sampler_desc.Filter = linear_filter ? D3D11_FILTER_MIN_MAG_MIP_LINEAR : D3D11_FILTER_MIN_MAG_MIP_POINT;
#endif
sampler_desc.Filter = linear_filter && d3d.current_filter_mode == LINEAR ? D3D11_FILTER_MIN_MAG_MIP_LINEAR : D3D11_FILTER_MIN_MAG_MIP_POINT;

sampler_desc.AddressU = gfx_cm_to_d3d11(cms);
sampler_desc.AddressV = gfx_cm_to_d3d11(cmt);
sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
Expand Down Expand Up @@ -672,12 +670,12 @@ static void gfx_d3d11_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_t
d3d.last_resource_views[i] = d3d.textures[d3d.current_texture_ids[i]].resource_view.Get();
d3d.context->PSSetShaderResources(i, 1, d3d.textures[d3d.current_texture_ids[i]].resource_view.GetAddressOf());

#if THREE_POINT_FILTERING
d3d.per_draw_cb_data.textures[i].width = d3d.textures[d3d.current_texture_ids[i]].width;
d3d.per_draw_cb_data.textures[i].height = d3d.textures[d3d.current_texture_ids[i]].height;
d3d.per_draw_cb_data.textures[i].linear_filtering = d3d.textures[d3d.current_texture_ids[i]].linear_filtering;
textures_changed = true;
#endif
if (d3d.current_filter_mode == THREE_POINT) {
d3d.per_draw_cb_data.textures[i].width = d3d.textures[d3d.current_texture_ids[i]].width;
d3d.per_draw_cb_data.textures[i].height = d3d.textures[d3d.current_texture_ids[i]].height;
d3d.per_draw_cb_data.textures[i].linear_filtering = d3d.textures[d3d.current_texture_ids[i]].linear_filtering;
textures_changed = true;
}

if (d3d.last_sampler_states[i].Get() != d3d.textures[d3d.current_texture_ids[i]].sampler_state.Get()) {
d3d.last_sampler_states[i] = d3d.textures[d3d.current_texture_ids[i]].sampler_state.Get();
Expand Down Expand Up @@ -880,6 +878,15 @@ void gfx_d3d11_select_texture_fb(int fbID) {
gfx_d3d11_select_texture(tile, d3d.framebuffers[fbID].texture_id);
}

void gfx_d3d11_set_texture_filter(FilteringMode mode) {
d3d.current_filter_mode = mode;
gfx_texture_cache_clear();
}

FilteringMode gfx_d3d11_get_texture_filter(void) {
return d3d.current_filter_mode;
}

std::map<std::pair<float, float>, uint16_t> gfx_d3d11_get_pixel_depth(int fb_id, const std::set<std::pair<float, float>>& coordinates) {
Framebuffer& fb = d3d.framebuffers[fb_id];
TextureData& td = d3d.textures[fb.texture_id];
Expand Down Expand Up @@ -1019,7 +1026,9 @@ struct GfxRenderingAPI gfx_direct3d11_api = {
gfx_d3d11_get_pixel_depth,
gfx_d3d11_get_framebuffer_texture_id,
gfx_d3d11_select_texture_fb,
gfx_d3d11_delete_texture
gfx_d3d11_delete_texture,
gfx_d3d11_set_texture_filter,
gfx_d3d11_get_texture_filter
};

#endif
56 changes: 45 additions & 11 deletions libultraship/Lib/Fast3D/gfx_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static uint32_t frame_count;
static vector<Framebuffer> framebuffers;
static size_t current_framebuffer;
static float current_noise_scale;
static FilteringMode current_filter_mode = THREE_POINT;

GLuint pixel_depth_rb, pixel_depth_fb;
size_t pixel_depth_rb_size;
Expand Down Expand Up @@ -211,7 +212,7 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
gfx_cc_get_features(shader_id0, shader_id1, &cc_features);

char vs_buf[1024];
char fs_buf[1024];
char fs_buf[3000];
size_t vs_len = 0;
size_t fs_len = 0;
size_t num_floats = 4;
Expand Down Expand Up @@ -312,21 +313,42 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
append_line(fs_buf, &fs_len, "}");
}

if (current_filter_mode == THREE_POINT) {
append_line(fs_buf, &fs_len, "#define TEX_OFFSET(off) texture2D(tex, texCoord - (off)/texSize)");
append_line(fs_buf, &fs_len, "vec4 filter3point(in sampler2D tex, in vec2 texCoord, in vec2 texSize) {");
append_line(fs_buf, &fs_len, " vec2 offset = fract(texCoord*texSize - vec2(0.5));");
append_line(fs_buf, &fs_len, " offset -= step(1.0, offset.x + offset.y);");
append_line(fs_buf, &fs_len, " vec4 c0 = TEX_OFFSET(offset);");
append_line(fs_buf, &fs_len, " vec4 c1 = TEX_OFFSET(vec2(offset.x - sign(offset.x), offset.y));");
append_line(fs_buf, &fs_len, " vec4 c2 = TEX_OFFSET(vec2(offset.x, offset.y - sign(offset.y)));");
append_line(fs_buf, &fs_len, " return c0 + abs(offset.x)*(c1-c0) + abs(offset.y)*(c2-c0);");
append_line(fs_buf, &fs_len, "}");
append_line(fs_buf, &fs_len, "vec4 hookTexture2D(in sampler2D tex, in vec2 uv, in vec2 texSize) {");
append_line(fs_buf, &fs_len, " return filter3point(tex, uv, texSize);");
append_line(fs_buf, &fs_len, "}");
} else {
append_line(fs_buf, &fs_len, "vec4 hookTexture2D(in sampler2D tex, in vec2 uv, in vec2 texSize) {");
append_line(fs_buf, &fs_len, " return texture2D(tex, uv);");
append_line(fs_buf, &fs_len, "}");
}

append_line(fs_buf, &fs_len, "void main() {");

for (int i = 0; i < 2; i++) {
if (cc_features.used_textures[i]) {
bool s = cc_features.clamp[i][0], t = cc_features.clamp[i][1];

fs_len += sprintf(fs_buf + fs_len, "vec2 texSize%d = textureSize(uTex%d, 0);\n", i, i);

if (!s && !t) {
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = texture2D(uTex%d, vTexCoord%d);\n", i, i, i);
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = hookTexture2D(uTex%d, vTexCoord%d, texSize%d);\n", i, i, i, i);
} else {
fs_len += sprintf(fs_buf + fs_len, "vec2 texSize%d = textureSize(uTex%d, 0);\n", i, i);
if (s && t) {
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = texture2D(uTex%d, clamp(vTexCoord%d, 0.5 / texSize%d, vec2(vTexClampS%d, vTexClampT%d)));\n", i, i, i, i, i, i);
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = hookTexture2D(uTex%d, clamp(vTexCoord%d, 0.5 / texSize%d, vec2(vTexClampS%d, vTexClampT%d)), texSize%d);\n", i, i, i, i, i, i, i);
} else if (s) {
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = texture2D(uTex%d, vec2(clamp(vTexCoord%d.s, 0.5 / texSize%d.s, vTexClampS%d), vTexCoord%d.t));\n", i, i, i, i, i, i);
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = hookTexture2D(uTex%d, vec2(clamp(vTexCoord%d.s, 0.5 / texSize%d.s, vTexClampS%d), vTexCoord%d.t), texSize%d);\n", i, i, i, i, i, i, i);
} else {
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = texture2D(uTex%d, vec2(vTexCoord%d.s, clamp(vTexCoord%d.t, 0.5 / texSize%d.t, vTexClampT%d)));\n", i, i, i, i, i, i);
fs_len += sprintf(fs_buf + fs_len, "vec4 texVal%d = hookTexture2D(uTex%d, vec2(vTexCoord%d.s, clamp(vTexCoord%d.t, 0.5 / texSize%d.t, vTexClampT%d)), texSize%d);\n", i, i, i, i, i, i, i);
}
}
}
Expand Down Expand Up @@ -422,9 +444,9 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad
GLint max_length = 0;
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &max_length);
char error_log[1024];
//fprintf(stderr, "Fragment shader compilation failed\n");
fprintf(stderr, "Fragment shader compilation failed\n");
glGetShaderInfoLog(fragment_shader, max_length, &max_length, &error_log[0]);
//fprintf(stderr, "%s\n", &error_log[0]);
fprintf(stderr, "%s\n", &error_log[0]);
abort();
}

Expand Down Expand Up @@ -552,9 +574,10 @@ static uint32_t gfx_cm_to_opengl(uint32_t val) {
}

static void gfx_opengl_set_sampler_parameters(int tile, bool linear_filter, uint32_t cms, uint32_t cmt) {
const GLint filter = linear_filter && current_filter_mode == LINEAR ? GL_LINEAR : GL_NEAREST;
glActiveTexture(GL_TEXTURE0 + tile);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gfx_cm_to_opengl(cms));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gfx_cm_to_opengl(cmt));
}
Expand Down Expand Up @@ -823,6 +846,15 @@ static std::map<std::pair<float, float>, uint16_t> gfx_opengl_get_pixel_depth(in
return res;
}

void gfx_opengl_set_texture_filter(FilteringMode mode) {
current_filter_mode = mode;
gfx_texture_cache_clear();
}

FilteringMode gfx_opengl_get_texture_filter(void) {
return current_filter_mode;
}

struct GfxRenderingAPI gfx_opengl_api = {
gfx_opengl_get_clip_parameters,
gfx_opengl_unload_shader,
Expand Down Expand Up @@ -853,7 +885,9 @@ struct GfxRenderingAPI gfx_opengl_api = {
gfx_opengl_get_pixel_depth,
gfx_opengl_get_framebuffer_texture_id,
gfx_opengl_select_texture_fb,
gfx_opengl_delete_texture
gfx_opengl_delete_texture,
gfx_opengl_set_texture_filter,
gfx_opengl_get_texture_filter
};

#endif
8 changes: 8 additions & 0 deletions libultraship/Lib/Fast3D/gfx_rendering_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ struct GfxClipParameters {
bool invert_y;
};

enum FilteringMode {
THREE_POINT,
LINEAR,
NONE
};

struct GfxRenderingAPI {
struct GfxClipParameters (*get_clip_parameters)(void);
void (*unload_shader)(struct ShaderProgram *old_prg);
Expand Down Expand Up @@ -46,6 +52,8 @@ struct GfxRenderingAPI {
void *(*get_framebuffer_texture_id)(int fb_id);
void (*select_texture_fb)(int fb_id);
void (*delete_texture)(uint32_t texID);
void (*set_texture_filter)(FilteringMode mode);
FilteringMode(*get_texture_filter)(void);
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <spdlog/details/synchronous_factory.h>
#include "SohImGuiImpl.h"
#include "GameSettings.h"
#include "Cvar.h"
#include <chrono>
#include <mutex>
#include <string>
Expand Down Expand Up @@ -45,7 +46,7 @@ class sohconsole_sink final : public base_sink<Mutex>
}
formatted.push_back('\0');
const char *msg_output = formatted.data();
if (Game::Settings.debug.soh_sink && SohImGui::console->opened)
if (CVar_GetS32("gSinkEnabled", 0) && SohImGui::console->opened)
SohImGui::console->Append("SoH Logging", priority, msg_output);
}

Expand Down
2 changes: 1 addition & 1 deletion libultraship/SohConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void Console::Draw() {
if (!this->opened) return;

ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver);
ImGui::Begin("Console", &this->opened);
ImGui::Begin("Console", nullptr);
const ImVec2 pos = ImGui::GetWindowPos();
const ImVec2 size = ImGui::GetWindowSize();

Expand Down
Loading

0 comments on commit 537455a

Please sign in to comment.