Skip to content
Permalink
Browse files
Merge pull request #6042 from stenzek/videocommon-pipelines
VideoCommon pipelines ("Abstract Pipeline")
  • Loading branch information
Helios747 committed Feb 23, 2018
2 parents a62343b + fec6bb4 commit b66f96c
Show file tree
Hide file tree
Showing 61 changed files with 2,085 additions and 299 deletions.
@@ -10,8 +10,6 @@
#include "Common/Logging/Log.h"

std::unique_ptr<cInterfaceBase> GLInterface;
static GLuint attributelessVAO = 0;
static GLuint attributelessVBO = 0;

void InitInterface()
{
@@ -29,7 +27,7 @@ GLuint OpenGL_CompileProgram(const std::string& vertexShader, const std::string&
const char* shader = vertexShader.c_str();
glShaderSource(vertexShaderID, 1, &shader, nullptr);
glCompileShader(vertexShaderID);
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
#if defined(_DEBUG) || defined(DEBUGFAST)
GLint Result = GL_FALSE;
char stringBuffer[1024];
GLsizei stringBufferUsage = 0;
@@ -56,7 +54,7 @@ GLuint OpenGL_CompileProgram(const std::string& vertexShader, const std::string&
shader = fragmentShader.c_str();
glShaderSource(fragmentShaderID, 1, &shader, nullptr);
glCompileShader(fragmentShaderID);
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
#if defined(_DEBUG) || defined(DEBUGFAST)
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderInfoLog(fragmentShaderID, 1024, &stringBufferUsage, stringBuffer);

@@ -80,7 +78,7 @@ GLuint OpenGL_CompileProgram(const std::string& vertexShader, const std::string&
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
#if defined(_DEBUG) || defined(DEBUGFAST)
glGetProgramiv(programID, GL_LINK_STATUS, &Result);
glGetProgramInfoLog(programID, 1024, &stringBufferUsage, stringBuffer);

@@ -102,45 +100,3 @@ GLuint OpenGL_CompileProgram(const std::string& vertexShader, const std::string&

return programID;
}

void OpenGL_CreateAttributelessVAO()
{
glGenVertexArrays(1, &attributelessVAO);
_dbg_assert_msg_(VIDEO, attributelessVAO != 0,
"Attributeless VAO should have been created successfully.")

// In a compatibility context, we require a valid, bound array buffer.
glGenBuffers(1, &attributelessVBO);
_dbg_assert_msg_(VIDEO, attributelessVBO != 0,
"Attributeless VBO should have been created successfully.")

// Initialize the buffer with nothing. 16 floats is an arbitrary size that may work around
// driver issues.
glBindBuffer(GL_ARRAY_BUFFER, attributelessVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16, nullptr, GL_STATIC_DRAW);

// We must also define vertex attribute 0.
glBindVertexArray(attributelessVAO);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
}

void OpenGL_BindAttributelessVAO()
{
_dbg_assert_msg_(VIDEO, attributelessVAO != 0,
"Attributeless VAO should have already been created.")
glBindVertexArray(attributelessVAO);
}

void OpenGL_DeleteAttributelessVAO()
{
_dbg_assert_msg_(VIDEO, attributelessVAO != 0,
"Attributeless VAO should have already been created.") if (attributelessVAO != 0)
{
glDeleteVertexArrays(1, &attributelessVAO);
glDeleteBuffers(1, &attributelessVBO);

attributelessVAO = 0;
attributelessVBO = 0;
}
}
@@ -17,19 +17,3 @@ void InitInterface();

// Helpers
GLuint OpenGL_CompileProgram(const std::string& vertexShader, const std::string& fragmentShader);

// Creates and deletes a VAO and VBO suitable for attributeless rendering.
// Called by the Renderer.
void OpenGL_CreateAttributelessVAO();
void OpenGL_DeleteAttributelessVAO();

// Binds the VAO suitable for attributeless rendering.
void OpenGL_BindAttributelessVAO();

// this should be removed in future, but as long as glsl is unstable, we should really read this
// messages
#if defined(_DEBUG) || defined(DEBUGFAST)
#define DEBUG_GLSL 1
#else
#define DEBUG_GLSL 0
#endif
@@ -13,6 +13,10 @@ set(SRCS
D3DTexture.h
D3DUtil.cpp
D3DUtil.h
DXPipeline.cpp
DXPipeline.h
DXShader.cpp
DXShader.h
DXTexture.cpp
DXTexture.h
FramebufferManager.cpp
@@ -43,6 +43,8 @@
<ClCompile Include="D3DState.cpp" />
<ClCompile Include="D3DTexture.cpp" />
<ClCompile Include="D3DUtil.cpp" />
<ClCompile Include="DXPipeline.cpp" />
<ClCompile Include="DXShader.cpp" />
<ClCompile Include="DXTexture.cpp" />
<ClCompile Include="FramebufferManager.cpp" />
<ClCompile Include="GeometryShaderCache.cpp" />
@@ -64,6 +66,8 @@
<ClInclude Include="D3DState.h" />
<ClInclude Include="D3DTexture.h" />
<ClInclude Include="D3DUtil.h" />
<ClInclude Include="DXPipeline.h" />
<ClInclude Include="DXShader.h" />
<ClInclude Include="DXTexture.h" />
<ClInclude Include="FramebufferManager.h" />
<ClInclude Include="GeometryShaderCache.h" />
@@ -64,6 +64,12 @@
<ClCompile Include="DXTexture.cpp">
<Filter>Render</Filter>
</ClCompile>
<ClCompile Include="DXShader.cpp">
<Filter>Render</Filter>
</ClCompile>
<ClCompile Include="DXPipeline.cpp">
<Filter>Render</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="D3DBase.h">
@@ -118,5 +124,11 @@
<ClInclude Include="DXTexture.h">
<Filter>Render</Filter>
</ClInclude>
<ClInclude Include="DXShader.h">
<Filter>Render</Filter>
</ClInclude>
<ClInclude Include="DXPipeline.h">
<Filter>Render</Filter>
</ClInclude>
</ItemGroup>
</Project>
@@ -511,6 +511,16 @@ const char* PixelShaderVersionString()
return "ps_4_0";
}

const char* ComputeShaderVersionString()
{
if (s_featlevel == D3D_FEATURE_LEVEL_11_0)
return "cs_5_0";
else if (s_featlevel == D3D_FEATURE_LEVEL_10_1)
return "cs_4_1";
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/
return "cs_4_0";
}

D3DTexture2D* GetBackBuffer()
{
return s_backbuf;
@@ -69,6 +69,7 @@ D3DTexture2D* GetBackBuffer();
const char* PixelShaderVersionString();
const char* GeometryShaderVersionString();
const char* VertexShaderVersionString();
const char* ComputeShaderVersionString();
bool BGRATexturesSupported();
bool AllowTearingSupported();

@@ -188,6 +188,66 @@ bool CompilePixelShader(const std::string& code, D3DBlob** blob, const D3D_SHADE
return SUCCEEDED(hr);
}

// bytecode->shader
ID3D11ComputeShader* CreateComputeShaderFromByteCode(const void* bytecode, size_t len)
{
ID3D11ComputeShader* shader;
HRESULT hr = D3D::device->CreateComputeShader(bytecode, len, nullptr, &shader);
if (FAILED(hr))
{
PanicAlert("CreateComputeShaderFromByteCode failed at %s %d\n", __FILE__, __LINE__);
return nullptr;
}
return shader;
}

// code->bytecode
bool CompileComputeShader(const std::string& code, D3DBlob** blob, const D3D_SHADER_MACRO* pDefines)
{
ID3D10Blob* shaderBuffer = nullptr;
ID3D10Blob* errorBuffer = nullptr;

#if defined(_DEBUG) || defined(DEBUGFAST)
UINT flags = D3D10_SHADER_DEBUG;
#else
UINT flags = D3D10_SHADER_OPTIMIZATION_LEVEL3;
#endif
HRESULT hr =
PD3DCompile(code.c_str(), code.length(), nullptr, pDefines, nullptr, "main",
D3D::ComputeShaderVersionString(), flags, 0, &shaderBuffer, &errorBuffer);

if (errorBuffer)
{
INFO_LOG(VIDEO, "Compute shader compiler messages:\n%s",
(const char*)errorBuffer->GetBufferPointer());
}

if (FAILED(hr))
{
static int num_failures = 0;
std::string filename = StringFromFormat("%sbad_cs_%04i.txt",
File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file;
File::OpenFStream(file, filename, std::ios_base::out);
file << code;
file.close();

PanicAlert("Failed to compile compute shader: %s\nDebug info (%s):\n%s", filename.c_str(),
D3D::ComputeShaderVersionString(),
reinterpret_cast<const char*>(errorBuffer->GetBufferPointer()));

*blob = nullptr;
errorBuffer->Release();
}
else
{
*blob = new D3DBlob(shaderBuffer);
shaderBuffer->Release();
}

return SUCCEEDED(hr);
}

ID3D11VertexShader* CompileAndCreateVertexShader(const std::string& code)
{
D3DBlob* blob = nullptr;
@@ -226,6 +286,19 @@ ID3D11PixelShader* CompileAndCreatePixelShader(const std::string& code)
return nullptr;
}

ID3D11ComputeShader* CompileAndCreateComputeShader(const std::string& code)
{
D3DBlob* blob = nullptr;
CompileComputeShader(code, &blob);
if (blob)
{
ID3D11ComputeShader* shader = CreateComputeShaderFromByteCode(blob);
blob->Release();
return shader;
}
return nullptr;
}

} // namespace

} // namespace DX11
@@ -19,19 +19,23 @@ namespace D3D
ID3D11VertexShader* CreateVertexShaderFromByteCode(const void* bytecode, size_t len);
ID3D11GeometryShader* CreateGeometryShaderFromByteCode(const void* bytecode, size_t len);
ID3D11PixelShader* CreatePixelShaderFromByteCode(const void* bytecode, size_t len);
ID3D11ComputeShader* CreateComputeShaderFromByteCode(const void* bytecode, size_t len);

// The returned bytecode buffers should be Release()d.
bool CompileVertexShader(const std::string& code, D3DBlob** blob);
bool CompileGeometryShader(const std::string& code, D3DBlob** blob,
const D3D_SHADER_MACRO* pDefines = nullptr);
bool CompilePixelShader(const std::string& code, D3DBlob** blob,
const D3D_SHADER_MACRO* pDefines = nullptr);
bool CompileComputeShader(const std::string& code, D3DBlob** blob,
const D3D_SHADER_MACRO* pDefines = nullptr);

// Utility functions
ID3D11VertexShader* CompileAndCreateVertexShader(const std::string& code);
ID3D11GeometryShader* CompileAndCreateGeometryShader(const std::string& code,
const D3D_SHADER_MACRO* pDefines = nullptr);
ID3D11PixelShader* CompileAndCreatePixelShader(const std::string& code);
ID3D11ComputeShader* CompileAndCreateComputeShader(const std::string& code);

inline ID3D11VertexShader* CreateVertexShaderFromByteCode(D3DBlob* bytecode)
{
@@ -45,21 +49,29 @@ inline ID3D11PixelShader* CreatePixelShaderFromByteCode(D3DBlob* bytecode)
{
return CreatePixelShaderFromByteCode(bytecode->Data(), bytecode->Size());
}
inline ID3D11ComputeShader* CreateComputeShaderFromByteCode(D3DBlob* bytecode)
{
return CreateComputeShaderFromByteCode(bytecode->Data(), bytecode->Size());
}

inline ID3D11VertexShader* CompileAndCreateVertexShader(D3DBlob* code)
{
return CompileAndCreateVertexShader((const char*)code->Data());
return CompileAndCreateVertexShader(reinterpret_cast<const char*>(code->Data()));
}

inline ID3D11GeometryShader*
CompileAndCreateGeometryShader(D3DBlob* code, const D3D_SHADER_MACRO* pDefines = nullptr)
{
return CompileAndCreateGeometryShader((const char*)code->Data(), pDefines);
return CompileAndCreateGeometryShader(reinterpret_cast<const char*>(code->Data()), pDefines);
}

inline ID3D11PixelShader* CompileAndCreatePixelShader(D3DBlob* code)
{
return CompileAndCreatePixelShader((const char*)code->Data());
return CompileAndCreatePixelShader(reinterpret_cast<const char*>(code->Data()));
}
inline ID3D11ComputeShader* CompileAndCreateComputeShader(D3DBlob* code)
{
return CompileAndCreateComputeShader(reinterpret_cast<const char*>(code->Data()));
}
}

0 comments on commit b66f96c

Please sign in to comment.