Skip to content

Commit

Permalink
D3/Q4/Prey: allow use high precision float on shader
Browse files Browse the repository at this point in the history
  • Loading branch information
glKarin committed Nov 19, 2024
1 parent 76e49ba commit e9a30ce
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android.changelog
Original file line number Diff line number Diff line change
@@ -1 +1 @@
doom3bfg, thedarkmod on mali GPU, phobos fix
doom3bfg, thedarkmod on mali GPU, phobos fix, highp
2 changes: 1 addition & 1 deletion .github/workflows/win_linux.changelog
Original file line number Diff line number Diff line change
@@ -1 +1 @@
phobos
phobos, highp
4 changes: 2 additions & 2 deletions Q3E/src/main/java/com/n0n3m4/q3e/Q3EInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,8 @@ public String MakeTempBaseCommand(Context context)
{
if(preferences.getBoolean(Q3EPreference.pref_harm_gzdoom_load_lights_pk3, true))
extraCommand = Q3EUtils.q3ei.GetGameCommandEngine(extraCommand).AddParam("file", "lights.pk3").toString();
if(preferences.getBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, true))
extraCommand = Q3EUtils.q3ei.GetGameCommandEngine(extraCommand).AddParam("file", "game_support.pk3").toString();
// if(preferences.getBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, true))
// extraCommand = Q3EUtils.q3ei.GetGameCommandEngine(extraCommand).AddParam("file", "game_support.pk3").toString();
if(preferences.getBoolean(Q3EPreference.pref_harm_gzdoom_load_brightmaps_pk3, true))
extraCommand = Q3EUtils.q3ei.GetGameCommandEngine(extraCommand).AddParam("file", "brightmaps.pk3").toString();
}
Expand Down
3 changes: 2 additions & 1 deletion Q3E/src/main/java/com/n0n3m4/q3e/Q3EPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ public final class Q3EPreference
public static final String pref_harm_r_stencilShadowSoft = "q3e_harm_r_stencilShadowSoft"; //k
public static final String pref_harm_r_stencilShadowCombine = "q3e_harm_r_stencilShadowCombine"; //k
public static final String pref_harm_r_autoAspectRatio = "q3e_harm_r_autoAspectRatio"; //k
public static final String pref_harm_r_useHighPrecision = "q3e_harm_r_useHighPrecision"; //k
public static final String pref_scrres_scheme = "harm_q3e_scrres";
public static final String pref_scrres_scale = "harm_q3e_scrres_scale";
public static final String pref_harm_r_shadowMapPerforatedShadow = "q3e_harm_r_shadowMapPerforatedShadow"; //k
public static final String pref_harm_gzdoom_load_lights_pk3 = "harm_gzdoom_load_lights_pk3";
public static final String pref_harm_gzdoom_load_game_support_pk3 = "harm_gzdoom_load_game_support_pk3";
//public static final String pref_harm_gzdoom_load_game_support_pk3 = "harm_gzdoom_load_game_support_pk3";
public static final String pref_harm_gzdoom_load_brightmaps_pk3 = "harm_gzdoom_load_brightmaps_pk3";

public static final String RUN_BACKGROUND = "harm_run_background";
Expand Down
7 changes: 7 additions & 0 deletions Q3E/src/main/jni/doom3/neo/renderer/RenderSystem_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ idCVar r_scaleMenusTo43( "r_scaleMenusTo43", "0", CVAR_RENDERER | CVAR_ARCHIVE |
// #warning "For fix `DOOM3: The lost mission` mod, when load `game/le_hell` map(loading resource `models/mapobjects/hell/hellintro.lwo` model, a larger scene, alloca() stack out of memory)."
/*static */idCVar harm_r_maxAllocStackMemory("harm_r_maxAllocStackMemory", "524288", CVAR_INTEGER|CVAR_RENDERER|CVAR_ARCHIVE, "Control allocate temporary memory when load model data, default value is `524288` bytes(Because stack memory is limited on OS:\n 0 = Always heap;\n Negative = Always stack;\n Positive = Max stack memory limit(If less than this `byte` value, call `alloca` in stack memory, else call `malloc`/`calloc` in heap memory)).");
#endif
idCVar harm_r_useHighPrecision("harm_r_useHighPrecision",
#ifdef __ANDROID__
"0"
#else
"1"
#endif
, CVAR_RENDERER | CVAR_BOOL | CVAR_INIT, "Use high precision float on GLSL shader");

#ifdef _USING_STB
idCVar r_screenshotFormat("r_screenshotFormat", "0", CVAR_RENDERER | CVAR_ARCHIVE | CVAR_INTEGER, "Screenshot format. 0 = TGA (default), 1 = BMP, 2 = PNG, 3 = JPG, 4 = DDS", 0, 4, idCmdSystem::ArgCompletion_Integer<0, 4>);
Expand Down
46 changes: 16 additions & 30 deletions Q3E/src/main/jni/doom3/neo/renderer/glsl/draw_glsl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,39 +530,23 @@ static int RB_GLSL_ParseMacros(const char *macros, idStrList &ret)
return counter;
}

static idStr RB_GLSL_SetupPrecision(const char *source, const char *precision)
static idStr RB_GLSL_ExpandMacros(const char *source, const char *macros, bool highp = false)
{
if(!precision || !precision[0])
return idStr(source);

idStr res(source);
res.Replace("precision", "// precision");

int index = res.Find("#version");
if(index == -1)
SHADER_ERROR("[Harmattan]: GLSL shader source can not find '#version'\n.");

index = res.Find('\n', index);
if(index == -1 || index + 1 == res.Length())
SHADER_ERROR("[Harmattan]: GLSL shader source '#version' not completed\n.");

res.Insert(idStr("precision ") + precision + " float;\n", index + 1);

//printf("%d|%s|\n%s\n", n, macros, res.c_str());
return res;
}
if(highp)
{
res.Replace("precision mediump float;", "precision highp float;");
res.Replace("precision lowp float;", "precision highp float;");
}

static idStr RB_GLSL_ExpandMacros(const char *source, const char *macros)
{
if(!macros || !macros[0])
return idStr(source);
return res;

idStrList list;
int n = RB_GLSL_ParseMacros(macros, list);
if(0 == n)
return idStr(source);
return res;

idStr res(source);
int index = res.Find("#version");
if(index == -1)
SHADER_ERROR("[Harmattan]: GLSL shader source can not find '#version'\n.");
Expand Down Expand Up @@ -1190,8 +1174,10 @@ int RB_GLSL_LoadShaderProgram(

if (!RB_GLSL_LinkShader(program, true)/* && !RB_GLSL_ValidateProgram(program)*/) {
common->Printf("[Harmattan]: 2. Load internal shader source\n");
idStr vs = RB_GLSL_ExpandMacros(default_vertex_shader_source, macros);
idStr fs = RB_GLSL_ExpandMacros(default_fragment_shader_source, macros);
if(harm_r_useHighPrecision.GetBool())
common->Printf("'%s' use high precision float\n", name);
idStr vs = RB_GLSL_ExpandMacros(default_vertex_shader_source, macros, harm_r_useHighPrecision.GetBool());
idStr fs = RB_GLSL_ExpandMacros(default_fragment_shader_source, macros, harm_r_useHighPrecision.GetBool());
if(!RB_GLSL_CreateShaderProgram(program, vs.c_str(), fs.c_str(), name, type))
{
SHADER_ERROR("[Harmattan]: Load internal shader program fail!\n");
Expand Down Expand Up @@ -1248,13 +1234,13 @@ void R_ExportGLSLShaderSource_f(const idCmdArgs &args)
fs = prop.default_fragment_shader_source.c_str();
macros = prop.macros.c_str();

idStr vsSrc = RB_GLSL_ExpandMacros(vs, macros);
idStr vsSrc = RB_GLSL_ExpandMacros(vs, macros, harm_r_useHighPrecision.GetBool());
idStr p(path);
p.Append(prop.vertex_shader_source_file);
fileSystem->WriteFile(p.c_str(), vsSrc.c_str(), vsSrc.Length(), "fs_basepath");
common->Printf("GLSL vertex shader: '%s'\n", p.c_str());

idStr fsSrc = RB_GLSL_ExpandMacros(fs, macros);
idStr fsSrc = RB_GLSL_ExpandMacros(fs, macros, harm_r_useHighPrecision.GetBool());
p = path;
p.Append(prop.fragment_shader_source_file);
fileSystem->WriteFile(p.c_str(), fsSrc.c_str(), fsSrc.Length(), "fs_basepath");
Expand Down Expand Up @@ -1299,12 +1285,12 @@ void R_PrintGLSLShaderSource_f(const idCmdArgs &args)
macros = prop.macros.c_str();
common->Printf("GLSL shader: %s\n\n", prop.name.c_str());

idStr vsSrc = RB_GLSL_ExpandMacros(vs, macros);
idStr vsSrc = RB_GLSL_ExpandMacros(vs, macros, harm_r_useHighPrecision.GetBool());
common->Printf(" Vertex shader: \n");
R_PrintGLSLShaderSource(vsSrc);
common->Printf("\n");

idStr fsSrc = RB_GLSL_ExpandMacros(fs, macros);
idStr fsSrc = RB_GLSL_ExpandMacros(fs, macros, harm_r_useHighPrecision.GetBool());
common->Printf(" Fragment shader: \n");
R_PrintGLSLShaderSource(fsSrc);
common->Printf("\n");
Expand Down
2 changes: 2 additions & 0 deletions Q3E/src/main/jni/doom3/neo/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -2342,4 +2342,6 @@ extern idCVar harm_r_lightingModel;
#define HARM_INTERACTION_SHADER_PBR 3
#define HARM_INTERACTION_SHADER_AMBIENT 4

extern idCVar harm_r_useHighPrecision;

#endif /* !__TR_LOCAL_H__ */
32 changes: 24 additions & 8 deletions idTech4Amm/src/main/java/com/n0n3m4/DIII4A/GameLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ else if (id == R.id.collapse_mods)
.putBoolean(PreferenceKey.COLLAPSE_MODS, isChecked)
.commit();
}
else if (id == R.id.cb_useHighPrecision)
{
if(Q3EUtils.q3ei.IsIdTech4())
setProp("harm_r_useHighPrecision", isChecked);
PreferenceManager.getDefaultSharedPreferences(GameLauncher.this).edit()
.putBoolean(Q3EPreference.pref_harm_r_useHighPrecision, isChecked)
.commit();
}

// Doom 3 BFG
else if (id == R.id.doom3bfg_useCompressionCache)
Expand Down Expand Up @@ -432,13 +440,13 @@ else if (id == R.id.gzdoom_load_lights_pk3)
.commit();
SetupGZDOOMFiles("file", "lights.pk3", isChecked);
}
else if (id == R.id.gzdoom_load_game_support_pk3)
/* else if (id == R.id.gzdoom_load_game_support_pk3)
{
PreferenceManager.getDefaultSharedPreferences(GameLauncher.this).edit()
.putBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, isChecked)
.commit();
SetupGZDOOMFiles("file", "game_support.pk3", isChecked);
}
}*/
else if (id == R.id.gzdoom_load_brightmaps_pk3)
{
PreferenceManager.getDefaultSharedPreferences(GameLauncher.this).edit()
Expand Down Expand Up @@ -1064,7 +1072,7 @@ public void updatehacktings()
if (!IsProp("harm_r_stencilShadowAlpha")) SetProp("harm_r_stencilShadowAlpha", "1.0");

V.cb_stencilShadowSoft.setChecked(getProp("harm_r_stencilShadowSoft", false));
if (!IsProp("harm_r_stencilShadowSoft")) setProp("harm_r_stencilShadowTranslucent", false);
if (!IsProp("harm_r_stencilShadowSoft")) setProp("harm_r_stencilShadowSoft", false);
V.cb_stencilShadowCombine.setChecked(getProp("harm_r_stencilShadowCombine", false));
if (!IsProp("harm_r_stencilShadowCombine")) setProp("harm_r_stencilShadowCombine", false);
V.cb_perforatedShadow.setChecked(getProp("r_forceShadowMapsOnAlphaTestedSurfaces", false));
Expand All @@ -1087,6 +1095,9 @@ public void updatehacktings()
index = Q3EUtils.parseInt_s(str, 1);
SelectCheckbox(V.rg_r_autoAspectRatio, index);
if (!IsProp("harm_r_autoAspectRatio")) SetProp("harm_r_autoAspectRatio", 1);

V.cb_useHighPrecision.setChecked(getProp("harm_r_useHighPrecision", false));
if (!IsProp("harm_r_useHighPrecision")) setProp("harm_r_useHighPrecision", false);
}
else if(Q3EUtils.q3ei.isQ2)
{
Expand Down Expand Up @@ -1661,6 +1672,8 @@ public void afterTextChanged(Editable s)
V.cb_stencilShadowCombine.setOnCheckedChangeListener(m_checkboxChangeListener);
V.cb_perforatedShadow.setChecked(mPrefs.getBoolean(Q3EPreference.pref_harm_r_shadowMapPerforatedShadow, false));
V.cb_perforatedShadow.setOnCheckedChangeListener(m_checkboxChangeListener);
V.cb_useHighPrecision.setChecked(mPrefs.getBoolean(Q3EPreference.pref_harm_r_useHighPrecision, false));
V.cb_useHighPrecision.setOnCheckedChangeListener(m_checkboxChangeListener);

boolean collapseMods = mPrefs.getBoolean(PreferenceKey.COLLAPSE_MODS, false);
V.collapse_mods.setChecked(collapseMods);
Expand Down Expand Up @@ -1764,11 +1777,11 @@ private void SetupUI_GZDOOM()
AddParam_temp("file", "lights.pk3");
V.gzdoom_load_lights_pk3.setOnCheckedChangeListener(m_checkboxChangeListener);

load = mPrefs.getBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, true);
/* load = mPrefs.getBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, true);
V.gzdoom_load_game_support_pk3.setChecked(load);
if (load && (Q3EUtils.q3ei.isDOOM))
AddParam_temp("file", "game_support.pk3");
V.gzdoom_load_game_support_pk3.setOnCheckedChangeListener(m_checkboxChangeListener);
V.gzdoom_load_game_support_pk3.setOnCheckedChangeListener(m_checkboxChangeListener);*/

load = mPrefs.getBoolean(Q3EPreference.pref_harm_gzdoom_load_brightmaps_pk3, true);
V.gzdoom_load_brightmaps_pk3.setChecked(load);
Expand Down Expand Up @@ -2276,11 +2289,12 @@ private void WritePreferences()
mEdtr.putBoolean(Q3EPreference.pref_harm_r_stencilShadowSoft, V.cb_stencilShadowSoft.isChecked());
mEdtr.putBoolean(Q3EPreference.pref_harm_r_stencilShadowCombine, V.cb_stencilShadowCombine.isChecked());
mEdtr.putBoolean(Q3EPreference.pref_harm_r_shadowMapPerforatedShadow, V.cb_perforatedShadow.isChecked());
mEdtr.putBoolean(Q3EPreference.pref_harm_r_useHighPrecision, V.cb_useHighPrecision.isChecked());
mEdtr.putInt(Q3EPreference.pref_harm_r_autoAspectRatio, GetCheckboxIndex(V.rg_r_autoAspectRatio));
mEdtr.putBoolean(PreferenceKey.COLLAPSE_MODS, V.collapse_mods.isChecked());

mEdtr.putBoolean(Q3EPreference.pref_harm_gzdoom_load_lights_pk3, V.gzdoom_load_lights_pk3.isChecked());
mEdtr.putBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, V.gzdoom_load_game_support_pk3.isChecked());
// mEdtr.putBoolean(Q3EPreference.pref_harm_gzdoom_load_game_support_pk3, V.gzdoom_load_game_support_pk3.isChecked());
mEdtr.putBoolean(Q3EPreference.pref_harm_gzdoom_load_brightmaps_pk3, V.gzdoom_load_brightmaps_pk3.isChecked());
// mEdtr.putString(Q3EUtils.q3ei.GetGameModPreferenceKey(), V.edt_fs_game.getText().toString());
mEdtr.commit();
Expand Down Expand Up @@ -3721,6 +3735,7 @@ private class ViewHolder
public CheckBox cb_s_useEAXReverb;
public Switch readonly_command;
public CheckBox cb_stencilShadowTranslucent;
public CheckBox cb_useHighPrecision;
public Switch editable_temp_command;
public LinearLayout temp_cmdline;
public LinearLayout idtech4_section;
Expand Down Expand Up @@ -3776,7 +3791,7 @@ private class ViewHolder
public CheckBox etw_stencilShadowPersonal;
public LinearLayout gzdoom_section;
public CheckBox gzdoom_load_lights_pk3;
public CheckBox gzdoom_load_game_support_pk3;
//public CheckBox gzdoom_load_game_support_pk3;
public CheckBox gzdoom_load_brightmaps_pk3;
public LinearLayout tdm_section;
public CheckBox tdm_useMediumPrecision;
Expand Down Expand Up @@ -3847,6 +3862,7 @@ public void Setup()
cb_s_useEAXReverb = findViewById(R.id.cb_s_useEAXReverb);
readonly_command = findViewById(R.id.readonly_command);
cb_stencilShadowTranslucent = findViewById(R.id.cb_stencilShadowTranslucent);
cb_useHighPrecision = findViewById(R.id.cb_useHighPrecision);
editable_temp_command = findViewById(R.id.editable_temp_command);
temp_cmdline = findViewById(R.id.temp_cmdline);
idtech4_section = findViewById(R.id.idtech4_section);
Expand Down Expand Up @@ -3902,7 +3918,7 @@ public void Setup()
etw_stencilShadowPersonal = findViewById(R.id.etw_stencilShadowPersonal);
gzdoom_section = findViewById(R.id.gzdoom_section);
gzdoom_load_lights_pk3 = findViewById(R.id.gzdoom_load_lights_pk3);
gzdoom_load_game_support_pk3 = findViewById(R.id.gzdoom_load_game_support_pk3);
//gzdoom_load_game_support_pk3 = findViewById(R.id.gzdoom_load_game_support_pk3);
gzdoom_load_brightmaps_pk3 = findViewById(R.id.gzdoom_load_brightmaps_pk3);
tdm_section = findViewById(R.id.tdm_section);
tdm_useMediumPrecision = findViewById(R.id.tdm_useMediumPrecision);
Expand Down
4 changes: 2 additions & 2 deletions idTech4Amm/src/main/res/layout/launcher_gzdoom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
android:checked="true"
android:text="@string/load_lights_pk3" />

<CheckBox
<!-- <CheckBox
android:id="@+id/gzdoom_load_game_support_pk3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/load_game_support_pk3" />
android:text="@string/load_game_support_pk3" />-->

<CheckBox
android:id="@+id/gzdoom_load_brightmaps_pk3"
Expand Down
6 changes: 6 additions & 0 deletions idTech4Amm/src/main/res/layout/launcher_tab1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,12 @@
android:layout_height="wrap_content"
android:text="@string/multi_threading" />

<CheckBox
android:id="@+id/cb_useHighPrecision"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/use_high_precision_float_on_glsl_shader_harm_r_usehighprecision" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions idTech4Amm/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,6 @@
<string name="not_allowed">Not allowed</string>
<string name="use_medium_precision_float_in_glsl_shader_harm_r_usemediumprecision">Use medium precision float instead of high precision in GLSL shader(harm_r_useMediumPrecision)</string>
<string name="phobos">Phobos</string>
<string name="use_high_precision_float_on_glsl_shader_harm_r_usehighprecision">Use high precision float on GLSL shader(harm_r_useHighPrecision)</string>

</resources>
1 change: 1 addition & 0 deletions idTech4Amm/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,5 @@
<string name="not_allowed">不允许</string>
<string name="use_medium_precision_float_in_glsl_shader_harm_r_usemediumprecision">GLSL着色器使用中精度浮点数代替高精度浮点数(harm_r_useMediumPrecision)</string>
<string name="phobos">Phobos</string>
<string name="use_high_precision_float_on_glsl_shader_harm_r_usehighprecision">GLSL着色器使用高精度浮点数(harm_r_useHighPrecision)</string>
</resources>
1 change: 1 addition & 0 deletions idTech4Amm/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,6 @@
<string name="not_allowed">Not allowed</string>
<string name="use_medium_precision_float_in_glsl_shader_harm_r_usemediumprecision">Use medium precision float instead of high precision in GLSL shader(harm_r_useMediumPrecision)</string>
<string name="phobos">Phobos</string>
<string name="use_high_precision_float_on_glsl_shader_harm_r_usehighprecision">Use high precision float on GLSL shader(harm_r_useHighPrecision)</string>

</resources>

0 comments on commit e9a30ce

Please sign in to comment.