2009-07-28 15:32:10 -06:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-07 22:25:12 -07:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "Common.h"
|
2009-09-02 01:25:22 -06:00
|
|
|
#include "FileUtil.h"
|
2008-12-07 22:25:12 -07:00
|
|
|
|
|
|
|
#include "D3DBase.h"
|
|
|
|
|
|
|
|
#include "Statistics.h"
|
|
|
|
#include "Profiler.h"
|
|
|
|
#include "VertexManager.h"
|
|
|
|
#include "OpcodeDecoding.h"
|
|
|
|
#include "IndexGenerator.h"
|
2008-12-25 08:56:36 -07:00
|
|
|
#include "VertexShaderManager.h"
|
2008-12-26 12:39:12 -07:00
|
|
|
#include "VertexShaderCache.h"
|
2008-12-25 08:56:36 -07:00
|
|
|
#include "PixelShaderManager.h"
|
2008-12-26 12:39:12 -07:00
|
|
|
#include "PixelShaderCache.h"
|
2008-12-07 22:25:12 -07:00
|
|
|
#include "Utils.h"
|
2009-02-28 15:10:38 -07:00
|
|
|
#include "NativeVertexFormat.h"
|
|
|
|
#include "NativeVertexWriter.h"
|
2009-03-07 11:05:29 -07:00
|
|
|
#include "TextureCache.h"
|
2009-02-28 15:10:38 -07:00
|
|
|
|
2009-06-22 03:31:30 -06:00
|
|
|
#include "BPStructs.h"
|
2009-02-28 15:10:38 -07:00
|
|
|
#include "XFStructs.h"
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2009-09-02 00:33:41 -06:00
|
|
|
#include "debugger/debugger.h"
|
|
|
|
|
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
using namespace D3D;
|
|
|
|
|
2009-02-28 15:10:38 -07:00
|
|
|
// internal state for loading vertices
|
|
|
|
extern NativeVertexFormat *g_nativeVertexFmt;
|
2008-12-07 22:25:12 -07:00
|
|
|
|
|
|
|
namespace VertexManager
|
|
|
|
{
|
|
|
|
|
2009-10-02 08:03:07 -06:00
|
|
|
static int lastPrimitive;
|
2009-09-19 07:14:55 -06:00
|
|
|
|
2009-10-02 08:03:07 -06:00
|
|
|
static u8 *LocalVBuffer;
|
|
|
|
static u16 *TIBuffer;
|
|
|
|
static u16 *LIBuffer;
|
|
|
|
static u16 *PIBuffer;
|
|
|
|
#define MAXVBUFFERSIZE 0x50000
|
|
|
|
#define MAXIBUFFERSIZE 0xFFFF
|
2009-10-06 08:24:10 -06:00
|
|
|
static bool Flushed=false;
|
2009-09-19 07:14:55 -06:00
|
|
|
|
2008-12-25 14:44:56 -07:00
|
|
|
void CreateDeviceObjects();
|
|
|
|
void DestroyDeviceObjects();
|
2008-12-07 22:25:12 -07:00
|
|
|
|
|
|
|
bool Init()
|
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
LocalVBuffer = new u8[MAXVBUFFERSIZE];
|
|
|
|
TIBuffer = new u16[MAXIBUFFERSIZE];
|
|
|
|
LIBuffer = new u16[MAXIBUFFERSIZE];
|
|
|
|
PIBuffer = new u16[MAXIBUFFERSIZE];
|
|
|
|
s_pCurBufferPointer = LocalVBuffer;
|
2009-10-06 08:24:10 -06:00
|
|
|
Flushed=false;
|
|
|
|
IndexGenerator::Start(TIBuffer,LIBuffer,PIBuffer);
|
2008-12-07 22:25:12 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-02 08:03:07 -06:00
|
|
|
void ResetBuffer()
|
|
|
|
{
|
|
|
|
s_pCurBufferPointer = LocalVBuffer;
|
|
|
|
}
|
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
DestroyDeviceObjects();
|
2009-10-02 08:03:07 -06:00
|
|
|
delete [] LocalVBuffer;
|
|
|
|
delete [] TIBuffer;
|
|
|
|
delete [] LIBuffer;
|
|
|
|
delete [] PIBuffer;
|
|
|
|
ResetBuffer();
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void CreateDeviceObjects()
|
|
|
|
{
|
2009-09-19 04:46:25 -06:00
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyDeviceObjects()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddIndices(int _primitive, int _numVertices)
|
|
|
|
{
|
2009-02-28 15:10:38 -07:00
|
|
|
switch (_primitive)
|
|
|
|
{
|
2009-10-06 08:24:10 -06:00
|
|
|
case GX_DRAW_QUADS: IndexGenerator::AddQuads(_numVertices); return;
|
|
|
|
case GX_DRAW_TRIANGLES: IndexGenerator::AddList(_numVertices); return;
|
|
|
|
case GX_DRAW_TRIANGLE_STRIP: IndexGenerator::AddStrip(_numVertices); return;
|
|
|
|
case GX_DRAW_TRIANGLE_FAN: IndexGenerator::AddFan(_numVertices); return;
|
|
|
|
case GX_DRAW_LINE_STRIP: IndexGenerator::AddLineStrip(_numVertices); return;
|
|
|
|
case GX_DRAW_LINES: IndexGenerator::AddLineList(_numVertices); return;
|
|
|
|
case GX_DRAW_POINTS: IndexGenerator::AddPoints(_numVertices); return;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 08:03:07 -06:00
|
|
|
|
|
|
|
|
2009-02-28 15:10:38 -07:00
|
|
|
int GetRemainingSize()
|
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
return MAXVBUFFERSIZE - (int)(s_pCurBufferPointer - LocalVBuffer);
|
2009-02-28 15:10:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddVertices(int _primitive, int _numVertices)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
if (_numVertices < 0)
|
2008-12-07 22:25:12 -07:00
|
|
|
return;
|
2009-10-02 08:03:07 -06:00
|
|
|
switch (_primitive)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
case GX_DRAW_QUADS:
|
|
|
|
case GX_DRAW_TRIANGLES:
|
|
|
|
case GX_DRAW_TRIANGLE_STRIP:
|
|
|
|
case GX_DRAW_TRIANGLE_FAN:
|
2009-10-06 08:24:10 -06:00
|
|
|
if(MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen() < 2 * _numVertices)
|
2009-10-02 08:03:07 -06:00
|
|
|
Flush();
|
|
|
|
break;
|
|
|
|
case GX_DRAW_LINE_STRIP:
|
|
|
|
case GX_DRAW_LINES:
|
2009-10-06 08:24:10 -06:00
|
|
|
if(MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen() < 2 * _numVertices)
|
2009-10-02 08:03:07 -06:00
|
|
|
Flush();
|
|
|
|
break;
|
|
|
|
case GX_DRAW_POINTS:
|
2009-10-06 08:24:10 -06:00
|
|
|
if(MAXIBUFFERSIZE - IndexGenerator::GetPointindexLen() < _numVertices)
|
2009-10-02 08:03:07 -06:00
|
|
|
Flush();
|
|
|
|
break;
|
|
|
|
default: return;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
2009-10-06 08:24:10 -06:00
|
|
|
if(Flushed)
|
|
|
|
{
|
|
|
|
IndexGenerator::Start(TIBuffer,LIBuffer,PIBuffer);
|
|
|
|
Flushed=false;
|
|
|
|
}
|
2009-10-02 08:03:07 -06:00
|
|
|
lastPrimitive = _primitive;
|
|
|
|
ADDSTAT(stats.thisFrame.numPrims, _numVertices);
|
|
|
|
INCSTAT(stats.thisFrame.numPrimitiveJoins);
|
|
|
|
AddIndices(_primitive, _numVertices);
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2009-10-31 20:03:26 -06:00
|
|
|
inline void DumpBadShaders()
|
|
|
|
{
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
|
|
std::string error_shaders;
|
|
|
|
error_shaders.append(VertexShaderCache::GetCurrentShaderCode());
|
|
|
|
error_shaders.append(PixelShaderCache::GetCurrentShaderCode());
|
|
|
|
char filename[512] = "bad_shader_combo_0.txt";
|
|
|
|
int which = 0;
|
|
|
|
while (File::Exists(filename))
|
|
|
|
{
|
|
|
|
which++;
|
|
|
|
sprintf(filename, "bad_shader_combo_%i.txt", which);
|
|
|
|
}
|
|
|
|
File::WriteStringToFile(true, error_shaders, filename);
|
|
|
|
PanicAlert("DrawIndexedPrimitiveUP failed. Shaders written to %s", filename);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
inline void Draw(int stride)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2009-10-06 08:24:10 -06:00
|
|
|
if(IndexGenerator::GetNumTriangles() > 0)
|
2009-09-19 07:14:55 -06:00
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
if (FAILED(D3D::dev->DrawIndexedPrimitiveUP(
|
2009-10-31 20:03:26 -06:00
|
|
|
D3DPT_TRIANGLELIST,
|
|
|
|
0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumTriangles(),
|
|
|
|
TIBuffer,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
LocalVBuffer,
|
|
|
|
stride)))
|
|
|
|
{
|
|
|
|
DumpBadShaders();
|
|
|
|
}
|
2009-10-02 08:03:07 -06:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
|
|
|
}
|
2009-10-06 08:24:10 -06:00
|
|
|
if(IndexGenerator::GetNumLines() > 0)
|
2009-10-02 08:03:07 -06:00
|
|
|
{
|
|
|
|
if (FAILED(D3D::dev->DrawIndexedPrimitiveUP(
|
2009-10-31 20:03:26 -06:00
|
|
|
D3DPT_LINELIST,
|
|
|
|
0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumLines(),
|
|
|
|
LIBuffer,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
LocalVBuffer,
|
|
|
|
stride)))
|
|
|
|
{
|
|
|
|
DumpBadShaders();
|
|
|
|
}
|
2009-10-02 08:03:07 -06:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
2009-09-19 07:14:55 -06:00
|
|
|
}
|
2009-10-06 08:24:10 -06:00
|
|
|
if(IndexGenerator::GetNumPoints() > 0)
|
2009-09-19 07:14:55 -06:00
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
if (FAILED(D3D::dev->DrawIndexedPrimitiveUP(
|
2009-10-31 20:03:26 -06:00
|
|
|
D3DPT_POINTLIST,
|
|
|
|
0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumPoints(),
|
|
|
|
PIBuffer,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
LocalVBuffer,
|
|
|
|
stride)))
|
|
|
|
{
|
|
|
|
DumpBadShaders();
|
|
|
|
}
|
2009-10-02 08:03:07 -06:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
2009-10-31 20:03:26 -06:00
|
|
|
}
|
2009-09-19 07:14:55 -06:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
|
|
|
void Flush()
|
|
|
|
{
|
2009-10-02 08:03:07 -06:00
|
|
|
if (LocalVBuffer == s_pCurBufferPointer) return;
|
2009-10-06 08:24:10 -06:00
|
|
|
if(Flushed) return;
|
|
|
|
Flushed=true;
|
2009-10-02 08:03:07 -06:00
|
|
|
|
2009-02-28 15:10:38 -07:00
|
|
|
DVSTARTPROFILE();
|
2009-10-02 08:03:07 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
u32 usedtextures = 0;
|
|
|
|
for (u32 i = 0; i < (u32)bpmem.genMode.numtevstages + 1; ++i) {
|
|
|
|
if (bpmem.tevorders[i/2].getEnable(i & 1))
|
|
|
|
usedtextures |= 1 << bpmem.tevorders[i/2].getTexMap(i & 1);
|
|
|
|
}
|
2009-03-07 11:05:29 -07:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
if (bpmem.genMode.numindstages > 0) {
|
|
|
|
for (u32 i = 0; i < (u32)bpmem.genMode.numtevstages + 1; ++i) {
|
|
|
|
if (bpmem.tevind[i].IsActive() && bpmem.tevind[i].bt < bpmem.genMode.numindstages) {
|
|
|
|
usedtextures |= 1 << bpmem.tevindref.getTexMap(bpmem.tevind[i].bt);
|
2009-03-07 11:05:29 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-06 08:24:10 -06:00
|
|
|
}
|
2009-03-07 11:05:29 -07:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
u32 nonpow2tex = 0;
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (usedtextures & (1 << i)) {
|
|
|
|
FourTexUnits &tex = bpmem.tex[i >> 2];
|
|
|
|
TextureCache::TCacheEntry* tentry = TextureCache::Load(i,
|
|
|
|
(tex.texImage3[i&3].image_base/* & 0x1FFFFF*/) << 5,
|
|
|
|
tex.texImage0[i&3].width+1, tex.texImage0[i&3].height+1,
|
|
|
|
tex.texImage0[i&3].format, tex.texTlut[i&3].tmem_offset<<9,
|
|
|
|
tex.texTlut[i&3].tlut_format);
|
|
|
|
|
|
|
|
if (tentry) {
|
|
|
|
PixelShaderManager::SetTexDims(i, tentry->w, tentry->h, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to load texture\n");});
|
|
|
|
ERROR_LOG(VIDEO, "error loading texture");
|
2009-03-07 11:05:29 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-06 08:24:10 -06:00
|
|
|
}
|
|
|
|
PixelShaderManager::SetTexturesUsed(0);
|
2009-07-19 02:17:41 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
// set global constants
|
|
|
|
VertexShaderManager::SetConstants();
|
|
|
|
PixelShaderManager::SetConstants();
|
2009-03-10 08:15:46 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
if (!PixelShaderCache::SetShader(false))
|
|
|
|
{
|
|
|
|
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
|
|
|
|
goto shader_fail;
|
|
|
|
}
|
|
|
|
if (!VertexShaderCache::SetShader(g_nativeVertexFmt->m_components))
|
|
|
|
{
|
|
|
|
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set vertex shader\n");});
|
|
|
|
goto shader_fail;
|
2009-03-10 08:15:46 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
}
|
2009-09-19 21:29:43 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
int stride = g_nativeVertexFmt->GetVertexStride();
|
|
|
|
g_nativeVertexFmt->SetupVertexPointers();
|
2009-09-19 07:14:55 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
Draw(stride);
|
2009-09-01 22:10:40 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
if (bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate)
|
|
|
|
{
|
|
|
|
DWORD write = 0;
|
|
|
|
if (!PixelShaderCache::SetShader(true))
|
|
|
|
{
|
|
|
|
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
|
|
|
|
goto shader_fail;
|
|
|
|
}
|
2009-10-02 08:03:07 -06:00
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
// update alpha only
|
|
|
|
D3D::SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA);
|
|
|
|
D3D::SetRenderState(D3DRS_ALPHABLENDENABLE, false);
|
|
|
|
|
|
|
|
Draw(stride);
|
|
|
|
|
|
|
|
if (bpmem.blendmode.alphaupdate)
|
|
|
|
write = D3DCOLORWRITEENABLE_ALPHA;
|
|
|
|
if (bpmem.blendmode.colorupdate)
|
|
|
|
write |= D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE;
|
|
|
|
if (bpmem.blendmode.blendenable || bpmem.blendmode.subtract)
|
|
|
|
D3D::SetRenderState(D3DRS_ALPHABLENDENABLE, true);
|
|
|
|
|
|
|
|
D3D::SetRenderState(D3DRS_COLORWRITEENABLE, write);
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
2009-10-06 08:24:10 -06:00
|
|
|
DEBUGGER_PAUSE_AT(NEXT_FLUSH,true);
|
|
|
|
|
|
|
|
shader_fail:
|
|
|
|
ResetBuffer();
|
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2009-02-28 15:10:38 -07:00
|
|
|
} // namespace
|