Skip to content

Commit

Permalink
Added customizable post processing shaders support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Jul 29, 2020
1 parent 954e511 commit a41968a
Show file tree
Hide file tree
Showing 30 changed files with 127 additions and 62 deletions.
1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/FXAA/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fast Approximate Anti-Aliasing shader.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/Greyscale/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Greyscale filter shader.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/Hue & Saturation/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Customizable effect to alter color hue and saturation.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ float3 HSVtoRGB(float3 HSV)

float4 main(
float2 vTexcoord : TEXCOORD0,
uniform sampler2D uTexture : TEXUNIT0) : COLOR
uniform sampler2D uTexture : TEXUNIT0,
uniform float Hue,
uniform float Saturation) : COLOR
{
float3 color = tex2D(uTexture, vTexcoord).rgb;
float3 col_hsv = RGBtoHSV(color);
col_hsv.y *= 0.8;
col_hsv.x *= Hue;
col_hsv.y *= Saturation;
float3 col_rgb = HSVtoRGB(col_hsv);

return float4(col_rgb, 1.0);
Expand Down
2 changes: 2 additions & 0 deletions Data/DaedalusX64/Shaders/Hue & Saturation/unif.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hue=1
Saturation=1
File renamed without changes.
1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/LCD 3x/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Smooth scaling algorithm with LCD screen effect.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/Negative/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Negative colors filter shader.
File renamed without changes.
File renamed without changes.
34 changes: 0 additions & 34 deletions Data/DaedalusX64/Shaders/Saturate_f.cg

This file was deleted.

1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/Sepia Tone/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sepia tone filter shader.
File renamed without changes.
File renamed without changes.
10 changes: 0 additions & 10 deletions Data/DaedalusX64/Shaders/Sepia Tone_v.cg

This file was deleted.

1 change: 1 addition & 0 deletions Data/DaedalusX64/Shaders/Sharp Bilinear (Grid)/desc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Smooth bilinear filtering with applied grid.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Smooth bilinear filtering with applied scanlines.
23 changes: 22 additions & 1 deletion Source/SysVita/Graphics/GraphicsContextVita.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool new_frame = true;

extern float gamma_val;

static GLuint emu_fb = 0xDEADBEEF, emu_fb_tex;
static GLuint emu_fb = 0xDEADBEEF, emu_fb_tex, emu_depth_buf_tex;

class IGraphicsContext : public CGraphicsContext
{
Expand Down Expand Up @@ -161,6 +161,11 @@ void IGraphicsContext::BeginFrame()
glGenTextures(1, &emu_fb_tex);
glBindTexture(GL_TEXTURE_2D, emu_fb_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
/*glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &emu_depth_buf_tex);
glBindTexture(GL_TEXTURE_2D, emu_depth_buf_tex);
vglTexImageDepthBuffer(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);*/
glGenFramebuffers(1, &emu_fb);
glBindFramebuffer(GL_FRAMEBUFFER, emu_fb);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, emu_fb_tex, 0);
Expand Down Expand Up @@ -211,6 +216,22 @@ void IGraphicsContext::UpdateFrame(bool wait_for_vbl)
vglStartRendering();
glBindTexture(GL_TEXTURE_2D, emu_fb_tex);
glUseProgram(cur_prog);

int i = 0;
while (prog_uniforms[i].idx != 0xDEADBEEF) {
switch (prog_uniforms[i].type) {
case UNIF_FLOAT:
glUniform1f(prog_uniforms[i].idx, prog_uniforms[i].value[0]);
break;
case UNIF_COLOR:
glUniform3fv(prog_uniforms[i].idx, 1, prog_uniforms[i].value);
break;
default:
break;
}
i++;
}

vglVertexAttribPointerMapped(0, vflux_vertices);
vglVertexAttribPointerMapped(1, vflux_texcoords);
vglDrawObjects(GL_TRIANGLE_FAN, 4, true);
Expand Down
18 changes: 17 additions & 1 deletion Source/SysVita/UI/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ struct Alert {

struct PostProcessingEffect {
char name[32];
char desc[128];
bool compiled;
bool customizable;
PostProcessingEffect *next;
};

Expand All @@ -300,6 +302,20 @@ struct ButtonSce {
const SceCtrlButtons btn;
};

enum UnifType {
UNIF_FLOAT = 1,
UNIF_COLOR = 3
};

struct Uniform {
GLint idx;
char name[30];
UnifType type;
float value[3];
};

extern Uniform prog_uniforms[8];

extern Dialog cur_dialog;
extern Alert cur_alert;
extern Download cur_download;
Expand Down Expand Up @@ -364,7 +380,7 @@ void setUiTheme(int theme);
void setTranslation(int idx);
void setTexCacheMode(int mode);
void setOverlay(int idx, Overlay *p);
void setPostProcessingEffect(int idx, PostProcessingEffect *p);
bool setPostProcessingEffect(int idx, PostProcessingEffect *p);
void stripGameName(char *name);
void showDialog(char *text, void (*yes_func)(), void (*no_func)(), int type, char *args);
void getDialogTextResult(char *text);
Expand Down
87 changes: 73 additions & 14 deletions Source/SysVita/UI/MenuBarScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static bool vflux_enabled = false;
static bool credits_window = false;
static bool debug_window = false;
static bool logs_window = false;
static bool post_processing_window = false;

extern EFrameskipValue gFrameskipValue;
extern bool kUpdateTexturesEveryFrame;
Expand All @@ -100,6 +101,23 @@ char *raw_net_romlist = nullptr;
PostProcessingEffect *effects_list = nullptr;
Overlay *overlays_list = nullptr;

Uniform prog_uniforms[8];

void loadUniformSettings(const char *path) {
FILE *config = fopen(path, "r");
int value, i = 0;
while (EOF != fscanf(config, "%[^=]=%d\n", prog_uniforms[i].name, &value))
{
prog_uniforms[i].idx = glGetUniformLocation(cur_prog, prog_uniforms[i].name);
prog_uniforms[i].type = (UnifType)value;
prog_uniforms[i].value[0] = prog_uniforms[i].value[1] = prog_uniforms[i].value[2] = prog_uniforms[i].value[3] = 1.0f;
i++;
}

fclose(config);
prog_uniforms[i].idx = 0xDEADBEEF;
}

void loadCompiledShader(int idx, char *file)
{
SceIoStat st;
Expand Down Expand Up @@ -160,7 +178,7 @@ void setOverlay(int idx, Overlay *p) {
gOverlay = idx;
}

void setPostProcessingEffect(int idx, PostProcessingEffect *p) {
bool setPostProcessingEffect(int idx, PostProcessingEffect *p) {
if (gPostProcessing != idx && idx) {
// Get the required instance if not specified
if (!p) {
Expand All @@ -187,14 +205,14 @@ void setPostProcessingEffect(int idx, PostProcessingEffect *p) {

char fpath[128];
if (p->compiled) {
sprintf(fpath, "%s%s_v.gxp", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
sprintf(fpath, "%s%s/vert.gxp", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
loadCompiledShader(shader_idx * 2, fpath);
sprintf(fpath, "%s%s_f.gxp", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
sprintf(fpath, "%s%s/frag.gxp", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
loadCompiledShader(shader_idx * 2 + 1, fpath);
} else {
sprintf(fpath, "%s%s_v.cg", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
sprintf(fpath, "%s%s/vert.cg", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
loadShader(shader_idx * 2, fpath);
sprintf(fpath, "%s%s_f.cg", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
sprintf(fpath, "%s%s/frag.cg", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
loadShader(shader_idx * 2 + 1, fpath);
}
glAttachShader(program[shader_idx], shaders[shader_idx * 2]);
Expand All @@ -205,8 +223,14 @@ void setPostProcessingEffect(int idx, PostProcessingEffect *p) {

glLinkProgram(program[shader_idx]);
cur_prog = program[shader_idx];

if (p->customizable) {
sprintf(fpath, "%s%s/unif.txt", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), p->name);
loadUniformSettings(fpath);
} else prog_uniforms[0].idx = 0xDEADBEEF;
}
gPostProcessing = idx;
return p ? p->customizable : false;
}

void install_data_files() {
Expand Down Expand Up @@ -397,19 +421,33 @@ void SetupPostProcessingLists() {
PostProcessingEffect *p = effects_list;
do {
const char *filename( find_data.Name );
if (strstr(filename, "_f.cg")) {

char fpath[256];
sprintf(fpath, "%s%s/frag.cg", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), filename);
SceIoStat stat;
if (sceIoGetstat(fpath, &stat) >= 0) {
p->next = (PostProcessingEffect*)malloc(sizeof(PostProcessingEffect));
p = p->next;
p->compiled = false;
snprintf(p->name, strlen(filename) - 4, filename);
p->next = nullptr;
} else if (strstr(filename, "_f.gxp")) {
p->next = (PostProcessingEffect*)malloc(sizeof(PostProcessingEffect));
p = p->next;
p->compiled = true;
snprintf(p->name, strlen(filename) - 5, filename);
strcpy(p->name, filename);
p->next = nullptr;
} else {
sprintf(fpath, "%s%s/frag.gxp", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), filename);
if (sceIoGetstat(fpath, &stat) >= 0) {
p->next = (PostProcessingEffect*)malloc(sizeof(PostProcessingEffect));
p = p->next;
p->compiled = true;
strcpy(p->name, filename);
p->next = nullptr;
} else continue;
}
sprintf(fpath, "%s%s/unif.txt", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), filename);
p->customizable = sceIoGetstat(fpath, &stat) >= 0;
sprintf(fpath, "%s%s/desc.txt", DAEDALUS_VITA_PATH_EXT("ux0:", "Shaders/"), filename);
FILE *dh = fopen(fpath, "rb");
int len = fread(p->desc, 1, 128, dh);
p->desc[len] = 0;
fclose(dh);
} while(IO::FindFileNext( find_handle, find_data ));
}

Expand Down Expand Up @@ -578,8 +616,9 @@ void DrawCommonMenuBar() {
int i = 0;
while (p) {
if (ImGui::MenuItem(p->name, nullptr, gPostProcessing == i)){
setPostProcessingEffect(i, p);
if (setPostProcessingEffect(i, p)) post_processing_window = true;
}
if (i) SetDescription(p->desc);
p = p->next;
if (!i && p) ImGui::Separator();
i++;
Expand Down Expand Up @@ -809,6 +848,26 @@ void DrawCommonWindows() {
ImGui::End();
}

if (post_processing_window) {
ImGui::Begin(lang_strings[STR_MENU_POST_PROCESSING], &post_processing_window);
int i = 0;
while (prog_uniforms[i].idx != 0xDEADBEEF) {
switch (prog_uniforms[i].type) {
case UNIF_FLOAT:
ImGui::SliderFloat(prog_uniforms[i].name, &prog_uniforms[i].value[0], 0.0f, 2.0f);
break;
case UNIF_COLOR:
ImGui::ColorPicker3(prog_uniforms[i].name, prog_uniforms[i].value);
break;
default:
break;
}
i++;
}

ImGui::End();
}

if (debug_window) {
ImGui::Begin(lang_strings[STR_MENU_DEBUGGER], &debug_window);
ImGui::Text("%s: 0x%04X", lang_strings[STR_CART_ID], g_ROM.rh.CartID);
Expand Down

0 comments on commit a41968a

Please sign in to comment.