From 3c0fb44f2e3510fd373dcb43d9d3e4d7ad4d2a99 Mon Sep 17 00:00:00 2001 From: xebra Date: Wed, 11 Jul 2018 01:09:20 +0900 Subject: [PATCH] [spline/bezier]Improve hwtess to use cached weights. --- GPU/Common/DrawEngineCommon.h | 9 +- GPU/Common/ShaderUniforms.cpp | 2 +- GPU/Common/SplineCommon.cpp | 119 ++++++-------- GPU/Common/SplineCommon.h | 40 ++++- GPU/D3D11/DrawEngineD3D11.cpp | 55 +++++-- GPU/D3D11/DrawEngineD3D11.h | 16 +- GPU/Directx9/DrawEngineDX9.cpp | 2 +- GPU/Directx9/DrawEngineDX9.h | 2 +- GPU/Directx9/VertexShaderGeneratorDX9.cpp | 174 +++++--------------- GPU/GLES/DrawEngineGLES.cpp | 24 ++- GPU/GLES/DrawEngineGLES.h | 5 +- GPU/GLES/ShaderManagerGLES.cpp | 21 ++- GPU/GLES/ShaderManagerGLES.h | 6 +- GPU/GLES/VertexShaderGeneratorGLES.cpp | 163 +++++-------------- GPU/GPUCommon.cpp | 23 +-- GPU/GPUState.h | 8 +- GPU/Vulkan/DrawEngineVulkan.cpp | 70 +++++--- GPU/Vulkan/DrawEngineVulkan.h | 19 +-- GPU/Vulkan/VertexShaderGeneratorVulkan.cpp | 180 ++++++--------------- 19 files changed, 386 insertions(+), 552 deletions(-) diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index 39c8647ce4..de8a467f4a 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -27,6 +27,7 @@ #include "GPU/Common/GPUDebugInterface.h" #include "GPU/Common/IndexGenerator.h" #include "GPU/Common/VertexDecoderCommon.h" +#include "GPU/Common/SplineCommon.h" class VertexDecoder; @@ -173,17 +174,11 @@ protected: // Hardware tessellation int numPatches; class TessellationDataTransfer { - protected: - // TODO: These aren't used by all backends. - int prevSize; - int prevSizeTex; - int prevSizeCol; public: virtual ~TessellationDataTransfer() {} void CopyControlPoints(float *pos, float *tex, float *col, int posStride, int texStride, int colStride, const SimpleVertex *const *points, int size, u32 vertType); // Send spline/bezier's control points to vertex shader through floating point texture. - virtual void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) = 0; - virtual void EndFrame() {} + virtual void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) = 0; }; TessellationDataTransfer *tessDataTransfer; }; diff --git a/GPU/Common/ShaderUniforms.cpp b/GPU/Common/ShaderUniforms.cpp index e0f6719502..6fcdfa311e 100644 --- a/GPU/Common/ShaderUniforms.cpp +++ b/GPU/Common/ShaderUniforms.cpp @@ -240,7 +240,7 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView } if (dirtyUniforms & DIRTY_BEZIERSPLINE) { - ub->spline_counts = BytesToUint32(gstate_c.spline_count_u, gstate_c.spline_count_v, gstate_c.spline_type_u, gstate_c.spline_type_v); + 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); } if (dirtyUniforms & DIRTY_DEPAL) { diff --git a/GPU/Common/SplineCommon.cpp b/GPU/Common/SplineCommon.cpp index 558570f3ea..ea8776ccb8 100644 --- a/GPU/Common/SplineCommon.cpp +++ b/GPU/Common/SplineCommon.cpp @@ -22,7 +22,6 @@ #include "Common/CPUDetect.h" #include "Common/MemoryUtil.h" -#include "Core/Config.h" #include "GPU/Common/GPUStateUtils.h" #include "GPU/Common/SplineCommon.h" @@ -60,32 +59,28 @@ static void BuildIndex(u16 *indices, int &count, int num_u, int num_v, GEPatchPr } } -struct Weight { - float weights[4], derivs[4]; -}; - class Bezier3DWeight { private: void CalcWeights(float t, Weight &w) { // Bernstein 3D basis polynomial - w.weights[0] = (1 - t) * (1 - t) * (1 - t); - w.weights[1] = 3 * t * (1 - t) * (1 - t); - w.weights[2] = 3 * t * t * (1 - t); - w.weights[3] = t * t * t; + w.basis[0] = (1 - t) * (1 - t) * (1 - t); + w.basis[1] = 3 * t * (1 - t) * (1 - t); + w.basis[2] = 3 * t * t * (1 - t); + w.basis[3] = t * t * t; // Derivative - w.derivs[0] = -3 * (1 - t) * (1 - t); - w.derivs[1] = 9 * t * t - 12 * t + 3; - w.derivs[2] = 3 * (2 - 3 * t) * t; - w.derivs[3] = 3 * t * t; + w.deriv[0] = -3 * (1 - t) * (1 - t); + w.deriv[1] = 9 * t * t - 12 * t + 3; + w.deriv[2] = 3 * (2 - 3 * t) * t; + w.deriv[3] = 3 * t * t; } public: Weight *CalcWeightsAll(u32 key) { int tess = (int)key; Weight *weights = new Weight[tess + 1]; - const float inv_u = 1.0f / (float)tess; + const float inv_tess = 1.0f / (float)tess; for (int i = 0; i < tess + 1; ++i) { - const float t = (float)i * inv_u; + const float t = (float)i * inv_tess; CalcWeights(t, weights[i]); } return weights; @@ -182,10 +177,10 @@ private: float c = (1 - f41) * (1 - f42); float d = (f42 * f52); #endif - w.weights[0] = a * (1 - f32); // (1-f30)*(1-f31)*(1-f32) - w.weights[1] = 1 - a - b + ((a + b + c - 1) * f32); - w.weights[2] = b + ((1 - b - c - d) * f32); - w.weights[3] = d * f32; // f32*f42*f52 + w.basis[0] = a * (1 - f32); // (1-f30)*(1-f31)*(1-f32) + w.basis[1] = 1 - a - b + ((a + b + c - 1) * f32); + w.basis[2] = b + ((1 - b - c - d) * f32); + w.basis[3] = d * f32; // f32*f42*f52 // Derivative float i1 = (1 - f31) * (1 - f32); @@ -196,10 +191,10 @@ private: float f241 = i2 * div._4_1; float f352 = i3 * div._5_2; - w.derivs[0] = 3 * (0 - f130); - w.derivs[1] = 3 * (f130 - f241); - w.derivs[2] = 3 * (f241 - f352); - w.derivs[3] = 3 * (f352 - 0); + w.deriv[0] = 3 * (0 - f130); + w.deriv[1] = 3 * (f130 - f241); + w.deriv[2] = 3 * (f241 - f352); + w.deriv[3] = 3 * (f352 - 0); } public: Weight *CalcWeightsAll(u32 key) { @@ -238,38 +233,9 @@ public: } }; -template -class WeightCache : public T { -private: - std::unordered_map weightsCache; -public: - Weight* operator [] (u32 key) { - Weight *&weights = weightsCache[key]; - if (!weights) - weights = CalcWeightsAll(key); - return weights; - } - - void Clear() { - for (auto it : weightsCache) - delete[] it.second; - weightsCache.clear(); - } -}; - static WeightCache bezierWeightsCache; static WeightCache splineWeightsCache; -struct Weight2D { - const Weight *u, *v; - - template - Weight2D(WeightCache &cache, u32 key_u, u32 key_v) { - u = cache[key_u]; - v = (key_u != key_v) ? cache[key_v] : u; // Use same weights if u == v - } -}; - void DrawEngineCommon::ClearSplineBezierWeights() { bezierWeightsCache.Clear(); splineWeightsCache.Clear(); @@ -293,13 +259,11 @@ static void TessellateSplinePatchHardware(u8 *&dest, u16 *indices, int &count, c for (int tile_v = 0; tile_v < spatch.tess_v + 1; ++tile_v) { for (int tile_u = 0; tile_u < spatch.tess_u + 1; ++tile_u) { SimpleVertex &vert = vertices[tile_v * (spatch.tess_u + 1) + tile_u]; - vert.pos.x = (float)tile_u * inv_u; - vert.pos.y = (float)tile_v * inv_v; - - // TODO: Move to shader uniform and unify this method spline and bezier if necessary. - // For compute normal - vert.nrm.x = inv_u; - vert.nrm.y = inv_v; + vert.pos.x = (float)tile_u; + vert.pos.y = (float)tile_v; + // For texcoord generation + vert.nrm.x = (float)tile_u * inv_u; + vert.nrm.y = (float)tile_v * inv_v; } } @@ -357,8 +321,11 @@ static void TessellateBezierPatchHardware(u8 *&dest, u16 *indices, int &count, i for (int tile_u = 0; tile_u < tess_u + 1; ++tile_u) { SimpleVertex &vert = vertices[tile_v * (tess_u + 1) + tile_u]; - vert.pos.x = (float)tile_u * inv_u; - vert.pos.y = (float)tile_v * inv_v; + vert.pos.x = (float)tile_u; + vert.pos.y = (float)tile_v; + // For texcoord generation + vert.nrm.x = (float)tile_u * inv_u; + vert.nrm.y = (float)tile_v * inv_v; } } @@ -433,13 +400,13 @@ public: const Weight &wu = weights.u[index_u]; // Pre-tessellate U lines - tess_pos.SampleU(wu.weights); + tess_pos.SampleU(wu.basis); if (sampleCol) - tess_col.SampleU(wu.weights); + tess_col.SampleU(wu.basis); if (sampleTex) - tess_tex.SampleU(wu.weights); + tess_tex.SampleU(wu.basis); if (sampleNrm) - tess_nrm.SampleU(wu.derivs); + tess_nrm.SampleU(wu.deriv); for (int tile_v = 0; tile_v < tess_v; ++tile_v) { const int index_v = patch.GetIndexV(patch_v, tile_v); @@ -448,22 +415,22 @@ public: SimpleVertex &vert = vertices[patch.GetIndex(index_u, index_v, patch_u, patch_v)]; // Tessellate - vert.pos = tess_pos.SampleV(wv.weights); + vert.pos = tess_pos.SampleV(wv.basis); if (sampleCol) { - vert.color_32 = tess_col.SampleV(wv.weights).ToRGBA(); + vert.color_32 = tess_col.SampleV(wv.basis).ToRGBA(); } else { vert.color_32 = defcolor; } if (sampleTex) { - tess_tex.SampleV(wv.weights).Write(vert.uv); + tess_tex.SampleV(wv.basis).Write(vert.uv); } else { // Generate texcoord vert.uv[0] = patch_u + tile_u * inv_u; vert.uv[1] = patch_v + tile_v * inv_v; } if (sampleNrm) { - const Vec3f derivU = tess_nrm.SampleV(wv.weights); - const Vec3f derivV = tess_pos.SampleV(wv.derivs); + const Vec3f derivU = tess_nrm.SampleV(wv.basis); + const Vec3f derivV = tess_pos.SampleV(wv.deriv); vert.nrm = Cross(derivU, derivV).Normalized(useSSE4); if (patch.patchFacing) @@ -569,7 +536,12 @@ void DrawEngineCommon::SubmitSpline(const void *control_points, const void *indi patch.patchFacing = patchFacing; if (CanUseHardwareTessellation(prim_type)) { - tessDataTransfer->SendDataToShader(points, count_u * count_v, origVertType); + const u32 key_u = splineWeightsCache.ToKey(tess_u, count_u, type_u); + const u32 key_v = splineWeightsCache.ToKey(tess_v, count_v, type_v); + Weight2D weights(splineWeightsCache, key_u, key_v); + weights.size_u = (count_u - 3) * tess_u + 1; + weights.size_v = (count_v - 3) * tess_v + 1; + tessDataTransfer->SendDataToShader(points, count_u * count_v, origVertType, weights); TessellateSplinePatchHardware(dest, quadIndices_, count, patch); numPatches = (count_u - 3) * (count_v - 3); } else { @@ -653,7 +625,10 @@ void DrawEngineCommon::SubmitBezier(const void *control_points, const void *indi int num_patches_u = (count_u - 1) / 3; int num_patches_v = (count_v - 1) / 3; if (CanUseHardwareTessellation(prim_type)) { - tessDataTransfer->SendDataToShader(points, count_u * count_v, origVertType); + Weight2D weights(bezierWeightsCache, tess_u, tess_v); + weights.size_u = tess_u + 1; + weights.size_v = tess_v + 1; + tessDataTransfer->SendDataToShader(points, count_u * count_v, origVertType, weights); TessellateBezierPatchHardware(dest, inds, count, tess_u, tess_v, prim_type); numPatches = num_patches_u * num_patches_v; } else { diff --git a/GPU/Common/SplineCommon.h b/GPU/Common/SplineCommon.h index cb0b2897fd..b15bd4d2e2 100644 --- a/GPU/Common/SplineCommon.h +++ b/GPU/Common/SplineCommon.h @@ -16,11 +16,13 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #pragma once +#include #include "Common/CommonTypes.h" #include "Common/Swap.h" #include "GPU/Math3D.h" #include "GPU/ge_constants.h" +#include "Core/Config.h" #define HALF_CEIL(x) (x + 1) / 2 // Integer ceil = (int)ceil((float)x / 2.0f) @@ -157,7 +159,43 @@ struct SplinePatchLocal { } }; -bool CanUseHardwareTessellation(GEPatchPrimType prim);void TessellateSplinePatch(u8 *&dest, u16 *indices, int &count, SplinePatchLocal &spatch, u32 origVertType, int maxVertices);void TessellateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType, int maxVertices); +struct Weight { + float basis[4], deriv[4]; +}; + +template +class WeightCache : public T { +private: + std::unordered_map weightsCache; +public: + Weight* operator [] (u32 key) { + Weight *&weights = weightsCache[key]; + if (!weights) + weights = CalcWeightsAll(key); + return weights; + } + + void Clear() { + for (auto it : weightsCache) + delete[] it.second; + weightsCache.clear(); + } +}; + +struct Weight2D { + const Weight *u, *v; + int size_u, size_v; + + template + Weight2D(WeightCache &cache, u32 key_u, u32 key_v) { + u = cache[key_u]; + v = (key_u != key_v) ? cache[key_v] : u; // Use same weights if u == v + } +}; + +bool CanUseHardwareTessellation(GEPatchPrimType prim); +void TessellateSplinePatch(u8 *&dest, u16 *indices, int &count, SplinePatchLocal &spatch, u32 origVertType, int maxVertices); +void TessellateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType, int maxVertices); #define TEMPLATE_PARAMETER_DISPATCHER(NAME, FUNCNAME) \ template \ diff --git a/GPU/D3D11/DrawEngineD3D11.cpp b/GPU/D3D11/DrawEngineD3D11.cpp index 63b753d1ea..1a77479851 100644 --- a/GPU/D3D11/DrawEngineD3D11.cpp +++ b/GPU/D3D11/DrawEngineD3D11.cpp @@ -692,7 +692,7 @@ rotateVBO: GPUDebug::NotifyDraw(); } -void DrawEngineD3D11::TessellationDataTransferD3D11::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) { +void DrawEngineD3D11::TessellationDataTransferD3D11::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) { struct TessData { float pos[3]; float pad1; float uv[2]; float pad2[2]; @@ -701,19 +701,19 @@ void DrawEngineD3D11::TessellationDataTransferD3D11::SendDataToShader(const Simp if (prevSize < size) { prevSize = size; - if (buf) { - buf->Release(); - view->Release(); + if (buf[0]) { + buf[0]->Release(); + view[0]->Release(); } desc.ByteWidth = size * sizeof(TessData); desc.StructureByteStride = sizeof(TessData); - device_->CreateBuffer(&desc, nullptr, &buf); - device_->CreateShaderResourceView(buf, 0, &view); - context_->VSSetShaderResources(0, 1, &view); + device_->CreateBuffer(&desc, nullptr, &buf[0]); + device_->CreateShaderResourceView(buf[0], nullptr, &view[0]); + context_->VSSetShaderResources(0, 1, &view[0]); } D3D11_MAPPED_SUBRESOURCE map; - context_->Map(buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); + context_->Map(buf[0], 0, D3D11_MAP_WRITE_DISCARD, 0, &map); uint8_t *data = (uint8_t *)map.pData; float *pos = (float *)(data); @@ -725,5 +725,42 @@ void DrawEngineD3D11::TessellationDataTransferD3D11::SendDataToShader(const Simp CopyControlPoints(pos, tex, col, stride, stride, stride, points, size, vertType); - context_->Unmap(buf, 0); + context_->Unmap(buf[0], 0); + + + // Weights U + if (prevSizeWeights[0] < weights.size_u) { + prevSizeWeights[0] = weights.size_u; + if (buf[1]) { + buf[1]->Release(); + view[1]->Release(); + } + desc.ByteWidth = weights.size_u * sizeof(Weight); + desc.StructureByteStride = sizeof(Weight); + + device_->CreateBuffer(&desc, nullptr, &buf[1]); + device_->CreateShaderResourceView(buf[1], nullptr, &view[1]); + context_->VSSetShaderResources(1, 1, &view[1]); + } + context_->Map(buf[1], 0, D3D11_MAP_WRITE_DISCARD, 0, &map); + memcpy(map.pData, weights.u, weights.size_u * sizeof(Weight)); + context_->Unmap(buf[1], 0); + + // Weights V + if (prevSizeWeights[1] < weights.size_v) { + prevSizeWeights[1] = weights.size_v; + if (buf[2]) { + buf[2]->Release(); + view[2]->Release(); + } + desc.ByteWidth = weights.size_v * sizeof(Weight); + desc.StructureByteStride = sizeof(Weight); + + device_->CreateBuffer(&desc, nullptr, &buf[2]); + device_->CreateShaderResourceView(buf[2], nullptr, &view[2]); + context_->VSSetShaderResources(2, 1, &view[2]); + } + context_->Map(buf[2], 0, D3D11_MAP_WRITE_DISCARD, 0, &map); + memcpy(map.pData, weights.v, weights.size_v * sizeof(Weight)); + context_->Unmap(buf[2], 0); } diff --git a/GPU/D3D11/DrawEngineD3D11.h b/GPU/D3D11/DrawEngineD3D11.h index e64296dba1..c910e2d365 100644 --- a/GPU/D3D11/DrawEngineD3D11.h +++ b/GPU/D3D11/DrawEngineD3D11.h @@ -203,9 +203,11 @@ private: private: ID3D11DeviceContext *context_; ID3D11Device *device_; - ID3D11Buffer *buf; - ID3D11ShaderResourceView *view; + ID3D11Buffer *buf[3]; + ID3D11ShaderResourceView *view[3]; D3D11_BUFFER_DESC desc; + int prevSize = 0; + int prevSizeWeights[2] = {}; public: TessellationDataTransferD3D11(ID3D11DeviceContext *context, ID3D11Device *device) : TessellationDataTransfer(), context_(context), device_(device), buf(), view(), desc() { @@ -215,12 +217,14 @@ private: desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; } ~TessellationDataTransferD3D11() { - if (buf) { - buf->Release(); - view->Release(); + for (int i = 0; i < 3; ++i) { + if (buf[i]) { + buf[i]->Release(); + view[i]->Release(); + } } } - void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) override; + void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) override; }; }; diff --git a/GPU/Directx9/DrawEngineDX9.cpp b/GPU/Directx9/DrawEngineDX9.cpp index f3f373f314..b7ad1027a9 100644 --- a/GPU/Directx9/DrawEngineDX9.cpp +++ b/GPU/Directx9/DrawEngineDX9.cpp @@ -624,7 +624,7 @@ rotateVBO: GPUDebug::NotifyDraw(); } -void DrawEngineDX9::TessellationDataTransferDX9::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) { +void DrawEngineDX9::TessellationDataTransferDX9::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) { } } // namespace diff --git a/GPU/Directx9/DrawEngineDX9.h b/GPU/Directx9/DrawEngineDX9.h index 1c7c082e7d..be54128be2 100644 --- a/GPU/Directx9/DrawEngineDX9.h +++ b/GPU/Directx9/DrawEngineDX9.h @@ -166,7 +166,7 @@ private: } ~TessellationDataTransferDX9() { } - void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) override; + void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) override; }; }; diff --git a/GPU/Directx9/VertexShaderGeneratorDX9.cpp b/GPU/Directx9/VertexShaderGeneratorDX9.cpp index 51df62a015..62ad03c781 100644 --- a/GPU/Directx9/VertexShaderGeneratorDX9.cpp +++ b/GPU/Directx9/VertexShaderGeneratorDX9.cpp @@ -271,82 +271,32 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage WRITE(p, " float3 pos; float pad1;\n"); WRITE(p, " float2 tex; float2 pad2;\n"); WRITE(p, " float4 col;\n"); - WRITE(p, "};"); + WRITE(p, "};\n"); WRITE(p, "StructuredBuffer tess_data : register(t0);\n"); + + WRITE(p, "struct TessWeight {\n"); + WRITE(p, " float4 basis;\n"); + WRITE(p, " float4 deriv;\n"); + WRITE(p, "};\n"); + WRITE(p, "StructuredBuffer tess_weights_u : register(t1);\n"); + WRITE(p, "StructuredBuffer tess_weights_v : register(t2);\n"); } const char *init[3] = { "0.0, 0.0", "0.0, 0.0, 0.0", "0.0, 0.0, 0.0, 0.0" }; for (int i = 2; i <= 4; i++) { // Define 3 types float2, float3, float4 - WRITE(p, "float%d tess_sample(in float%d points[16], in float2 weights[4]) {\n", i, i); + WRITE(p, "float%d tess_sample(in float%d points[16], float4 weights_u, float4 weights_v) {\n", i, i); WRITE(p, " float%d pos = float%d(%s);\n", i, i, init[i - 2]); - WRITE(p, " for (int i = 0; i < 4; ++i) {\n"); - WRITE(p, " for (int j = 0; j < 4; ++j) {\n"); - WRITE(p, " float f = weights[j].x * weights[i].y;\n"); + WRITE(p, " for (int v = 0; v < 4; ++v) {\n"); + WRITE(p, " for (int u = 0; u < 4; ++u) {\n"); + WRITE(p, " float f = weights_u[u] * weights_v[v];\n"); WRITE(p, " if (f != 0.0)\n"); - WRITE(p, " pos = pos + f * points[i * 4 + j];\n"); + WRITE(p, " pos = pos + f * points[v * 4 + u];\n"); WRITE(p, " }\n"); WRITE(p, " }\n"); WRITE(p, " return pos;\n"); WRITE(p, "}\n"); } - if (doSpline) { - WRITE(p, "void spline_knot(int2 num_patches, int2 type, out float2 knot[6], int2 patch_pos) {\n"); - WRITE(p, " for (int i = 0; i < 6; ++i) {\n"); - WRITE(p, " knot[i] = float2(i + patch_pos.x - 2, i + patch_pos.y - 2);\n"); - WRITE(p, " }\n"); - // WRITE(p, " if ((type.x & 1) != 0) {\n"); - WRITE(p, " if ((type.x == 1) || (type.x == 3)) {\n"); - WRITE(p, " if (patch_pos.x <= 2)\n"); - WRITE(p, " knot[0].x = 0.0;\n"); - WRITE(p, " if (patch_pos.x <= 1)\n"); - WRITE(p, " knot[1].x = 0.0;\n"); - WRITE(p, " }\n"); - // WRITE(p, " if ((type.x & 2) != 0) {\n"); - WRITE(p, " if ((type.x == 2) || (type.x == 3)) {\n"); - WRITE(p, " if (patch_pos.x >= (num_patches.x - 2))\n"); - WRITE(p, " knot[5].x = num_patches.x;\n"); - WRITE(p, " if (patch_pos.x == (num_patches.x - 1))\n"); - WRITE(p, " knot[4].x = num_patches.x;\n"); - WRITE(p, " }\n"); - // WRITE(p, " if ((type.y & 1) != 0) {\n"); - WRITE(p, " if ((type.y == 1) || (type.y == 3)) {\n"); - WRITE(p, " if (patch_pos.y <= 2)\n"); - WRITE(p, " knot[0].y = 0.0;\n"); - WRITE(p, " if (patch_pos.y <= 1)\n"); - WRITE(p, " knot[1].y = 0.0;\n"); - WRITE(p, " }\n"); - // WRITE(p, " if ((type.y & 2) != 0) {\n"); - WRITE(p, " if ((type.y == 2) || (type.y == 3)) {\n"); - WRITE(p, " if (patch_pos.y >= (num_patches.y - 2))\n"); - WRITE(p, " knot[5].y = num_patches.y;\n"); - WRITE(p, " if (patch_pos.y == (num_patches.y - 1))\n"); - WRITE(p, " knot[4].y = num_patches.y;\n"); - WRITE(p, " }\n"); - WRITE(p, "}\n"); - - WRITE(p, "void spline_weight(float2 t, in float2 knot[6], out float2 weights[4]) {\n"); - // TODO: Maybe compilers could be coaxed into vectorizing this code without the above explicitly... - WRITE(p, " float2 t0 = (t - knot[0]);\n"); - WRITE(p, " float2 t1 = (t - knot[1]);\n"); - WRITE(p, " float2 t2 = (t - knot[2]);\n"); - // TODO: All our knots are integers so we should be able to get rid of these divisions (How?) - WRITE(p, " float2 f30 = t0 / (knot[3] - knot[0]);\n"); - WRITE(p, " float2 f41 = t1 / (knot[4] - knot[1]);\n"); - WRITE(p, " float2 f52 = t2 / (knot[5] - knot[2]);\n"); - WRITE(p, " float2 f31 = t1 / (knot[3] - knot[1]);\n"); - WRITE(p, " float2 f42 = t2 / (knot[4] - knot[2]);\n"); - WRITE(p, " float2 f32 = t2 / (knot[3] - knot[2]);\n"); - WRITE(p, " float2 a = (1.0 - f30)*(1.0 - f31);\n"); - WRITE(p, " float2 b = (f31*f41);\n"); - WRITE(p, " float2 c = (1.0 - f41)*(1.0 - f42);\n"); - WRITE(p, " float2 d = (f42*f52);\n"); - WRITE(p, " weights[0] = a - (a*f32);\n"); - WRITE(p, " weights[1] = 1.0 - a - b + ((a + b + c - 1.0)*f32);\n"); - WRITE(p, " weights[2] = b + ((1.0 - b - c - d)*f32);\n"); - WRITE(p, " weights[3] = d*f32;\n"); - WRITE(p, "}\n"); - } } WRITE(p, "VS_OUT main(VS_IN In) {\n"); @@ -398,20 +348,29 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage if (!enableBones) { // Hardware tessellation if (doSpline || doBezier) { - WRITE(p, " uint u_spline_count_u = u_spline_counts & 0xFF;\n"); - WRITE(p, " uint u_spline_count_v = (u_spline_counts >> 8) & 0xFF;\n"); - WRITE(p, " uint num_patches_u = %s;\n", doBezier ? "(u_spline_count_u - 1) / 3u" : "u_spline_count_u - 3"); - WRITE(p, " float2 tess_pos = In.position.xy;\n"); - WRITE(p, " int u = In.instanceId %% num_patches_u;\n"); - WRITE(p, " int v = In.instanceId / num_patches_u;\n"); + WRITE(p, " int2 spline_num_patches = int2((u_spline_counts >> 0) & 0xFF, (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, " 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.x;\n"); + WRITE(p, " int v = In.instanceId / spline_num_patches.x;\n"); WRITE(p, " int2 patch_pos = int2(u, v);\n"); + WRITE(p, " int2 vertex_pos = int2(In.position.xy);\n"); + if (doSpline) { + WRITE(p, " if ((vertex_pos.x == spline_tess.x) && (u < spline_num_patches.x - 1))\n"); + WRITE(p, " u++;\n"); // Use next patch position + WRITE(p, " if ((vertex_pos.y == spline_tess.y) && (v < spline_num_patches.y - 1))\n"); + WRITE(p, " v++;\n"); // Use next patch position + WRITE(p, " vertex_pos += patch_pos * spline_tess;\n"); + } + // Load 4x4 control points WRITE(p, " float3 _pos[16];\n"); WRITE(p, " float2 _tex[16];\n"); WRITE(p, " float4 _col[16];\n"); WRITE(p, " int index;\n"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - WRITE(p, " index = (%i + v%s) * u_spline_count_u + (%i + u%s);\n", i, doBezier ? " * 3" : "", j, doBezier ? " * 3" : ""); + WRITE(p, " index = (%i + v%s) * spline_count_u + (%i + u%s);\n", i, doBezier ? " * 3" : "", j, doBezier ? " * 3" : ""); WRITE(p, " _pos[%i] = tess_data[index].pos;\n", i * 4 + j); if (doTexture && hasTexcoord && hasTexcoordTess) WRITE(p, " _tex[%i] = tess_data[index].tex;\n", i * 4 + j); @@ -419,77 +378,32 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage WRITE(p, " _col[%i] = tess_data[index].col;\n", i * 4 + j); } } - WRITE(p, " float2 weights[4];\n"); - if (doBezier) { - // Bernstein 3D - WRITE(p, " weights[0] = (1.0 - tess_pos) * (1.0 - tess_pos) * (1.0 - tess_pos);\n"); - WRITE(p, " weights[1] = 3.0 * tess_pos * (1.0 - tess_pos) * (1.0 - tess_pos);\n"); - WRITE(p, " weights[2] = 3.0 * tess_pos * tess_pos * (1.0 - tess_pos);\n"); - WRITE(p, " weights[3] = tess_pos * tess_pos * tess_pos;\n"); - } else if (doSpline) { - WRITE(p, " int2 spline_num_patches = int2(u_spline_count_u - 3, u_spline_count_v - 3);\n"); - WRITE(p, " int u_spline_type_u = (u_spline_counts >> 16) & 0xFF;\n"); - WRITE(p, " int u_spline_type_v = (u_spline_counts >> 24) & 0xFF;\n"); - WRITE(p, " int2 spline_type = int2(u_spline_type_u, u_spline_type_v);\n"); - WRITE(p, " float2 knots[6];\n"); - WRITE(p, " spline_knot(spline_num_patches, spline_type, knots, patch_pos);\n"); - WRITE(p, " spline_weight(tess_pos + patch_pos, knots, weights);\n"); - } - WRITE(p, " float3 pos = tess_sample(_pos, weights);\n"); + + // Basis polynomials as weight coefficients + WRITE(p, " float4 basis_u = tess_weights_u[vertex_pos.x].basis;\n"); + WRITE(p, " float4 basis_v = tess_weights_v[vertex_pos.y].basis;\n"); + + // Tessellate + WRITE(p, " float3 pos = tess_sample(_pos, basis_u, basis_v);\n"); if (doTexture && hasTexcoord) { if (hasTexcoordTess) - WRITE(p, " float2 tex = tess_sample(_tex, weights);\n"); + WRITE(p, " float2 tex = tess_sample(_tex, basis_u, basis_v);\n"); else - WRITE(p, " float2 tex = tess_pos + patch_pos;\n"); + WRITE(p, " float2 tex = In.normal.xy + float2(patch_pos);\n"); } if (hasColor) { if (hasColorTess) - WRITE(p, " float4 col = tess_sample(_col, weights);\n"); + WRITE(p, " float4 col = tess_sample(_col, basis_u, basis_v);\n"); else WRITE(p, " float4 col = tess_data[0].col;\n"); } if (hasNormal) { - // Curved surface is probably always need to compute normal(not sampling from control points) - if (doBezier) { - // Bernstein derivative - WRITE(p, " float2 bernderiv[4];\n"); - WRITE(p, " bernderiv[0] = -3.0 * (tess_pos - 1.0) * (tess_pos - 1.0); \n"); - WRITE(p, " bernderiv[1] = 9.0 * tess_pos * tess_pos - 12.0 * tess_pos + 3.0; \n"); - WRITE(p, " bernderiv[2] = 3.0 * (2.0 - 3.0 * tess_pos) * tess_pos; \n"); - WRITE(p, " bernderiv[3] = 3.0 * tess_pos * tess_pos; \n"); + // Derivatives as weight coefficients + WRITE(p, " float4 deriv_u = tess_weights_u[vertex_pos.x].deriv;\n"); + WRITE(p, " float4 deriv_v = tess_weights_v[vertex_pos.y].deriv;\n"); - WRITE(p, " float2 bernderiv_u[4];\n"); - WRITE(p, " float2 bernderiv_v[4];\n"); - WRITE(p, " for (int i = 0; i < 4; i++) {\n"); - WRITE(p, " bernderiv_u[i] = float2(bernderiv[i].x, weights[i].y);\n"); - WRITE(p, " bernderiv_v[i] = float2(weights[i].x, bernderiv[i].y);\n"); - WRITE(p, " }\n"); - - WRITE(p, " float3 du = tess_sample(_pos, bernderiv_u);\n"); - WRITE(p, " float3 dv = tess_sample(_pos, bernderiv_v);\n"); - } else if (doSpline) { - WRITE(p, " float2 tess_next_u = float2(In.normal.x, 0.0);\n"); - WRITE(p, " float2 tess_next_v = float2(0.0, In.normal.y);\n"); - // Right - WRITE(p, " float2 tess_pos_r = tess_pos + tess_next_u;\n"); - WRITE(p, " spline_weight(tess_pos_r + patch_pos, knots, weights);\n"); - WRITE(p, " float3 pos_r = tess_sample(_pos, weights);\n"); - // Left - WRITE(p, " float2 tess_pos_l = tess_pos - tess_next_u;\n"); - WRITE(p, " spline_weight(tess_pos_l + patch_pos, knots, weights);\n"); - WRITE(p, " float3 pos_l = tess_sample(_pos, weights);\n"); - // Down - WRITE(p, " float2 tess_pos_d = tess_pos + tess_next_v;\n"); - WRITE(p, " spline_weight(tess_pos_d + patch_pos, knots, weights);\n"); - WRITE(p, " float3 pos_d = tess_sample(_pos, weights);\n"); - // Up - WRITE(p, " float2 tess_pos_u = tess_pos - tess_next_v;\n"); - WRITE(p, " spline_weight(tess_pos_u + patch_pos, knots, weights);\n"); - WRITE(p, " float3 pos_u = tess_sample(_pos, weights);\n"); - - WRITE(p, " float3 du = pos_r - pos_l;\n"); - WRITE(p, " float3 dv = pos_d - pos_u;\n"); - } + WRITE(p, " float3 du = tess_sample(_pos, deriv_u, basis_v);\n"); + WRITE(p, " float3 dv = tess_sample(_pos, basis_u, deriv_v);\n"); WRITE(p, " float3 nrm = cross(du, dv);\n"); WRITE(p, " nrm = normalize(nrm);\n"); } diff --git a/GPU/GLES/DrawEngineGLES.cpp b/GPU/GLES/DrawEngineGLES.cpp index 3d0a077a00..6fd3d68ced 100644 --- a/GPU/GLES/DrawEngineGLES.cpp +++ b/GPU/GLES/DrawEngineGLES.cpp @@ -87,7 +87,8 @@ DrawEngineGLES::DrawEngineGLES(Draw::DrawContext *draw) : vai_(256), draw_(draw) InitDeviceObjects(); - tessDataTransfer = new TessellationDataTransferGLES(render_); + tessDataTransferGLES = new TessellationDataTransferGLES(render_); + tessDataTransfer = tessDataTransferGLES; } DrawEngineGLES::~DrawEngineGLES() { @@ -166,7 +167,7 @@ void DrawEngineGLES::EndFrame() { FrameData &frameData = frameData_[render_->GetCurFrame()]; render_->EndPushBuffer(frameData.pushIndex); render_->EndPushBuffer(frameData.pushVertex); - tessDataTransfer->EndFrame(); + tessDataTransferGLES->EndFrame(); } struct GlTypeInfo { @@ -655,7 +656,7 @@ bool DrawEngineGLES::IsCodePtrVertexDecoder(const u8 *ptr) const { return decJitCache_->IsInSpace(ptr); } -void DrawEngineGLES::TessellationDataTransferGLES::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) { +void DrawEngineGLES::TessellationDataTransferGLES::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) { bool hasColor = (vertType & GE_VTYPE_COL_MASK) != 0; bool hasTexCoord = (vertType & GE_VTYPE_TC_MASK) != 0; int sizeColor = hasColor ? size : 1; @@ -683,6 +684,23 @@ void DrawEngineGLES::TessellationDataTransferGLES::SendDataToShader(const Simple renderManager_->TextureSubImage(data_tex[0], 0, 0, 1, size, 1, GL_RGBA, GL_FLOAT, (u8 *)tex, GLRAllocType::NEW); // Color renderManager_->TextureSubImage(data_tex[0], 0, 0, 2, sizeColor, 1, GL_RGBA, GL_FLOAT, (u8 *)col, GLRAllocType::NEW); + + // Weight U + if (data_tex[1]) + renderManager_->DeleteTexture(data_tex[1]); + data_tex[1] = renderManager_->CreateTexture(GL_TEXTURE_2D); + renderManager_->TextureImage(data_tex[1], 0, weights.size_u * 2, 1, GL_RGBA32F, GL_RGBA, GL_FLOAT, (uint8_t *)weights.u, GLRAllocType::NONE, false); + renderManager_->FinalizeTexture(data_tex[1], 0, false); + renderManager_->BindTexture(TEX_SLOT_SPLINE_WEIGHTS_U, data_tex[1]); + + // Weight V + if (data_tex[2]) + renderManager_->DeleteTexture(data_tex[2]); + data_tex[2] = renderManager_->CreateTexture(GL_TEXTURE_2D); + renderManager_->TextureImage(data_tex[2], 0, weights.size_v * 2, 1, GL_RGBA32F, GL_RGBA, GL_FLOAT, (uint8_t *)weights.v, GLRAllocType::NONE, false); + renderManager_->FinalizeTexture(data_tex[2], 0, false); + renderManager_->BindTexture(TEX_SLOT_SPLINE_WEIGHTS_V, data_tex[2]); + } void DrawEngineGLES::TessellationDataTransferGLES::EndFrame() { diff --git a/GPU/GLES/DrawEngineGLES.h b/GPU/GLES/DrawEngineGLES.h index 19df863133..07707a435b 100644 --- a/GPU/GLES/DrawEngineGLES.h +++ b/GPU/GLES/DrawEngineGLES.h @@ -218,7 +218,8 @@ private: ~TessellationDataTransferGLES() { EndFrame(); } - void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) override; - void EndFrame() override; // Queues textures for deletion. + void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) override; + void EndFrame(); // Queues textures for deletion. }; + TessellationDataTransferGLES *tessDataTransferGLES; }; diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index fbe7e8a746..1df6f865cf 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -162,10 +162,8 @@ LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, queries.push_back({ &u_tess_points, "u_tess_points" }); 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_spline_count_u, "u_spline_count_u" }); - queries.push_back({ &u_spline_count_v, "u_spline_count_v" }); - queries.push_back({ &u_spline_type_u, "u_spline_type_u" }); - queries.push_back({ &u_spline_type_v, "u_spline_type_v" }); + queries.push_back({ &u_spline_tess, "u_spline_tess" }); + queries.push_back({ &u_spline_num_patches, "u_spline_num_patches" }); queries.push_back({ &u_depal, "u_depal" }); attrMask = vs->GetAttrMask(); @@ -567,13 +565,14 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid) { } if (dirty & DIRTY_BEZIERSPLINE) { - render_->SetUniformI1(&u_spline_count_u, gstate_c.spline_count_u); - if (u_spline_count_v != -1) - render_->SetUniformI1(&u_spline_count_v, gstate_c.spline_count_v); - if (u_spline_type_u != -1) - render_->SetUniformI1(&u_spline_type_u, gstate_c.spline_type_u); - if (u_spline_type_v != -1) - render_->SetUniformI1(&u_spline_type_v, gstate_c.spline_type_v); + if (u_spline_tess != -1) { + int tess[] = { gstate_c.spline_tess_u, gstate_c.spline_tess_v }; + render_->SetUniformI(&u_spline_tess, 2, tess); + } + if (u_spline_num_patches != -1) { + int num_patches[] = { gstate_c.spline_num_patches_u, gstate_c.spline_num_patches_v }; + render_->SetUniformI(&u_spline_num_patches, 2, num_patches); + } } } diff --git a/GPU/GLES/ShaderManagerGLES.h b/GPU/GLES/ShaderManagerGLES.h index ab551db593..b335099f74 100644 --- a/GPU/GLES/ShaderManagerGLES.h +++ b/GPU/GLES/ShaderManagerGLES.h @@ -121,10 +121,8 @@ public: int u_tess_points; // Control Points int u_tess_weights_u; int u_tess_weights_v; - int u_spline_count_u; - int u_spline_count_v; - int u_spline_type_u; - int u_spline_type_v; + int u_spline_tess; + int u_spline_num_patches; }; // Real public interface diff --git a/GPU/GLES/VertexShaderGeneratorGLES.cpp b/GPU/GLES/VertexShaderGeneratorGLES.cpp index 0c06553f84..d7f8a39f45 100644 --- a/GPU/GLES/VertexShaderGeneratorGLES.cpp +++ b/GPU/GLES/VertexShaderGeneratorGLES.cpp @@ -383,79 +383,23 @@ 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_v;\n"); - WRITE(p, "uniform int u_spline_count_u;\n"); + WRITE(p, "uniform ivec2 u_spline_num_patches;\n"); + WRITE(p, "uniform ivec2 u_spline_tess;\n"); for (int i = 2; i <= 4; i++) { // Define 3 types vec2, vec3, vec4 - WRITE(p, "vec%d tess_sample(in vec%d points[16], in vec2 weights[4]) {\n", i, i); + WRITE(p, "vec%d tess_sample(in vec%d points[16], vec4 weights_u, vec4 weights_v) {\n", i, i); WRITE(p, " vec%d pos = vec%d(0.0);\n", i, i); - WRITE(p, " for (int i = 0; i < 4; ++i) {\n"); - WRITE(p, " for (int j = 0; j < 4; ++j) {\n"); - WRITE(p, " float f = weights[j].x * weights[i].y;\n"); + WRITE(p, " for (int v = 0; v < 4; ++v) {\n"); + WRITE(p, " for (int u = 0; u < 4; ++u) {\n"); + WRITE(p, " float f = weights_u[u] * weights_v[v];\n"); WRITE(p, " if (f != 0.0)\n"); - WRITE(p, " pos = pos + f * points[i * 4 + j];\n"); + WRITE(p, " pos += f * points[v * 4 + u];\n"); WRITE(p, " }\n"); WRITE(p, " }\n"); WRITE(p, " return pos;\n"); WRITE(p, "}\n"); } - if (doSpline) { - WRITE(p, "uniform int u_spline_count_v;\n"); - WRITE(p, "uniform int u_spline_type_u;\n"); - WRITE(p, "uniform int u_spline_type_v;\n"); - - WRITE(p, "void spline_knot(ivec2 num_patches, ivec2 type, out vec2 knot[6], ivec2 patch_pos) {\n"); - WRITE(p, " for (int i = 0; i < 6; ++i) {\n"); - WRITE(p, " knot[i] = vec2(float(i + patch_pos.x - 2), float(i + patch_pos.y - 2));\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.x & 1) != 0) {\n"); - WRITE(p, " if (patch_pos.x <= 2)\n"); - WRITE(p, " knot[0].x = 0.0;\n"); - WRITE(p, " if (patch_pos.x <= 1)\n"); - WRITE(p, " knot[1].x = 0.0;\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.x & 2) != 0) {\n"); - WRITE(p, " if (patch_pos.x >= (num_patches.x - 2))\n"); - WRITE(p, " knot[5].x = float(num_patches.x);\n"); - WRITE(p, " if (patch_pos.x == (num_patches.x - 1))\n"); - WRITE(p, " knot[4].x = float(num_patches.x);\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.y & 1) != 0) {\n"); - WRITE(p, " if (patch_pos.y <= 2)\n"); - WRITE(p, " knot[0].y = 0.0;\n"); - WRITE(p, " if (patch_pos.y <= 1)\n"); - WRITE(p, " knot[1].y = 0.0;\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.y & 2) != 0) {\n"); - WRITE(p, " if (patch_pos.y >= (num_patches.y - 2))\n"); - WRITE(p, " knot[5].y = float(num_patches.y);\n"); - WRITE(p, " if (patch_pos.y == (num_patches.y - 1))\n"); - WRITE(p, " knot[4].y = float(num_patches.y);\n"); - WRITE(p, " }\n"); - WRITE(p, "}\n"); - - WRITE(p, "void spline_weight(vec2 t, in vec2 knot[6], out vec2 weights[4]) {\n"); - // TODO: Maybe compilers could be coaxed into vectorizing this code without the above explicitly... - WRITE(p, " vec2 t0 = (t - knot[0]);\n"); - WRITE(p, " vec2 t1 = (t - knot[1]);\n"); - WRITE(p, " vec2 t2 = (t - knot[2]);\n"); - // TODO: All our knots are integers so we should be able to get rid of these divisions (How?) - WRITE(p, " vec2 f30 = t0 / (knot[3] - knot[0]);\n"); - WRITE(p, " vec2 f41 = t1 / (knot[4] - knot[1]);\n"); - WRITE(p, " vec2 f52 = t2 / (knot[5] - knot[2]);\n"); - WRITE(p, " vec2 f31 = t1 / (knot[3] - knot[1]);\n"); - WRITE(p, " vec2 f42 = t2 / (knot[4] - knot[2]);\n"); - WRITE(p, " vec2 f32 = t2 / (knot[3] - knot[2]);\n"); - WRITE(p, " vec2 a = (1.0 - f30)*(1.0 - f31);\n"); - WRITE(p, " vec2 b = (f31*f41);\n"); - WRITE(p, " vec2 c = (1.0 - f41)*(1.0 - f42);\n"); - WRITE(p, " vec2 d = (f42*f52);\n"); - WRITE(p, " weights[0] = a - (a*f32);\n"); - WRITE(p, " weights[1] = vec2(1.0) - a - b + ((a + b + c - vec2(1.0))*f32);\n"); - WRITE(p, " weights[2] = b + ((vec2(1.0) - b - c - d)*f32);\n"); - WRITE(p, " weights[3] = d*f32;\n"); - WRITE(p, "}\n"); - } } WRITE(p, "void main() {\n"); @@ -496,16 +440,25 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask, if (!enableBones) { // Hardware tessellation if (doBezier || doSpline) { + 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 %% u_spline_num_patches.x;\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 vertex_pos = ivec2(position.xy);\n"); + if (doSpline) { + WRITE(p, " if ((vertex_pos.x == u_spline_tess.x) && (u < u_spline_num_patches.x - 1))\n"); + WRITE(p, " u++;\n"); // Use next patch position + WRITE(p, " if ((vertex_pos.y == u_spline_tess.y) && (v < u_spline_num_patches.y - 1))\n"); + WRITE(p, " v++;\n"); // Use next patch position + WRITE(p, " vertex_pos += patch_pos * u_spline_tess;\n"); + } + // Load 4x4 control points WRITE(p, " vec3 _pos[16];\n"); WRITE(p, " vec2 _tex[16];\n"); WRITE(p, " vec4 _col[16];\n"); - WRITE(p, " int num_patches_u = %s;\n", doBezier ? "(u_spline_count_u - 1) / 3" : "u_spline_count_u - 3"); - WRITE(p, " int u = int(mod(float(gl_InstanceID), float(num_patches_u)));\n"); - WRITE(p, " int v = gl_InstanceID / num_patches_u;\n"); - WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n"); WRITE(p, " for (int i = 0; i < 4; i++) {\n"); WRITE(p, " for (int j = 0; j < 4; j++) {\n"); - WRITE(p, " int index = (i + v%s) * u_spline_count_u + (j + u%s);\n", doBezier ? " * 3" : "", doBezier ? " * 3" : ""); + WRITE(p, " int index = (i + v%s) * spline_count_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); if (doTexture && hasTexcoord && hasTexcoordTess) WRITE(p, " _tex[i * 4 + j] = %s(u_tess_points, ivec2(index, 1), 0).xy;\n", texelFetch); @@ -513,76 +466,32 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask, WRITE(p, " _col[i * 4 + j] = %s(u_tess_points, ivec2(index, 2), 0).rgba;\n", texelFetch); WRITE(p, " }\n"); WRITE(p, " }\n"); - WRITE(p, " vec2 tess_pos = position.xy;\n"); - WRITE(p, " vec2 weights[4];\n"); - if (doBezier) { - // Bernstein 3D - WRITE(p, " weights[0] = (1.0 - tess_pos) * (1.0 - tess_pos) * (1.0 - tess_pos);\n"); - WRITE(p, " weights[1] = 3.0 * tess_pos * (1.0 - tess_pos) * (1.0 - tess_pos);\n"); - WRITE(p, " weights[2] = 3.0 * tess_pos * tess_pos * (1.0 - tess_pos);\n"); - WRITE(p, " weights[3] = tess_pos * tess_pos * tess_pos;\n"); - } else { // Spline - WRITE(p, " ivec2 spline_num_patches = ivec2(u_spline_count_u - 3, u_spline_count_v - 3);\n"); - WRITE(p, " ivec2 spline_type = ivec2(u_spline_type_u, u_spline_type_v);\n"); - WRITE(p, " vec2 knots[6];\n"); - WRITE(p, " spline_knot(spline_num_patches, spline_type, knots, patch_pos);\n"); - WRITE(p, " spline_weight(tess_pos + vec2(patch_pos), knots, weights);\n"); - } - WRITE(p, " vec3 pos = tess_sample(_pos, weights);\n"); + + // Basis polynomials as weight coefficients + WRITE(p, " vec4 basis_u = %s(u_tess_weights_u, %s, 0);\n", texelFetch, "ivec2(vertex_pos.x * 2, 0)"); + WRITE(p, " vec4 basis_v = %s(u_tess_weights_v, %s, 0);\n", texelFetch, "ivec2(vertex_pos.y * 2, 0)"); + + // Tessellate + WRITE(p, " vec3 pos = tess_sample(_pos, basis_u, basis_v);\n"); if (doTexture && hasTexcoord) { if (hasTexcoordTess) - WRITE(p, " vec2 tex = tess_sample(_tex, weights);\n"); + WRITE(p, " vec2 tex = tess_sample(_tex, basis_u, basis_v);\n"); else - WRITE(p, " vec2 tex = tess_pos + vec2(patch_pos);\n"); + WRITE(p, " vec2 tex = normal.xy + vec2(patch_pos);\n"); } if (hasColor) { if (hasColorTess) - WRITE(p, " vec4 col = tess_sample(_col, weights);\n"); + WRITE(p, " vec4 col = tess_sample(_col, basis_u, basis_v);\n"); else WRITE(p, " vec4 col = %s(u_tess_points, ivec2(0, 2), 0).rgba;\n", texelFetch); } if (hasNormal) { - // Curved surface is probably always need to compute normal(not sampling from control points) - if (doBezier) { - // Bernstein derivative - WRITE(p, " vec2 bernderiv[4];\n"); - WRITE(p, " bernderiv[0] = -3.0 * (tess_pos - 1.0) * (tess_pos - 1.0); \n"); - WRITE(p, " bernderiv[1] = 9.0 * tess_pos * tess_pos - 12.0 * tess_pos + 3.0; \n"); - WRITE(p, " bernderiv[2] = 3.0 * (2.0 - 3.0 * tess_pos) * tess_pos; \n"); - WRITE(p, " bernderiv[3] = 3.0 * tess_pos * tess_pos; \n"); + // Derivatives as weight coefficients + WRITE(p, " vec4 deriv_u = %s(u_tess_weights_u, %s, 0);\n", texelFetch, "ivec2(vertex_pos.x * 2 + 1, 0)"); + WRITE(p, " vec4 deriv_v = %s(u_tess_weights_v, %s, 0);\n", texelFetch, "ivec2(vertex_pos.y * 2 + 1, 0)"); - WRITE(p, " vec2 bernderiv_u[4];\n"); - WRITE(p, " vec2 bernderiv_v[4];\n"); - WRITE(p, " for (int i = 0; i < 4; i++) {\n"); - WRITE(p, " bernderiv_u[i] = vec2(bernderiv[i].x, weights[i].y);\n"); - WRITE(p, " bernderiv_v[i] = vec2(weights[i].x, bernderiv[i].y);\n"); - WRITE(p, " }\n"); - - WRITE(p, " vec3 du = tess_sample(_pos, bernderiv_u);\n"); - WRITE(p, " vec3 dv = tess_sample(_pos, bernderiv_v);\n"); - } else { // Spline - WRITE(p, " vec2 tess_next_u = vec2(normal.x, 0.0);\n"); - WRITE(p, " vec2 tess_next_v = vec2(0.0, normal.y);\n"); - // Right - WRITE(p, " vec2 tess_pos_r = tess_pos + tess_next_u;\n"); - WRITE(p, " spline_weight(tess_pos_r + vec2(patch_pos), knots, weights);\n"); - WRITE(p, " vec3 pos_r = tess_sample(_pos, weights);\n"); - // Left - WRITE(p, " vec2 tess_pos_l = tess_pos - tess_next_u;\n"); - WRITE(p, " spline_weight(tess_pos_l + vec2(patch_pos), knots, weights);\n"); - WRITE(p, " vec3 pos_l = tess_sample(_pos, weights);\n"); - // Down - WRITE(p, " vec2 tess_pos_d = tess_pos + tess_next_v;\n"); - WRITE(p, " spline_weight(tess_pos_d + vec2(patch_pos), knots, weights);\n"); - WRITE(p, " vec3 pos_d = tess_sample(_pos, weights);\n"); - // Up - WRITE(p, " vec2 tess_pos_u = tess_pos - tess_next_v;\n"); - WRITE(p, " spline_weight(tess_pos_u + vec2(patch_pos), knots, weights);\n"); - WRITE(p, " vec3 pos_u = tess_sample(_pos, weights);\n"); - - WRITE(p, " vec3 du = pos_r - pos_l;\n"); - WRITE(p, " vec3 dv = pos_d - pos_u;\n"); - } + WRITE(p, " vec3 du = tess_sample(_pos, deriv_u, basis_v);\n"); + WRITE(p, " vec3 dv = tess_sample(_pos, basis_u, deriv_v);\n"); WRITE(p, " vec3 nrm = cross(du, dv);\n"); WRITE(p, " nrm = normalize(nrm);\n"); } diff --git a/GPU/GPUCommon.cpp b/GPU/GPUCommon.cpp index a0ecf7dc4d..2bdd7d4d7d 100644 --- a/GPU/GPUCommon.cpp +++ b/GPU/GPUCommon.cpp @@ -1760,9 +1760,10 @@ void GPUCommon::Execute_Bezier(u32 op, u32 diff) { if (CanUseHardwareTessellation(patchPrim)) { gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE); gstate_c.bezier = true; - if (gstate_c.spline_count_u != bz_ucount) { + int num_patches_u = (bz_ucount - 1) / 3; + if (gstate_c.spline_num_patches_u != num_patches_u) { gstate_c.Dirty(DIRTY_BEZIERSPLINE); - gstate_c.spline_count_u = bz_ucount; + gstate_c.spline_num_patches_u = num_patches_u; } } @@ -1824,14 +1825,18 @@ void GPUCommon::Execute_Spline(u32 op, u32 diff) { if (CanUseHardwareTessellation(patchPrim)) { gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE); gstate_c.spline = true; - bool countsChanged = gstate_c.spline_count_u != sp_ucount || gstate_c.spline_count_v != sp_vcount; - bool typesChanged = gstate_c.spline_type_u != sp_utype || gstate_c.spline_type_v != sp_vtype; - if (countsChanged || typesChanged) { + int tess_u = gstate.getPatchDivisionU(); + int tess_v = gstate.getPatchDivisionV(); + 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 patchesChanged = gstate_c.spline_num_patches_u != num_patches_u || gstate_c.spline_num_patches_v != num_patches_v; + if (divsChanged || patchesChanged) { gstate_c.Dirty(DIRTY_BEZIERSPLINE); - gstate_c.spline_count_u = sp_ucount; - gstate_c.spline_count_v = sp_vcount; - gstate_c.spline_type_u = sp_utype; - gstate_c.spline_type_v = sp_vtype; + gstate_c.spline_tess_u = tess_u; + gstate_c.spline_tess_v = tess_v; + gstate_c.spline_num_patches_u = num_patches_u; + gstate_c.spline_num_patches_v = num_patches_v; } } diff --git a/GPU/GPUState.h b/GPU/GPUState.h index a73023927f..ce5d7116d3 100644 --- a/GPU/GPUState.h +++ b/GPU/GPUState.h @@ -602,10 +602,10 @@ struct GPUStateCache { bool bezier; bool spline; - int spline_count_u; - int spline_count_v; - int spline_type_u; - int spline_type_v; + int spline_tess_u; + int spline_tess_v; + int spline_num_patches_u; + int spline_num_patches_v; bool useShaderDepal; GEBufferFormat depalFramebufferFormat; diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index e100513f2b..c8b2c43da1 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -69,6 +69,8 @@ enum { DRAW_BINDING_DYNUBO_LIGHT = 4, DRAW_BINDING_DYNUBO_BONE = 5, DRAW_BINDING_TESS_STORAGE_BUF = 6, + DRAW_BINDING_TESS_STORAGE_BUF_WU = 7, + DRAW_BINDING_TESS_STORAGE_BUF_WV = 8, }; enum { @@ -96,7 +98,7 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan, Draw::DrawContext *dra void DrawEngineVulkan::InitDeviceObjects() { // All resources we need for PSP drawing. Usually only bindings 0 and 2-4 are populated. - VkDescriptorSetLayoutBinding bindings[7]{}; + VkDescriptorSetLayoutBinding bindings[9]{}; bindings[0].descriptorCount = 1; bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; @@ -126,6 +128,14 @@ void DrawEngineVulkan::InitDeviceObjects() { bindings[6].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; bindings[6].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; bindings[6].binding = DRAW_BINDING_TESS_STORAGE_BUF; + bindings[7].descriptorCount = 1; + bindings[7].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + bindings[7].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + bindings[7].binding = DRAW_BINDING_TESS_STORAGE_BUF_WU; + bindings[8].descriptorCount = 1; + bindings[8].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + bindings[8].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + bindings[8].binding = DRAW_BINDING_TESS_STORAGE_BUF_WV; VkDevice device = vulkan_->GetDevice(); @@ -167,7 +177,8 @@ void DrawEngineVulkan::InitDeviceObjects() { vertexCache_ = new VulkanPushBuffer(vulkan_, VERTEX_CACHE_SIZE); - tessDataTransfer = new TessellationDataTransferVulkan(vulkan_); + tessDataTransferVulkan = new TessellationDataTransferVulkan(vulkan_); + tessDataTransfer = tessDataTransferVulkan; } DrawEngineVulkan::~DrawEngineVulkan() { @@ -202,7 +213,7 @@ void DrawEngineVulkan::FrameData::Destroy(VulkanContext *vulkan) { void DrawEngineVulkan::DestroyDeviceObjects() { delete tessDataTransfer; - tessDataTransfer = nullptr; + tessDataTransfer = tessDataTransferVulkan = nullptr; for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) { frame_[i].Destroy(vulkan_); @@ -258,7 +269,7 @@ void DrawEngineVulkan::BeginFrame() { frame->pushIndex->Begin(vulkan_); // TODO: How can we make this nicer... - ((TessellationDataTransferVulkan *)tessDataTransfer)->SetPushBuffer(frame->pushUBO); + tessDataTransferVulkan->SetPushBuffer(frame->pushUBO); DirtyAllUBOs(); @@ -470,23 +481,32 @@ VkDescriptorSet DrawEngineVulkan::GetOrCreateDescriptorSet(VkImageView imageView n++; } - // Tessellation data buffer. Make sure this is declared outside the if to avoid optimizer - // shenanigans. - VkDescriptorBufferInfo tess_buf{}; + // Tessellation data buffer. if (tess) { - VkBuffer buf; - VkDeviceSize offset; - VkDeviceSize range; - ((TessellationDataTransferVulkan *)tessDataTransfer)->GetBufferAndOffset(&buf, &offset, &range); - assert(buf); - tess_buf.buffer = buf; - tess_buf.offset = offset; - tess_buf.range = range; - tessOffset_ = offset; + const VkDescriptorBufferInfo *bufInfo = tessDataTransferVulkan->GetBufferInfo(); + // Control Points writes[n].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writes[n].pNext = nullptr; writes[n].dstBinding = DRAW_BINDING_TESS_STORAGE_BUF; - writes[n].pBufferInfo = &tess_buf; + writes[n].pBufferInfo = &bufInfo[0]; + writes[n].descriptorCount = 1; + writes[n].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + writes[n].dstSet = desc; + n++; + // Weights U + writes[n].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writes[n].pNext = nullptr; + writes[n].dstBinding = DRAW_BINDING_TESS_STORAGE_BUF_WU; + writes[n].pBufferInfo = &bufInfo[1]; + writes[n].descriptorCount = 1; + writes[n].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + writes[n].dstSet = desc; + n++; + // Weights V + writes[n].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writes[n].pNext = nullptr; + writes[n].dstBinding = DRAW_BINDING_TESS_STORAGE_BUF_WV; + writes[n].pBufferInfo = &bufInfo[2]; writes[n].descriptorCount = 1; writes[n].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; writes[n].dstSet = desc; @@ -1001,7 +1021,7 @@ DrawEngineVulkan::TessellationDataTransferVulkan::TessellationDataTransferVulkan DrawEngineVulkan::TessellationDataTransferVulkan::~TessellationDataTransferVulkan() { } -void DrawEngineVulkan::TessellationDataTransferVulkan::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) { +void DrawEngineVulkan::TessellationDataTransferVulkan::SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) { // SSBOs that are not simply float1 or float2 need to be padded up to a float4 size. vec3 members // also need to be 16-byte aligned, hence the padding. struct TessData { @@ -1011,8 +1031,8 @@ void DrawEngineVulkan::TessellationDataTransferVulkan::SendDataToShader(const Si }; int ssboAlignment = vulkan_->GetPhysicalDeviceProperties(vulkan_->GetCurrentPhysicalDevice()).limits.minStorageBufferOffsetAlignment; - uint8_t *data = (uint8_t *)push_->PushAligned(size * sizeof(TessData), &offset_, &buf_, ssboAlignment); - range_ = size * sizeof(TessData); + uint8_t *data = (uint8_t *)push_->PushAligned(size * sizeof(TessData), (uint32_t *)&bufInfo_[0].offset, &bufInfo_[0].buffer, ssboAlignment); + bufInfo_[0].range = size * sizeof(TessData); float *pos = (float *)(data); float *tex = (float *)(data + offsetof(TessData, uv)); @@ -1020,4 +1040,14 @@ void DrawEngineVulkan::TessellationDataTransferVulkan::SendDataToShader(const Si int stride = sizeof(TessData) / sizeof(float); CopyControlPoints(pos, tex, col, stride, stride, stride, points, size, vertType); + + // Weights U + data = (uint8_t *)push_->PushAligned(weights.size_u * sizeof(Weight), (uint32_t *)&bufInfo_[1].offset, &bufInfo_[1].buffer, ssboAlignment); + memcpy(data, weights.u, weights.size_u * sizeof(Weight)); + bufInfo_[1].range = weights.size_u * sizeof(Weight); + + // Weights V + data = (uint8_t *)push_->PushAligned(weights.size_v * sizeof(Weight), (uint32_t *)&bufInfo_[2].offset, &bufInfo_[2].buffer, ssboAlignment); + memcpy(data, weights.v, weights.size_v * sizeof(Weight)); + bufInfo_[2].range = weights.size_v * sizeof(Weight); } diff --git a/GPU/Vulkan/DrawEngineVulkan.h b/GPU/Vulkan/DrawEngineVulkan.h index a9837f44a3..8cb4c82350 100644 --- a/GPU/Vulkan/DrawEngineVulkan.h +++ b/GPU/Vulkan/DrawEngineVulkan.h @@ -284,24 +284,13 @@ private: ~TessellationDataTransferVulkan(); void SetPushBuffer(VulkanPushBuffer *push) { push_ = push; } - void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType) override; - - void GetBufferAndOffset(VkBuffer *buf, VkDeviceSize *offset, VkDeviceSize *range) { - *buf = buf_; - *offset = (VkDeviceSize)offset_; - *range = (VkDeviceSize)range_; - - buf_ = 0; - offset_ = 0; - range_ = 0; - } - + void SendDataToShader(const SimpleVertex *const *points, int size, u32 vertType, const Weight2D &weights) override; + const VkDescriptorBufferInfo *GetBufferInfo() { return bufInfo_; } private: VulkanContext *vulkan_; VulkanPushBuffer *push_; // Updated each frame. - uint32_t offset_ = 0; - uint32_t range_ = 0; - VkBuffer buf_ = VK_NULL_HANDLE; + VkDescriptorBufferInfo bufInfo_[3] = {}; }; + TessellationDataTransferVulkan *tessDataTransferVulkan; }; diff --git a/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp b/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp index cf8fb819a2..6598e4000a 100644 --- a/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp +++ b/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp @@ -219,78 +219,36 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) { WRITE(p, " vec4 pos;\n"); WRITE(p, " vec4 uv;\n"); WRITE(p, " vec4 color;\n"); - WRITE(p, "};"); + WRITE(p, "};\n"); WRITE(p, "layout (std430, set = 0, binding = 6) readonly buffer s_tess_data {\n"); - WRITE(p, " TessData data[];"); + WRITE(p, " TessData data[];\n"); WRITE(p, "} tess_data;\n"); + WRITE(p, "layout (std430) struct TessWeight {\n"); + WRITE(p, " vec4 basis;\n"); + WRITE(p, " vec4 deriv;\n"); + WRITE(p, "};\n"); + WRITE(p, "layout (std430, set = 0, binding = 7) buffer s_tess_weights_u {\n"); + WRITE(p, " TessWeight data[];\n"); + WRITE(p, "} tess_weights_u;\n"); + WRITE(p, "layout (std430, set = 0, binding = 8) buffer s_tess_weights_v {\n"); + WRITE(p, " TessWeight data[];\n"); + WRITE(p, "} tess_weights_v;\n"); + for (int i = 2; i <= 4; i++) { // Define 3 types vec2, vec3, vec4 - WRITE(p, "vec%d tess_sample(in vec%d points[16], in vec2 weights[4]) {\n", i, i); - WRITE(p, " vec%d pos = vec%d(0);\n", i, i); - WRITE(p, " for (int i = 0; i < 4; ++i) {\n"); - WRITE(p, " for (int j = 0; j < 4; ++j) {\n"); - WRITE(p, " float f = weights[j].x * weights[i].y;\n"); - WRITE(p, " if (f != 0)\n"); - WRITE(p, " pos = pos + f * points[i * 4 + j];\n"); + WRITE(p, "vec%d tess_sample(in vec%d points[16], vec4 weights_u, vec4 weights_v) {\n", i, i); + WRITE(p, " vec%d pos = vec%d(0.0);\n", i, i); + WRITE(p, " for (int v = 0; v < 4; ++v) {\n"); + WRITE(p, " for (int u = 0; u < 4; ++u) {\n"); + WRITE(p, " float f = weights_u[u] * weights_v[v];\n"); + WRITE(p, " if (f != 0.0)\n"); + WRITE(p, " pos += f * points[v * 4 + u];\n"); WRITE(p, " }\n"); WRITE(p, " }\n"); WRITE(p, " return pos;\n"); WRITE(p, "}\n"); } - if (doSpline) { - WRITE(p, "void spline_knot(ivec2 num_patches, ivec2 type, out vec2 knot[6], ivec2 patch_pos) {\n"); - WRITE(p, " for (int i = 0; i < 6; ++i) {\n"); - WRITE(p, " knot[i] = vec2(i + patch_pos.x - 2, i + patch_pos.y - 2);\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.x & 1) != 0) {\n"); - WRITE(p, " if (patch_pos.x <= 2)\n"); - WRITE(p, " knot[0].x = 0;\n"); - WRITE(p, " if (patch_pos.x <= 1)\n"); - WRITE(p, " knot[1].x = 0;\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.x & 2) != 0) {\n"); - WRITE(p, " if (patch_pos.x >= (num_patches.x - 2))\n"); - WRITE(p, " knot[5].x = num_patches.x;\n"); - WRITE(p, " if (patch_pos.x == (num_patches.x - 1))\n"); - WRITE(p, " knot[4].x = num_patches.x;\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.y & 1) != 0) {\n"); - WRITE(p, " if (patch_pos.y <= 2)\n"); - WRITE(p, " knot[0].y = 0;\n"); - WRITE(p, " if (patch_pos.y <= 1)\n"); - WRITE(p, " knot[1].y = 0;\n"); - WRITE(p, " }\n"); - WRITE(p, " if ((type.y & 2) != 0) {\n"); - WRITE(p, " if (patch_pos.y >= (num_patches.y - 2))\n"); - WRITE(p, " knot[5].y = num_patches.y;\n"); - WRITE(p, " if (patch_pos.y == (num_patches.y - 1))\n"); - WRITE(p, " knot[4].y = num_patches.y;\n"); - WRITE(p, " }\n"); - WRITE(p, "}\n"); - - WRITE(p, "void spline_weight(vec2 t, in vec2 knot[6], out vec2 weights[4]) {\n"); - // TODO: Maybe compilers could be coaxed into vectorizing this code without the above explicitly... - WRITE(p, " vec2 t0 = (t - knot[0]);\n"); - WRITE(p, " vec2 t1 = (t - knot[1]);\n"); - WRITE(p, " vec2 t2 = (t - knot[2]);\n"); - // TODO: All our knots are integers so we should be able to get rid of these divisions (How?) - WRITE(p, " vec2 f30 = t0 / (knot[3] - knot[0]);\n"); - WRITE(p, " vec2 f41 = t1 / (knot[4] - knot[1]);\n"); - WRITE(p, " vec2 f52 = t2 / (knot[5] - knot[2]);\n"); - WRITE(p, " vec2 f31 = t1 / (knot[3] - knot[1]);\n"); - WRITE(p, " vec2 f42 = t2 / (knot[4] - knot[2]);\n"); - WRITE(p, " vec2 f32 = t2 / (knot[3] - knot[2]);\n"); - WRITE(p, " vec2 a = (1 - f30)*(1 - f31);\n"); - WRITE(p, " vec2 b = (f31*f41);\n"); - WRITE(p, " vec2 c = (1 - f41)*(1 - f42);\n"); - WRITE(p, " vec2 d = (f42*f52);\n"); - WRITE(p, " weights[0] = a - (a*f32);\n"); - WRITE(p, " weights[1] = 1 - a - b + ((a + b + c - 1)*f32);\n"); - WRITE(p, " weights[2] = b + ((1 - b - c - d)*f32);\n"); - WRITE(p, " weights[3] = d*f32;\n"); - WRITE(p, "}\n"); - } } WRITE(p, "void main() {\n"); @@ -330,15 +288,25 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) { // Step 1: World Transform / Skinning if (!enableBones) { if (doBezier || doSpline) { + WRITE(p, " ivec2 spline_num_patches = ivec2((base.spline_counts >> 0) & 0xff, (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, " 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.x;\n"); + WRITE(p, " int v = gl_InstanceIndex / spline_num_patches.x;\n"); + WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n"); + WRITE(p, " ivec2 vertex_pos = ivec2(position.xy);\n"); + if (doSpline) { + WRITE(p, " if ((vertex_pos.x == spline_tess.x) && (u < spline_num_patches.x - 1))\n"); + WRITE(p, " u++;\n"); // Use next patch position + WRITE(p, " if ((vertex_pos.y == spline_tess.y) && (v < spline_num_patches.y - 1))\n"); + WRITE(p, " v++;\n"); // Use next patch position + WRITE(p, " vertex_pos += patch_pos * spline_tess;\n"); + } + // Load 4x4 control points WRITE(p, " vec3 _pos[16];\n"); WRITE(p, " vec2 _tex[16];\n"); WRITE(p, " vec4 _col[16];\n"); - WRITE(p, " int spline_count_u = int(base.spline_counts & 0xff);\n"); - WRITE(p, " int spline_count_v = int((base.spline_counts >> 8) & 0xff);\n"); - WRITE(p, " int num_patches_u = %s;\n", doBezier ? "(spline_count_u - 1) / 3" : "spline_count_u - 3"); - WRITE(p, " int u = int(mod(gl_InstanceIndex, num_patches_u));\n"); - WRITE(p, " int v = gl_InstanceIndex / num_patches_u;\n"); - WRITE(p, " ivec2 patch_pos = ivec2(u, v);\n"); WRITE(p, " for (int i = 0; i < 4; i++) {\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" : ""); @@ -349,78 +317,32 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) { WRITE(p, " _col[i * 4 + j] = tess_data.data[idx].color;\n"); WRITE(p, " }\n"); WRITE(p, " }\n"); - WRITE(p, " vec2 tess_pos = position.xy;\n"); - WRITE(p, " vec2 weights[4];\n"); - if (doBezier) { - // Bernstein 3D - WRITE(p, " weights[0] = (1 - tess_pos) * (1 - tess_pos) * (1 - tess_pos);\n"); - WRITE(p, " weights[1] = 3 * tess_pos * (1 - tess_pos) * (1 - tess_pos);\n"); - WRITE(p, " weights[2] = 3 * tess_pos * tess_pos * (1 - tess_pos);\n"); - WRITE(p, " weights[3] = tess_pos * tess_pos * tess_pos;\n"); - } else { // Spline - WRITE(p, " ivec2 spline_num_patches = ivec2(spline_count_u - 3, spline_count_v - 3);\n"); - WRITE(p, " int spline_type_u = int((base.spline_counts >> 16) & 0xff);\n"); - WRITE(p, " int spline_type_v = int((base.spline_counts >> 24) & 0xff);\n"); - WRITE(p, " ivec2 spline_type = ivec2(spline_type_u, spline_type_v);\n"); - WRITE(p, " vec2 knots[6];\n"); - WRITE(p, " spline_knot(spline_num_patches, spline_type, knots, patch_pos);\n"); - WRITE(p, " spline_weight(tess_pos + patch_pos, knots, weights);\n"); - } - WRITE(p, " vec3 pos = tess_sample(_pos, weights);\n"); + + // Basis polynomials as weight coefficients + WRITE(p, " vec4 basis_u = tess_weights_u.data[vertex_pos.x].basis;\n"); + WRITE(p, " vec4 basis_v = tess_weights_v.data[vertex_pos.y].basis;\n"); + + // Tessellate + WRITE(p, " vec3 pos = tess_sample(_pos, basis_u, basis_v);\n"); if (doTexture && hasTexcoord) { if (hasTexcoordTess) - WRITE(p, " vec2 tex = tess_sample(_tex, weights);\n"); + WRITE(p, " vec2 tex = tess_sample(_tex, basis_u, basis_v);\n"); else - WRITE(p, " vec2 tex = tess_pos + patch_pos;\n"); + WRITE(p, " vec2 tex = normal.xy + vec2(patch_pos);\n"); } if (hasColor) { if (hasColorTess) - WRITE(p, " vec4 col = tess_sample(_col, weights);\n"); + WRITE(p, " vec4 col = tess_sample(_col, basis_u, basis_v);\n"); else WRITE(p, " vec4 col = tess_data.data[0].color;\n"); } if (hasNormal) { - // Curved surface is probably always need to compute normal(not sampling from control points) - if (doBezier) { - // Bernstein derivative - WRITE(p, " vec2 bernderiv[4];\n"); - WRITE(p, " bernderiv[0] = -3 * (tess_pos - 1) * (tess_pos - 1); \n"); - WRITE(p, " bernderiv[1] = 9 * tess_pos * tess_pos - 12 * tess_pos + 3; \n"); - WRITE(p, " bernderiv[2] = 3 * (2 - 3 * tess_pos) * tess_pos; \n"); - WRITE(p, " bernderiv[3] = 3 * tess_pos * tess_pos; \n"); + // Derivatives as weight coefficients + WRITE(p, " vec4 deriv_u = tess_weights_u.data[vertex_pos.x].deriv;\n"); + WRITE(p, " vec4 deriv_v = tess_weights_v.data[vertex_pos.y].deriv;\n"); - WRITE(p, " vec2 bernderiv_u[4];\n"); - WRITE(p, " vec2 bernderiv_v[4];\n"); - WRITE(p, " for (int i = 0; i < 4; i++) {\n"); - WRITE(p, " bernderiv_u[i] = vec2(bernderiv[i].x, weights[i].y);\n"); - WRITE(p, " bernderiv_v[i] = vec2(weights[i].x, bernderiv[i].y);\n"); - WRITE(p, " }\n"); - - WRITE(p, " vec3 du = tess_sample(_pos, bernderiv_u);\n"); - WRITE(p, " vec3 dv = tess_sample(_pos, bernderiv_v);\n"); - } else { // Spline - WRITE(p, " vec2 tess_next_u = vec2(normal.x, 0);\n"); - WRITE(p, " vec2 tess_next_v = vec2(0, normal.y);\n"); - // Right - WRITE(p, " vec2 tess_pos_r = tess_pos + tess_next_u;\n"); - WRITE(p, " spline_weight(tess_pos_r + patch_pos, knots, weights);\n"); - WRITE(p, " vec3 pos_r = tess_sample(_pos, weights);\n"); - // Left - WRITE(p, " vec2 tess_pos_l = tess_pos - tess_next_u;\n"); - WRITE(p, " spline_weight(tess_pos_l + patch_pos, knots, weights);\n"); - WRITE(p, " vec3 pos_l = tess_sample(_pos, weights);\n"); - // Down - WRITE(p, " vec2 tess_pos_d = tess_pos + tess_next_v;\n"); - WRITE(p, " spline_weight(tess_pos_d + patch_pos, knots, weights);\n"); - WRITE(p, " vec3 pos_d = tess_sample(_pos, weights);\n"); - // Up - WRITE(p, " vec2 tess_pos_u = tess_pos - tess_next_v;\n"); - WRITE(p, " spline_weight(tess_pos_u + patch_pos, knots, weights);\n"); - WRITE(p, " vec3 pos_u = tess_sample(_pos, weights);\n"); - - WRITE(p, " vec3 du = pos_r - pos_l;\n"); - WRITE(p, " vec3 dv = pos_d - pos_u;\n"); - } + WRITE(p, " vec3 du = tess_sample(_pos, deriv_u, basis_v);\n"); + WRITE(p, " vec3 dv = tess_sample(_pos, basis_u, deriv_v);\n"); WRITE(p, " vec3 nrm = cross(du, dv);\n"); WRITE(p, " nrm = normalize(nrm);\n"); }