From 1f9918a7c8aa847376aee00afad0a6fbaef0da7e Mon Sep 17 00:00:00 2001 From: baldurk Date: Fri, 20 Mar 2015 20:52:31 +0000 Subject: [PATCH] Fix some crashes and bad handling of indices * If buffer wasn't a multiple of 4, a Buffer.BlockCopy call could crash * Unsigned byte indices (for GL) weren't supported * Post-transform index buffer was assumed to just be uints, but in fact it is the same size as the drawcall's existing indices. --- renderdocui/Windows/BufferViewer.cs | 50 ++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/renderdocui/Windows/BufferViewer.cs b/renderdocui/Windows/BufferViewer.cs index 7d9d4b120..6824d1323 100644 --- a/renderdocui/Windows/BufferViewer.cs +++ b/renderdocui/Windows/BufferViewer.cs @@ -923,11 +923,16 @@ namespace renderdocui.Windows { ret.Indices = new uint[rawidxs.Length / input.Drawcall.indexByteWidth]; - if (input.Drawcall.indexByteWidth == 2) + if (input.Drawcall.indexByteWidth == 1) + { + for (int i = 0; i < rawidxs.Length; i++) + ret.Indices[i] = rawidxs[i]; + } + else if (input.Drawcall.indexByteWidth == 2) { ushort[] tmp = new ushort[rawidxs.Length / 2]; - Buffer.BlockCopy(rawidxs, 0, tmp, 0, rawidxs.Length); + Buffer.BlockCopy(rawidxs, 0, tmp, 0, tmp.Length * sizeof(ushort)); for (int i = 0; i < tmp.Length; i++) { @@ -936,7 +941,7 @@ namespace renderdocui.Windows } else if (input.Drawcall.indexByteWidth == 4) { - Buffer.BlockCopy(rawidxs, 0, ret.Indices, 0, rawidxs.Length); + Buffer.BlockCopy(rawidxs, 0, ret.Indices, 0, ret.Indices.Length * sizeof(uint)); } } @@ -948,8 +953,28 @@ namespace renderdocui.Windows } else { - ret.DataIndices = new uint[rawidxs.Length / sizeof(uint)]; - Buffer.BlockCopy(rawidxs, 0, ret.DataIndices, 0, rawidxs.Length); + ret.DataIndices = new uint[rawidxs.Length / input.Drawcall.indexByteWidth]; + + if (input.Drawcall.indexByteWidth == 1) + { + for (int i = 0; i < rawidxs.Length; i++) + ret.DataIndices[i] = rawidxs[i]; + } + else if (input.Drawcall.indexByteWidth == 2) + { + ushort[] tmp = new ushort[rawidxs.Length / 2]; + + Buffer.BlockCopy(rawidxs, 0, tmp, 0, tmp.Length * sizeof(ushort)); + + for (int i = 0; i < tmp.Length; i++) + { + ret.DataIndices[i] = tmp[i]; + } + } + else if (input.Drawcall.indexByteWidth == 4) + { + Buffer.BlockCopy(rawidxs, 0, ret.DataIndices, 0, ret.DataIndices.Length * sizeof(uint)); + } } } @@ -975,11 +1000,16 @@ namespace renderdocui.Windows { ret.Indices = new uint[rawidxs.Length / input.Drawcall.indexByteWidth]; - if (input.Drawcall.indexByteWidth == 2) + if (input.Drawcall.indexByteWidth == 1) + { + for (int i = 0; i < rawidxs.Length; i++) + ret.Indices[i] = rawidxs[i]; + } + else if (input.Drawcall.indexByteWidth == 2) { ushort[] tmp = new ushort[rawidxs.Length / 2]; - Buffer.BlockCopy(rawidxs, 0, tmp, 0, rawidxs.Length); + Buffer.BlockCopy(rawidxs, 0, tmp, 0, tmp.Length * sizeof(ushort)); for (int i = 0; i < tmp.Length; i++) { @@ -988,16 +1018,14 @@ namespace renderdocui.Windows } else if (input.Drawcall.indexByteWidth == 4) { - Buffer.BlockCopy(rawidxs, 0, ret.Indices, 0, rawidxs.Length); + Buffer.BlockCopy(rawidxs, 0, ret.Indices, 0, ret.Indices.Length * sizeof(uint)); } } maxIndex = 0; foreach (var i in ret.Indices) { - if (input.Drawcall.indexByteWidth == 2 && i == input.IndexRestartValue && input.IndexRestart) - continue; - if (input.Drawcall.indexByteWidth == 4 && i == input.IndexRestartValue && input.IndexRestart) + if (i == input.IndexRestartValue && input.IndexRestart) continue; maxIndex = Math.Max(maxIndex, i);