mirror of
https://github.com/ZDoom/Raze.git
synced 2025-05-30 00:41:24 +00:00
- updated to GZDoom's new texture management system.
This commit is contained in:
parent
7a1fda2cc2
commit
c4017de12f
68 changed files with 2046 additions and 1782 deletions
|
@ -27,8 +27,8 @@
|
|||
#include "hw_material.h"
|
||||
#include "texturemanager.h"
|
||||
#include "c_cvars.h"
|
||||
#include "v_video.h"
|
||||
|
||||
EXTERN_CVAR(Bool, gl_texture_usehires)
|
||||
IHardwareTexture* CreateHardwareTexture();
|
||||
|
||||
//===========================================================================
|
||||
|
@ -37,99 +37,100 @@ IHardwareTexture* CreateHardwareTexture();
|
|||
//
|
||||
//===========================================================================
|
||||
|
||||
FMaterial::FMaterial(FTexture * tx, bool expanded)
|
||||
FMaterial::FMaterial(FGameTexture * tx, int scaleflags)
|
||||
{
|
||||
mShaderIndex = SHADER_Default;
|
||||
sourcetex = tex = tx;
|
||||
sourcetex = tx;
|
||||
auto imgtex = tx->GetTexture();
|
||||
mTextureLayers.Push({ imgtex, scaleflags });
|
||||
|
||||
if (tx->UseType == ETextureType::SWCanvas && static_cast<FWrapperTexture*>(tx)->GetColorFormat() == 0)
|
||||
if (tx->GetUseType() == ETextureType::SWCanvas && static_cast<FWrapperTexture*>(imgtex)->GetColorFormat() == 0)
|
||||
{
|
||||
mShaderIndex = SHADER_Paletted;
|
||||
}
|
||||
else if (tx->isWarped())
|
||||
{
|
||||
mShaderIndex = tx->isWarped(); // This picks SHADER_Warp1 or SHADER_Warp2
|
||||
}
|
||||
else if (tx->isHardwareCanvas())
|
||||
{
|
||||
if (tx->shaderindex >= FIRST_USER_SHADER)
|
||||
if (tx->GetShaderIndex() >= FIRST_USER_SHADER)
|
||||
{
|
||||
mShaderIndex = tx->shaderindex;
|
||||
mShaderIndex = tx->GetShaderIndex();
|
||||
}
|
||||
// no brightmap for cameratexture
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tx->Normal && tx->Specular)
|
||||
if (tx->isWarped())
|
||||
{
|
||||
for (auto &texture : { tx->Normal, tx->Specular })
|
||||
mShaderIndex = tx->isWarped(); // This picks SHADER_Warp1 or SHADER_Warp2
|
||||
}
|
||||
// Note that the material takes no ownership of the texture!
|
||||
else if (tx->Normal.get() && tx->Specular.get())
|
||||
{
|
||||
for (auto &texture : { tx->Normal.get(), tx->Specular.get() })
|
||||
{
|
||||
mTextureLayers.Push(texture);
|
||||
mTextureLayers.Push({ texture, 0 });
|
||||
}
|
||||
mShaderIndex = SHADER_Specular;
|
||||
}
|
||||
else if (tx->Normal && tx->Metallic && tx->Roughness && tx->AmbientOcclusion)
|
||||
else if (tx->Normal.get() && tx->Metallic.get() && tx->Roughness.get() && tx->AmbientOcclusion.get())
|
||||
{
|
||||
for (auto &texture : { tx->Normal, tx->Metallic, tx->Roughness, tx->AmbientOcclusion })
|
||||
for (auto &texture : { tx->Normal.get(), tx->Metallic.get(), tx->Roughness.get(), tx->AmbientOcclusion.get() })
|
||||
{
|
||||
mTextureLayers.Push(texture);
|
||||
mTextureLayers.Push({ texture, 0 });
|
||||
}
|
||||
mShaderIndex = SHADER_PBR;
|
||||
}
|
||||
|
||||
// Note that these layers must present a valid texture even if not used, because empty TMUs in the shader are an undefined condition.
|
||||
tx->CreateDefaultBrightmap();
|
||||
if (tx->Brightmap)
|
||||
auto placeholder = TexMan.GameByIndex(1);
|
||||
if (tx->Brightmap.get())
|
||||
{
|
||||
mTextureLayers.Push(tx->Brightmap);
|
||||
if (mShaderIndex == SHADER_Specular)
|
||||
mShaderIndex = SHADER_SpecularBrightmap;
|
||||
else if (mShaderIndex == SHADER_PBR)
|
||||
mShaderIndex = SHADER_PBRBrightmap;
|
||||
else
|
||||
mShaderIndex = SHADER_Brightmap;
|
||||
mTextureLayers.Push({ tx->Brightmap.get(), scaleflags });
|
||||
mLayerFlags |= TEXF_Brightmap;
|
||||
}
|
||||
else
|
||||
{
|
||||
mTextureLayers.Push({ placeholder->GetTexture(), 0 });
|
||||
}
|
||||
if (tx->Detailmap.get())
|
||||
{
|
||||
mTextureLayers.Push({ tx->Detailmap.get(), 0 });
|
||||
mLayerFlags |= TEXF_Detailmap;
|
||||
}
|
||||
else
|
||||
{
|
||||
mTextureLayers.Push({ placeholder->GetTexture(), 0 });
|
||||
}
|
||||
if (tx->Glowmap.get())
|
||||
{
|
||||
mTextureLayers.Push({ tx->Glowmap.get(), scaleflags });
|
||||
mLayerFlags |= TEXF_Glowmap;
|
||||
}
|
||||
else
|
||||
{
|
||||
mTextureLayers.Push({ placeholder->GetTexture(), 0 });
|
||||
}
|
||||
|
||||
if (tx->shaderindex >= FIRST_USER_SHADER)
|
||||
auto index = tx->GetShaderIndex();
|
||||
if (index >= FIRST_USER_SHADER)
|
||||
{
|
||||
const UserShaderDesc &usershader = usershaders[tx->shaderindex - FIRST_USER_SHADER];
|
||||
const UserShaderDesc &usershader = usershaders[index - FIRST_USER_SHADER];
|
||||
if (usershader.shaderType == mShaderIndex) // Only apply user shader if it matches the expected material
|
||||
{
|
||||
for (auto &texture : tx->CustomShaderTextures)
|
||||
{
|
||||
if (texture == nullptr) continue;
|
||||
mTextureLayers.Push(texture);
|
||||
mTextureLayers.Push({ texture.get(), 0 }); // scalability should be user-definable.
|
||||
}
|
||||
mShaderIndex = tx->shaderindex;
|
||||
mShaderIndex = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
mWidth = tx->GetTexelWidth();
|
||||
mHeight = tx->GetTexelHeight();
|
||||
mLeftOffset = tx->GetLeftOffset(0); // These only get used by decals and decals should not use renderer-specific offsets.
|
||||
mTopOffset = tx->GetTopOffset(0);
|
||||
mRenderWidth = tx->GetScaledWidth();
|
||||
mRenderHeight = tx->GetScaledHeight();
|
||||
mSpriteU[0] = mSpriteV[0] = 0.f;
|
||||
mSpriteU[1] = mSpriteV[1] = 1.f;
|
||||
|
||||
mExpanded = expanded;
|
||||
if (expanded)
|
||||
{
|
||||
int oldwidth = mWidth;
|
||||
int oldheight = mHeight;
|
||||
|
||||
mTrimResult = TrimBorders(trim); // get the trim size before adding the empty frame
|
||||
mWidth += 2;
|
||||
mHeight += 2;
|
||||
mRenderWidth = mRenderWidth * mWidth / oldwidth;
|
||||
mRenderHeight = mRenderHeight * mHeight / oldheight;
|
||||
|
||||
}
|
||||
SetSpriteRect();
|
||||
mScaleFlags = scaleflags;
|
||||
|
||||
mTextureLayers.ShrinkToFit();
|
||||
tx->Material[expanded] = this;
|
||||
if (tx->isHardwareCanvas()) tx->bTranslucent = 0;
|
||||
tx->Material[scaleflags] = this;
|
||||
if (tx->isHardwareCanvas()) tx->SetTranslucent(false);
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
@ -142,193 +143,22 @@ FMaterial::~FMaterial()
|
|||
{
|
||||
}
|
||||
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Set the sprite rectangle
|
||||
//
|
||||
//
|
||||
//===========================================================================
|
||||
|
||||
void FMaterial::SetSpriteRect()
|
||||
IHardwareTexture *FMaterial::GetLayer(int i, int translation, MaterialLayerInfo **pLayer) const
|
||||
{
|
||||
auto leftOffset = tex->GetLeftOffsetHW();
|
||||
auto topOffset = tex->GetTopOffsetHW();
|
||||
|
||||
float fxScale = (float)tex->Scale.X;
|
||||
float fyScale = (float)tex->Scale.Y;
|
||||
|
||||
// mSpriteRect is for positioning the sprite in the scene.
|
||||
mSpriteRect.left = -leftOffset / fxScale;
|
||||
mSpriteRect.top = -topOffset / fyScale;
|
||||
mSpriteRect.width = mWidth / fxScale;
|
||||
mSpriteRect.height = mHeight / fyScale;
|
||||
|
||||
if (mExpanded)
|
||||
{
|
||||
// a little adjustment to make sprites look better with texture filtering:
|
||||
// create a 1 pixel wide empty frame around them.
|
||||
|
||||
int oldwidth = mWidth - 2;
|
||||
int oldheight = mHeight - 2;
|
||||
|
||||
leftOffset += 1;
|
||||
topOffset += 1;
|
||||
|
||||
// Reposition the sprite with the frame considered
|
||||
mSpriteRect.left = -leftOffset / fxScale;
|
||||
mSpriteRect.top = -topOffset / fyScale;
|
||||
mSpriteRect.width = mWidth / fxScale;
|
||||
mSpriteRect.height = mHeight / fyScale;
|
||||
|
||||
if (mTrimResult)
|
||||
{
|
||||
mSpriteRect.left += trim[0] / fxScale;
|
||||
mSpriteRect.top += trim[1] / fyScale;
|
||||
|
||||
mSpriteRect.width -= (oldwidth - trim[2]) / fxScale;
|
||||
mSpriteRect.height -= (oldheight - trim[3]) / fyScale;
|
||||
|
||||
mSpriteU[0] = trim[0] / (float)mWidth;
|
||||
mSpriteV[0] = trim[1] / (float)mHeight;
|
||||
mSpriteU[1] -= (oldwidth - trim[0] - trim[2]) / (float)mWidth;
|
||||
mSpriteV[1] -= (oldheight - trim[1] - trim[3]) / (float)mHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Finds empty space around the texture.
|
||||
// Used for sprites that got placed into a huge empty frame.
|
||||
//
|
||||
//===========================================================================
|
||||
|
||||
bool FMaterial::TrimBorders(uint16_t *rect)
|
||||
{
|
||||
|
||||
auto texbuffer = sourcetex->CreateTexBuffer(0);
|
||||
int w = texbuffer.mWidth;
|
||||
int h = texbuffer.mHeight;
|
||||
auto Buffer = texbuffer.mBuffer;
|
||||
|
||||
if (texbuffer.mBuffer == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (w != mWidth || h != mHeight)
|
||||
{
|
||||
// external Hires replacements cannot be trimmed.
|
||||
return false;
|
||||
}
|
||||
|
||||
int size = w*h;
|
||||
if (size == 1)
|
||||
{
|
||||
// nothing to be done here.
|
||||
rect[0] = 0;
|
||||
rect[1] = 0;
|
||||
rect[2] = 1;
|
||||
rect[3] = 1;
|
||||
return true;
|
||||
}
|
||||
int first, last;
|
||||
|
||||
for(first = 0; first < size; first++)
|
||||
{
|
||||
if (Buffer[first*4+3] != 0) break;
|
||||
}
|
||||
if (first >= size)
|
||||
{
|
||||
// completely empty
|
||||
rect[0] = 0;
|
||||
rect[1] = 0;
|
||||
rect[2] = 1;
|
||||
rect[3] = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
for(last = size-1; last >= first; last--)
|
||||
{
|
||||
if (Buffer[last*4+3] != 0) break;
|
||||
}
|
||||
|
||||
rect[1] = first / w;
|
||||
rect[3] = 1 + last/w - rect[1];
|
||||
|
||||
rect[0] = 0;
|
||||
rect[2] = w;
|
||||
|
||||
unsigned char *bufferoff = Buffer + (rect[1] * w * 4);
|
||||
h = rect[3];
|
||||
|
||||
for(int x = 0; x < w; x++)
|
||||
{
|
||||
for(int y = 0; y < h; y++)
|
||||
{
|
||||
if (bufferoff[(x+y*w)*4+3] != 0) goto outl;
|
||||
}
|
||||
rect[0]++;
|
||||
}
|
||||
outl:
|
||||
rect[2] -= rect[0];
|
||||
|
||||
for(int x = w-1; rect[2] > 1; x--)
|
||||
{
|
||||
for(int y = 0; y < h; y++)
|
||||
{
|
||||
if (bufferoff[(x+y*w)*4+3] != 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
rect[2]--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//===========================================================================
|
||||
|
||||
IHardwareTexture *FMaterial::GetLayer(int i, int translation, FTexture **pLayer)
|
||||
{
|
||||
FTexture *layer = i == 0 ? tex : mTextureLayers[i - 1];
|
||||
if (pLayer) *pLayer = layer;
|
||||
auto &layer = mTextureLayers[i];
|
||||
if (pLayer) *pLayer = &layer;
|
||||
|
||||
if (layer && layer->UseType!=ETextureType::Null)
|
||||
{
|
||||
IHardwareTexture *hwtex = layer->SystemTextures.GetHardwareTexture(translation, mExpanded);
|
||||
if (hwtex == nullptr)
|
||||
{
|
||||
hwtex = CreateHardwareTexture();
|
||||
layer->SystemTextures.AddHardwareTexture(translation, mExpanded, hwtex);
|
||||
}
|
||||
return hwtex;
|
||||
}
|
||||
if (layer.layerTexture) return layer.layerTexture->GetHardwareTexture(translation, layer.scaleFlags);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//===========================================================================
|
||||
|
||||
int FMaterial::GetAreas(FloatRect **pAreas) const
|
||||
{
|
||||
if (mShaderIndex == SHADER_Default) // texture splitting can only be done if there's no attached effects
|
||||
{
|
||||
*pAreas = sourcetex->areas;
|
||||
return sourcetex->areacount;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Gets a texture from the texture manager and checks its validity for
|
||||
|
@ -336,75 +166,27 @@ int FMaterial::GetAreas(FloatRect **pAreas) const
|
|||
//
|
||||
//==========================================================================
|
||||
|
||||
FMaterial * FMaterial::ValidateTexture(FTexture * tex, bool expand, bool create)
|
||||
FMaterial * FMaterial::ValidateTexture(FGameTexture * gtex, int scaleflags, bool create)
|
||||
{
|
||||
again:
|
||||
if (tex && tex->isValid())
|
||||
#if 0
|
||||
if (gtex && gtex->isValid())
|
||||
{
|
||||
if (tex->bNoExpand) expand = false;
|
||||
if (!gtex->expandSprites()) scaleflags &= ~CTF_Expand;
|
||||
|
||||
FMaterial *hwtex = tex->Material[expand];
|
||||
FMaterial *hwtex = gtex->Material[scaleflags];
|
||||
if (hwtex == NULL && create)
|
||||
{
|
||||
if (expand)
|
||||
{
|
||||
if (tex->isWarped() || tex->isHardwareCanvas() || tex->shaderindex >= FIRST_USER_SHADER || (tex->shaderindex >= SHADER_Specular && tex->shaderindex <= SHADER_PBRBrightmap))
|
||||
{
|
||||
tex->bNoExpand = true;
|
||||
goto again;
|
||||
}
|
||||
if (tex->Brightmap != NULL &&
|
||||
(tex->GetTexelWidth() != tex->Brightmap->GetTexelWidth() ||
|
||||
tex->GetTexelHeight() != tex->Brightmap->GetTexelHeight())
|
||||
)
|
||||
{
|
||||
// do not expand if the brightmap's size differs.
|
||||
tex->bNoExpand = true;
|
||||
goto again;
|
||||
}
|
||||
}
|
||||
hwtex = new FMaterial(tex, expand);
|
||||
hwtex = screen->CreateMaterial(gtex, scaleflags);
|
||||
}
|
||||
return hwtex;
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FMaterial * FMaterial::ValidateTexture(FTextureID no, bool expand, bool translate, bool create)
|
||||
{
|
||||
return ValidateTexture(TexMan.GetTexture(no, translate), expand, create);
|
||||
}
|
||||
|
||||
|
||||
void DeleteMaterial(FMaterial* mat)
|
||||
{
|
||||
delete mat;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Make sprite offset adjustment user-configurable per renderer.
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
extern int r_spriteadjustSW, r_spriteadjustHW;
|
||||
|
||||
CUSTOM_CVAR(Int, r_spriteadjust, 2, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
||||
{
|
||||
r_spriteadjustHW = !!(self & 2);
|
||||
r_spriteadjustSW = !!(self & 1);
|
||||
for (int i = 0; i < TexMan.NumTextures(); i++)
|
||||
{
|
||||
auto tex = TexMan.GetTexture(FSetTextureID(i));
|
||||
if (tex->GetTexelLeftOffset(0) != tex->GetTexelLeftOffset(1) || tex->GetTexelTopOffset(0) != tex->GetTexelTopOffset(1))
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
auto mat = tex->GetMaterial(i);
|
||||
if (mat != nullptr) mat->SetSpriteRect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue