- Added a framework for drawing the 2D screen elements with Direct3D textures.

They are not actually drawn with it yet, nor is it complete, but it's
  something to start with.
- Split up DCanvas::DrawTexture() into more pieces to make it easier to
  virtualize.
- Removed support for non-32-bit palette textures from D3DFB. What kind of
  card supports pixel shaders but not 32-bit textures?


SVN r605 (trunk)
This commit is contained in:
Randy Heit 2007-12-20 04:36:43 +00:00
commit 111853e623
17 changed files with 1867 additions and 984 deletions

View file

@ -81,6 +81,27 @@ struct FBVERTEX
};
#define D3DFVF_FBVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)
class D3DTex : public FNativeTexture
{
public:
D3DTex(FTexture *tex, IDirect3DDevice9 *D3DDevice);
~D3DTex();
FTexture *GameTex;
IDirect3DTexture9 *Tex;
// Texture coordinates to use for the lower-right corner, should this
// texture prove to be larger than the game texture it represents.
FLOAT TX, TY;
bool IsGray;
bool Create(IDirect3DDevice9 *D3DDevice);
bool Update();
D3DFORMAT GetTexFormat();
FTextureFormat ToTexFmt(D3DFORMAT fmt);
};
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
@ -101,6 +122,7 @@ EXTERN_CVAR (Bool, fullscreen)
EXTERN_CVAR (Float, Gamma)
EXTERN_CVAR (Int, vid_displaybits)
EXTERN_CVAR (Bool, vid_vsync)
EXTERN_CVAR (Float, transsouls)
extern IDirect3D9 *D3D;
@ -108,75 +130,7 @@ extern cycle_t BlitCycles;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
#if 0
// This is the HLSL code:
// Technically, Palette only needs to be a sampler1D, but that
// produces assembly code to copy index.x to index.y, which is
// totally unnecessary.
sampler2D Image : register(s0);
sampler2D Palette : register(s1);
float4 Flash : register(c0);
float4 InvFlash : register(c1);
float4 main (float2 texCoord : TEXCOORD0) : COLOR
{
float4 index = tex2D (Image, texCoord);
float4 rgb = tex2D (Palette, index);
return Flash + rgb * InvFlash;
}
#endif
#if 0
//
// Generated by Microsoft (R) D3DX9 Shader Compiler 9.15.779.0000
//
// fxc paltex.ps /Tps_1_4 /VnPalTexShaderDef /Fh
//
//
// Parameters:
//
// float4 Flash;
// sampler2D Image;
// float4 InvFlash;
// sampler2D Palette;
//
//
// Registers:
//
// Name Reg Size
// ------------ ----- ----
// Flash c0 1
// InvFlash c1 1
// Image s0 1
// Palette s1 1
//
ps_1_4
texld r0, t0
phase
texld r1, r0
mad r0, r1, c1, c0
// approximately 3 instruction slots used (2 texture, 1 arithmetic)
#endif
const DWORD PalTexShaderDef[] =
{
0xffff0104, 0x003bfffe, 0x42415443, 0x0000001c, 0x000000b4, 0xffff0104,
0x00000004, 0x0000001c, 0x00000100, 0x000000ad, 0x0000006c, 0x00000002,
0x00020001, 0x00000074, 0x00000000, 0x00000084, 0x00000003, 0x00000001,
0x0000008c, 0x00000000, 0x0000009c, 0x00010002, 0x00020001, 0x00000074,
0x00000000, 0x000000a5, 0x00010003, 0x00000001, 0x0000008c, 0x00000000,
0x73616c46, 0xabab0068, 0x00030001, 0x00040001, 0x00000001, 0x00000000,
0x67616d49, 0xabab0065, 0x000c0004, 0x00010001, 0x00000001, 0x00000000,
0x46766e49, 0x6873616c, 0x6c615000, 0x65747465, 0x5f737000, 0x00345f31,
0x7263694d, 0x666f736f, 0x52282074, 0x33442029, 0x20395844, 0x64616853,
0x43207265, 0x69706d6f, 0x2072656c, 0x35312e39, 0x3937372e, 0x3030302e,
0xabab0030, 0x00000042, 0x800f0000, 0xb0e40000, 0x0000fffd, 0x00000042,
0x800f0001, 0x80e40000, 0x00000004, 0x800f0000, 0x80e40001, 0xa0e40001,
0xa0e40000, 0x0000ffff
};
#include "fb_d3d9_shaders.h"
// PUBLIC DATA DEFINITIONS -------------------------------------------------
@ -192,6 +146,8 @@ D3DFB::D3DFB (int width, int height, bool fullscreen)
VertexBuffer = NULL;
FBTexture = NULL;
PaletteTexture = NULL;
StencilPaletteTexture = NULL;
ShadedPaletteTexture = NULL;
PalTexShader = NULL;
FBFormat = D3DFMT_UNKNOWN;
PalFormat = D3DFMT_UNKNOWN;
@ -202,10 +158,11 @@ D3DFB::D3DFB (int width, int height, bool fullscreen)
BlendingRect.right = FBWidth;
BlendingRect.bottom = FBHeight;
UseBlendingRect = false;
In2D = 0;
Gamma = 1.0;
memset (FlashConstants, 0, sizeof(FlashConstants));
FlashConstants[1][3] = 1.f; // Always use alpha from palette (which is always 1, so meh)
FlashConstants[0][3] = FlashConstants[0][2] = FlashConstants[0][1] = FlashConstants[0][0] = 0;
FlashConstants[1][3] = FlashConstants[1][2] = FlashConstants[1][1] = FlashConstants[1][0] = 1;
FlashColor = 0;
FlashAmount = 0;
@ -250,7 +207,7 @@ D3DFB::D3DFB (int width, int height, bool fullscreen)
{
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
if (FAILED(D3D->CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &D3DDevice)))
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &d3dpp, &D3DDevice)))
{
D3DDevice = NULL;
}
@ -321,7 +278,14 @@ bool D3DFB::CreateResources ()
{
return false;
}
if (!CreateFBTexture() || !CreatePaletteTexture())
if (FAILED(D3DDevice->CreatePixelShader (PlainShaderDef, &PlainShader)))
{
return false;
}
if (!CreateFBTexture() ||
!CreatePaletteTexture() ||
!CreateStencilPaletteTexture() ||
!CreateShadedPaletteTexture())
{
return false;
}
@ -351,11 +315,26 @@ void D3DFB::ReleaseResources ()
PaletteTexture->Release();
PaletteTexture = NULL;
}
if (StencilPaletteTexture != NULL)
{
StencilPaletteTexture->Release();
StencilPaletteTexture = NULL;
}
if (ShadedPaletteTexture != NULL)
{
ShadedPaletteTexture->Release();
ShadedPaletteTexture = NULL;
}
if (PalTexShader != NULL)
{
PalTexShader->Release();
PalTexShader = NULL;
}
if (PlainShader != NULL)
{
PlainShader->Release();
PlainShader = NULL;
}
}
bool D3DFB::Reset ()
@ -426,11 +405,7 @@ void D3DFB::DoOffByOneCheck ()
{ 255.5f, 0.5f, 0.5f, 1.f, texright, texbot },
{ -0.5f, 0.5f, 0.5f, 1.f, 0.f, texbot }
};
float flash[2][4] =
{
{ 0.f, 0.f, 0.f, 0.f },
{ 1.f, 1.f, 1.f, 1.f }
};
float ps_constants[2][4] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 } };
union
{
@ -445,29 +420,18 @@ void D3DFB::DoOffByOneCheck ()
}
// Create an easily recognizable R3G3B2 palette.
if (PalFormat == D3DFMT_A8R8G8B8)
for (i = 0; i < 256; ++i)
{
for (i = 0; i < 256; ++i)
{
Pal32[i][0] = BYTE(i & 0x03) << 6; // blue
Pal32[i][1] = BYTE(i & 0x1C) << 3; // green
Pal32[i][2] = BYTE(i & 0xE0); // red;
Pal32[i][3] = 255;
}
}
else
{
for (i = 0; i < 256; ++i)
{
Pal16[i] = WORD((i & 0xE0) << 8) | // red
((i & 0x1C) << 6) | // green
((i & 0x03) << 3); // blue
}
Pal32[i][0] = BYTE(i & 0x03) << 6; // blue
Pal32[i][1] = BYTE(i & 0x1C) << 3; // green
Pal32[i][2] = BYTE(i & 0xE0); // red;
Pal32[i][3] = 255;
}
// Upload the palette
if (SUCCEEDED(PaletteTexture->LockRect (0, &lockrect, NULL, 0)))
{
memcpy (lockrect.pBits, Pal32, 256 * ((PalFormat == D3DFMT_A8R8G8B8) ? 4 : 2));
memcpy (lockrect.pBits, Pal32, 256 * 4);
PaletteTexture->UnlockRect (0);
}
else
@ -513,7 +477,7 @@ void D3DFB::DoOffByOneCheck ()
D3DDevice->SetTexture (1, PaletteTexture);
D3DDevice->SetFVF (D3DFVF_FBVERTEX);
D3DDevice->SetPixelShader (PalTexShader);
D3DDevice->SetPixelShaderConstantF (0, flash[0], 2);
D3DDevice->SetPixelShaderConstantF (0, ps_constants[0], 2);
D3DDevice->DrawPrimitiveUP (D3DPT_TRIANGLEFAN, 2, verts, sizeof(FBVERTEX));
D3DDevice->EndScene();
D3DDevice->SetRenderTarget (0, savedrendertarget);
@ -612,18 +576,50 @@ bool D3DFB::CreatePaletteTexture ()
{
if (FAILED(D3DDevice->CreateTexture (256, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &PaletteTexture, NULL)))
{
if (FAILED(D3DDevice->CreateTexture (256, 1, 1, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &PaletteTexture, NULL)))
{
return false;
}
else
{
PalFormat = D3DFMT_R5G6B5;
}
return false;
}
else
PalFormat = D3DFMT_A8R8G8B8;
return true;
}
bool D3DFB::CreateStencilPaletteTexture()
{
// The stencil palette is a special palette where the first entry is zero alpha,
// and everything else is white with full alpha.
if (FAILED(D3DDevice->CreateTexture(256, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &StencilPaletteTexture, NULL)))
{
PalFormat = D3DFMT_A8R8G8B8;
return false;
}
D3DLOCKED_RECT lockrect;
if (SUCCEEDED(StencilPaletteTexture->LockRect(0, &lockrect, NULL, 0)))
{
DWORD *pix = (DWORD *)lockrect.pBits;
*pix = 0;
memset(pix + 1, 0xFF, 255*4);
StencilPaletteTexture->UnlockRect(0);
}
return true;
}
bool D3DFB::CreateShadedPaletteTexture()
{
// The shaded palette is similar to the stencil palette, except each entry's
// alpha is the same as its index.
if (FAILED(D3DDevice->CreateTexture(256, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &ShadedPaletteTexture, NULL)))
{
return false;
}
D3DLOCKED_RECT lockrect;
if (SUCCEEDED(ShadedPaletteTexture->LockRect(0, &lockrect, NULL, 0)))
{
BYTE *pix = (BYTE *)lockrect.pBits;
for (int i = 0; i < 256; ++i)
{
pix[3] = i;
pix[2] = pix[1] = pix[0] = 255;
pix += 4;
}
ShadedPaletteTexture->UnlockRect(0);
}
return true;
}
@ -805,8 +801,22 @@ void D3DFB::Unlock ()
}
}
// When In2D == 0: Copy buffer to screen and present
// When In2D == 1: Copy buffer to screen but do not present
// When In2D == 2: Do nothing
// When In2D == 3: Present and set In2D to 0
void D3DFB::Update ()
{
assert(In2D != 2);
if (In2D == 3)
{
D3DDevice->EndScene();
D3DDevice->Present(NULL, NULL, NULL, NULL);
In2D = false;
return;
}
if (LockCount != 1)
{
//I_FatalError ("Framebuffer must have exactly 1 lock to be updated");
@ -837,6 +847,11 @@ void D3DFB::Update ()
LockCount = 0;
PaintToWindow ();
if (In2D == 0)
{
D3DDevice->EndScene();
D3DDevice->Present(NULL, NULL, NULL, NULL);
}
unclock (BlitCycles);
LOG1 ("cycles = %d\n", BlitCycles);
@ -847,8 +862,6 @@ void D3DFB::Update ()
bool D3DFB::PaintToWindow ()
{
RECT texrect = { 0, 0, Width, Height };
D3DLOCKED_RECT lockrect;
HRESULT hr;
if (LockCount != 0)
@ -864,7 +877,17 @@ bool D3DFB::PaintToWindow ()
return false;
}
}
if ((FBWidth == Width && FBHeight == Height && SUCCEEDED(FBTexture->LockRect (0, &lockrect, NULL, D3DLOCK_DISCARD))) ||
Draw3DPart();
return true;
}
void D3DFB::Draw3DPart()
{
RECT texrect = { 0, 0, Width, Height };
D3DLOCKED_RECT lockrect;
if ((FBWidth == Width && FBHeight == Height &&
SUCCEEDED(FBTexture->LockRect (0, &lockrect, NULL, D3DLOCK_DISCARD))) ||
SUCCEEDED(FBTexture->LockRect (0, &lockrect, &texrect, 0)))
{
if (lockrect.Pitch == Pitch)
@ -898,6 +921,7 @@ bool D3DFB::PaintToWindow ()
D3DDevice->SetFVF (D3DFVF_FBVERTEX);
D3DDevice->SetPixelShader (PalTexShader);
D3DDevice->SetPixelShaderConstantF (0, FlashConstants[0], 2);
D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
if (!UseBlendingRect || FlashConstants[1][0] == 1)
{ // The whole screen as a single quad.
D3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
@ -907,9 +931,7 @@ bool D3DFB::PaintToWindow ()
D3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 24, 2); // middle
// The rest is drawn unblended, so reset the shader constant.
static const float FlashZero[2][4] =
{ { 0, 0, 0, 0 },
{ 1, 1, 1, 0 } };
static const float FlashZero[2][4] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 } };
D3DDevice->SetPixelShaderConstantF (0, FlashZero[0], 2);
D3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 4, 2); // left
@ -917,8 +939,26 @@ bool D3DFB::PaintToWindow ()
D3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 12, 4); // bottom
D3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 18, 4); // top
}
D3DDevice->EndScene();
return SUCCEEDED(D3DDevice->Present(NULL, NULL, NULL, NULL));
if (UseBlendingRect && FlashConstants[1][0] != 1 && RateX)
{
float left = float(RateX) - 0.5f;
float top = (TrueHeight - Height) * 0.5f - 0.5f;
float right = float(Width) - 0.5f;
float bot = float(8) + top;
float texleft = float(RateX) / float(FBWidth);
float texright = float(Width) / float(FBWidth);
float texbot = float(8) / float(FBHeight);
// Redraw the vid_fps part without the flash
FBVERTEX verts[4] =
{
{ left, top, 0.5f, 1.f, texleft, 0.f },
{ right, top, 0.5f, 1.f, texright, 0.f },
{ right, bot, 0.5f, 1.f, texright, texbot },
{ left, bot, 0.5f, 1.f, texleft, texbot }
};
D3DDevice->DrawPrimitiveUP (D3DPT_TRIANGLEFAN, 2, verts, sizeof(FBVERTEX));
}
}
void D3DFB::UploadPalette ()
@ -936,39 +976,21 @@ void D3DFB::UploadPalette ()
// check yet. Otherwise, wait until the next time the palette changes.
NeedPalUpdate = (OffByOneAt < 0);
if (PalFormat == D3DFMT_A8R8G8B8)
BYTE *pix = (BYTE *)lockrect.pBits;
for (i = 0; i < OffByOneAt; ++i, pix += 4)
{
BYTE *pix = (BYTE *)lockrect.pBits;
for (i = 0; i < OffByOneAt; ++i, pix += 4)
{
pix[0] = GammaTable[SourcePalette[i].b];
pix[1] = GammaTable[SourcePalette[i].g];
pix[2] = GammaTable[SourcePalette[i].r];
pix[3] = 255;
}
for (; i < 256; ++i, pix += 4)
{
pix[0] = GammaTable[SourcePalette[i-1].b];
pix[1] = GammaTable[SourcePalette[i-1].g];
pix[2] = GammaTable[SourcePalette[i-1].r];
pix[3] = 255;
}
pix[0] = GammaTable[SourcePalette[i].b];
pix[1] = GammaTable[SourcePalette[i].g];
pix[2] = GammaTable[SourcePalette[i].r];
pix[3] = (i == 0 ? 0 : 255);
// To let masked textures work, the first palette entry's alpha is 0.
}
else
for (; i < 256; ++i, pix += 4)
{
WORD *pix = (WORD *)lockrect.pBits;
for (i = 0; i < OffByOneAt; ++i, ++pix)
{
*pix = ((GammaTable[SourcePalette[i].r] >> 3) << 11) |
((GammaTable[SourcePalette[i].g] >> 2) << 5) |
(GammaTable[SourcePalette[i].b] >> 3);
}
for (; i < 256; ++i, ++pix)
{
*pix = ((GammaTable[SourcePalette[i-1].r] >> 3) << 11) |
((GammaTable[SourcePalette[i-1].g] >> 2) << 5) |
(GammaTable[SourcePalette[i-1].b] >> 3);
}
pix[0] = GammaTable[SourcePalette[i-1].b];
pix[1] = GammaTable[SourcePalette[i-1].g];
pix[2] = GammaTable[SourcePalette[i-1].r];
pix[3] = 255;
}
PaletteTexture->UnlockRect (0);
}
@ -1065,3 +1087,433 @@ void D3DFB::SetBlendingRect(int x1, int y1, int x2, int y2)
}
}
}
/**************************************************************************/
/* 2D Stuff */
/**************************************************************************/
//==========================================================================
//
// D3DTex Constructor
//
//==========================================================================
D3DTex::D3DTex(FTexture *tex, IDirect3DDevice9 *D3DDevice)
{
GameTex = tex;
Tex = NULL;
IsGray = false;
Create(D3DDevice);
}
//==========================================================================
//
// D3DTex Destructor
//
//==========================================================================
D3DTex::~D3DTex()
{
if (Tex != NULL)
{
Tex->Release();
Tex = NULL;
}
}
//==========================================================================
//
// D3DTex :: Create
//
// Creates an IDirect3DTexture9 for the texture and copies the image data
// to it. Note that unlike FTexture, this image is row-major.
//
//==========================================================================
bool D3DTex::Create(IDirect3DDevice9 *D3DDevice)
{
HRESULT hr;
int w, h;
if (Tex != NULL)
{
Tex->Release();
Tex = NULL;
}
w = GameTex->GetWidth();
h = GameTex->GetHeight();
// We don't really want mip-maps, but specifying the flag is the only
// way to use D3DPOOL_MANAGED, according to the docs.
hr = D3DDevice->CreateTexture(w, h, 1, 0,
GetTexFormat(), D3DPOOL_MANAGED, &Tex, NULL);
if (FAILED(hr))
{ // Try again, using power-of-2 sizes
int i;
for (i = 1; i < w; i <<= 1) {} w = i;
for (i = 1; i < h; i <<= 1) {} h = i;
hr = D3DDevice->CreateTexture(w, h, 1, 0,
GetTexFormat(), D3DPOOL_MANAGED, &Tex, NULL);
if (FAILED(hr))
{
return false;
}
}
if (!Update())
{
Tex->Release();
Tex = NULL;
return false;
}
return true;
}
//==========================================================================
//
// D3DTex :: Update
//
// Copies image data from the underlying FTexture to the D3D texture.
//
//==========================================================================
bool D3DTex::Update()
{
D3DSURFACE_DESC desc;
D3DLOCKED_RECT lrect;
RECT rect;
assert(Tex != NULL);
assert(GameTex != NULL);
if (FAILED(Tex->GetLevelDesc(0, &desc)))
{
return false;
}
rect.left = 0;
rect.top = 0;
rect.right = GameTex->GetWidth();
rect.bottom = GameTex->GetHeight();
if (FAILED(Tex->LockRect(0, &lrect, &rect, 0)))
{
return false;
}
GameTex->FillBuffer((BYTE *)lrect.pBits, lrect.Pitch, ToTexFmt(desc.Format));
Tex->UnlockRect(0);
return true;
}
//==========================================================================
//
// D3DTex :: GetTexFormat
//
// Returns the texture format that would best fit this texture.
//
//==========================================================================
D3DFORMAT D3DTex::GetTexFormat()
{
FTextureFormat fmt = GameTex->GetFormat();
IsGray = false;
switch (fmt)
{
case TEX_Pal: return D3DFMT_L8;
case TEX_Gray: IsGray = true; return D3DFMT_L8;
case TEX_RGB: return D3DFMT_A8R8G8B8;
case TEX_DXT1: return D3DFMT_DXT1;
case TEX_DXT2: return D3DFMT_DXT2;
case TEX_DXT3: return D3DFMT_DXT3;
case TEX_DXT4: return D3DFMT_DXT4;
case TEX_DXT5: return D3DFMT_DXT5;
default: I_FatalError ("GameTex->GetFormat() returned invalid format.");
}
return D3DFMT_L8;
}
//==========================================================================
//
// D3DTex :: ToTexFmt
//
// Converts a D3DFORMAT constant to something the FTexture system
// understands.
//
//==========================================================================
FTextureFormat D3DTex::ToTexFmt(D3DFORMAT fmt)
{
switch (fmt)
{
case D3DFMT_L8: return IsGray ? TEX_Gray : TEX_Pal;
case D3DFMT_A8R8G8B8: return TEX_RGB;
case D3DFMT_DXT1: return TEX_DXT1;
case D3DFMT_DXT2: return TEX_DXT2;
case D3DFMT_DXT3: return TEX_DXT3;
case D3DFMT_DXT4: return TEX_DXT4;
case D3DFMT_DXT5: return TEX_DXT5;
default:
assert(0); // LOL WUT?
return TEX_Pal;
}
}
//==========================================================================
//
// D3DFB :: Begin2D
//
// Begins 2D mode drawing operations. In particular, DrawTexture is
// rerouted to use Direct3D instead of the software renderer.
//
//==========================================================================
void D3DFB::Begin2D()
{
if (In2D)
{
return;
}
In2D = 1;
Update();
In2D = 2;
// Set default state for 2D rendering.
float ps_constants[2][4] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 } };
D3DDevice->SetPixelShaderConstantF (0, ps_constants[0], 2);
D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
D3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
// This is set by Update()
//D3DDevice->SetTexture(1, PaletteTexture);
}
void D3DFB::End2D()
{
if (In2D == 2)
{
In2D = 3;
}
}
FNativeTexture *D3DFB::CreateTexture(FTexture *gametex)
{
return new D3DTex(gametex, D3DDevice);
}
//==========================================================================
//
// D3DFB :: DrawTexture
//
// If not in 2D mode, just call the normal software version.
// If in 2D mode, then use Direct3D calls to perform the drawing.
//
//==========================================================================
void STACK_ARGS D3DFB::DrawTexture (FTexture *img, int x, int y, int tags_first, ...)
{
va_list tags;
va_start(tags, tags_first);
if (In2D < 2)
{
DrawTextureV(img, x, y, tags_first, tags);
return;
}
DrawParms parms;
if (!ParseDrawTextureTags(img, x, y, tags_first, tags, &parms))
{
return;
}
D3DTex *tex = static_cast<D3DTex *>(img->GetNative());
if (tex == NULL)
{
assert(tex != NULL);
return;
}
float xscale = float(parms.destwidth) / parms.texwidth / 65536.f;
float yscale = float(parms.destheight) / parms.texheight / 65536.f;
float x0 = float(parms.x) / 65536.f - float(parms.left) * xscale;
float y0 = float(parms.y) / 65536.f - float(parms.top) * yscale;
float x1 = x0 + float(parms.destwidth) / 65536.f;
float y1 = y0 + float(parms.destheight) / 65536.f;
float u0 = 0.f;
float v0 = 0.f;
float u1 = 1.f;
float v1 = 1.f;
float uscale = 1.f / parms.texwidth / u1;
float vscale = 1.f / parms.texheight / v1 / yscale;
if (y0 < parms.uclip)
{
v0 += float(parms.uclip - y0) * vscale;
y0 = float(parms.uclip);
}
if (y1 > parms.dclip)
{
v1 -= float(y1 - parms.dclip) * vscale;
y1 = float(parms.dclip);
}
if (parms.flipX)
{
swap(u0, u1);
}
if (parms.windowleft > 0 || parms.windowright < parms.texwidth)
{
x0 += parms.windowleft * xscale;
u0 += parms.windowleft * uscale;
x1 -= (parms.texwidth - parms.windowright) * xscale;
u1 -= (parms.texwidth - parms.windowright) * uscale;
}
if (x0 < parms.lclip)
{
u0 += float(parms.lclip - x0) * uscale / xscale;
x0 = float(parms.lclip);
}
if (x1 > parms.rclip)
{
u1 -= float(x1 - parms.rclip) * uscale / xscale;
x1 = float(parms.rclip);
}
x0 -= 0.5f;
y0 -= 0.5f;
x1 -= 0.5f;
y1 -= 0.5f;
FBVERTEX verts[4] =
{
{ x0, y0, 0.5f, 1.f, u0, v0 },
{ x1, y0, 0.5f, 1.f, u1, v0 },
{ x1, y1, 0.5f, 1.f, u1, v1 },
{ x0, y1, 0.5f, 1.f, u0, v1 }
};
if (!SetStyle(parms.style, parms.alpha, parms.fillcolor, parms.masked))
{
return;
}
D3DDevice->SetTexture(0, tex->Tex);
D3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &verts, sizeof(FBVERTEX));
}
//==========================================================================
//
// D3DFB :: SetStyle
//
// Patterned after R_SetPatchStyle.
//
//==========================================================================
bool D3DFB::SetStyle(int style, fixed_t alpha_fixed, DWORD color, INTBOOL masked)
{
D3DBLEND fglevel, bglevel;
float alpha;
bool stencilling;
alpha = clamp<fixed_t> (alpha_fixed, 0, FRACUNIT) / 65536.f;
if (style == STYLE_OptFuzzy)
{
style = STYLE_Translucent;
}
else if (style == STYLE_SoulTrans)
{
style = STYLE_Translucent;
alpha = transsouls;
}
// FIXME: STYLE_Fuzzy is not written
if (style == STYLE_Fuzzy)
{
style = STYLE_Translucent;
alpha = transsouls;
}
stencilling = false;
switch (style)
{
// Special modes
case STYLE_Shaded:
if (alpha > 0)
{
float constant[4] = { RPART(color)/255.f,GPART(color)/255.f,BPART(color)/255.f,alpha };
D3DDevice->SetPixelShaderConstantF(1, constant, 1);
D3DDevice->SetTexture(1, ShadedPaletteTexture);
D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
D3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
return true;
}
return false;
// Standard modes
case STYLE_Stencil:
stencilling = true;
case STYLE_Normal:
fglevel = D3DBLEND_SRCALPHA;
bglevel = D3DBLEND_INVSRCALPHA;
alpha = 1;
break;
case STYLE_TranslucentStencil:
stencilling = true;
case STYLE_Translucent:
fglevel = D3DBLEND_SRCALPHA;
bglevel = D3DBLEND_INVSRCALPHA;
if (alpha == 0)
{
return false;
}
if (alpha == 1 && style == STYLE_Translucent)
{
style = STYLE_Normal;
}
break;
case STYLE_Add:
fglevel = D3DBLEND_SRCALPHA;
bglevel = D3DBLEND_ONE;
break;
default:
return false;
}
// Masking can only be turned off for STYLE_Normal, because it requires
// turning off the alpha blend.
if (!masked && style == STYLE_Normal)
{
D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}
else
{
D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_SRCBLEND, fglevel);
D3DDevice->SetRenderState(D3DRS_DESTBLEND, bglevel);
if (!stencilling)
{
float constant[4] = { 1,1,1,alpha };
D3DDevice->SetPixelShaderConstantF(1, constant, 1);
D3DDevice->SetTexture(1, PaletteTexture);
}
else
{
float constant[4] = { RPART(color)/255.f,GPART(color)/255.f,BPART(color)/255.f,alpha };
D3DDevice->SetPixelShaderConstantF(1, constant, 1);
D3DDevice->SetTexture(1, StencilPaletteTexture);
}
}
return true;
}