Ver código fonte

fix problem with locks

Kolja Strohm 1 mês atrás
pai
commit
86b462df4b
2 arquivos alterados com 29 adições e 17 exclusões
  1. 21 15
      FactoryCraft/Chunk.cpp
  2. 8 2
      FactoryCraft/CustomDX11API.cpp

+ 21 - 15
FactoryCraft/Chunk.cpp

@@ -15,7 +15,10 @@ Chunk::Chunk(Framework::Point location)
       location(location),
       location(location),
       isLoading(0),
       isLoading(0),
       lightChanged(0),
       lightChanged(0),
-      modelChanged(0)
+      modelChanged(0),
+      bLock(),
+      vLock(),
+      aLock()
 {
 {
     blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
     blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
     memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
     memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
@@ -316,24 +319,27 @@ void Chunk::load(Framework::StreamReader* zReader)
                         World::INSTANCE->getChunkReadLock().lock();
                         World::INSTANCE->getChunkReadLock().lock();
                         Chunk* c = World::INSTANCE->zChunk(
                         Chunk* c = World::INSTANCE->zChunk(
                             World::INSTANCE->getChunkCenter(pos.x, pos.y));
                             World::INSTANCE->getChunkCenter(pos.x, pos.y));
-                        c->getBlockReadLock().lock();
-                        Block* zB = c ? c->zBlockAt(pos) : 0;
-                        if (zB)
+                        if (c)
                         {
                         {
-                            bool visible = zB->isVisible();
-                            zB->setLightData(
-                                getOppositeDirection(getDirectionFromIndex(i)),
-                                (unsigned char*)lightData,
-                                c);
-                            if (zB->isVisible() && !visible)
+                            c->getBlockReadLock().lock();
+                            Block* zB = c->zBlockAt(pos);
+                            if (zB)
                             {
                             {
-                                zB->tick(0);
-                                vLock.lockWrite();
-                                c->visibleBlocks.add(zB);
-                                vLock.unlockWrite();
+                                bool visible = zB->isVisible();
+                                zB->setLightData(getOppositeDirection(
+                                                     getDirectionFromIndex(i)),
+                                    (unsigned char*)lightData,
+                                    c);
+                                if (zB->isVisible() && !visible)
+                                {
+                                    zB->tick(0);
+                                    vLock.lockWrite();
+                                    c->visibleBlocks.add(zB);
+                                    vLock.unlockWrite();
+                                }
                             }
                             }
+                            c->getBlockReadLock().unlock();
                         }
                         }
-                        c->getBlockReadLock().unlock();
                         World::INSTANCE->getChunkReadLock().unlock();
                         World::INSTANCE->getChunkReadLock().unlock();
                     }
                     }
                 }
                 }

+ 8 - 2
FactoryCraft/CustomDX11API.cpp

@@ -1,8 +1,8 @@
 #include "CustomDX11API.h"
 #include "CustomDX11API.h"
 
 
-#include <Shader.h>
-#include <DXBuffer.h>
 #include <d3d11.h>
 #include <d3d11.h>
+#include <DXBuffer.h>
+#include <Shader.h>
 
 
 #include "CustomUIDX11PixelShader.h"
 #include "CustomUIDX11PixelShader.h"
 #include "CustomUIDX11VertexShader.h"
 #include "CustomUIDX11VertexShader.h"
@@ -29,8 +29,10 @@ Framework::DX11VertexShader* CustomDX11API::initializeVertexShader(
     rasterDesc.ScissorEnable = false;
     rasterDesc.ScissorEnable = false;
     rasterDesc.SlopeScaledDepthBias = 0.0f;
     rasterDesc.SlopeScaledDepthBias = 0.0f;
     ID3D11Device* device;
     ID3D11Device* device;
+    deviceLock.lock();
     zContext()->GetDevice(&device);
     zContext()->GetDevice(&device);
     device->CreateRasterizerState(&rasterDesc, &noCull);
     device->CreateRasterizerState(&rasterDesc, &noCull);
+    deviceLock.unlock();
 
 
     DX11VertexShader* shader = DirectX11::initializeVertexShader(
     DX11VertexShader* shader = DirectX11::initializeVertexShader(
         (unsigned char*)CustomUIDX11VertexShader,
         (unsigned char*)CustomUIDX11VertexShader,
@@ -60,7 +62,9 @@ void CustomDX11API::setVertexLightBuffer(Framework::DXBuffer* zBuffer)
     {
     {
         ID3D11ShaderResourceView* v[1];
         ID3D11ShaderResourceView* v[1];
         v[0] = *buffer;
         v[0] = *buffer;
+        deviceLock.lock();
         zContext()->VSSetShaderResources(0, 1, v);
         zContext()->VSSetShaderResources(0, 1, v);
+        deviceLock.unlock();
     }
     }
 }
 }
 
 
@@ -71,5 +75,7 @@ int CustomDX11API::getVertexShaderLightBufferIndex() const
 
 
 void CustomDX11API::setCullBack(bool cull)
 void CustomDX11API::setCullBack(bool cull)
 {
 {
+    deviceLock.lock();
     zContext()->RSSetState(cull ? texturRS : noCull);
     zContext()->RSSetState(cull ? texturRS : noCull);
+    deviceLock.unlock();
 }
 }