2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:25:12 +00: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 07:25:22 +00:00
|
|
|
#include "FileUtil.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
#include "D3DBase.h"
|
2010-06-29 14:40:37 +00:00
|
|
|
#include "Fifo.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "Statistics.h"
|
|
|
|
#include "Profiler.h"
|
|
|
|
#include "VertexManager.h"
|
|
|
|
#include "OpcodeDecoding.h"
|
|
|
|
#include "IndexGenerator.h"
|
2008-12-25 15:56:36 +00:00
|
|
|
#include "VertexShaderManager.h"
|
2008-12-26 19:39:12 +00:00
|
|
|
#include "VertexShaderCache.h"
|
2008-12-25 15:56:36 +00:00
|
|
|
#include "PixelShaderManager.h"
|
2008-12-26 19:39:12 +00:00
|
|
|
#include "PixelShaderCache.h"
|
2009-02-28 22:10:38 +00:00
|
|
|
#include "NativeVertexFormat.h"
|
|
|
|
#include "NativeVertexWriter.h"
|
2009-03-07 18:05:29 +00:00
|
|
|
#include "TextureCache.h"
|
2010-08-04 21:02:32 +00:00
|
|
|
#include "main.h"
|
2009-02-28 22:10:38 +00:00
|
|
|
|
2009-06-22 09:31:30 +00:00
|
|
|
#include "BPStructs.h"
|
2009-02-28 22:10:38 +00:00
|
|
|
#include "XFStructs.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-09-02 06:33:41 +00:00
|
|
|
#include "debugger/debugger.h"
|
|
|
|
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
using namespace D3D;
|
|
|
|
|
2009-02-28 22:10:38 +00:00
|
|
|
// internal state for loading vertices
|
|
|
|
extern NativeVertexFormat *g_nativeVertexFmt;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
namespace VertexManager
|
|
|
|
{
|
|
|
|
|
2009-10-02 14:03:07 +00:00
|
|
|
static int lastPrimitive;
|
2009-09-19 13:14:55 +00:00
|
|
|
|
2010-06-05 01:38:22 +00:00
|
|
|
static u8 *LocalVBuffer;
|
2009-10-02 14:03:07 +00:00
|
|
|
static u16 *TIBuffer;
|
2010-06-05 01:38:22 +00:00
|
|
|
static u16 *LIBuffer;
|
2010-05-04 14:43:30 +00:00
|
|
|
static u16 *PIBuffer;
|
2009-10-02 14:03:07 +00:00
|
|
|
#define MAXVBUFFERSIZE 0x50000
|
|
|
|
#define MAXIBUFFERSIZE 0xFFFF
|
2009-10-06 14:24:10 +00:00
|
|
|
static bool Flushed=false;
|
2009-09-19 13:14:55 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
bool Init()
|
|
|
|
{
|
2009-10-02 14:03:07 +00:00
|
|
|
LocalVBuffer = new u8[MAXVBUFFERSIZE];
|
|
|
|
TIBuffer = new u16[MAXIBUFFERSIZE];
|
|
|
|
LIBuffer = new u16[MAXIBUFFERSIZE];
|
|
|
|
PIBuffer = new u16[MAXIBUFFERSIZE];
|
|
|
|
s_pCurBufferPointer = LocalVBuffer;
|
2010-08-29 23:08:56 +00:00
|
|
|
s_pBaseBufferPointer = LocalVBuffer;
|
2009-10-06 14:24:10 +00:00
|
|
|
Flushed=false;
|
2010-06-05 01:38:22 +00:00
|
|
|
IndexGenerator::Start(TIBuffer,LIBuffer,PIBuffer);
|
2008-12-08 05:25:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-02 14:03:07 +00:00
|
|
|
void ResetBuffer()
|
|
|
|
{
|
2010-06-05 01:38:22 +00:00
|
|
|
s_pCurBufferPointer = LocalVBuffer;
|
2009-10-02 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
2009-10-02 14:03:07 +00:00
|
|
|
delete [] LocalVBuffer;
|
|
|
|
delete [] TIBuffer;
|
|
|
|
delete [] LIBuffer;
|
|
|
|
delete [] PIBuffer;
|
|
|
|
ResetBuffer();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddIndices(int _primitive, int _numVertices)
|
|
|
|
{
|
2009-02-28 22:10:38 +00:00
|
|
|
switch (_primitive)
|
|
|
|
{
|
2010-06-05 01:38:22 +00:00
|
|
|
case GX_DRAW_QUADS: IndexGenerator::AddQuads(_numVertices); break;
|
2010-05-22 21:58:43 +00:00
|
|
|
case GX_DRAW_TRIANGLES: IndexGenerator::AddList(_numVertices); break;
|
2010-01-23 12:56:07 +00:00
|
|
|
case GX_DRAW_TRIANGLE_STRIP: IndexGenerator::AddStrip(_numVertices); break;
|
|
|
|
case GX_DRAW_TRIANGLE_FAN: IndexGenerator::AddFan(_numVertices); break;
|
|
|
|
case GX_DRAW_LINE_STRIP: IndexGenerator::AddLineStrip(_numVertices); break;
|
2010-06-05 01:38:22 +00:00
|
|
|
case GX_DRAW_LINES: IndexGenerator::AddLineList(_numVertices); break;
|
2010-01-23 12:56:07 +00:00
|
|
|
case GX_DRAW_POINTS: IndexGenerator::AddPoints(_numVertices); break;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 14:03:07 +00:00
|
|
|
|
|
|
|
|
2009-02-28 22:10:38 +00:00
|
|
|
int GetRemainingSize()
|
|
|
|
{
|
2010-06-16 10:12:57 +00:00
|
|
|
return MAXVBUFFERSIZE - (int)(s_pCurBufferPointer - LocalVBuffer);
|
2009-02-28 22:10:38 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 14:43:30 +00:00
|
|
|
int GetRemainingVertices(int primitive)
|
|
|
|
{
|
|
|
|
switch (primitive)
|
|
|
|
{
|
2010-06-05 01:38:22 +00:00
|
|
|
case GX_DRAW_QUADS:
|
|
|
|
case GX_DRAW_TRIANGLES:
|
|
|
|
case GX_DRAW_TRIANGLE_STRIP:
|
|
|
|
case GX_DRAW_TRIANGLE_FAN:
|
2010-05-04 14:43:30 +00:00
|
|
|
return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen())/3;
|
|
|
|
case GX_DRAW_LINE_STRIP:
|
|
|
|
case GX_DRAW_LINES:
|
|
|
|
return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen())/2;
|
|
|
|
case GX_DRAW_POINTS:
|
|
|
|
return (MAXIBUFFERSIZE - IndexGenerator::GetPointindexLen());
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 22:10:38 +00:00
|
|
|
void AddVertices(int _primitive, int _numVertices)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-05-04 14:43:30 +00:00
|
|
|
if (_numVertices <= 0)
|
2008-12-08 05:25:12 +00:00
|
|
|
return;
|
2009-10-02 14:03:07 +00:00
|
|
|
switch (_primitive)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-06-05 01:38:22 +00:00
|
|
|
case GX_DRAW_QUADS:
|
|
|
|
case GX_DRAW_TRIANGLES:
|
|
|
|
case GX_DRAW_TRIANGLE_STRIP:
|
|
|
|
case GX_DRAW_TRIANGLE_FAN:
|
2010-05-04 14:43:30 +00:00
|
|
|
if(MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen() < 3 * _numVertices)
|
2009-10-02 14:03:07 +00:00
|
|
|
Flush();
|
|
|
|
break;
|
|
|
|
case GX_DRAW_LINE_STRIP:
|
|
|
|
case GX_DRAW_LINES:
|
2009-10-06 14:24:10 +00:00
|
|
|
if(MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen() < 2 * _numVertices)
|
2009-10-02 14:03:07 +00:00
|
|
|
Flush();
|
|
|
|
break;
|
|
|
|
case GX_DRAW_POINTS:
|
2009-10-06 14:24:10 +00:00
|
|
|
if(MAXIBUFFERSIZE - IndexGenerator::GetPointindexLen() < _numVertices)
|
2009-10-02 14:03:07 +00:00
|
|
|
Flush();
|
|
|
|
break;
|
|
|
|
default: return;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2009-10-06 14:24:10 +00:00
|
|
|
if(Flushed)
|
|
|
|
{
|
|
|
|
IndexGenerator::Start(TIBuffer,LIBuffer,PIBuffer);
|
|
|
|
Flushed=false;
|
|
|
|
}
|
2010-06-05 01:38:22 +00:00
|
|
|
lastPrimitive = _primitive;
|
2009-10-02 14:03:07 +00:00
|
|
|
ADDSTAT(stats.thisFrame.numPrims, _numVertices);
|
|
|
|
INCSTAT(stats.thisFrame.numPrimitiveJoins);
|
|
|
|
AddIndices(_primitive, _numVertices);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-11-01 02:03:26 +00: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 14:24:10 +00:00
|
|
|
inline void Draw(int stride)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-10-06 14:24:10 +00:00
|
|
|
if(IndexGenerator::GetNumTriangles() > 0)
|
2009-09-19 13:14:55 +00:00
|
|
|
{
|
2009-10-02 14:03:07 +00:00
|
|
|
if (FAILED(D3D::dev->DrawIndexedPrimitiveUP(
|
2009-11-01 02:03:26 +00:00
|
|
|
D3DPT_TRIANGLELIST,
|
2010-06-05 01:38:22 +00:00
|
|
|
0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumTriangles(),
|
|
|
|
TIBuffer,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
LocalVBuffer,
|
|
|
|
stride)))
|
2009-11-01 02:03:26 +00:00
|
|
|
{
|
|
|
|
DumpBadShaders();
|
|
|
|
}
|
2009-10-02 14:03:07 +00:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
|
|
|
}
|
2009-10-06 14:24:10 +00:00
|
|
|
if(IndexGenerator::GetNumLines() > 0)
|
2009-10-02 14:03:07 +00:00
|
|
|
{
|
|
|
|
if (FAILED(D3D::dev->DrawIndexedPrimitiveUP(
|
2009-11-01 02:03:26 +00:00
|
|
|
D3DPT_LINELIST,
|
2010-06-05 01:38:22 +00:00
|
|
|
0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumLines(),
|
|
|
|
LIBuffer,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
LocalVBuffer,
|
|
|
|
stride)))
|
2009-11-01 02:03:26 +00:00
|
|
|
{
|
|
|
|
DumpBadShaders();
|
|
|
|
}
|
2009-10-02 14:03:07 +00:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
2009-09-19 13:14:55 +00:00
|
|
|
}
|
2009-10-06 14:24:10 +00:00
|
|
|
if(IndexGenerator::GetNumPoints() > 0)
|
2009-09-19 13:14:55 +00:00
|
|
|
{
|
2009-10-02 14:03:07 +00:00
|
|
|
if (FAILED(D3D::dev->DrawIndexedPrimitiveUP(
|
2009-11-01 02:03:26 +00:00
|
|
|
D3DPT_POINTLIST,
|
2010-06-05 01:38:22 +00:00
|
|
|
0, IndexGenerator::GetNumVerts(), IndexGenerator::GetNumPoints(),
|
|
|
|
PIBuffer,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
LocalVBuffer,
|
|
|
|
stride)))
|
2009-11-01 02:03:26 +00:00
|
|
|
{
|
|
|
|
DumpBadShaders();
|
|
|
|
}
|
2009-10-02 14:03:07 +00:00
|
|
|
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
|
2009-11-01 02:03:26 +00:00
|
|
|
}
|
2009-09-19 13:14:55 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
void Flush()
|
|
|
|
{
|
2010-06-05 01:38:22 +00:00
|
|
|
if (LocalVBuffer == s_pCurBufferPointer) return;
|
2009-10-06 14:24:10 +00:00
|
|
|
if(Flushed) return;
|
|
|
|
Flushed=true;
|
2010-06-29 14:40:37 +00:00
|
|
|
VideoFifo_CheckEFBAccess();
|
2009-02-28 22:10:38 +00:00
|
|
|
DVSTARTPROFILE();
|
2009-10-02 14:03:07 +00:00
|
|
|
|
2009-10-06 14:24:10 +00: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 18:05:29 +00:00
|
|
|
|
2009-10-06 14:24:10 +00: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 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-06 14:24:10 +00:00
|
|
|
}
|
2009-03-07 18:05:29 +00:00
|
|
|
|
2009-10-06 14:24:10 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (usedtextures & (1 << i)) {
|
2010-01-21 22:32:24 +00:00
|
|
|
Renderer::SetSamplerState(i & 3, i >> 2);
|
2009-10-06 14:24:10 +00:00
|
|
|
FourTexUnits &tex = bpmem.tex[i >> 2];
|
|
|
|
TextureCache::TCacheEntry* tentry = TextureCache::Load(i,
|
2010-06-05 01:38:22 +00:00
|
|
|
(tex.texImage3[i&3].image_base/* & 0x1FFFFF*/) << 5,
|
|
|
|
tex.texImage0[i&3].width + 1, tex.texImage0[i&3].height + 1,
|
2009-10-06 14:24:10 +00:00
|
|
|
tex.texImage0[i&3].format, tex.texTlut[i&3].tmem_offset<<9,
|
2010-04-14 13:57:16 +00:00
|
|
|
tex.texTlut[i&3].tlut_format,
|
2010-06-05 01:38:22 +00:00
|
|
|
(tex.texMode0[i&3].min_filter & 3) && (tex.texMode0[i&3].min_filter != 8) && g_ActiveConfig.bUseNativeMips,
|
2010-06-06 14:44:35 +00:00
|
|
|
(tex.texMode1[i&3].max_lod >> 4));
|
2009-10-06 14:24:10 +00:00
|
|
|
|
|
|
|
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 18:05:29 +00:00
|
|
|
}
|
2009-12-07 18:48:31 +00:00
|
|
|
|
2009-03-07 18:05:29 +00:00
|
|
|
}
|
2009-10-06 14:24:10 +00:00
|
|
|
}
|
2009-07-19 08:17:41 +00:00
|
|
|
|
2009-10-06 14:24:10 +00:00
|
|
|
// set global constants
|
|
|
|
VertexShaderManager::SetConstants();
|
|
|
|
PixelShaderManager::SetConstants();
|
2009-03-10 14:15:46 +00:00
|
|
|
|
2010-09-23 02:17:48 +00:00
|
|
|
if (!PixelShaderCache::SetShader(false,g_nativeVertexFmt->m_components))
|
2009-10-06 14:24:10 +00:00
|
|
|
{
|
|
|
|
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 14:15:46 +00:00
|
|
|
|
2009-10-06 14:24:10 +00:00
|
|
|
}
|
2009-09-20 03:29:43 +00:00
|
|
|
|
2009-10-06 14:24:10 +00:00
|
|
|
int stride = g_nativeVertexFmt->GetVertexStride();
|
|
|
|
g_nativeVertexFmt->SetupVertexPointers();
|
2010-01-12 03:39:14 +00:00
|
|
|
|
2009-10-06 14:24:10 +00:00
|
|
|
Draw(stride);
|
2009-09-02 04:10:40 +00:00
|
|
|
|
2010-06-05 01:38:22 +00:00
|
|
|
if (bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate)
|
2009-10-06 14:24:10 +00:00
|
|
|
{
|
2010-06-12 15:38:42 +00:00
|
|
|
DWORD write = 0;
|
2010-09-23 02:17:48 +00:00
|
|
|
if (!PixelShaderCache::SetShader(true,g_nativeVertexFmt->m_components))
|
2010-06-12 15:38:42 +00:00
|
|
|
{
|
|
|
|
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
|
|
|
|
goto shader_fail;
|
|
|
|
}
|
|
|
|
// update alpha only
|
ok big changes here:
in videocommon little fix for the alpha test values, return to the original values as they are more accurate.
in D3D:
huge change in state management, now all the state management is centralized and redundant state changes are eliminated.
Fixed the overlapped viewport error in non ati cards:
the error was caused by this: when a viewport is defined larger than the current rendertarget, an error is thrown and the last valid viewport is used, this is the reference behavior, in ati cards if a larger viewport is defined, no eror is returned, the rendering is valid and is rendered using the projection defined by the viewport but limited to the rendertarget are, exactly like opengl or the GC hardware.
to solve this in reference drivers defined a large rendertarget (2x the size of the original) and proceed to render in a centered quad insithe the larger rendertarget, in this way larger viewports always falls inside a valid rendertarget size, the drawback of this is the waste of resources. it can be dynamized, depending or games or changed at runtime when a oversized viewport is detected, but i live that to future commits.
please test this and let me know the results.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4841 8ced0084-cf51-0410-be5f-012b33b47a6e
2010-01-15 15:52:08 +00:00
|
|
|
D3D::ChangeRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA);
|
|
|
|
D3D::ChangeRenderState(D3DRS_ALPHABLENDENABLE, false);
|
2010-06-12 15:38:42 +00:00
|
|
|
|
|
|
|
Draw(stride);
|
2010-01-12 03:39:14 +00:00
|
|
|
D3D::RefreshRenderState(D3DRS_COLORWRITEENABLE);
|
2010-06-05 01:38:22 +00:00
|
|
|
D3D::RefreshRenderState(D3DRS_ALPHABLENDENABLE);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2009-10-06 14:24:10 +00:00
|
|
|
DEBUGGER_PAUSE_AT(NEXT_FLUSH,true);
|
|
|
|
|
|
|
|
shader_fail:
|
|
|
|
ResetBuffer();
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 22:10:38 +00:00
|
|
|
} // namespace
|