[spline/bezier]Improve shader uniforms a bit.

This commit is contained in:
xebra 2018-07-14 09:40:13 +09:00
parent de5975f13e
commit d4a667397c
8 changed files with 32 additions and 29 deletions

View File

@ -240,7 +240,7 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView
} }
if (dirtyUniforms & DIRTY_BEZIERSPLINE) { if (dirtyUniforms & DIRTY_BEZIERSPLINE) {
ub->spline_counts = BytesToUint32(gstate_c.spline_num_patches_u, gstate_c.spline_num_patches_v, gstate_c.spline_tess_u, gstate_c.spline_tess_v); ub->spline_counts = BytesToUint32(gstate_c.spline_num_patches_u, gstate_c.spline_num_points_u, gstate_c.spline_tess_u, gstate_c.spline_tess_v);
} }
if (dirtyUniforms & DIRTY_DEPAL) { if (dirtyUniforms & DIRTY_DEPAL) {

View File

@ -309,12 +309,12 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, "};\n"); WRITE(p, "};\n");
WRITE(p, "void tessellate(in VS_IN In, out Tess tess) {\n"); WRITE(p, "void tessellate(in VS_IN In, out Tess tess) {\n");
WRITE(p, " int2 spline_num_patches = int2((u_spline_counts >> 0) & 0xFF, (u_spline_counts >> 8) & 0xFF);\n"); WRITE(p, " int spline_num_patches_u = int(u_spline_counts & 0xff);\n");
WRITE(p, " int spline_num_points_u = int((u_spline_counts >> 8) & 0xff);\n");
WRITE(p, " int2 spline_tess = int2((u_spline_counts >> 16) & 0xFF, (u_spline_counts >> 24) & 0xFF);\n"); WRITE(p, " int2 spline_tess = int2((u_spline_counts >> 16) & 0xFF, (u_spline_counts >> 24) & 0xFF);\n");
// Calculate current patch position and vertex position(index for the weights) // Calculate current patch position and vertex position(index for the weights)
WRITE(p, " int spline_count_u = %s;\n", doBezier ? "spline_num_patches.x * 3 + 1" : "spline_num_patches.x + 3"); WRITE(p, " int u = In.instanceId %% spline_num_patches_u;\n");
WRITE(p, " int u = In.instanceId %% spline_num_patches.x;\n"); WRITE(p, " int v = In.instanceId / spline_num_patches_u;\n");
WRITE(p, " int v = In.instanceId / spline_num_patches.x;\n");
WRITE(p, " int2 patch_pos = int2(u, v);\n"); WRITE(p, " int2 patch_pos = int2(u, v);\n");
WRITE(p, " int2 vertex_pos = int2(In.position.xy);\n"); WRITE(p, " int2 vertex_pos = int2(In.position.xy);\n");
if (doSpline) { if (doSpline) {
@ -329,7 +329,7 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, " int index;\n"); WRITE(p, " int index;\n");
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
WRITE(p, " index = (%i + v%s) * spline_count_u + (%i + u%s);\n", i, doBezier ? " * 3" : "", j, doBezier ? " * 3" : ""); WRITE(p, " index = (%i + v%s) * spline_num_points_u + (%i + u%s);\n", i, doBezier ? " * 3" : "", j, doBezier ? " * 3" : "");
WRITE(p, " _pos[%i] = tess_data[index].pos;\n", i * 4 + j); WRITE(p, " _pos[%i] = tess_data[index].pos;\n", i * 4 + j);
if (doTexture && hasTexcoord && hasTexcoordTess) if (doTexture && hasTexcoord && hasTexcoordTess)
WRITE(p, " _tex[%i] = tess_data[index].tex;\n", i * 4 + j); WRITE(p, " _tex[%i] = tess_data[index].tex;\n", i * 4 + j);

View File

@ -163,7 +163,7 @@ LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs,
queries.push_back({ &u_tess_weights_u, "u_tess_weights_u" }); queries.push_back({ &u_tess_weights_u, "u_tess_weights_u" });
queries.push_back({ &u_tess_weights_v, "u_tess_weights_v" }); queries.push_back({ &u_tess_weights_v, "u_tess_weights_v" });
queries.push_back({ &u_spline_tess, "u_spline_tess" }); queries.push_back({ &u_spline_tess, "u_spline_tess" });
queries.push_back({ &u_spline_num_patches, "u_spline_num_patches" }); queries.push_back({ &u_spline_counts, "u_spline_counts" });
queries.push_back({ &u_depal, "u_depal" }); queries.push_back({ &u_depal, "u_depal" });
attrMask = vs->GetAttrMask(); attrMask = vs->GetAttrMask();
@ -569,9 +569,9 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid) {
int tess[] = { gstate_c.spline_tess_u, gstate_c.spline_tess_v }; int tess[] = { gstate_c.spline_tess_u, gstate_c.spline_tess_v };
render_->SetUniformI(&u_spline_tess, 2, tess); render_->SetUniformI(&u_spline_tess, 2, tess);
} }
if (u_spline_num_patches != -1) { if (u_spline_counts != -1) {
int num_patches[] = { gstate_c.spline_num_patches_u, gstate_c.spline_num_patches_v }; int counts[] = { gstate_c.spline_num_patches_u, gstate_c.spline_num_points_u };
render_->SetUniformI(&u_spline_num_patches, 2, num_patches); render_->SetUniformI(&u_spline_counts, 2, counts);
} }
} }
} }

View File

@ -122,7 +122,7 @@ public:
int u_tess_weights_u; int u_tess_weights_u;
int u_tess_weights_v; int u_tess_weights_v;
int u_spline_tess; int u_spline_tess;
int u_spline_num_patches; int u_spline_counts;
}; };
// Real public interface // Real public interface

View File

@ -383,7 +383,7 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,
WRITE(p, "uniform sampler2D u_tess_weights_u;\n"); WRITE(p, "uniform sampler2D u_tess_weights_u;\n");
WRITE(p, "uniform sampler2D u_tess_weights_v;\n"); WRITE(p, "uniform sampler2D u_tess_weights_v;\n");
WRITE(p, "uniform ivec2 u_spline_num_patches;\n"); WRITE(p, "uniform ivec2 u_spline_counts;\n");
WRITE(p, "uniform ivec2 u_spline_tess;\n"); WRITE(p, "uniform ivec2 u_spline_tess;\n");
for (int i = 2; i <= 4; i++) { for (int i = 2; i <= 4; i++) {
@ -412,10 +412,11 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,
WRITE(p, "};\n"); WRITE(p, "};\n");
WRITE(p, "void tessellate(out Tess tess) {\n"); WRITE(p, "void tessellate(out Tess tess) {\n");
WRITE(p, " int spline_num_patches_u = u_spline_counts[0];\n");
WRITE(p, " int spline_num_points_u = u_spline_counts[1];\n");
// Calculate current patch position and vertex position(index for the weights) // Calculate current patch position and vertex position(index for the weights)
WRITE(p, " int spline_count_u = %s;\n", doBezier ? "u_spline_num_patches.x * 3 + 1" : "u_spline_num_patches.x + 3"); WRITE(p, " int u = gl_InstanceID %% spline_num_patches_u;\n");
WRITE(p, " int u = gl_InstanceID %% u_spline_num_patches.x;\n"); WRITE(p, " int v = gl_InstanceID / spline_num_patches_u;\n");
WRITE(p, " int v = gl_InstanceID / u_spline_num_patches.x;\n");
WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n"); WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n");
WRITE(p, " ivec2 vertex_pos = ivec2(position.xy);\n"); WRITE(p, " ivec2 vertex_pos = ivec2(position.xy);\n");
if (doSpline) { if (doSpline) {
@ -429,7 +430,7 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,
WRITE(p, " vec4 _col[16];\n"); WRITE(p, " vec4 _col[16];\n");
WRITE(p, " for (int i = 0; i < 4; i++) {\n"); WRITE(p, " for (int i = 0; i < 4; i++) {\n");
WRITE(p, " for (int j = 0; j < 4; j++) {\n"); WRITE(p, " for (int j = 0; j < 4; j++) {\n");
WRITE(p, " int index = (i + v%s) * spline_count_u + (j + u%s);\n", doBezier ? " * 3" : "", doBezier ? " * 3" : ""); WRITE(p, " int index = (i + v%s) * spline_num_points_u + (j + u%s);\n", doBezier ? " * 3" : "", doBezier ? " * 3" : "");
WRITE(p, " _pos[i * 4 + j] = %s(u_tess_points, ivec2(index, 0), 0).xyz;\n", texelFetch); WRITE(p, " _pos[i * 4 + j] = %s(u_tess_points, ivec2(index, 0), 0).xyz;\n", texelFetch);
if (doTexture && hasTexcoord && hasTexcoordTess) if (doTexture && hasTexcoord && hasTexcoordTess)
WRITE(p, " _tex[i * 4 + j] = %s(u_tess_points, ivec2(index, 1), 0).xy;\n", texelFetch); WRITE(p, " _tex[i * 4 + j] = %s(u_tess_points, ivec2(index, 1), 0).xy;\n", texelFetch);

View File

@ -1761,9 +1761,12 @@ void GPUCommon::Execute_Bezier(u32 op, u32 diff) {
gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE); gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE);
gstate_c.bezier = true; gstate_c.bezier = true;
int num_patches_u = (bz_ucount - 1) / 3; int num_patches_u = (bz_ucount - 1) / 3;
if (gstate_c.spline_num_patches_u != num_patches_u) { bool patchesChanged = gstate_c.spline_num_patches_u != num_patches_u;
bool countsChanged = gstate_c.spline_num_points_u != bz_ucount;
if (patchesChanged || countsChanged) {
gstate_c.Dirty(DIRTY_BEZIERSPLINE); gstate_c.Dirty(DIRTY_BEZIERSPLINE);
gstate_c.spline_num_patches_u = num_patches_u; gstate_c.spline_num_patches_u = num_patches_u;
gstate_c.spline_num_points_u = bz_ucount;
} }
} }
@ -1828,15 +1831,15 @@ void GPUCommon::Execute_Spline(u32 op, u32 diff) {
int tess_u = gstate.getPatchDivisionU(); int tess_u = gstate.getPatchDivisionU();
int tess_v = gstate.getPatchDivisionV(); int tess_v = gstate.getPatchDivisionV();
int num_patches_u = sp_ucount - 3; int num_patches_u = sp_ucount - 3;
int num_patches_v = sp_vcount - 3;
bool divsChanged = gstate_c.spline_tess_u != tess_u || gstate_c.spline_tess_v != tess_v; bool divsChanged = gstate_c.spline_tess_u != tess_u || gstate_c.spline_tess_v != tess_v;
bool patchesChanged = gstate_c.spline_num_patches_u != num_patches_u || gstate_c.spline_num_patches_v != num_patches_v; bool patchesChanged = gstate_c.spline_num_patches_u != num_patches_u;
if (divsChanged || patchesChanged) { bool countsChanged = gstate_c.spline_num_points_u != sp_ucount;
if (divsChanged || patchesChanged || countsChanged) {
gstate_c.Dirty(DIRTY_BEZIERSPLINE); gstate_c.Dirty(DIRTY_BEZIERSPLINE);
gstate_c.spline_tess_u = tess_u; gstate_c.spline_tess_u = tess_u;
gstate_c.spline_tess_v = tess_v; gstate_c.spline_tess_v = tess_v;
gstate_c.spline_num_patches_u = num_patches_u; gstate_c.spline_num_patches_u = num_patches_u;
gstate_c.spline_num_patches_v = num_patches_v; gstate_c.spline_num_points_u = sp_ucount;
} }
} }

View File

@ -605,7 +605,7 @@ struct GPUStateCache {
int spline_tess_u; int spline_tess_u;
int spline_tess_v; int spline_tess_v;
int spline_num_patches_u; int spline_num_patches_u;
int spline_num_patches_v; int spline_num_points_u;
bool useShaderDepal; bool useShaderDepal;
GEBufferFormat depalFramebufferFormat; GEBufferFormat depalFramebufferFormat;

View File

@ -261,13 +261,12 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) {
WRITE(p, "};\n"); WRITE(p, "};\n");
WRITE(p, "void tessellate(out Tess tess) {\n"); WRITE(p, "void tessellate(out Tess tess) {\n");
// Calculate current patch position and vertex position(index for the weights) WRITE(p, " int spline_num_patches_u = int(base.spline_counts & 0xff);\n");
WRITE(p, " ivec2 spline_num_patches = ivec2((base.spline_counts >> 0) & 0xff, (base.spline_counts >> 8) & 0xff);\n"); WRITE(p, " int spline_num_points_u = int((base.spline_counts >> 8) & 0xff);\n");
WRITE(p, " ivec2 spline_tess = ivec2((base.spline_counts >> 16) & 0xff, (base.spline_counts >> 24) & 0xff);\n"); WRITE(p, " ivec2 spline_tess = ivec2((base.spline_counts >> 16) & 0xff, (base.spline_counts >> 24) & 0xff);\n");
// Calculate current patch position and vertex position(index for the weights)
WRITE(p, " int spline_count_u = %s;\n", doBezier ? "spline_num_patches.x * 3 + 1" : "spline_num_patches.x + 3"); WRITE(p, " int u = gl_InstanceIndex %% spline_num_patches_u;\n");
WRITE(p, " int u = gl_InstanceIndex %% spline_num_patches.x;\n"); WRITE(p, " int v = gl_InstanceIndex / spline_num_patches_u;\n");
WRITE(p, " int v = gl_InstanceIndex / spline_num_patches.x;\n");
WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n"); WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n");
WRITE(p, " ivec2 vertex_pos = ivec2(position.xy);\n"); WRITE(p, " ivec2 vertex_pos = ivec2(position.xy);\n");
if (doSpline) { if (doSpline) {
@ -281,7 +280,7 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) {
WRITE(p, " vec4 _col[16];\n"); WRITE(p, " vec4 _col[16];\n");
WRITE(p, " for (int i = 0; i < 4; i++) {\n"); WRITE(p, " for (int i = 0; i < 4; i++) {\n");
WRITE(p, " for (int j = 0; j < 4; j++) {\n"); WRITE(p, " for (int j = 0; j < 4; j++) {\n");
WRITE(p, " int idx = (i + v%s) * spline_count_u + (j + u%s);\n", doBezier ? " * 3" : "", doBezier ? " * 3" : ""); WRITE(p, " int idx = (i + v%s) * spline_num_points_u + (j + u%s);\n", doBezier ? " * 3" : "", doBezier ? " * 3" : "");
WRITE(p, " _pos[i * 4 + j] = tess_data.data[idx].pos.xyz;\n"); WRITE(p, " _pos[i * 4 + j] = tess_data.data[idx].pos.xyz;\n");
if (doTexture && hasTexcoord && hasTexcoordTess) if (doTexture && hasTexcoord && hasTexcoordTess)
WRITE(p, " _tex[i * 4 + j] = tess_data.data[idx].uv.xy;\n"); WRITE(p, " _tex[i * 4 + j] = tess_data.data[idx].uv.xy;\n");