Fix gamma slider via pixel shader #178 (#481)

* Fix gamma slider via pixel shader #178

* LCE-like gamma using postprocess shader
This commit is contained in:
ModMaker101
2026-03-05 14:41:17 -05:00
committed by GitHub
parent 55231bb8d3
commit cbcf3de358
8 changed files with 800 additions and 109 deletions

View File

@@ -1,5 +1,4 @@

#include "stdafx.h"
#include "stdafx.h"
#include "..\..\Minecraft.World\net.minecraft.world.entity.item.h"
#include "..\..\Minecraft.World\net.minecraft.world.entity.player.h"
#include "..\..\Minecraft.World\net.minecraft.world.level.tile.entity.h"

View File

@@ -0,0 +1,57 @@
#pragma once
#include <d3d11.h>
class PostProcesser
{
public:
static PostProcesser& GetInstance()
{
static PostProcesser instance;
return instance;
}
void Init();
void Apply() const;
void SetViewport(const D3D11_VIEWPORT& viewport);
void ResetViewport();
void CopyBackbuffer(); // Copy backbuffer once before multi-pass gamma
void ApplyFromCopied() const; // Apply gamma using already-copied offscreen texture
void Cleanup();
void SetGamma(float gamma);
float GetGamma() const { return m_gamma; }
PostProcesser(const PostProcesser&) = delete;
PostProcesser& operator=(const PostProcesser&) = delete;
private:
PostProcesser();
~PostProcesser();
static bool IsRunningUnderWine();
ID3D11Texture2D* m_pGammaOffscreenTex = nullptr;
ID3D11ShaderResourceView* m_pGammaOffscreenSRV = nullptr;
ID3D11RenderTargetView* m_pGammaOffscreenRTV = nullptr;
ID3D11VertexShader* m_pGammaVS = nullptr;
ID3D11PixelShader* m_pGammaPS = nullptr;
ID3D11Buffer* m_pGammaCB = nullptr;
ID3D11SamplerState* m_pGammaSampler = nullptr;
ID3D11RasterizerState* m_pGammaRastState = nullptr;
ID3D11DepthStencilState* m_pGammaDepthState = nullptr;
ID3D11BlendState* m_pGammaBlendState = nullptr;
bool m_initialized = false;
float m_gamma = 1.0f;
bool m_wineMode = false;
D3D11_VIEWPORT m_customViewport;
bool m_useCustomViewport = false;
struct GammaCBData
{
float gamma;
float pad[3];
};
static const char* g_gammaVSCode;
static const char* g_gammaPSCode;
};

View File

@@ -51,6 +51,7 @@
#include "TexturePackRepository.h"
#include "TexturePack.h"
#include "TextureAtlas.h"
#include "Common/PostProcesser.h"
bool GameRenderer::anaglyph3d = false;
int GameRenderer::anaglyphPass = 0;
@@ -832,124 +833,253 @@ void GameRenderer::tickLightTexture()
void GameRenderer::updateLightTexture(float a)
{
// 4J-JEV: Now doing light textures on PER PLAYER basis.
// 4J - we *had* added separate light textures for all dimensions, and this loop to update them all here
for(int j = 0; j < XUSER_MAX_COUNT; j++ )
{
// Loop over all the players
shared_ptr<MultiplayerLocalPlayer> player = Minecraft::GetInstance()->localplayers[j];
if (player == NULL) continue;
CachePlayerGammas();
Level *level = player->level; // 4J - was mc->level when it was just to update the one light texture
for (int j = 0; j < XUSER_MAX_COUNT; j++)
{
shared_ptr<MultiplayerLocalPlayer> player = Minecraft::GetInstance()->localplayers[j];
if (player == nullptr)
continue;
float skyDarken1 = level->getSkyDarken((float) 1);
for (int i = 0; i < 256; i++)
{
float darken = skyDarken1 * 0.95f + 0.05f;
float sky = level->dimension->brightnessRamp[i / 16] * darken;
float block = level->dimension->brightnessRamp[i % 16] * (blr * 0.1f + 1.5f);
Level *level = player->level;
if (level->skyFlashTime > 0)
{
sky = level->dimension->brightnessRamp[i / 16];
}
const float skyDarken1 = level->getSkyDarken(1.0f);
for (int i = 0; i < 256; i++)
{
const float darken = skyDarken1 * 0.95f + 0.05f;
float sky = level->dimension->brightnessRamp[i / 16] * darken;
const float block = level->dimension->brightnessRamp[i % 16] * (blr * 0.1f + 1.5f);
float rs = sky * (skyDarken1 * 0.65f + 0.35f);
float gs = sky * (skyDarken1 * 0.65f + 0.35f);
float bs = sky;
if (level->skyFlashTime < 0)
{
sky = level->dimension->brightnessRamp[i / 16];
}
float rb = block;
float gb = block * ((block * 0.6f + 0.4f) * 0.6f + 0.4f);
float bb = block * ((block * block) * 0.6f + 0.4f);
const float rs = sky * (skyDarken1 * 0.65f + 0.35f);
const float gs = sky * (skyDarken1 * 0.65f + 0.35f);
const float bs = sky;
float _r = (rs + rb);
float _g = (gs + gb);
float _b = (bs + bb);
const float rb = block;
const float gb = block * ((block * 0.6f + 0.4f) * 0.6f + 0.4f);
const float bb = block * ((block * block) * 0.6f + 0.4f);
_r = _r * 0.96f + 0.03f;
_g = _g * 0.96f + 0.03f;
_b = _b * 0.96f + 0.03f;
float _r = (rs + rb);
float _g = (gs + gb);
float _b = (bs + bb);
if (darkenWorldAmount > 0)
{
float amount = darkenWorldAmountO + (darkenWorldAmount - darkenWorldAmountO) * a;
_r = _r * (1.0f - amount) + (_r * .7f) * amount;
_g = _g * (1.0f - amount) + (_g * .6f) * amount;
_b = _b * (1.0f - amount) + (_b * .6f) * amount;
}
_r = _r * 0.96f + 0.03f;
_g = _g * 0.96f + 0.03f;
_b = _b * 0.96f + 0.03f;
if (level->dimension->id == 1)
{
_r = (0.22f + rb * 0.75f);
_g = (0.28f + gb * 0.75f);
_b = (0.25f + bb * 0.75f);
}
if (darkenWorldAmount > 0)
{
const float amount = darkenWorldAmountO + (darkenWorldAmount - darkenWorldAmountO) * a;
_r = _r * (1.0f - amount) + (_r * 0.7f) * amount;
_g = _g * (1.0f - amount) + (_g * 0.6f) * amount;
_b = _b * (1.0f - amount) + (_b * 0.6f) * amount;
}
if (player->hasEffect(MobEffect::nightVision))
{
float scale = getNightVisionScale(player, a);
{
float dist = 1.0f / _r;
if (dist > (1.0f / _g))
{
dist = (1.0f / _g);
}
if (dist > (1.0f / _b))
{
dist = (1.0f / _b);
}
_r = _r * (1.0f - scale) + (_r * dist) * scale;
_g = _g * (1.0f - scale) + (_g * dist) * scale;
_b = _b * (1.0f - scale) + (_b * dist) * scale;
}
}
if (level->dimension->id == 1)
{
_r = (0.22f + rb * 0.75f);
_g = (0.28f + gb * 0.75f);
_b = (0.25f + bb * 0.75f);
}
if (_r > 1) _r = 1;
if (_g > 1) _g = 1;
if (_b > 1) _b = 1;
if (player->hasEffect(MobEffect::nightVision))
{
const float scale = getNightVisionScale(player, a);
float dist = 1.0f / _r;
if (dist > (1.0f / _g))
dist = (1.0f / _g);
if (dist > (1.0f / _b))
dist = (1.0f / _b);
_r = _r * (1.0f - scale) + (_r * dist) * scale;
_g = _g * (1.0f - scale) + (_g * dist) * scale;
_b = _b * (1.0f - scale) + (_b * dist) * scale;
}
float brightness = mc->options->gamma;
if (_r > 1.0f)
_r = 1.0f;
if (_r < 0.0f)
_r = 0.0f;
if (_g > 1.0f)
_g = 1.0f;
if (_g < 0.0f)
_g = 0.0f;
if (_b > 1.0f)
_b = 1.0f;
if (_b < 0.0f)
_b = 0.0f;
float ir = 1 - _r;
float ig = 1 - _g;
float ib = 1 - _b;
ir = 1 - (ir * ir * ir * ir);
ig = 1 - (ig * ig * ig * ig);
ib = 1 - (ib * ib * ib * ib);
_r = _r * (1 - brightness) + ir * brightness;
_g = _g * (1 - brightness) + ig * brightness;
_b = _b * (1 - brightness) + ib * brightness;
_r = _r * 0.96f + 0.03f;
_g = _g * 0.96f + 0.03f;
_b = _b * 0.96f + 0.03f;
if (_r > 1) _r = 1;
if (_g > 1) _g = 1;
if (_b > 1) _b = 1;
if (_r < 0) _r = 0;
if (_g < 0) _g = 0;
if (_b < 0) _b = 0;
int alpha = 255;
int r = (int) (_r * 255);
int g = (int) (_g * 255);
int b = (int) (_b * 255);
constexpr int alpha = 255;
const int r = static_cast<int>(_r * 255);
const int g = static_cast<int>(_g * 255);
const int b = static_cast<int>(_b * 255);
#if ( defined _DURANGO || defined _WIN64 || __PSVITA__ )
lightPixels[j][i] = alpha << 24 | b << 16 | g << 8 | r;
lightPixels[j][i] = alpha << 24 | b << 16 | g << 8 | r;
#elif ( defined _XBOX || defined __ORBIS__ )
lightPixels[j][i] = alpha << 24 | r << 16 | g << 8 | b;
lightPixels[j][i] = alpha << 24 | r << 16 | g << 8 | b;
#else
lightPixels[j][i] = r << 24 | g << 16 | b << 8 | alpha;
lightPixels[j][i] = r << 24 | g << 16 | b << 8 | alpha;
#endif
}
}
mc->textures->replaceTextureDirect( lightPixels[j], 16, 16, getLightTexture(j,level) );
// lightTexture->upload(); // 4J: not relevant
mc->textures->replaceTextureDirect(lightPixels[j], 16, 16, getLightTexture(j, level));
}
}
//_updateLightTexture = false;
}
float GameRenderer::ComputeGammaFromSlider(float slider0to100)
{
float slider = slider0to100;
slider = max(slider, 0.0f);
slider = min(slider, 100.0f);
if (slider > 50.0f)
return 1.0f + (slider - 50.0f) / 50.0f * 3.0f; // 1.0 -> 4.0
else
return 1.0f - (50.0f - slider) / 50.0f * 0.85f; // 1.0 -> 0.15
}
void GameRenderer::CachePlayerGammas()
{
for (int j = 0; j < XUSER_MAX_COUNT && j < NUM_LIGHT_TEXTURES; ++j)
{
std::shared_ptr<MultiplayerLocalPlayer> player = Minecraft::GetInstance()->localplayers[j];
if (!player)
{
m_cachedGammaPerPlayer[j] = 1.0f;
continue;
}
const float slider = app.GetGameSettings(j, eGameSetting_Gamma); // 0..100
m_cachedGammaPerPlayer[j] = ComputeGammaFromSlider(slider);
}
}
bool GameRenderer::ComputeViewportForPlayer(int j, D3D11_VIEWPORT &outViewport) const
{
int active = 0;
int indexMap[NUM_LIGHT_TEXTURES] = {-1, -1, -1, -1};
for (int i = 0; i < XUSER_MAX_COUNT && i < NUM_LIGHT_TEXTURES; ++i)
{
if (Minecraft::GetInstance()->localplayers[i])
indexMap[active++] = i;
}
if (active <= 1)
{
outViewport.TopLeftX = 0.0f;
outViewport.TopLeftY = 0.0f;
outViewport.Width = static_cast<FLOAT>(mc->width);
outViewport.Height = static_cast<FLOAT>(mc->height);
outViewport.MinDepth = 0.0f;
outViewport.MaxDepth = 1.0f;
return true;
}
int k = -1;
for (int ord = 0; ord < active; ++ord)
if (indexMap[ord] == j)
{
k = ord;
break;
}
if (k < 0)
return false;
const float width = static_cast<float>(mc->width);
const float height = static_cast<float>(mc->height);
if (active == 2)
{
const float halfH = height * 0.5f;
outViewport.TopLeftX = 0.0f;
outViewport.Width = width;
outViewport.MinDepth = 0.0f;
outViewport.MaxDepth = 1.0f;
if (k == 0)
{
outViewport.TopLeftY = 0.0f;
outViewport.Height = halfH;
}
else
{
outViewport.TopLeftY = halfH;
outViewport.Height = halfH;
}
return true;
}
else
{
const float halfW = width * 0.5f;
const float halfH = height * 0.5f;
const int row = (k >= 2) ? 1 : 0;
const int col = (k % 2);
outViewport.TopLeftX = col ? halfW : 0.0f;
outViewport.TopLeftY = row ? halfH : 0.0f;
outViewport.Width = halfW;
outViewport.Height = halfH;
outViewport.MinDepth = 0.0f;
outViewport.MaxDepth = 1.0f;
return true;
}
}
uint32_t GameRenderer::BuildPlayerViewports(D3D11_VIEWPORT *outViewports, float *outGammas, UINT maxCount) const
{
UINT count = 0;
for (int j = 0; j < XUSER_MAX_COUNT && j < NUM_LIGHT_TEXTURES && count < maxCount; ++j)
{
if (!Minecraft::GetInstance()->localplayers[j])
continue;
D3D11_VIEWPORT vp;
if (!ComputeViewportForPlayer(j, vp))
continue;
outViewports[count] = vp;
outGammas[count] = m_cachedGammaPerPlayer[j];
++count;
}
return count;
}
void GameRenderer::ApplyGammaPostProcess() const
{
D3D11_VIEWPORT vps[NUM_LIGHT_TEXTURES];
float gammas[NUM_LIGHT_TEXTURES];
const UINT n = BuildPlayerViewports(vps, gammas, NUM_LIGHT_TEXTURES);
if (n == 0)
return;
bool anyEffect = false;
for (UINT i = 0; i < n; ++i)
{
if (gammas[i] < 0.99f || gammas[i] > 1.01f)
{
anyEffect = true;
break;
}
}
if (!anyEffect)
return;
if (n == 1)
{
PostProcesser::GetInstance().SetGamma(gammas[0]);
PostProcesser::GetInstance().Apply();
}
else
{
PostProcesser::GetInstance().CopyBackbuffer();
for (UINT i = 0; i < n; ++i)
{
PostProcesser::GetInstance().SetGamma(gammas[i]);
PostProcesser::GetInstance().SetViewport(vps[i]);
PostProcesser::GetInstance().ApplyFromCopied();
}
PostProcesser::GetInstance().ResetViewport();
}
}
float GameRenderer::getNightVisionScale(shared_ptr<Player> player, float a)
@@ -1039,6 +1169,7 @@ void GameRenderer::render(float a, bool bFirst)
lastNsTime = System::nanoTime();
ApplyGammaPostProcess();
if (!mc->options->hideGui || mc->screen != NULL)
{

View File

@@ -1,4 +1,9 @@
#pragma once
#ifdef _WIN64
#include <d3d11.h>
#endif
class Minecraft;
class Entity;
class Random;
@@ -72,6 +77,14 @@ private:
float darkenWorldAmount;
float darkenWorldAmountO;
// Gamma caching
float m_cachedGammaPerPlayer[NUM_LIGHT_TEXTURES];
static float ComputeGammaFromSlider(float slider0to100);
void CachePlayerGammas();
void ApplyGammaPostProcess() const;
bool ComputeViewportForPlayer(int j, D3D11_VIEWPORT& outViewport) const;
uint32_t BuildPlayerViewports(D3D11_VIEWPORT* outViewports, float* outGammas, UINT maxCount) const;
bool isInClouds;
float m_fov;

View File

@@ -1571,7 +1571,7 @@ if not exist "$(TargetDir)\savedata" mkdir "$(TargetDir)\savedata"</Command>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>d3d11.lib;..\Minecraft.World\x64_Debug\Minecraft.World.lib;%(AdditionalDependencies);XInput9_1_0.lib;..\Minecraft.Client\Windows64\Miles\Lib\mss64.lib;wsock32.lib</AdditionalDependencies>
<AdditionalDependencies>legacy_stdio_definitions.lib;d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
</Link>
@@ -1800,7 +1800,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU</C
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>legacy_stdio_definitions.lib;d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>legacy_stdio_definitions.lib;d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
@@ -1846,7 +1846,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU</C
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>legacy_stdio_definitions.lib;d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>legacy_stdio_definitions.lib;d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
</Link>
@@ -1891,7 +1891,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU</C
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
</Link>
@@ -1932,7 +1932,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU</C
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
</Link>
@@ -1973,7 +1973,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU</C
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
</Link>
@@ -2014,7 +2014,7 @@ xcopy /q /y /i /s /e $(ProjectDir)DurangoMedia\CU $(LayoutDir)Image\Loose\CU</C
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>d3d11.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>d3d11.lib;d3dcompiler.lib;..\Minecraft.World\x64_Release\Minecraft.World.lib;XInput9_1_0.lib;Windows64\Iggy\lib\iggy_w64.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ShowProgress>NotSet</ShowProgress>
<SuppressStartupBanner>false</SuppressStartupBanner>
</Link>
@@ -6082,6 +6082,7 @@ xcopy /q /y /i /s /e $(ProjectDir)Durango\CU $(LayoutDir)Image\Loose\CU</Comman
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='CONTENTPACKAGE_SYMBOLS|PSVita'">false</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="Common\Potion_Macros.h" />
<ClInclude Include="Common\PostProcesser.h" />
<ClInclude Include="Common\Telemetry\TelemetryManager.h" />
<ClInclude Include="Common\Trial\TrialMode.h" />
<ClInclude Include="Common\Tutorial\AreaConstraint.h" />
@@ -38458,6 +38459,36 @@ xcopy /q /y /i /s /e $(ProjectDir)Durango\CU $(LayoutDir)Image\Loose\CU</Comman
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_Vita|ORBIS'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="Windows64\PostProcesser.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='CONTENTPACKAGE_SYMBOLS|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ReleaseForArt|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_NO_TU|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='DebugContentPackage|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_Vita|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='CONTENTPACKAGE_SYMBOLS|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ReleaseForArt|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_NO_TU|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_Vita|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Xbox 360'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='CONTENTPACKAGE_SYMBOLS|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ReleaseForArt|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_NO_TU|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage_Vita|ORBIS'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|PS3'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|PS3'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|PSVita'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|PSVita'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='CONTENTPACKAGE_SYMBOLS|PSVita'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage|PSVita'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="Windows64\KeyboardMouseInput.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='ContentPackage|Durango'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='CONTENTPACKAGE_SYMBOLS|Durango'">true</ExcludedFromBuild>

View File

@@ -723,10 +723,10 @@
<Filter Include="PSVita\Miles Sound System\lib">
<UniqueIdentifier>{0061db22-43de-4b54-a161-c43958cdcd7e}</UniqueIdentifier>
</Filter>
<Filter Include="net\minecraft\client\resources">
<Filter Include="net\minecraft\client\resources">
<UniqueIdentifier>{889a84db-3009-4a7c-8234-4bf93d412690}</UniqueIdentifier>
</Filter>
<Filter Include="Windows64\Source Files\Network">
<Filter Include="Windows64\Source Files\Network">
<UniqueIdentifier>{e5d7fb24-25b8-413c-84ec-974bf0d4a3d1}</UniqueIdentifier>
</Filter>
</ItemGroup>
@@ -2098,6 +2098,9 @@
<ClInclude Include="Common\Potion_Macros.h">
<Filter>Common</Filter>
</ClInclude>
<ClInclude Include="Common\PostProcesser.h">
<Filter>Common</Filter>
</ClInclude>
<ClInclude Include="PS3\4JLibs\inc\4J_Input.h">
<Filter>PS3\4JLibs\inc</Filter>
</ClInclude>
@@ -4920,6 +4923,9 @@
<ClCompile Include="Windows64\Windows64_Minecraft.cpp">
<Filter>Windows64\Source Files</Filter>
</ClCompile>
<ClCompile Include="Windows64\PostProcesser.cpp">
<Filter>Windows64\Source Files</Filter>
</ClCompile>
<ClCompile Include="Windows64\KeyboardMouseInput.cpp">
<Filter>Windows64\Source Files</Filter>
</ClCompile>

View File

@@ -0,0 +1,451 @@
#include "stdafx.h"
#include "..\Common\PostProcesser.h"
#include <d3dcompiler.h>
#include <vector>
#pragma comment(lib, "d3dcompiler.lib")
extern ID3D11Device* g_pd3dDevice;
extern ID3D11DeviceContext* g_pImmediateContext;
extern IDXGISwapChain* g_pSwapChain;
extern ID3D11RenderTargetView* g_pRenderTargetView;
const char* PostProcesser::g_gammaVSCode =
"void main(uint id : SV_VertexID, out float4 pos : SV_Position, out float2 uv : TEXCOORD0)\n"
"{\n"
" uv = float2((id << 1) & 2, id & 2);\n"
" pos = float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);\n"
"}\n";
const char* PostProcesser::g_gammaPSCode =
"cbuffer GammaCB : register(b0)\n"
"{\n"
" float gamma;\n"
" float3 pad;\n"
"};\n"
"Texture2D sceneTex : register(t0);\n"
"SamplerState sceneSampler : register(s0);\n"
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target\n"
"{\n"
" float4 color = sceneTex.Sample(sceneSampler, uv);\n"
" color.rgb = pow(max(color.rgb, 0.0), 1.0 / gamma);\n"
" return color;\n"
"}\n";
PostProcesser::PostProcesser() = default;
PostProcesser::~PostProcesser()
{
Cleanup();
}
bool PostProcesser::IsRunningUnderWine()
{
const HMODULE ntdll = GetModuleHandleA("ntdll.dll");
if (ntdll)
{
if (GetProcAddress(ntdll, "wine_get_version"))
return true;
}
return false;
}
void PostProcesser::SetGamma(float gamma)
{
m_gamma = gamma;
}
void PostProcesser::Init()
{
if (!g_pd3dDevice || !g_pSwapChain)
return;
if (m_initialized)
return;
m_wineMode = IsRunningUnderWine();
HRESULT hr;
ID3D11Texture2D* pBackBuffer = nullptr;
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&pBackBuffer));
if (FAILED(hr))
return;
D3D11_TEXTURE2D_DESC bbDesc;
pBackBuffer->GetDesc(&bbDesc);
pBackBuffer->Release();
DXGI_FORMAT texFormat = bbDesc.Format;
if (m_wineMode)
{
texFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
}
D3D11_TEXTURE2D_DESC texDesc;
ZeroMemory(&texDesc, sizeof(texDesc));
texDesc.Width = bbDesc.Width;
texDesc.Height = bbDesc.Height;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = texFormat;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
hr = g_pd3dDevice->CreateTexture2D(&texDesc, nullptr, &m_pGammaOffscreenTex);
if (FAILED(hr))
return;
hr = g_pd3dDevice->CreateShaderResourceView(m_pGammaOffscreenTex, nullptr, &m_pGammaOffscreenSRV);
if (FAILED(hr))
return;
hr = g_pd3dDevice->CreateRenderTargetView(m_pGammaOffscreenTex, nullptr, &m_pGammaOffscreenRTV);
if (FAILED(hr))
return;
ID3DBlob* vsBlob = nullptr;
ID3DBlob* errBlob = nullptr;
UINT compileFlags = D3DCOMPILE_ENABLE_STRICTNESS;
if (m_wineMode)
{
compileFlags |= D3DCOMPILE_AVOID_FLOW_CONTROL;
}
hr = D3DCompile(g_gammaVSCode, strlen(g_gammaVSCode), "GammaVS", nullptr, nullptr, "main", "vs_4_0", compileFlags, 0, &vsBlob, &errBlob);
if (FAILED(hr))
{
if (errBlob)
errBlob->Release();
return;
}
hr = g_pd3dDevice->CreateVertexShader(vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), nullptr, &m_pGammaVS);
vsBlob->Release();
if (errBlob)
errBlob->Release();
if (FAILED(hr))
return;
errBlob = nullptr;
ID3DBlob* psBlob = nullptr;
hr = D3DCompile(g_gammaPSCode, strlen(g_gammaPSCode), "GammaPS", nullptr, nullptr, "main", "ps_4_0", compileFlags, 0, &psBlob, &errBlob);
if (FAILED(hr))
{
if (errBlob)
errBlob->Release();
return;
}
hr = g_pd3dDevice->CreatePixelShader(psBlob->GetBufferPointer(), psBlob->GetBufferSize(), nullptr, &m_pGammaPS);
psBlob->Release();
if (errBlob)
errBlob->Release();
if (FAILED(hr))
return;
D3D11_BUFFER_DESC cbDesc;
ZeroMemory(&cbDesc, sizeof(cbDesc));
cbDesc.ByteWidth = sizeof(GammaCBData);
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
GammaCBData initData = {1.0f, {0, 0, 0}};
D3D11_SUBRESOURCE_DATA srData;
srData.pSysMem = &initData;
srData.SysMemPitch = 0;
srData.SysMemSlicePitch = 0;
hr = g_pd3dDevice->CreateBuffer(&cbDesc, &srData, &m_pGammaCB);
if (FAILED(hr))
return;
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &m_pGammaSampler);
if (FAILED(hr))
return;
D3D11_RASTERIZER_DESC rasDesc;
ZeroMemory(&rasDesc, sizeof(rasDesc));
rasDesc.FillMode = D3D11_FILL_SOLID;
rasDesc.CullMode = D3D11_CULL_NONE;
rasDesc.DepthClipEnable = FALSE;
rasDesc.ScissorEnable = FALSE;
hr = g_pd3dDevice->CreateRasterizerState(&rasDesc, &m_pGammaRastState);
if (FAILED(hr))
return;
D3D11_DEPTH_STENCIL_DESC dsDesc;
ZeroMemory(&dsDesc, sizeof(dsDesc));
dsDesc.DepthEnable = FALSE;
dsDesc.StencilEnable = FALSE;
hr = g_pd3dDevice->CreateDepthStencilState(&dsDesc, &m_pGammaDepthState);
if (FAILED(hr))
return;
D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(blendDesc));
blendDesc.RenderTarget[0].BlendEnable = FALSE;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
hr = g_pd3dDevice->CreateBlendState(&blendDesc, &m_pGammaBlendState);
if (FAILED(hr))
return;
m_initialized = true;
}
void PostProcesser::Apply() const
{
if (!m_initialized)
return;
if (m_gamma > 0.99f && m_gamma < 1.01f)
return;
ID3D11DeviceContext *ctx = g_pImmediateContext;
ID3D11Texture2D *pBackBuffer = nullptr;
HRESULT hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID *>(&pBackBuffer));
if (FAILED(hr))
return;
ctx->CopyResource(m_pGammaOffscreenTex, pBackBuffer);
D3D11_MAPPED_SUBRESOURCE mapped;
const D3D11_MAP mapType = m_wineMode ? D3D11_MAP_WRITE_NO_OVERWRITE : D3D11_MAP_WRITE_DISCARD;
hr = ctx->Map(m_pGammaCB, 0, mapType, 0, &mapped);
if (SUCCEEDED(hr))
{
GammaCBData *cb = static_cast<GammaCBData *>(mapped.pData);
cb->gamma = m_gamma;
ctx->Unmap(m_pGammaCB, 0);
}
ID3D11RenderTargetView* oldRTV = nullptr;
ID3D11DepthStencilView* oldDSV = nullptr;
ctx->OMGetRenderTargets(1, &oldRTV, &oldDSV);
UINT numViewports = 1;
D3D11_VIEWPORT oldViewport;
ctx->RSGetViewports(&numViewports, &oldViewport);
ID3D11RenderTargetView* bbRTV = g_pRenderTargetView;
ctx->OMSetRenderTargets(1, &bbRTV, nullptr);
D3D11_TEXTURE2D_DESC bbDesc;
pBackBuffer->GetDesc(&bbDesc);
pBackBuffer->Release();
D3D11_VIEWPORT vp;
if (m_useCustomViewport)
{
vp = m_customViewport;
}
else
{
vp.Width = static_cast<FLOAT>(bbDesc.Width);
vp.Height = static_cast<FLOAT>(bbDesc.Height);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
}
ctx->RSSetViewports(1, &vp);
ctx->IASetInputLayout(nullptr);
ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
ctx->VSSetShader(m_pGammaVS, nullptr, 0);
ctx->PSSetShader(m_pGammaPS, nullptr, 0);
ctx->PSSetShaderResources(0, 1, &m_pGammaOffscreenSRV);
ctx->PSSetSamplers(0, 1, &m_pGammaSampler);
ctx->PSSetConstantBuffers(0, 1, &m_pGammaCB);
ctx->RSSetState(m_pGammaRastState);
ctx->OMSetDepthStencilState(m_pGammaDepthState, 0);
constexpr float blendFactor[4] = {0, 0, 0, 0};
ctx->OMSetBlendState(m_pGammaBlendState, blendFactor, 0xFFFFFFFF);
ctx->Draw(3, 0);
ID3D11ShaderResourceView* nullSrv = nullptr;
ctx->PSSetShaderResources(0, 1, &nullSrv);
ctx->OMSetRenderTargets(1, &oldRTV, oldDSV);
ctx->RSSetViewports(1, &oldViewport);
if (oldRTV)
oldRTV->Release();
if (oldDSV)
oldDSV->Release();
}
void PostProcesser::SetViewport(const D3D11_VIEWPORT& viewport)
{
m_customViewport = viewport;
m_useCustomViewport = true;
}
void PostProcesser::ResetViewport()
{
m_useCustomViewport = false;
}
void PostProcesser::CopyBackbuffer()
{
if (!m_initialized)
return;
ID3D11DeviceContext* ctx = g_pImmediateContext;
ID3D11Texture2D* pBackBuffer = nullptr;
HRESULT hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&pBackBuffer));
if (FAILED(hr))
return;
ctx->CopyResource(m_pGammaOffscreenTex, pBackBuffer);
pBackBuffer->Release();
}
void PostProcesser::ApplyFromCopied() const
{
if (!m_initialized)
return;
if (m_gamma > 0.99f && m_gamma < 1.01f)
return;
ID3D11DeviceContext* ctx = g_pImmediateContext;
D3D11_MAPPED_SUBRESOURCE mapped;
const D3D11_MAP mapType = m_wineMode ? D3D11_MAP_WRITE_NO_OVERWRITE : D3D11_MAP_WRITE_DISCARD;
const HRESULT hr = ctx->Map(m_pGammaCB, 0, mapType, 0, &mapped);
if (SUCCEEDED(hr))
{
const auto cb = static_cast<GammaCBData*>(mapped.pData);
cb->gamma = m_gamma;
ctx->Unmap(m_pGammaCB, 0);
}
ID3D11RenderTargetView* oldRTV = nullptr;
ID3D11DepthStencilView* oldDSV = nullptr;
ctx->OMGetRenderTargets(1, &oldRTV, &oldDSV);
UINT numViewports = 1;
D3D11_VIEWPORT oldViewport;
ctx->RSGetViewports(&numViewports, &oldViewport);
ID3D11RenderTargetView* bbRTV = g_pRenderTargetView;
ctx->OMSetRenderTargets(1, &bbRTV, nullptr);
D3D11_VIEWPORT vp;
if (m_useCustomViewport)
{
vp = m_customViewport;
}
else
{
D3D11_TEXTURE2D_DESC texDesc;
m_pGammaOffscreenTex->GetDesc(&texDesc);
vp.Width = static_cast<FLOAT>(texDesc.Width);
vp.Height = static_cast<FLOAT>(texDesc.Height);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
}
ctx->RSSetViewports(1, &vp);
ctx->IASetInputLayout(nullptr);
ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
ctx->VSSetShader(m_pGammaVS, nullptr, 0);
ctx->PSSetShader(m_pGammaPS, nullptr, 0);
ctx->PSSetShaderResources(0, 1, &m_pGammaOffscreenSRV);
ctx->PSSetSamplers(0, 1, &m_pGammaSampler);
ctx->PSSetConstantBuffers(0, 1, &m_pGammaCB);
ctx->RSSetState(m_pGammaRastState);
ctx->OMSetDepthStencilState(m_pGammaDepthState, 0);
constexpr float blendFactor[4] = {0, 0, 0, 0};
ctx->OMSetBlendState(m_pGammaBlendState, blendFactor, 0xFFFFFFFF);
ctx->Draw(3, 0);
ID3D11ShaderResourceView* nullSrv = nullptr;
ctx->PSSetShaderResources(0, 1, &nullSrv);
ctx->OMSetRenderTargets(1, &oldRTV, oldDSV);
ctx->RSSetViewports(1, &oldViewport);
if (oldRTV)
oldRTV->Release();
if (oldDSV)
oldDSV->Release();
}
void PostProcesser::Cleanup()
{
if (m_pGammaBlendState)
{
m_pGammaBlendState->Release();
m_pGammaBlendState = nullptr;
}
if (m_pGammaDepthState)
{
m_pGammaDepthState->Release();
m_pGammaDepthState = nullptr;
}
if (m_pGammaRastState)
{
m_pGammaRastState->Release();
m_pGammaRastState = nullptr;
}
if (m_pGammaSampler)
{
m_pGammaSampler->Release();
m_pGammaSampler = nullptr;
}
if (m_pGammaCB)
{
m_pGammaCB->Release();
m_pGammaCB = nullptr;
}
if (m_pGammaPS)
{
m_pGammaPS->Release();
m_pGammaPS = nullptr;
}
if (m_pGammaVS)
{
m_pGammaVS->Release();
m_pGammaVS = nullptr;
}
if (m_pGammaOffscreenRTV)
{
m_pGammaOffscreenRTV->Release();
m_pGammaOffscreenRTV = nullptr;
}
if (m_pGammaOffscreenSRV)
{
m_pGammaOffscreenSRV->Release();
m_pGammaOffscreenSRV = nullptr;
}
if (m_pGammaOffscreenTex)
{
m_pGammaOffscreenTex->Release();
m_pGammaOffscreenTex = nullptr;
}
m_initialized = false;
}

View File

@@ -40,6 +40,7 @@
#include "Resource.h"
#include "..\..\Minecraft.World\compression.h"
#include "..\..\Minecraft.World\OldChunkStorage.h"
#include "Common/PostProcesser.h"
#include "Network\WinsockNetLayer.h"
#include "Xbox/resource.h"
@@ -874,6 +875,8 @@ HRESULT InitDevice()
RenderManager.Initialise(g_pd3dDevice, g_pSwapChain);
PostProcesser::GetInstance().Init();
return S_OK;
}