Remove generic CreateVb<T>, force everyone to use raw pointers

This commit is contained in:
UnknownShadow200 2017-06-11 22:45:41 +10:00
parent 3710558b92
commit ac276d3611
8 changed files with 46 additions and 62 deletions

View File

@ -247,12 +247,6 @@ namespace ClassicalSharp.GraphicsAPI {
}
D3D.VertexFormat[] formatMapping;
public override int CreateVb<T>(T[] vertices, VertexFormat format, int count) {
int size = count * strideSizes[(int)format];
DataBuffer buffer = device.CreateVertexBuffer(size, Usage.None, formatMapping[(int)format], Pool.Default);
buffer.SetData(vertices, size, LockFlags.None);
return GetOrExpand(ref vBuffers, buffer, vBufferSize);
}
public override int CreateVb(IntPtr vertices, VertexFormat format, int count) {
int size = count * strideSizes[(int)format];

View File

@ -142,10 +142,6 @@ namespace ClassicalSharp.GraphicsAPI {
/// <summary> Creates a vertex buffer that can have its data dynamically updated. </summary>
public abstract int CreateDynamicVb(VertexFormat format, int maxVertices);
/// <summary> Creates a static vertex buffer that has its data set at creation,
/// but the vertex buffer's data cannot be updated after creation. </summary>
public abstract int CreateVb<T>(T[] vertices, VertexFormat format, int count) where T : struct;
/// <summary> Creates a static vertex buffer that has its data set at creation,
/// but the vertex buffer's data cannot be updated after creation. </summary>
public abstract int CreateVb(IntPtr vertices, VertexFormat format, int count);

View File

@ -189,13 +189,6 @@ namespace ClassicalSharp.GraphicsAPI {
return id;
}
public override int CreateVb<T>(T[] vertices, VertexFormat format, int count) {
int id = GenAndBind(BufferTarget.ArrayBuffer);
int sizeInBytes = count * strideSizes[(int)format];
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeInBytes), vertices, BufferUsage.StaticDraw);
return id;
}
public override int CreateVb(IntPtr vertices, VertexFormat format, int count) {
int id = GenAndBind(BufferTarget.ArrayBuffer);
int sizeInBytes = count * strideSizes[(int)format];

View File

@ -158,13 +158,6 @@ namespace ClassicalSharp.GraphicsAPI {
return id;
}
public override int CreateVb<T>(T[] vertices, VertexFormat format, int count) {
int id = GenAndBind(All.ArrayBuffer);
int sizeInBytes = count * strideSizes[(int)format];
GL.BufferData(All.ArrayBuffer, new IntPtr(sizeInBytes), vertices, All.StaticDraw);
return id;
}
public override int CreateVb(IntPtr vertices, VertexFormat format, int count) {
int id = GenAndBind(All.ArrayBuffer);
int sizeInBytes = count * strideSizes[(int)format];

View File

@ -62,7 +62,7 @@ namespace ClassicalSharp {
chunkEndX = xMax; chunkEndZ = zMax;
Stretch(x1, y1, z1);
PostStretchTiles(x1, y1, z1);
PostStretchTiles(x1, y1, z1);
for (int y = y1, yy = 0; y < yMax; y++, yy++) {
for (int z = z1, zz = 0; z < zMax; z++, zz++) {
@ -104,7 +104,7 @@ namespace ClassicalSharp {
index++;
chunkIndex++;
if (x < 0) continue;
if (x >= width) break;
if (x >= width) break;
BlockID rawBlock = mapPtr[index];
allAir = allAir && info.Draw[rawBlock] == DrawType.Gas;
@ -116,9 +116,9 @@ namespace ClassicalSharp {
outAllAir = allAir;
if (x1 == 0 || y1 == 0 || z1 == 0 || x1 + chunkSize >= width ||
y1 + chunkSize >= height || z1 + chunkSize >= length) allSolid = false;
y1 + chunkSize >= height || z1 + chunkSize >= length) allSolid = false;
if (allAir || allSolid) return true;
if (allAir || allSolid) return true;
light.LightHint(x1 - 1, z1 - 1, mapPtr);
return false;
}
@ -144,10 +144,13 @@ namespace ClassicalSharp {
ChunkPartInfo info;
int vertCount = (part.iCount / 6 * 4) + 2;
info.VbId = gfx.CreateVb(part.vertices, VertexFormat.P3fT2fC4b, vertCount);
fixed (VertexP3fT2fC4b* ptr = part.vertices) {
info.VbId = gfx.CreateVb((IntPtr)ptr, VertexFormat.P3fT2fC4b, vertCount);
}
info.IndicesCount = part.iCount;
info.LeftCount = (ushort)part.vCount[Side.Left];
info.LeftCount = (ushort)part.vCount[Side.Left];
info.RightCount = (ushort)part.vCount[Side.Right];
info.FrontCount = (ushort)part.vCount[Side.Front];
info.BackCount = (ushort)part.vCount[Side.Back];
@ -217,9 +220,9 @@ namespace ClassicalSharp {
#endif
// All of these function calls are inlined as they can be called tens of millions to hundreds of millions of times.
if (counts[index] == 0 ||
(x == 0 && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(x != 0 && (hidden[tileIdx + chunk[cIndex - 1]] & (1 << Side.Left)) != 0)) {
if (counts[index] == 0 ||
(x == 0 && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(x != 0 && (hidden[tileIdx + chunk[cIndex - 1]] & (1 << Side.Left)) != 0)) {
counts[index] = 0;
} else {
int count = StretchZ(index, x, y, z, cIndex, b, Side.Left);
@ -227,9 +230,9 @@ namespace ClassicalSharp {
}
index++;
if (counts[index] == 0 ||
(x == maxX && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(x != maxX && (hidden[tileIdx + chunk[cIndex + 1]] & (1 << Side.Right)) != 0)) {
if (counts[index] == 0 ||
(x == maxX && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(x != maxX && (hidden[tileIdx + chunk[cIndex + 1]] & (1 << Side.Right)) != 0)) {
counts[index] = 0;
} else {
int count = StretchZ(index, x, y, z, cIndex, b, Side.Right);
@ -237,9 +240,9 @@ namespace ClassicalSharp {
}
index++;
if (counts[index] == 0 ||
(z == 0 && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(z != 0 && (hidden[tileIdx + chunk[cIndex - 18]] & (1 << Side.Front)) != 0)) {
if (counts[index] == 0 ||
(z == 0 && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(z != 0 && (hidden[tileIdx + chunk[cIndex - 18]] & (1 << Side.Front)) != 0)) {
counts[index] = 0;
} else {
int count = StretchX(index, x, y, z, cIndex, b, Side.Front);
@ -247,9 +250,9 @@ namespace ClassicalSharp {
}
index++;
if (counts[index] == 0 ||
(z == maxZ && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(z != maxZ && (hidden[tileIdx + chunk[cIndex + 18]] & (1 << Side.Back)) != 0)) {
if (counts[index] == 0 ||
(z == maxZ && (y < sidesLevel || (b >= Block.Water && b <= Block.StillLava && y < edgeLevel))) ||
(z != maxZ && (hidden[tileIdx + chunk[cIndex + 18]] & (1 << Side.Back)) != 0)) {
counts[index] = 0;
} else {
int count = StretchX(index, x, y, z, cIndex, b, Side.Back);
@ -258,7 +261,7 @@ namespace ClassicalSharp {
index++;
if (counts[index] == 0 || y == 0 ||
(hidden[tileIdx + chunk[cIndex - 324]] & (1 << Side.Bottom)) != 0) {
(hidden[tileIdx + chunk[cIndex - 324]] & (1 << Side.Bottom)) != 0) {
counts[index] = 0;
} else {
int count = StretchX(index, x, y, z, cIndex, b, Side.Bottom);
@ -267,7 +270,7 @@ namespace ClassicalSharp {
index++;
if (counts[index] == 0 ||
(hidden[tileIdx + chunk[cIndex + 324]] & (1 << Side.Top)) != 0) {
(hidden[tileIdx + chunk[cIndex + 324]] & (1 << Side.Top)) != 0) {
counts[index] = 0;
} else if (b < Block.Water || b > Block.StillLava) {
int count = StretchX(index, x, y, z, cIndex, b, Side.Top);
@ -292,7 +295,7 @@ namespace ClassicalSharp {
protected bool OccludedLiquid(int chunkIndex) {
chunkIndex += 324;
return
return
info.FullOpaque[chunk[chunkIndex]]
&& info.Draw[chunk[chunkIndex - 18]] != DrawType.Gas
&& info.Draw[chunk[chunkIndex - 1]] != DrawType.Gas

View File

@ -193,7 +193,10 @@ namespace ClassicalSharp.Renderers {
DrawZ(map.Length, 0, map.Width, y1, y2, axisSize, col, v, ref index);
DrawX(0, 0, map.Length, y1, y2, axisSize, col, v, ref index);
DrawX(map.Width, 0, map.Length, y1, y2, axisSize, col, v, ref index);
sidesVb = gfx.CreateVb(v, VertexFormat.P3fT2fC4b, sidesVertices);
fixed (VertexP3fT2fC4b* ptr = v) {
sidesVb = gfx.CreateVb((IntPtr)ptr, VertexFormat.P3fT2fC4b, sidesVertices);
}
}
void RebuildEdges(int y, int axisSize) {
@ -217,7 +220,10 @@ namespace ClassicalSharp.Renderers {
DrawY(r.X, r.Y, r.X + r.Width, r.Y + r.Height, y, axisSize, col,
HorOffset(block), YOffset(block), v, ref index);
}
edgesVb = gfx.CreateVb(v, VertexFormat.P3fT2fC4b, edgesVertices);
fixed (VertexP3fT2fC4b* ptr = v) {
edgesVb = gfx.CreateVb((IntPtr)ptr, VertexFormat.P3fT2fC4b, edgesVertices);
}
}
float HorOffset(BlockID block) {

View File

@ -134,14 +134,14 @@ namespace ClassicalSharp.Renderers {
gfx.SetFogDensity(fogDensity);
} else if (game.World.Env.ExpFog) {
gfx.SetFogMode(Fog.Exp);
// f = 1-z/end f = e^(-dz)
// solve for f = 0.01 gives:
// e^(-dz)=0.01 --> -dz=ln(0.01)
// 0.99=z/end --> z=end*0.99
// therefore
// d = -ln(0.01)/(end*0.99)
double density = -Math.Log(0.01) / (game.ViewDistance * 0.99);
gfx.SetFogDensity((float)density);
// f = 1-z/end f = e^(-dz)
// solve for f = 0.01 gives:
// e^(-dz)=0.01 --> -dz=ln(0.01)
// 0.99=z/end --> z=end*0.99
// therefore
// d = -ln(0.01)/(end*0.99)
double density = -Math.Log(0.01) / (game.ViewDistance * 0.99);
gfx.SetFogDensity((float)density);
} else {
gfx.SetFogMode(Fog.Linear);
gfx.SetFogEnd(game.ViewDistance);
@ -181,7 +181,9 @@ namespace ClassicalSharp.Renderers {
VertexP3fT2fC4b[] vertices = new VertexP3fT2fC4b[cloudVertices];
DrawCloudsY(x1, z1, x2, z2, map.Env.CloudHeight, axisSize, map.Env.CloudsCol.Pack(), vertices);
cloudsVb = gfx.CreateVb(vertices, VertexFormat.P3fT2fC4b, cloudVertices);
fixed (VertexP3fT2fC4b* ptr = vertices) {
cloudsVb = gfx.CreateVb((IntPtr)ptr, VertexFormat.P3fT2fC4b, cloudVertices);
}
}
void RebuildSky(int extent, int axisSize) {
@ -194,7 +196,9 @@ namespace ClassicalSharp.Renderers {
int height = Math.Max(map.Height + 2 + 6, map.Env.CloudHeight + 6);
DrawSkyY(x1, z1, x2, z2, height, axisSize, map.Env.SkyCol.Pack(), vertices);
skyVb = gfx.CreateVb(vertices, VertexFormat.P3fC4b, skyVertices);
fixed (VertexP3fC4b* ptr = vertices) {
skyVb = gfx.CreateVb((IntPtr)ptr, VertexFormat.P3fC4b, skyVertices);
}
}
void DrawSkyY(int x1, int z1, int x2, int z2, int y, int axisSize, int col, VertexP3fC4b[] vertices) {

View File

@ -25,11 +25,6 @@ namespace OpenTK.Graphics.OpenGL {
Interop.Calli( (int)target, size, data, (int)usage, BufferDataAddress );
} static IntPtr BufferDataAddress, BufferDataARBAddress;
public static void BufferData<T>( BufferTarget target, IntPtr size, T[] data, BufferUsage usage ) where T : struct {
IntPtr dataPtr = Interop.Fixed( ref data[0] );
Interop.Calli( (int)target, size, dataPtr, (int)usage, BufferDataAddress );
}
public static void BufferSubData( BufferTarget target, IntPtr offset, IntPtr size, IntPtr data ) {
Interop.Calli( (int)target, offset, size, data, BufferSubDataAddress );
} static IntPtr BufferSubDataAddress, BufferSubDataARBAddress;