Merge pull request #6854 from unknownbrackets/d3d9

Report shader errors in Direct3D 9, plug some leaks
This commit is contained in:
Henrik Rydgård 2014-09-07 23:38:35 +02:00
commit d98480085a
9 changed files with 123 additions and 36 deletions

View File

@ -21,6 +21,7 @@
#include <map>
#include "helper/global.h"
#include "base/logging.h"
#include "math/lin/matrix4x4.h"
#include "util/text/utf8.h"
@ -42,11 +43,27 @@ PSShader::PSShader(const char *code, bool useHWTransform) : failed_(false), useH
OutputDebugString(ConvertUTF8ToWString(code).c_str());
#endif
bool success;
std::string errorMessage;
success = CompilePixelShader(code, &shader, &constant);
success = CompilePixelShader(code, &shader, &constant, errorMessage);
if (!errorMessage.empty()) {
if (success) {
ERROR_LOG(G3D, "Warnings in shader compilation!");
} else {
ERROR_LOG(G3D, "Error in shader compilation!");
}
ERROR_LOG(G3D, "Messages: %s", errorMessage.c_str());
ERROR_LOG(G3D, "Shader source:\n%s", code);
OutputDebugStringUTF8("Messages:\n");
OutputDebugStringUTF8(errorMessage.c_str());
Reporting::ReportMessage("D3D error in shader compilation: info: %s / code: %s", errorMessage.c_str(), code);
}
if (!success) {
failed_ = true;
if (shader)
shader->Release();
shader = NULL;
} else {
DEBUG_LOG(G3D, "Compiled shader:\n%s\n", (const char *)code);
@ -55,6 +72,8 @@ PSShader::PSShader(const char *code, bool useHWTransform) : failed_(false), useH
PSShader::~PSShader() {
pD3Ddevice->SetPixelShader(NULL);
if (constant)
constant->Release();
if (shader)
shader->Release();
}
@ -65,11 +84,27 @@ VSShader::VSShader(const char *code, bool useHWTransform) : failed_(false), useH
OutputDebugString(ConvertUTF8ToWString(code).c_str());
#endif
bool success;
std::string errorMessage;
success = CompileVertexShader(code, &shader, &constant);
success = CompileVertexShader(code, &shader, &constant, errorMessage);
if (!errorMessage.empty()) {
if (success) {
ERROR_LOG(G3D, "Warnings in shader compilation!");
} else {
ERROR_LOG(G3D, "Error in shader compilation!");
}
ERROR_LOG(G3D, "Messages: %s", errorMessage.c_str());
ERROR_LOG(G3D, "Shader source:\n%s", code);
OutputDebugStringUTF8("Messages:\n");
OutputDebugStringUTF8(errorMessage.c_str());
Reporting::ReportMessage("D3D error in shader compilation: info: %s / code: %s", errorMessage.c_str(), code);
}
if (!success) {
failed_ = true;
if (shader)
shader->Release();
shader = NULL;
} else {
DEBUG_LOG(G3D, "Compiled shader:\n%s\n", (const char *)code);
@ -78,6 +113,8 @@ VSShader::VSShader(const char *code, bool useHWTransform) : failed_(false), useH
VSShader::~VSShader() {
pD3Ddevice->SetVertexShader(NULL);
if (constant)
constant->Release();
if (shader)
shader->Release();
}

View File

@ -177,6 +177,10 @@ TransformDrawEngineDX9::~TransformDrawEngineDX9() {
FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE);
FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE);
for (auto decl = vertexDeclMap_.begin(); decl != vertexDeclMap_.end(); ++decl) {
decl->second->Release();
}
delete [] quadIndices_;
for (auto iter = decoderMap_.begin(); iter != decoderMap_.end(); iter++) {
@ -229,10 +233,6 @@ static void VertexAttribSetup(D3DVERTEXELEMENT9 * VertexElement, u8 fmt, u8 offs
VertexElement->UsageIndex = usage_index;
}
static IDirect3DVertexDeclaration9* pHardwareVertexDecl = NULL;
static std::map<u32, IDirect3DVertexDeclaration9 *> vertexDeclMap;
static D3DVERTEXELEMENT9 VertexElements[8];
// TODO: Use VBO and get rid of the vertexData pointers - with that, we will supply only offsets
static void LogDecFmtForDraw(const DecVtxFormat &decFmt) {
// Vertices Elements orders
@ -269,12 +269,12 @@ static void LogDecFmtForDraw(const DecVtxFormat &decFmt) {
//pD3Ddevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
}
static void SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &decFmt, u32 pspFmt) {
auto vertexDeclCached = vertexDeclMap.find(pspFmt);
IDirect3DVertexDeclaration9 *TransformDrawEngineDX9::SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &decFmt, u32 pspFmt) {
auto vertexDeclCached = vertexDeclMap_.find(pspFmt);
if (vertexDeclCached==vertexDeclMap.end()) {
D3DVERTEXELEMENT9 * VertexElement = &VertexElements[0];
int offset = 0;
if (vertexDeclCached == vertexDeclMap_.end()) {
D3DVERTEXELEMENT9 VertexElements[8];
D3DVERTEXELEMENT9 *VertexElement = &VertexElements[0];
// Vertices Elements orders
// WEIGHT
@ -320,7 +320,8 @@ static void SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &dec
D3DVERTEXELEMENT9 end = D3DDECL_END();
memcpy(VertexElement, &end, sizeof(D3DVERTEXELEMENT9));
// Create declaration
// Create declaration
IDirect3DVertexDeclaration9 *pHardwareVertexDecl;
HRESULT hr = pD3Ddevice->CreateVertexDeclaration( VertexElements, &pHardwareVertexDecl );
if (FAILED(hr)) {
// Log
@ -329,10 +330,11 @@ static void SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &dec
}
// Add it to map
vertexDeclMap[pspFmt] = pHardwareVertexDecl;
vertexDeclMap_[pspFmt] = pHardwareVertexDecl;
return pHardwareVertexDecl;
} else {
// Set it from map
pHardwareVertexDecl = vertexDeclCached->second;
return vertexDeclCached->second;
}
}
@ -1218,7 +1220,7 @@ rotateVBO:
DEBUG_LOG(G3D, "Flush prim %i! %i verts in one go", prim, vertexCount);
SetupDecFmtForDraw(program, dec_->GetDecVtxFmt(), dec_->VertexType());
IDirect3DVertexDeclaration9 *pHardwareVertexDecl = SetupDecFmtForDraw(program, dec_->GetDecVtxFmt(), dec_->VertexType());
if (pHardwareVertexDecl) {
pD3Ddevice->SetVertexDeclaration(pHardwareVertexDecl);

View File

@ -142,6 +142,7 @@ private:
void SoftwareTransformAndDraw(int prim, u8 *decoded, LinkedShaderDX9 *program, int vertexCount, u32 vertexType, void *inds, int indexType, const DecVtxFormat &decVtxFormat, int maxIndex);
void ApplyDrawState(int prim);
bool IsReallyAClear(int numVerts) const;
IDirect3DVertexDeclaration9 *SetupDecFmtForDraw(LinkedShaderDX9 *program, const DecVtxFormat &decFmt, u32 pspFmt);
// Preprocessing for spline/bezier
u32 NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr, VertexDecoderDX9 *dec, int lowerBound, int upperBound, u32 vertType);
@ -184,6 +185,7 @@ private:
TransformedVertex *transformedExpanded;
std::map<u32, VertexArrayInfoDX9 *> vai_;
std::map<u32, IDirect3DVertexDeclaration9 *> vertexDeclMap_;
// Fixed index buffer for easy quad generation from spline/bezier
u16 *quadIndices_;

View File

@ -41,5 +41,6 @@ void * fbo_get_rtt(FBO *fbo);
// To get default depth and rt surface
void fbo_init();
void fbo_shutdown();
};

View File

@ -65,9 +65,7 @@ static const D3DVERTEXELEMENT9 SoftTransVertexElements[] =
LPDIRECT3DVERTEXSHADER9 pFramebufferVertexShader = NULL; // Vertex Shader
LPDIRECT3DPIXELSHADER9 pFramebufferPixelShader = NULL; // Pixel Shader
bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD3DXCONSTANTTABLE * pShaderTable) {
LPD3DXCONSTANTTABLE shaderTable = *pShaderTable;
bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) {
ID3DXBuffer* pShaderCode = NULL;
ID3DXBuffer* pErrorMsg = NULL;
@ -85,10 +83,16 @@ bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD
&pErrorMsg,
pShaderTable);
if( FAILED(hr) )
{
OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
DebugBreak();
if (pErrorMsg) {
errorMessage = (CHAR *)pErrorMsg->GetBufferPointer();
pErrorMsg->Release();
} else {
errorMessage = "";
}
if (FAILED(hr)) {
if (pShaderCode)
pShaderCode->Release();
return false;
}
@ -101,9 +105,7 @@ bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, LPD
return true;
}
bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, LPD3DXCONSTANTTABLE * pShaderTable) {
LPD3DXCONSTANTTABLE shaderTable = *pShaderTable;
bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, LPD3DXCONSTANTTABLE *pShaderTable, std::string &errorMessage) {
ID3DXBuffer* pShaderCode = NULL;
ID3DXBuffer* pErrorMsg = NULL;
@ -121,10 +123,16 @@ bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, L
&pErrorMsg,
pShaderTable);
if( FAILED(hr) )
{
OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
DebugBreak();
if (pErrorMsg) {
errorMessage = (CHAR *)pErrorMsg->GetBufferPointer();
pErrorMsg->Release();
} else {
errorMessage = "";
}
if (FAILED(hr)) {
if (pShaderCode)
pShaderCode->Release();
return false;
}
@ -154,9 +162,12 @@ void CompileShaders() {
&pErrorMsg,
NULL);
if( FAILED(hr) )
{
if (pErrorMsg) {
OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
pErrorMsg->Release();
}
if (FAILED(hr)) {
DebugBreak();
}
@ -165,6 +176,10 @@ void CompileShaders() {
&pFramebufferVertexShader );
pShaderCode->Release();
if (pErrorMsg) {
OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
pErrorMsg->Release();
}
// Compile pixel shader.
hr = dyn_D3DXCompileShader(pscode,
@ -178,9 +193,12 @@ void CompileShaders() {
&pErrorMsg,
NULL);
if( FAILED(hr) )
{
if (pErrorMsg) {
OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
pErrorMsg->Release();
}
if (FAILED(hr)) {
DebugBreak();
}
@ -196,6 +214,20 @@ void CompileShaders() {
pD3Ddevice->CreateVertexDeclaration( SoftTransVertexElements, &pSoftVertexDecl );
}
void DestroyShaders() {
if (pFramebufferVertexShader) {
pFramebufferVertexShader->Release();
}
if (pFramebufferPixelShader) {
pFramebufferPixelShader->Release();
}
if (pFramebufferVertexDecl) {
pFramebufferVertexDecl->Release();
}
if (pSoftVertexDecl) {
pSoftVertexDecl->Release();
}
}
bool useVsync = false;

View File

@ -9,6 +9,7 @@
#define D3DFMT(x) x
#endif
#include <string>
#include <d3d9.h>
struct ID3DXConstantTable;
@ -24,8 +25,9 @@ extern IDirect3DVertexDeclaration9* pFramebufferVertexDecl;
extern IDirect3DVertexDeclaration9* pSoftVertexDecl;
void CompileShaders();
bool CompilePixelShader(const char * code, LPDIRECT3DPIXELSHADER9 * pShader, ID3DXConstantTable **pShaderTable);
bool CompileVertexShader(const char * code, LPDIRECT3DVERTEXSHADER9 * pShader, ID3DXConstantTable **pShaderTable);
bool CompilePixelShader(const char *code, LPDIRECT3DPIXELSHADER9 *pShader, ID3DXConstantTable **pShaderTable, std::string &errorMessage);
bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, ID3DXConstantTable **pShaderTable, std::string &errorMessage);
void DestroyShaders();
void DirectxInit(HWND window);
#define D3DBLEND_UNK D3DSTENCILOP_FORCE_DWORD

View File

@ -116,8 +116,12 @@ void D3D9_Resize(HWND window) {
}
void D3D9_Shutdown() {
DX9::DestroyShaders();
DX9::fbo_shutdown();
device->EndScene();
device->Release();
d3d->Release();
DX9::pD3Ddevice = NULL;
DX9::pD3Ddevice = NULL;
hWnd = NULL;
}

View File

@ -25,6 +25,7 @@
#include "base/logging.h"
#include "thin3d/d3dx9_loader.h"
#include "GPU/Directx9/helper/global.h"
#include "GPU/Directx9/helper/fbo.h"
#include "file/vfs.h"
#include "file/zip_read.h"
@ -80,6 +81,12 @@ bool WindowsHeadlessHostDx9::InitGL(std::string *error_message)
void WindowsHeadlessHostDx9::ShutdownGL()
{
DX9::DestroyShaders();
DX9::fbo_shutdown();
DX9::pD3Ddevice->EndScene();
DX9::pD3Ddevice->Release();
DX9::pD3Ddevice = NULL;
hWnd = NULL;
}
bool WindowsHeadlessHostDx9::ResizeGL()

2
native

@ -1 +1 @@
Subproject commit 69bad8ba74dda4745f4ad5d12fe0cfdbf4230f1a
Subproject commit d5711f360dc8789d65db9f80b9502a9ed26dbe99