Browse Source

support mip mapped textures

Kolja Strohm 1 year ago
parent
commit
688f61e340
3 changed files with 45 additions and 15 deletions
  1. 1 0
      DX11GraphicsApi.cpp
  2. 41 15
      Textur.cpp
  3. 3 0
      Textur.h

+ 1 - 0
DX11GraphicsApi.cpp

@@ -423,6 +423,7 @@ void DirectX11::initialize(
     renderB->neuBild(this->backBufferSize.x, this->backBufferSize.y, 0);
 
     uiTextur = createOrGetTextur("_f_Render_Bild", renderB);
+    ((DX11Textur*)uiTextur)->setUseMips(0);
 
     texturModel = new TexturModel(this);
     texturModel->setSize(this->backBufferSize);

+ 41 - 15
Textur.cpp

@@ -90,7 +90,8 @@ DX11Textur::DX11Textur(ID3D11Device* device, ID3D11DeviceContext* context)
       view(0),
       device(device),
       context(context),
-      renderTarget(0)
+      renderTarget(0),
+      useMips(1)
 {}
 
 DX11Textur::~DX11Textur()
@@ -117,27 +118,44 @@ bool DX11Textur::updateTextur()
         bufferDesc.Width = bild->getBreite();
         bufferDesc.Height = bild->getHeight();
         bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
-        bufferDesc.BindFlags = (renderTarget ? D3D11_BIND_RENDER_TARGET : 0)
+        bufferDesc.BindFlags
+            = (renderTarget || useMips ? D3D11_BIND_RENDER_TARGET : 0)
                              | D3D11_BIND_SHADER_RESOURCE;
-        bufferDesc.CPUAccessFlags = renderTarget ? 0 : D3D11_CPU_ACCESS_WRITE;
+        bufferDesc.CPUAccessFlags = renderTarget || useMips ? 0 : D3D11_CPU_ACCESS_WRITE;
         bufferDesc.SampleDesc.Count = 1;
-        bufferDesc.MipLevels = 1;
-        bufferDesc.Usage
-            = renderTarget ? D3D11_USAGE_DEFAULT : D3D11_USAGE_DYNAMIC;
+        bufferDesc.MipLevels = useMips ? 0 : 1;
+        bufferDesc.Usage = renderTarget || useMips ? D3D11_USAGE_DEFAULT
+                                                   : D3D11_USAGE_DYNAMIC;
+        bufferDesc.MiscFlags = useMips ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;
         HRESULT r = device->CreateTexture2D(&bufferDesc, 0, &txt);
         if (r != S_OK) return 0;
     }
     if (!renderTarget && (bild->getRend() || changed))
     {
         changed = 0;
-        D3D11_MAPPED_SUBRESOURCE buffer;
-        context->Map(txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer);
-        int* bgBuff = bild->getBuffer();
-        int tmpBr = 4 * bild->getBreite();
-        for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
-             ++y, pitch += buffer.RowPitch, bry += bild->getBreite())
-            memcpy(&((BYTE*)buffer.pData)[pitch], (void*)&(bgBuff[bry]), tmpBr);
-        context->Unmap(txt, 0);
+        if (useMips)
+        {
+            context->UpdateSubresource(txt,
+                0,
+                0,
+                bild->getBuffer(),
+                4 * bild->getBreite(),
+                4 * bild->getBreite() * bild->getHeight());
+        }
+        else
+        {
+            D3D11_MAPPED_SUBRESOURCE buffer;
+            context->Map(
+                txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer);
+            int* bgBuff = bild->getBuffer();
+            int tmpBr = 4 * bild->getBreite();
+            for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
+                 ++y, pitch += buffer.RowPitch, bry += bild->getBreite())
+                memcpy(&((BYTE*)buffer.pData)[pitch],
+                    (void*)&(bgBuff[bry]),
+                    tmpBr);
+            context->Unmap(txt, 0);
+        }
     }
     if (!view || lastGr != bild->getSize())
     {
@@ -147,9 +165,10 @@ bool DX11Textur::updateTextur()
         memset(&resourceDesk, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
         resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
         resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
-        resourceDesk.Texture2D.MipLevels = 1;
+        resourceDesk.Texture2D.MipLevels = useMips ? -1 : 1;
         HRESULT r = device->CreateShaderResourceView(txt, &resourceDesk, &view);
         if (r != S_OK) return 0;
+        if (context && useMips) context->GenerateMips(view);
     }
     lastGr = bild->getSize();
 #endif
@@ -177,9 +196,16 @@ DX11Textur::operator ID3D11Texture2D*() const
 //! specifies that this texture is used as a render target
 void DX11Textur::setRenderTarget(bool rt)
 {
+    if (rt) useMips = 0;
     renderTarget = rt;
 }
 
+//! specifies if a mip map should be generated
+void DX11Textur::setUseMips(bool useMips)
+{
+    this->useMips = useMips;
+}
+
 //! copy the texture to an image
 void DX11Textur::copyToImage(Bild* zB)
 {

+ 3 - 0
Textur.h

@@ -74,6 +74,7 @@ namespace Framework
         ID3D11Device* device;
         ID3D11DeviceContext* context;
         bool renderTarget;
+        bool useMips;
 
     public:
         DLLEXPORT DX11Textur(
@@ -90,6 +91,8 @@ namespace Framework
         DLLEXPORT operator ID3D11Texture2D*() const;
         //! specifies that this texture is used as a render target
         DLLEXPORT void setRenderTarget(bool rt);
+        //! specifies if a mip map should be generated
+        DLLEXPORT void setUseMips(bool useMips);
         //! copy the texture to an image
         DLLEXPORT void copyToImage(Bild* zB);
     };