Fixed a crash when minimizing during resources loading

This commit is contained in:
codeimp 2009-06-16 17:29:00 +00:00
parent 656edc7f13
commit 0a10e5bfef
2 changed files with 104 additions and 54 deletions

View file

@ -342,6 +342,8 @@ namespace CodeImp.DoomBuilder.Map
General.Plugins.OnSectorFloorSurfaceUpdate(this, ref floorvertices); General.Plugins.OnSectorFloorSurfaceUpdate(this, ref floorvertices);
surfaceentry.floorvertices = floorvertices; surfaceentry.floorvertices = floorvertices;
surfaceentry.floortexture = longfloortexname; surfaceentry.floortexture = longfloortexname;
if(surfaceentry.ceilvertices == null)
surfaceentry.ceilvertices = floorvertices;
// Update entry // Update entry
surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry); surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry);
@ -359,7 +361,9 @@ namespace CodeImp.DoomBuilder.Map
General.Plugins.OnSectorCeilingSurfaceUpdate(this, ref ceilvertices); General.Plugins.OnSectorCeilingSurfaceUpdate(this, ref ceilvertices);
surfaceentry.ceilvertices = ceilvertices; surfaceentry.ceilvertices = ceilvertices;
surfaceentry.ceiltexture = longceiltexname; surfaceentry.ceiltexture = longceiltexname;
if(surfaceentry.floorvertices == null)
surfaceentry.floorvertices = ceilvertices;
// Update entry // Update entry
surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry); surfaceentry = General.Map.CRenderer2D.Surfaces.UpdateSurfaces(surfaceentry);
General.Map.CRenderer2D.Surfaces.UnlockBuffers(); General.Map.CRenderer2D.Surfaces.UnlockBuffers();

View file

@ -101,8 +101,14 @@ namespace CodeImp.DoomBuilder.Rendering
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets) foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
{ {
// Dispose vertex buffers // Dispose vertex buffers
foreach(VertexBuffer vb in set.Value.buffers) for(int i = 0; i < set.Value.buffers.Count; i++)
vb.Dispose(); {
if(set.Value.buffers[i] != null)
{
set.Value.buffers[i].Dispose();
set.Value.buffers[i] = null;
}
}
} }
sets = null; sets = null;
@ -119,11 +125,18 @@ namespace CodeImp.DoomBuilder.Rendering
resourcesunloaded = true; resourcesunloaded = true;
foreach(KeyValuePair<int, SurfaceBufferSet> set in sets) foreach(KeyValuePair<int, SurfaceBufferSet> set in sets)
{ {
foreach(VertexBuffer vb in set.Value.buffers) // Dispose vertex buffers
vb.Dispose(); for(int i = 0; i < set.Value.buffers.Count; i++)
{
set.Value.buffers.Clear(); if(set.Value.buffers[i] != null)
{
set.Value.buffers[i].Dispose();
set.Value.buffers[i] = null;
}
}
} }
lockedbuffers.Clear();
} }
// Called when all resource must be reloaded // Called when all resource must be reloaded
@ -157,7 +170,7 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.Dispose(); bstream.Dispose();
// Add to list // Add to list
set.Value.buffers.Add(b); set.Value.buffers[i] = b;
} }
} }
@ -235,7 +248,8 @@ namespace CodeImp.DoomBuilder.Rendering
// This ensures there is enough space for a given number of free entries (also adds new bufers if needed) // This ensures there is enough space for a given number of free entries (also adds new bufers if needed)
private void EnsureFreeBufferSpace(SurfaceBufferSet set, int freeentries) private void EnsureFreeBufferSpace(SurfaceBufferSet set, int freeentries)
{ {
DataStream bstream; DataStream bstream = null;
VertexBuffer vb = null;
// Check if we have to add entries // Check if we have to add entries
int addentries = freeentries - set.holes.Count; int addentries = freeentries - set.holes.Count;
@ -260,12 +274,22 @@ namespace CodeImp.DoomBuilder.Rendering
// Calculate the number of vertices that will be // Calculate the number of vertices that will be
int buffernumvertices = bufferentries * verticesperentry; int buffernumvertices = bufferentries * verticesperentry;
// Make the new buffer! if(!resourcesunloaded)
VertexBuffer b = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices, {
Usage.None, VertexFormat.None, Pool.Default); // Make the new buffer!
vb = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices,
Usage.None, VertexFormat.None, Pool.Default);
// Add it. Also add available entries as holes, because they are not used yet. // Add it.
set.buffers.Add(b); set.buffers.Add(vb);
}
else
{
// We can't make a vertexbuffer right now
set.buffers.Add(null);
}
// Also add available entries as holes, because they are not used yet.
set.buffersizes.Add(buffernumvertices); set.buffersizes.Add(buffernumvertices);
for(int i = 0; i < bufferentries; i++) for(int i = 0; i < bufferentries; i++)
set.holes.Add(new SurfaceEntry(set.numvertices, set.buffers.Count - 1, i * verticesperentry)); set.holes.Add(new SurfaceEntry(set.numvertices, set.buffers.Count - 1, i * verticesperentry));
@ -284,7 +308,9 @@ namespace CodeImp.DoomBuilder.Rendering
bstream.Dispose(); bstream.Dispose();
set.buffers[bufferindex].Tag = null; set.buffers[bufferindex].Tag = null;
} }
set.buffers[bufferindex].Dispose();
if((set.buffers[bufferindex] != null) && !resourcesunloaded)
set.buffers[bufferindex].Dispose();
// Get the entries that are in this buffer only // Get the entries that are in this buffer only
List<SurfaceEntry> theseentries = new List<SurfaceEntry>(); List<SurfaceEntry> theseentries = new List<SurfaceEntry>();
@ -300,18 +326,24 @@ namespace CodeImp.DoomBuilder.Rendering
// Calculate the number of vertices that will be // Calculate the number of vertices that will be
int buffernumvertices = bufferentries * verticesperentry; int buffernumvertices = bufferentries * verticesperentry;
// Make the new buffer! if(!resourcesunloaded)
VertexBuffer b = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices, {
Usage.None, VertexFormat.None, Pool.Default); // Make the new buffer and lock it
vb = new VertexBuffer(General.Map.Graphics.Device, FlatVertex.Stride * buffernumvertices,
Usage.None, VertexFormat.None, Pool.Default);
bstream = vb.Lock(0, FlatVertex.Stride * theseentries.Count * verticesperentry, LockFlags.Discard);
}
// Start refilling the buffer with sector geometry // Start refilling the buffer with sector geometry
int vertexoffset = 0; int vertexoffset = 0;
bstream = b.Lock(0, FlatVertex.Stride * theseentries.Count * verticesperentry, LockFlags.Discard);
foreach(SurfaceEntry e in theseentries) foreach(SurfaceEntry e in theseentries)
{ {
// Fill buffer if(!resourcesunloaded)
bstream.WriteRange(e.floorvertices); {
bstream.WriteRange(e.ceilvertices); // Fill buffer
bstream.WriteRange(e.floorvertices);
bstream.WriteRange(e.ceilvertices);
}
// Set the new location in the buffer // Set the new location in the buffer
e.vertexoffset = vertexoffset; e.vertexoffset = vertexoffset;
@ -320,12 +352,20 @@ namespace CodeImp.DoomBuilder.Rendering
vertexoffset += verticesperentry; vertexoffset += verticesperentry;
} }
// Unlock buffer if(!resourcesunloaded)
b.Unlock(); {
bstream.Dispose(); // Unlock buffer
vb.Unlock();
bstream.Dispose();
set.buffers[bufferindex] = vb;
}
else
{
// No vertex buffer at this time, sorry
set.buffers[bufferindex] = null;
}
// Set the new buffer and add available entries as holes, because they are not used yet. // Set the new buffer and add available entries as holes, because they are not used yet.
set.buffers[bufferindex] = b;
set.buffersizes[bufferindex] = buffernumvertices; set.buffersizes[bufferindex] = buffernumvertices;
set.holes.Clear(); set.holes.Clear();
for(int i = 0; i < bufferentries - theseentries.Count; i++) for(int i = 0; i < bufferentries - theseentries.Count; i++)
@ -348,7 +388,7 @@ namespace CodeImp.DoomBuilder.Rendering
{ {
if(entry.floorvertices.Length != entry.ceilvertices.Length) if(entry.floorvertices.Length != entry.ceilvertices.Length)
General.Fail("Floor vertices has different length from ceiling vertices!"); General.Fail("Floor vertices has different length from ceiling vertices!");
int numvertices = entry.floorvertices.Length; int numvertices = entry.floorvertices.Length;
// Free entry when number of vertices have changed // Free entry when number of vertices have changed
@ -373,25 +413,28 @@ namespace CodeImp.DoomBuilder.Rendering
set.entries.Add(nentry); set.entries.Add(nentry);
entry = nentry; entry = nentry;
} }
// Lock the buffer if(!resourcesunloaded)
DataStream bstream;
VertexBuffer vb = set.buffers[entry.bufferindex];
if(vb.Tag == null)
{ {
bstream = vb.Lock(0, set.buffersizes[entry.bufferindex] * FlatVertex.Stride, LockFlags.None); // Lock the buffer
vb.Tag = bstream; DataStream bstream;
lockedbuffers.Add(vb); VertexBuffer vb = set.buffers[entry.bufferindex];
if(vb.Tag == null)
{
bstream = vb.Lock(0, set.buffersizes[entry.bufferindex] * FlatVertex.Stride, LockFlags.None);
vb.Tag = bstream;
lockedbuffers.Add(vb);
}
else
{
bstream = (DataStream)vb.Tag;
}
// Write the vertices to buffer
bstream.Seek(entry.vertexoffset * FlatVertex.Stride, SeekOrigin.Begin);
bstream.WriteRange(entry.floorvertices);
bstream.WriteRange(entry.ceilvertices);
} }
else
{
bstream = (DataStream)vb.Tag;
}
// Write the vertices to buffer
bstream.Seek(entry.vertexoffset * FlatVertex.Stride, SeekOrigin.Begin);
bstream.WriteRange(entry.floorvertices);
bstream.WriteRange(entry.ceilvertices);
} }
return entry; return entry;
@ -414,19 +457,22 @@ namespace CodeImp.DoomBuilder.Rendering
// This unlocks the locked buffers // This unlocks the locked buffers
public void UnlockBuffers() public void UnlockBuffers()
{ {
foreach(VertexBuffer vb in lockedbuffers) if(!resourcesunloaded)
{ {
if(vb.Tag != null) foreach(VertexBuffer vb in lockedbuffers)
{ {
DataStream bstream = (DataStream)vb.Tag; if(vb.Tag != null)
vb.Unlock(); {
bstream.Dispose(); DataStream bstream = (DataStream)vb.Tag;
vb.Tag = null; vb.Unlock();
bstream.Dispose();
vb.Tag = null;
}
} }
// Clear list
lockedbuffers = new List<VertexBuffer>();
} }
// Clear list
lockedbuffers = new List<VertexBuffer>();
} }
// This gets or creates a set for a specific number of vertices // This gets or creates a set for a specific number of vertices