Refactor GLSL shader bindings

This commit is contained in:
Jack Andersen 2016-07-07 14:05:22 -10:00
parent 5c91ba8da7
commit 44e8317d49
5 changed files with 34 additions and 26 deletions

View File

@ -20,7 +20,6 @@ class GLDataFactory : public IGraphicsDataFactory
static ThreadLocalPtr<struct GLData> m_deferredData;
std::unordered_set<struct GLData*> m_committedData;
std::mutex m_committedMutex;
std::vector<int> m_texUnis;
void destroyData(IGraphicsData*);
void destroyAllData();
public:
@ -54,7 +53,7 @@ public:
IVertexFormat* newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements);
IShaderPipeline* newShaderPipeline(const char* vertSource, const char* fragSource,
size_t texCount, const char* texArrayName,
size_t texCount, const char** texNames,
size_t uniformBlockCount, const char** uniformBlockNames,
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
bool depthTest, bool depthWrite, bool backfaceCulling);

View File

@ -437,7 +437,7 @@ static const GLenum BLEND_FACTOR_TABLE[] =
IShaderPipeline* GLDataFactory::Context::newShaderPipeline
(const char* vertSource, const char* fragSource,
size_t texCount, const char* texArrayName,
size_t texCount, const char** texNames,
size_t uniformBlockCount, const char** uniformBlockNames,
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
bool depthTest, bool depthWrite, bool backfaceCulling)
@ -511,17 +511,15 @@ IShaderPipeline* GLDataFactory::Context::newShaderPipeline
}
}
if (texCount && texArrayName)
if (texCount && texNames)
{
GLint texLoc = glGetUniformLocation(shader.m_prog, texArrayName);
if (texLoc < 0)
Log.report(logvisor::Error, "unable to find sampler variable '%s'", texArrayName);
else
for (int i=0 ; i<texCount ; ++i)
{
if (texCount > m_parent.m_texUnis.size())
for (size_t i=m_parent.m_texUnis.size() ; i<texCount ; ++i)
m_parent.m_texUnis.push_back(i);
glUniform1iv(texLoc, m_parent.m_texUnis.size(), m_parent.m_texUnis.data());
GLint texLoc = glGetUniformLocation(shader.m_prog, texNames[i]);
if (texLoc < 0)
Log.report(logvisor::Error, "unable to find sampler variable '%s'", texNames[i]);
else
glUniform1i(texLoc, i);
}
}

View File

@ -1258,7 +1258,7 @@ class VulkanTextureD : public ITextureD
texCreateInfo.arrayLayers = 1;
texCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
texCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
texCreateInfo.initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
texCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
texCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
texCreateInfo.queueFamilyIndexCount = 0;
texCreateInfo.pQueueFamilyIndices = nullptr;
@ -1274,7 +1274,6 @@ class VulkanTextureD : public ITextureD
ThrowIfFailed(vk::CreateImage(ctx->m_dev, &texCreateInfo, nullptr, &m_gpuTex[i]));
m_descInfo[i].sampler = ctx->m_linearSampler;
m_descInfo[i].imageView = m_gpuView[i];
m_descInfo[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
}
@ -1334,6 +1333,8 @@ public:
/* create image view */
viewInfo.image = m_gpuTex[i];
ThrowIfFailed(vk::CreateImageView(ctx->m_dev, &viewInfo, nullptr, &m_gpuView[i]));
m_descInfo[i].imageView = m_gpuView[i];
}
}
@ -1758,7 +1759,7 @@ class VulkanShaderPipeline : public IShaderPipeline
depthStencilInfo.flags = 0;
depthStencilInfo.depthTestEnable = depthTest;
depthStencilInfo.depthWriteEnable = depthWrite;
depthStencilInfo.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
depthStencilInfo.depthCompareOp = VK_COMPARE_OP_GREATER_OR_EQUAL;
depthStencilInfo.front.compareOp = VK_COMPARE_OP_ALWAYS;
depthStencilInfo.back.compareOp = VK_COMPARE_OP_ALWAYS;
@ -2672,7 +2673,7 @@ void VulkanTextureD::update(int b)
vk::UnmapMemory(m_q->m_ctx->m_dev, m_cpuMem);
SetImageLayout(cmdBuf, m_gpuTex[b], VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, 1);
/* Put the copy command into the command buffer */
@ -2742,7 +2743,7 @@ VulkanDataFactory::VulkanDataFactory(IGraphicsContext* parent, VulkanContext* ct
layoutBindings[i].binding = i;
layoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
layoutBindings[i].descriptorCount = 1;
layoutBindings[i].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
layoutBindings[i].stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
layoutBindings[i].pImmutableSamplers = nullptr;
}
for (int i=BOO_GLSL_MAX_UNIFORM_COUNT ; i<BOO_GLSL_MAX_UNIFORM_COUNT+BOO_GLSL_MAX_TEXTURE_COUNT ; ++i)
@ -2850,10 +2851,16 @@ IShaderPipeline* VulkanDataFactory::Context::newShaderPipeline
Log.report(logvisor::Fatal, "unable to link shader program\n%s", prog.getInfoLog());
return nullptr;
}
glslang::GlslangToSpv(*prog.getIntermediate(EShLangVertex), vertBlobOut);
//spv::Disassemble(std::cerr, vertBlobOut);
glslang::GlslangToSpv(*prog.getIntermediate(EShLangFragment), fragBlobOut);
//spv::Disassemble(std::cerr, fragBlobOut);
if (vertBlobOut.empty())
{
glslang::GlslangToSpv(*prog.getIntermediate(EShLangVertex), vertBlobOut);
//spv::Disassemble(std::cerr, vertBlobOut);
}
if (fragBlobOut.empty())
{
glslang::GlslangToSpv(*prog.getIntermediate(EShLangFragment), fragBlobOut);
//spv::Disassemble(std::cerr, fragBlobOut);
}
}
VkShaderModuleCreateInfo smCreateInfo = {};

View File

@ -420,10 +420,12 @@ public:
sigemptyset(&s.sa_mask);
s.sa_flags = 0;
sigaction(SIGINT, &s, nullptr);
sigaction(SIGUSR2, &s, nullptr);
sigset_t waitmask, origmask;
sigemptyset(&waitmask);
sigaddset(&waitmask, SIGINT);
sigaddset(&waitmask, SIGUSR2);
pthread_sigmask(SIG_BLOCK, &waitmask, &origmask);
/* Spawn client thread */
@ -437,7 +439,7 @@ public:
innerLk.unlock();
initcv.notify_one();
clientReturn = m_callback.appMain(this);
pthread_kill(mainThread, SIGINT);
pthread_kill(mainThread, SIGUSR2);
});
initcv.wait(outerLk);
@ -450,7 +452,7 @@ public:
FD_SET(m_dbusFd, &fds);
if (pselect(m_maxFd+1, &fds, NULL, NULL, NULL, &origmask) < 0)
{
/* SIGINT handled here */
/* SIGINT/SIGUSR2 handled here */
if (errno == EINTR)
break;
}

View File

@ -296,15 +296,17 @@ struct TestApplicationCallback : IApplicationCallback
"#version 330\n"
BOO_GLSL_BINDING_HEAD
"precision highp float;\n"
"TBINDING0 uniform sampler2D texs[1];\n"
"TBINDING0 uniform sampler2D tex;\n"
"layout(location=0) out vec4 out_frag;\n"
"in vec2 out_uv;\n"
"void main()\n"
"{\n"
" out_frag = texture(texs[0], out_uv);\n"
" out_frag = texture(tex, out_uv);\n"
"}\n";
pipeline = glF.newShaderPipeline(VS, FS, 1, "texs", 0, nullptr,
static const char* texName = "tex";
pipeline = glF.newShaderPipeline(VS, FS, 1, &texName, 0, nullptr,
BlendFactor::One, BlendFactor::Zero,
Primitive::TriStrips, true, true, false);
}