Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
VSync option and memory usage optimization.
  • Loading branch information
rdoeffinger committed Jul 28, 2019
2 parents 3f87c2d + 0b6058b commit 6eccb8b
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions impl11/ddraw/Direct3DDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,17 @@ void Direct3DDevice::UpdatePixelShader(ID3D11DeviceContext *context, Direct3DTex
}
else if (!this->_renderStates->AlphaTestEnabled || this->_renderStates->AlphaFunc == D3DCMP_ALWAYS)
{
texture->_refCount++;
context->PSSetShaderResources(0, 1, texture->_textureView.GetAddressOf());
texture->_refCount--;

pixelShader = this->_deviceResources->_pixelShaderTexture;
}
else
{
texture->_refCount++;
context->PSSetShaderResources(0, 1, texture->_textureView.GetAddressOf());
texture->_refCount--;

#if LOGGER
if (this->_renderStates->AlphaFunc != D3DCMP_NOTEQUAL || this->_renderStates->AlphaRef != 0)
Expand Down
5 changes: 3 additions & 2 deletions impl11/ddraw/Direct3DTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ HRESULT Direct3DTexture::Load(
LogText(str.str());
#endif

d3dTexture->_textureView->AddRef();
*&this->_textureView = d3dTexture->_textureView.Get();
this->_textureView->AddRef();

return D3D_OK;
}

Expand Down Expand Up @@ -421,8 +422,8 @@ HRESULT Direct3DTexture::Load(
this->_deviceResources->_d3dDeviceContext->GenerateMips(d3dTexture->_textureView);
}

d3dTexture->_textureView->AddRef();
*&this->_textureView = d3dTexture->_textureView.Get();
this->_textureView->AddRef();

return D3D_OK;
}
Expand Down
26 changes: 24 additions & 2 deletions impl11/ddraw/MipmapSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
#include "common.h"
#include "DeviceResources.h"
#include "MipmapSurface.h"
#include "TextureSurface.h"
#include "Direct3DTexture.h"

// defining NOMINMAX unfortunately breaks compilation of some DX headers
#undef min
#undef max
#include <algorithm>

MipmapSurface::MipmapSurface(DeviceResources* deviceResources, DWORD width, DWORD height, DDPIXELFORMAT& pixelFormat, DWORD mipmapCount, char* buffer)
MipmapSurface::MipmapSurface(DeviceResources* deviceResources, TextureSurface* surface, DWORD width, DWORD height, DDPIXELFORMAT& pixelFormat, DWORD mipmapCount, char* buffer)
{
this->_refCount = 1;
this->_deviceResources = deviceResources;

this->_surface = surface;

this->_width = width;
this->_height = height;
this->_pixelFormat = pixelFormat;
Expand All @@ -26,7 +29,7 @@ MipmapSurface::MipmapSurface(DeviceResources* deviceResources, DWORD width, DWOR

if (this->_mipmapCount > 1)
{
*this->_mipmap.GetAddressOf() = new MipmapSurface(this->_deviceResources, std::max(this->_width / 2, 1ul), std::max(this->_height / 2, 1ul), this->_pixelFormat, this->_mipmapCount - 1, this->_buffer + this->_bufferSize);
*this->_mipmap.GetAddressOf() = new MipmapSurface(this->_deviceResources, surface, std::max(this->_width / 2, 1ul), std::max(this->_height / 2, 1ul), this->_pixelFormat, this->_mipmapCount - 1, this->_buffer + this->_bufferSize);
}
}

Expand Down Expand Up @@ -741,6 +744,25 @@ HRESULT MipmapSurface::Unlock(
LogText(str.str());
#endif

if (this->_mipmapCount == 1)
{
this->_surface->_d3dTexture->Load(this->_surface->_d3dTexture);

if (this->_surface->_buffer != nullptr)
{
delete[] this->_surface->_buffer;
this->_surface->_buffer = nullptr;
this->_surface->_bufferSize = 0;
this->_surface->_width = 0;
this->_surface->_height = 0;

this->_buffer = nullptr;
this->_bufferSize = 0;
this->_width = 0;
this->_height = 0;
}
}

return DD_OK;
}

Expand Down
6 changes: 5 additions & 1 deletion impl11/ddraw/MipmapSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

#pragma once

class TextureSurface;

class MipmapSurface : public IDirectDrawSurface
{
public:
MipmapSurface(DeviceResources* deviceResources, DWORD width, DWORD height, DDPIXELFORMAT& pixelFormat, DWORD mipmapCount, char* buffer);
MipmapSurface(DeviceResources* deviceResources, TextureSurface* surface, DWORD width, DWORD height, DDPIXELFORMAT& pixelFormat, DWORD mipmapCount, char* buffer);

virtual ~MipmapSurface();

Expand Down Expand Up @@ -90,6 +92,8 @@ class MipmapSurface : public IDirectDrawSurface

DeviceResources* _deviceResources;

TextureSurface* _surface;

DWORD _width;
DWORD _height;
DDPIXELFORMAT _pixelFormat;
Expand Down
2 changes: 1 addition & 1 deletion impl11/ddraw/PrimarySurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ HRESULT PrimarySurface::Flip(

this->_deviceResources->_d3dDeviceContext->ResolveSubresource(this->_deviceResources->_backBuffer, 0, this->_deviceResources->_offscreenBuffer, 0, DXGI_FORMAT_B8G8R8A8_UNORM);

if (FAILED(hr = this->_deviceResources->_swapChain->Present(1, 0)))
if (FAILED(hr = this->_deviceResources->_swapChain->Present(g_config.VSyncEnabled ? 1 : 0, 0)))
{
static bool messageShown = false;

Expand Down
18 changes: 17 additions & 1 deletion impl11/ddraw/TextureSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TextureSurface::TextureSurface(DeviceResources* deviceResources, bool allocOnLoa

if (this->_mipmapCount > 1)
{
*this->_mipmap.GetAddressOf() = new MipmapSurface(this->_deviceResources, std::max(this->_width / 2, 1ul), std::max(this->_height / 2, 1ul), this->_pixelFormat, this->_mipmapCount - 1, this->_buffer + this->_bufferSize / 2);
*this->_mipmap.GetAddressOf() = new MipmapSurface(this->_deviceResources, this, std::max(this->_width / 2, 1ul), std::max(this->_height / 2, 1ul), this->_pixelFormat, this->_mipmapCount - 1, this->_buffer + this->_bufferSize / 2);
}
}

Expand All @@ -51,6 +51,7 @@ TextureSurface::~TextureSurface()
if (this->_buffer != nullptr)
{
delete[] this->_buffer;
this->_buffer = nullptr;
}
}

Expand Down Expand Up @@ -798,6 +799,21 @@ HRESULT TextureSurface::Unlock(
LogText(str.str());
#endif

// do not free memory for ModelIndex_418_16000_0_ResData_Fonts
if (this->_pixelFormat.dwRGBBitCount == 32 && this->_mipmapCount == 1)
{
this->_d3dTexture->Load(this->_d3dTexture);

if (this->_buffer != nullptr)
{
delete[] this->_buffer;
this->_buffer = nullptr;
this->_bufferSize = 0;
this->_width = 0;
this->_height = 0;
}
}

return DD_OK;
}

Expand Down
5 changes: 5 additions & 0 deletions impl11/ddraw/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Config::Config()
this->MultisamplingAntialiasingEnabled = false;
this->AnisotropicFilteringEnabled = true;
this->GenerateMipMaps = -1;
this->VSyncEnabled = true;
this->WireframeFillMode = false;
this->ScalingType = 0;
this->Fullscreen = 0;
Expand Down Expand Up @@ -123,6 +124,10 @@ Config::Config()
{
this->GenerateMipMaps = stoi(value);
}
else if (name == "EnableVSync")
{
this->VSyncEnabled = stoi(value) != 0;
}
else if (name == "FillWireframe")
{
this->WireframeFillMode = stoi(value) != 0;
Expand Down
1 change: 1 addition & 0 deletions impl11/ddraw/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config
bool MultisamplingAntialiasingEnabled;
bool AnisotropicFilteringEnabled;
int GenerateMipMaps;
bool VSyncEnabled;
bool WireframeFillMode;
int ScalingType;

Expand Down
3 changes: 3 additions & 0 deletions impl11/ddraw/ddraw.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ EnableAnisotropicFiltering = 1
; ScalingType = 0 (standard linear scaling), 1 (smoother, alpha blended scaling), 2 (Lanczos scaling) or 3 (point-sampling)
ScalingType = 0

; EnableVSync = 0 (vsync off) or 1 (vsync on)
EnableVSync = 1

; ========================
; Input related options
; ========================
Expand Down

0 comments on commit 6eccb8b

Please sign in to comment.