metaforce/specter/lib/View.cpp

213 lines
6.5 KiB
C++
Raw Normal View History

2015-11-21 23:45:02 +00:00
#include "Specter/View.hpp"
2015-11-26 00:24:01 +00:00
#include "Specter/ViewSystem.hpp"
2015-11-21 23:45:02 +00:00
namespace Specter
{
2015-11-26 00:24:01 +00:00
void View::System::init(boo::GLDataFactory* factory)
{
static const char* VS =
"#version 330\n"
"layout(location=0) in vec3 posIn;\n"
"layout(location=1) in vec4 colorIn;\n"
SPECTER_VIEW_VERT_BLOCK_GLSL
"struct VertToFrag\n"
"{\n"
" vec4 color;\n"
"};\n"
"out VertToFrag vtf;\n"
"void main()\n"
"{\n"
" vtf.color = colorIn;\n"
2015-11-26 23:03:56 +00:00
" gl_Position = mv * vec4(posIn, 1.0);\n"
2015-11-26 00:24:01 +00:00
"}\n";
static const char* FS =
"#version 330\n"
"struct VertToFrag\n"
"{\n"
" vec4 color;\n"
"};\n"
"in VertToFrag vtf;\n"
"layout(location=0) out vec4 colorOut;\n"
"void main()\n"
"{\n"
" colorOut = vtf.color;\n"
"}\n";
static const char* BlockNames[] = {"SpecterViewBlock"};
m_solidShader = factory->newShaderPipeline(VS, FS, 0, nullptr, 1, BlockNames,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
false, false, false);
}
2015-11-28 04:05:27 +00:00
#if _WIN32
2015-11-26 00:24:01 +00:00
2015-11-27 22:20:22 +00:00
void View::System::init(boo::ID3DDataFactory* factory)
{
static const char* VS =
"struct VertData\n"
"{\n"
" float3 posIn : POSITION;\n"
" float4 colorIn : COLOR;\n"
"};\n"
SPECTER_VIEW_VERT_BLOCK_HLSL
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
"};\n"
"VertToFrag main(in VertData v)\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.color = v.colorIn;\n"
" vtf.position = mul(mv, float4(v.posIn, 1.0));\n"
" return vtf;\n"
"}\n";
static const char* FS =
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
"};\n"
"float4 main(in VertToFrag vtf) : SV_Target0\n"
"{\n"
" return vtf.color;\n"
"}\n";
boo::VertexElementDescriptor vdescs[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4},
{nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced}
};
m_vtxFmt = factory->newVertexFormat(2, vdescs);
ComPtr<ID3DBlob> vertBlob;
ComPtr<ID3DBlob> fragBlob;
ComPtr<ID3DBlob> pipeBlob;
m_solidShader = factory->newShaderPipeline(VS, FS, vertBlob, fragBlob, pipeBlob, m_vtxFmt,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
false, false, false);
}
2015-11-28 04:05:27 +00:00
#elif BOO_HAS_METAL
void View::System::init(boo::MetalDataFactory* factory)
{
static const char* VS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertData\n"
"{\n"
" float3 posIn [[ attribute(0) ]];\n"
" float4 colorIn [[ attribute(1) ]];\n"
"};\n"
SPECTER_VIEW_VERT_BLOCK_METAL
"struct VertToFrag\n"
"{\n"
" float4 position [[ position ]];\n"
" float4 color;\n"
"};\n"
"vertex VertToFrag vmain(VertData v [[ stage_in ]], constant SpecterViewBlock& view [[ buffer(2) ]])\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.color = v.colorIn;\n"
" vtf.position = view.mv * float4(v.posIn, 1.0);\n"
" return vtf;\n"
"}\n";
static const char* FS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertToFrag\n"
"{\n"
" float4 position [[ position ]];\n"
" float4 color;\n"
"};\n"
"fragment float4 fmain(VertToFrag vtf [[ stage_in ]])\n"
"{\n"
" return vtf.color;\n"
"}\n";
boo::VertexElementDescriptor vdescs[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4},
{nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced}
};
m_vtxFmt = factory->newVertexFormat(2, vdescs);
m_solidShader = factory->newShaderPipeline(VS, FS, m_vtxFmt, 1,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
false, false, false);
}
#endif
2015-11-27 22:20:22 +00:00
2015-11-26 00:24:01 +00:00
View::View(ViewSystem& system)
{
m_bgVertBuf =
system.m_factory->newDynamicBuffer(boo::BufferUse::Vertex,
sizeof(Zeus::CVector3f), 4);
m_bgInstBuf =
system.m_factory->newDynamicBuffer(boo::BufferUse::Vertex,
sizeof(Zeus::CColor), 1);
2015-11-26 07:35:43 +00:00
m_viewVertBlockBuf =
2015-11-26 00:24:01 +00:00
system.m_factory->newDynamicBuffer(boo::BufferUse::Uniform,
sizeof(VertexBlock), 1);
if (!system.m_viewSystem.m_vtxFmt)
{
boo::VertexElementDescriptor vdescs[] =
{
2015-11-27 22:20:22 +00:00
{m_bgVertBuf, nullptr, boo::VertexSemantic::Position4},
2015-11-26 00:24:01 +00:00
{m_bgInstBuf, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced}
};
m_bgVtxFmt = system.m_factory->newVertexFormat(2, vdescs);
m_bgShaderBinding =
system.m_factory->newShaderDataBinding(system.m_viewSystem.m_solidShader, m_bgVtxFmt,
m_bgVertBuf, m_bgInstBuf, nullptr, 1,
2015-11-26 07:35:43 +00:00
(boo::IGraphicsBuffer**)&m_viewVertBlockBuf,
2015-11-27 22:20:22 +00:00
0, nullptr);
}
else
{
m_bgShaderBinding =
system.m_factory->newShaderDataBinding(system.m_viewSystem.m_solidShader, system.m_viewSystem.m_vtxFmt,
m_bgVertBuf, m_bgInstBuf, nullptr, 1,
(boo::IGraphicsBuffer**)&m_viewVertBlockBuf,
2015-11-26 00:24:01 +00:00
0, nullptr);
}
}
2015-11-26 23:03:56 +00:00
void View::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
2015-11-26 07:35:43 +00:00
{
2015-11-26 23:03:56 +00:00
m_viewVertBlock.setViewRect(root, sub);
m_bgRect[0].assign(0.f, sub.size[1], 0.f);
m_bgRect[1].assign(0.f, 0.f, 0.f);
m_bgRect[2].assign(sub.size[0], sub.size[1], 0.f);
m_bgRect[3].assign(sub.size[0], 0.f, 0.f);
2015-11-26 07:35:43 +00:00
m_bgValidSlots = 0;
}
2015-11-26 00:24:01 +00:00
void View::draw(boo::IGraphicsCommandQueue* gfxQ)
{
int pendingSlot = 1 << gfxQ->pendingDynamicSlot();
2015-11-26 07:35:43 +00:00
if ((m_bgValidSlots & pendingSlot) == 0)
2015-11-26 00:24:01 +00:00
{
2015-11-26 07:35:43 +00:00
m_viewVertBlockBuf->load(&m_viewVertBlock, sizeof(VertexBlock));
2015-11-26 00:24:01 +00:00
m_bgVertBuf->load(m_bgRect, sizeof(Zeus::CVector3f) * 4);
m_bgInstBuf->load(&m_bgColor, sizeof(Zeus::CColor));
2015-11-26 07:35:43 +00:00
m_bgValidSlots |= pendingSlot;
2015-11-26 00:24:01 +00:00
}
2015-11-26 07:35:43 +00:00
gfxQ->setShaderDataBinding(m_bgShaderBinding);
2015-11-26 23:03:56 +00:00
gfxQ->setDrawPrimitive(boo::Primitive::TriStrips);
2015-11-26 00:24:01 +00:00
gfxQ->draw(0, 4);
}
2015-11-21 23:45:02 +00:00
}