Expand spline data into a separate buffer. Prevent overflowing it by reducing the size of spline draws if necessary (but shouldn't really happen).

Might help #7386
This commit is contained in:
Henrik Rydgard committed 2015-01-29 16:04:00 +01:00
1 parent 626e2fc2c8
commit 033798c796
9 files changed
+68 -44

No files matched your search

+8
View File
@@ -25,6 +25,13 @@
class VertexDecoder;
enum {
VERTEX_BUFFER_MAX = 65536,
DECODED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 48,
DECODED_INDEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 20,
SPLINE_BUFFER_SIZE = VERTEX_BUFFER_MAX * 20,
};
class DrawEngineCommon {
public:
virtual ~DrawEngineCommon();
@@ -42,4 +49,5 @@ protected:
// Vertex collector buffers
u8 *decoded;
u16 *decIndex;
u8 *splineBuffer;
};
+34 -18
View File
@@ -80,22 +80,23 @@ inline float bern2deriv(float x) { return 3 * (2 - 3 * x) * x; }
inline float bern3deriv(float x) { return 3 * x * x; }
// http://en.wikipedia.org/wiki/Bernstein_polynomial
Vec3Packedf Bernstein3D(const Vec3Packedf p0, const Vec3Packedf p1, const Vec3Packedf p2, const Vec3Packedf p3, float x) {
static Vec3Packedf Bernstein3D(const Vec3Packedf p0, const Vec3Packedf p1, const Vec3Packedf p2, const Vec3Packedf p3, float x) {
if (x == 0) return p0;
else if (x == 1) return p3;
return p0 * bern0(x) + p1 * bern1(x) + p2 * bern2(x) + p3 * bern3(x);
}
Vec3Packedf Bernstein3DDerivative(const Vec3Packedf p0, const Vec3Packedf p1, const Vec3Packedf p2, const Vec3Packedf p3, float x) {
static Vec3Packedf Bernstein3DDerivative(const Vec3Packedf p0, const Vec3Packedf p1, const Vec3Packedf p2, const Vec3Packedf p3, float x) {
return p0 * bern0deriv(x) + p1 * bern1deriv(x) + p2 * bern2deriv(x) + p3 * bern3deriv(x);
}
void spline_n_4(int i, float t, float *knot, float *splineVal) {
static void spline_n_4(int i, float t, float *knot, float *splineVal) {
knot += i + 1;
float t0 = (t - knot[0]);
float t1 = (t - knot[1]);
float t2 = (t - knot[2]);
// TODO: All our knots are integers so we should be able to get rid of these divisions.
float f30 = t0/(knot[3]-knot[0]);
float f41 = t1/(knot[4]-knot[1]);
float f52 = t2/(knot[5]-knot[2]);
@@ -114,7 +115,7 @@ void spline_n_4(int i, float t, float *knot, float *splineVal) {
}
// knot should be an array sized n + 5 (n + 1 + 1 + degree (cubic))
void spline_knot(int n, int type, float *knot) {
static void spline_knot(int n, int type, float *knot) {
memset(knot, 0, sizeof(float) * (n + 5));
for (int i = 0; i < n - 1; ++i)
knot[i + 3] = (float)i;
@@ -135,7 +136,7 @@ void spline_knot(int n, int type, float *knot) {
}
}
void _SplinePatchLowQuality(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType) {
static void _SplinePatchLowQuality(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType) {
// Fast and easy way - just draw the control points, generate some very basic normal vector substitutes.
// Very inaccurate but okay for Loco Roco. Maybe should keep it as an option because it's fast.
@@ -203,7 +204,7 @@ void _SplinePatchLowQuality(u8 *&dest, u16 *indices, int &count, const SplinePat
}
void _SplinePatchFullQuality(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType, int quality) {
static void _SplinePatchFullQuality(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType, int quality, int maxVertices) {
// Full (mostly) correct tessellation of spline patches.
// Not very fast.
@@ -211,21 +212,30 @@ void _SplinePatchFullQuality(u8 *&dest, u16 *indices, int &count, const SplineP
int n = spatch.count_u - 1;
int m = spatch.count_v - 1;
float *knot_u = new float[n + 5];
float *knot_v = new float[m + 5];
spline_knot(n, spatch.type_u, knot_u);
spline_knot(m, spatch.type_v, knot_v);
// Increase tesselation based on the size. Should be approximately right?
// JPCSP is wrong at least because their method results in square loco roco.
int patch_div_s = (spatch.count_u - 3) * gstate.getPatchDivisionU();
int patch_div_t = (spatch.count_v - 3) * gstate.getPatchDivisionV();
patch_div_s /= quality;
patch_div_t /= quality;
if (quality > 1) {
patch_div_s /= quality;
patch_div_t /= quality;
}
if (patch_div_s < 2) patch_div_s = 2;
if (patch_div_t < 2) patch_div_t = 2;
// Downsample until it fits, in case crazy tesselation factors are sent.
while ((patch_div_s + 1) * (patch_div_t + 1) > maxVertices) {
patch_div_s /= 2;
patch_div_t /= 2;
}
// First compute all the vertices and put them in an array
SimpleVertex *&vertices = (SimpleVertex*&)dest;
@@ -351,21 +361,21 @@ void _SplinePatchFullQuality(u8 *&dest, u16 *indices, int &count, const SplineP
}
}
void TesselateSplinePatch(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType) {
void TesselateSplinePatch(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType, int maxVertexCount) {
switch (g_Config.iSplineBezierQuality) {
case LOW_QUALITY:
_SplinePatchLowQuality(dest, indices, count, spatch, origVertType);
break;
case MEDIUM_QUALITY:
_SplinePatchFullQuality(dest, indices, count, spatch, origVertType, 2);
_SplinePatchFullQuality(dest, indices, count, spatch, origVertType, 2, maxVertexCount);
break;
case HIGH_QUALITY:
_SplinePatchFullQuality(dest, indices, count, spatch, origVertType, 1);
_SplinePatchFullQuality(dest, indices, count, spatch, origVertType, 1, maxVertexCount);
break;
}
}
void _BezierPatchLowQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType) {
static void _BezierPatchLowQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType) {
const float third = 1.0f / 3.0f;
// Fast and easy way - just draw the control points, generate some very basic normal vector subsitutes.
// Very inaccurate though but okay for Loco Roco. Maybe should keep it as an option.
@@ -426,11 +436,17 @@ void _BezierPatchLowQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, in
}
}
void _BezierPatchHighQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType) {
static void _BezierPatchHighQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType, int maxVertices) {
const float third = 1.0f / 3.0f;
// Full correct tesselation of bezier patches.
// Note: Does not handle splines correctly.
// Downsample until it fits, in case crazy tesselation factors are sent.
while ((tess_u + 1) * (tess_v + 1) > maxVertices) {
tess_u /= 2;
tess_v /= 2;
}
// First compute all the vertices and put them in an array
SimpleVertex *&vertices = (SimpleVertex*&)dest;
@@ -518,7 +534,7 @@ void _BezierPatchHighQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, i
delete[] horiz;
GEPatchPrimType prim_type = gstate.getPatchPrimitiveType();
// Tesselate.
// Combine the vertices into triangles.
for (int tile_v = 0; tile_v < tess_v; ++tile_v) {
for (int tile_u = 0; tile_u < tess_u; ++tile_u) {
int total = patch.index * (tess_u + 1) * (tess_v + 1);
@@ -534,16 +550,16 @@ void _BezierPatchHighQuality(u8 *&dest, u16 *&indices, int &count, int tess_u, i
dest += (tess_u + 1) * (tess_v + 1) * sizeof(SimpleVertex);
}
void TesselateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType) {
void TesselateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType, int maxVertices) {
switch (g_Config.iSplineBezierQuality) {
case LOW_QUALITY:
_BezierPatchLowQuality(dest, indices, count, tess_u, tess_v, patch, origVertType);
break;
case MEDIUM_QUALITY:
_BezierPatchHighQuality(dest, indices, count, tess_u / 2, tess_v / 2, patch, origVertType);
_BezierPatchHighQuality(dest, indices, count, tess_u / 2, tess_v / 2, patch, origVertType, maxVertices);
break;
case HIGH_QUALITY:
_BezierPatchHighQuality(dest, indices, count, tess_u, tess_v, patch, origVertType);
_BezierPatchHighQuality(dest, indices, count, tess_u, tess_v, patch, origVertType, maxVertices);
break;
}
}
+2 -2
View File
@@ -166,5 +166,5 @@ enum quality {
HIGH_QUALITY = 2,
};
void TesselateSplinePatch(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType);
void TesselateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType);
void TesselateSplinePatch(u8 *&dest, u16 *indices, int &count, const SplinePatchLocal &spatch, u32 origVertType, int maxVertices);
void TesselateBezierPatch(u8 *&dest, u16 *&indices, int &count, int tess_u, int tess_v, const BezierPatch &patch, u32 origVertType, int maxVertices);
+1 -1
View File
@@ -134,7 +134,7 @@ void PrintDecodedVertex(VertexReader &vtx) {
printf("P: %f %f %f\n", pos[0], pos[1], pos[2]);
}
VertexDecoder::VertexDecoder() : jitted_(0) {
VertexDecoder::VertexDecoder() : jitted_(0), decoded_(nullptr), ptr_(nullptr) {
}
void VertexDecoder::Step_WeightsU8() const
+8 -7
View File
@@ -79,7 +79,8 @@ void TransformDrawEngineDX9::SubmitSpline(const void *control_points, const void
patch.count_v = count_v;
patch.points = points;
TesselateSplinePatch(dest, quadIndices_, count, patch, origVertType);
int maxVertices = SPLINE_BUFFER_SIZE / vertexSize;
TesselateSplinePatch(dest, quadIndices_, count, patch, origVertType, maxVertices);
delete[] points;
@@ -95,7 +96,7 @@ void TransformDrawEngineDX9::SubmitSpline(const void *control_points, const void
gstate_c.uv.vOff = 0;
}
int bytesRead;
int bytesRead = 0;
SubmitPrim(decoded2, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
Flush();
@@ -154,10 +155,9 @@ void TransformDrawEngineDX9::SubmitBezier(const void *control_points, const void
}
}
u8 *decoded2 = decoded + 65536 * 18;
u8 *dest = splineBuffer;
int count = 0;
u8 *dest = decoded2;
// Simple approximation of the real tesselation factor.
// We shouldn't really split up into separate 4x4 patches, instead we should do something that works
@@ -169,10 +169,11 @@ void TransformDrawEngineDX9::SubmitBezier(const void *control_points, const void
if (tess_u < 4) tess_u = 4;
if (tess_v < 4) tess_v = 4;
int maxVertices = SPLINE_BUFFER_SIZE / vertexSize;
u16 *inds = quadIndices_;
for (int patch_idx = 0; patch_idx < num_patches_u*num_patches_v; ++patch_idx) {
BezierPatch& patch = patches[patch_idx];
TesselateBezierPatch(dest, inds, count, tess_u, tess_v, patch, origVertType);
TesselateBezierPatch(dest, inds, count, tess_u, tess_v, patch, origVertType, maxVertices);
}
delete[] patches;
@@ -188,8 +189,8 @@ void TransformDrawEngineDX9::SubmitBezier(const void *control_points, const void
gstate_c.uv.vOff = 0;
}
int bytesRead;
SubmitPrim(decoded2, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
int bytesRead = 0;
SubmitPrim(dest, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
Flush();
+2 -3
View File
@@ -71,9 +71,6 @@ int D3DPrimCount(D3DPRIMITIVETYPE prim, int size) {
}
enum {
VERTEX_BUFFER_MAX = 65536,
DECODED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 48,
DECODED_INDEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 20,
TRANSFORMED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * sizeof(TransformedVertex)
};
@@ -109,6 +106,7 @@ TransformDrawEngineDX9::TransformDrawEngineDX9()
// All this is a LOT of memory, need to see if we can cut down somehow.
decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE);
decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE);
splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE);
transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE);
transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE);
@@ -128,6 +126,7 @@ TransformDrawEngineDX9::~TransformDrawEngineDX9() {
DestroyDeviceObjects();
FreeMemoryPages(decoded, DECODED_VERTEX_BUFFER_SIZE);
FreeMemoryPages(decIndex, DECODED_INDEX_BUFFER_SIZE);
FreeMemoryPages(splineBuffer, SPLINE_BUFFER_SIZE);
FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE);
FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE);
delete[] quadIndices_;
+1
View File
@@ -458,6 +458,7 @@ GLES_GPU::~GLES_GPU() {
depalShaderCache_.Clear();
fragmentTestCache_.Clear();
delete shaderManager_;
shaderManager_ = nullptr;
glstate.SetVSyncInterval(0);
}
+10 -10
View File
@@ -20,6 +20,7 @@
#include "Core/MemMap.h"
#include "GPU/Math3D.h"
#include "GPU/Common/SplineCommon.h"
#include "GPU/Common/DrawEngineCommon.h"
#include "GPU/Common/VertexDecoderCommon.h"
// Here's how to evaluate them fast:
@@ -69,10 +70,8 @@ void TransformDrawEngine::SubmitSpline(const void *control_points, const void *i
points[idx] = simplified_control_points + idx;
}
u8 *decoded2 = decoded + 65536 * 18;
int count = 0;
u8 *dest = decoded2;
u8 *dest = splineBuffer;
SplinePatchLocal patch;
patch.type_u = type_u;
@@ -81,7 +80,8 @@ void TransformDrawEngine::SubmitSpline(const void *control_points, const void *i
patch.count_v = count_v;
patch.points = points;
TesselateSplinePatch(dest, quadIndices_, count, patch, origVertType);
int maxVertexCount = SPLINE_BUFFER_SIZE / vertexSize;
TesselateSplinePatch(splineBuffer, quadIndices_, count, patch, origVertType, maxVertexCount);
delete[] points;
@@ -98,7 +98,7 @@ void TransformDrawEngine::SubmitSpline(const void *control_points, const void *i
}
int bytesRead;
SubmitPrim(decoded2, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
SubmitPrim(splineBuffer, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
Flush();
@@ -123,6 +123,7 @@ void TransformDrawEngine::SubmitBezier(const void *control_points, const void *i
GetIndexBounds(indices, count_u*count_v, vertType, &index_lower_bound, &index_upper_bound);
// Simplify away bones and morph before proceeding
// There are normally not a lot of control points so just splitting decoded should be reasonably safe, although not great.
SimpleVertex *simplified_control_points = (SimpleVertex *)(decoded + 65536 * 12);
u8 *temp_buffer = decoded + 65536 * 18;
@@ -156,10 +157,8 @@ void TransformDrawEngine::SubmitBezier(const void *control_points, const void *i
}
}
u8 *decoded2 = decoded + 65536 * 18;
int count = 0;
u8 *dest = decoded2;
u8 *dest = splineBuffer;
// Simple approximation of the real tesselation factor.
// We shouldn't really split up into separate 4x4 patches, instead we should do something that works
@@ -172,9 +171,10 @@ void TransformDrawEngine::SubmitBezier(const void *control_points, const void *i
if (tess_v < 4) tess_v = 4;
u16 *inds = quadIndices_;
int maxVertices = SPLINE_BUFFER_SIZE / vertexSize;
for (int patch_idx = 0; patch_idx < num_patches_u*num_patches_v; ++patch_idx) {
BezierPatch& patch = patches[patch_idx];
TesselateBezierPatch(dest, inds, count, tess_u, tess_v, patch, origVertType);
TesselateBezierPatch(dest, inds, count, tess_u, tess_v, patch, origVertType, maxVertices);
}
delete[] patches;
@@ -191,7 +191,7 @@ void TransformDrawEngine::SubmitBezier(const void *control_points, const void *i
}
int bytesRead;
SubmitPrim(decoded2, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
SubmitPrim(dest, quadIndices_, primType[prim_type], count, vertTypeWithIndex16, &bytesRead);
Flush();
+2 -3
View File
@@ -103,9 +103,6 @@ extern const GLuint glprim[8] = {
};
enum {
VERTEX_BUFFER_MAX = 65536,
DECODED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 48,
DECODED_INDEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 20,
TRANSFORMED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * sizeof(TransformedVertex)
};
@@ -140,6 +137,7 @@ TransformDrawEngine::TransformDrawEngine()
// All this is a LOT of memory, need to see if we can cut down somehow.
decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE);
decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE);
splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE);
transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE);
transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE);
@@ -159,6 +157,7 @@ TransformDrawEngine::~TransformDrawEngine() {
DestroyDeviceObjects();
FreeMemoryPages(decoded, DECODED_VERTEX_BUFFER_SIZE);
FreeMemoryPages(decIndex, DECODED_INDEX_BUFFER_SIZE);
FreeMemoryPages(splineBuffer, SPLINE_BUFFER_SIZE);
FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE);
FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE);
delete [] quadIndices_;