void Program::Init(const char *name, const Shader::Defines &defines) { string sn(name); sn += ".geom"; ifstream f(sn.c_str()); if(f.good()) Init(VertexShader(name, defines), GeometryShader(name, defines), FragmentShader(name, defines), name); else Init(VertexShader(name, defines), FragmentShader(name, defines), name); f.close(); }
bool Rasterizer::VInit(DXDriver* _driver) { if (m_pVertexShaderStage || m_pPixelShaderStage || m_pGeometryShaderStage) { PrintError(AT, "shader stages already created"); return false; } m_pVertexShaderStage = VNEW VertexShader(); m_pGeometryShaderStage = VNEW GeometryShader(); m_pPixelShaderStage = VNEW PixelShader(); if (!m_pVertexShaderStage->Init(L"Assets/Shaders/RasterNodeVS.hlsl", _driver->GetDevice())) //if (!m_pVertexShaderStage->Init(L"Assets/Shaders/vtest.hlsl", _driver->GetDevice())) { PrintError(AT, "failed to create vertex shader stage"); return false; } if (!m_pGeometryShaderStage->Init(L"Assets/Shaders/RasterNodeGS.hlsl", _driver->GetDevice())) //if (!m_pGeometryShaderStage->Init(L"Assets/Shaders/gtest.hlsl", _driver->GetDevice())) { PrintError(AT, "failed to create geometry shader stage"); return false; } if (!m_pPixelShaderStage->Init(L"Assets/Shaders/RasterNodePS.hlsl", _driver->GetDevice())) //if (!m_pPixelShaderStage->Init(L"Assets/Shaders/ptest.hlsl", _driver->GetDevice())) { PrintError(AT, "failed to create pixel shader stage"); return false; } RenderTarget* renderTarget = nullptr; for (unsigned int i = RT_COLOR; i < RENDERTARGET_COUNT; ++i) { _driver->GetRenderTarget(i, renderTarget); if (!renderTarget) return false; renderTarget->GetView(m_pDeferredRenderTargets[i]); if (!m_pDeferredRenderTargets[i]) return false; } return true; }
ShaderProgram::ShaderProgram( const ResChar vertexShaderInitializer[], const ResChar geometryShaderInitializer[], const ResChar fragmentShaderInitializer[], const ResChar definesTable[]) : _compiledVertexShader(&::Assets::GetAssetComp<CompiledShaderByteCode>(vertexShaderInitializer, definesTable)) // (odd..?) , _compiledPixelShader(&::Assets::GetAssetComp<CompiledShaderByteCode>(fragmentShaderInitializer, definesTable)) // (odd..?) , _vertexShader(*_compiledVertexShader) , _pixelShader(*_compiledPixelShader) , _compiledGeometryShader(nullptr) { if (geometryShaderInitializer && geometryShaderInitializer[0]) { _compiledGeometryShader = &::Assets::GetAssetComp<CompiledShaderByteCode>(geometryShaderInitializer, definesTable); _geometryShader = GeometryShader(*_compiledGeometryShader); } _validationCallback = std::make_shared<Assets::DependencyValidation>(); Assets::RegisterAssetDependency(_validationCallback, _compiledVertexShader->GetDependencyValidation()); Assets::RegisterAssetDependency(_validationCallback, _compiledPixelShader->GetDependencyValidation()); if (_compiledGeometryShader) { Assets::RegisterAssetDependency(_validationCallback, _compiledGeometryShader->GetDependencyValidation()); } }
void FLightPropagationVolume::Visualise(FRHICommandList& RHICmdList, const FViewInfo& View) const { SCOPED_DRAW_EVENT(RHICmdList, LpvVisualise); check(View.GetFeatureLevel() == ERHIFeatureLevel::SM5); TShaderMapRef<FLpvVisualiseVS> VertexShader(View.ShaderMap); TShaderMapRef<FLpvVisualiseGS> GeometryShader(View.ShaderMap); TShaderMapRef<FLpvVisualisePS> PixelShader(View.ShaderMap); RHICmdList.SetDepthStencilState(TStaticDepthStencilState<false, CF_Always>::GetRHI()); RHICmdList.SetRasterizerState(TStaticRasterizerState<FM_Solid, CM_None>::GetRHI()); RHICmdList.SetBlendState(TStaticBlendState<CW_RGB, BO_Add, BF_One, BF_One>::GetRHI()); SetGlobalBoundShaderState(RHICmdList, View.GetFeatureLevel(), LpvVisBoundShaderState, GSimpleElementVertexDeclaration.VertexDeclarationRHI, *VertexShader, *PixelShader, *GeometryShader); VertexShader->SetParameters(RHICmdList, View); GeometryShader->SetParameters(RHICmdList, View); PixelShader->SetParameters(RHICmdList, this, View); RHICmdList.SetStreamSource(0, NULL, 0, 0); RHICmdList.DrawPrimitive(PT_PointList, 0, 1, 32 * 3); PixelShader->UnbindBuffers(RHICmdList); }
RippleTexProg(GLuint ripple_tex_size) : tex_1(prog(), "Tex1") , tex_2(prog(), "Tex2") , new_drop(prog(), "NewDrop") { AttachShader(VertexShader( ObjectDesc("Ripple texture vertex shader"), StrLit( "#version 330\n" "void main(void)" "{" " gl_Position = vec4(0.0, 0.0, 0.0, 1.0);" "}" ) )); AttachShader(GeometryShader( ObjectDesc("Ripple texture geometry shader"), StrLit( "#version 330\n" "layout (points) in;" "layout (triangle_strip, max_vertices = 4) out;" "uniform int TexSize;" "out vec2 geomTexCoord;" "void make_vertex(vec2 pos, vec2 tc)" "{" " gl_Position = vec4(pos, 0.0, 1.0);" " geomTexCoord = tc;" " EmitVertex();" "}" "void main(void)" "{" " float rts = TexSize;" " make_vertex(vec2(-1.0,-1.0), vec2( 0, 0));" " make_vertex(vec2(-1.0, 1.0), vec2( 0, rts));" " make_vertex(vec2( 1.0,-1.0), vec2(rts, 0));" " make_vertex(vec2( 1.0, 1.0), vec2(rts, rts));" " EndPrimitive();" "}" ) )); AttachShader(FragmentShader( ObjectDesc("Ripple texture fragment shader"), StrLit( "#version 330\n" "uniform ivec2 NewDrop;" "uniform sampler2D Tex1, Tex2;" "uniform int TexSize;" "in vec2 geomTexCoord;" "layout (location = 0) out vec4 fragBump;" "layout (location = 1) out float fragHeight;" "ivec2 wrap_tc(ivec2 tc)" "{" " if(tc.x < 0) tc.x = TexSize-1;" " if(tc.x >= TexSize) tc.x = 0;" " if(tc.y < 0) tc.y = TexSize-1;" " if(tc.y >= TexSize) tc.y = 0;" " return tc;" "}" "float height_at(sampler2D tex, ivec2 tc, float factor)" "{" " return texelFetch(tex, wrap_tc(tc), 0).r * factor;" "}" "void main(void)" "{" " ivec2 TC = ivec2(geomTexCoord);" " float ch = height_at(Tex2, TC, 1.0);" " float xp1 = height_at(Tex2, TC+ivec2( 1, 0), 0.25);" " float xm1 = height_at(Tex2, TC+ivec2(-1, 0), 0.25);" " float yp1 = height_at(Tex2, TC+ivec2( 0, 1), 0.25);" " float ym1 = height_at(Tex2, TC+ivec2( 0,-1), 0.25);" " fragHeight = xp1 + xm1 + yp1 + ym1;" " fragHeight += height_at(Tex2, TC+ivec2( 1,-1), 0.25);" " fragHeight += height_at(Tex2, TC+ivec2( 1, 1), 0.25);" " fragHeight += height_at(Tex2, TC+ivec2(-1,-1), 0.25);" " fragHeight += height_at(Tex2, TC+ivec2(-1, 1), 0.25);" " fragHeight -= height_at(Tex1, TC, 1.0);" " vec2 d = NewDrop - TC;" " fragHeight += length(d)<8?1.0:0.0;" " vec3 fragNormal = vec3(" " (xm1 - ch) + (ch - xp1)," " (ym1 - ch) + (ch - yp1)," " 0.1" " );" " fragBump = vec4(" " normalize(fragNormal)," " fragHeight" " );" "}" ) )); Link(); ProgramUniform<GLint>(prog(), "TexSize").Set(ripple_tex_size); }
static Program make(void) { Program prog; prog << VertexShader(ObjectDesc("Draw"), "#version 330\n" "uniform mat4 CameraMatrix;" "in vec4 Position;" "out vec3 vertColor;" "void main(void)" "{" " gl_Position = CameraMatrix * Position;" " vertColor = normalize(vec3(1)-Position.rgb);" "}" ); prog << GeometryShader(ObjectDesc("Draw"), "#version 330\n" "layout(points) in;" "layout(triangle_strip, max_vertices = 4) out;" "uniform mat4 ProjectionMatrix;" "in vec3 vertColor[1];" "out vec3 geomColor;" "void main(void)" "{" " geomColor = vertColor[0];" " float s = 0.1;" " float yo[2] = float[2](-1.0, 1.0);" " float xo[2] = float[2](-1.0, 1.0);" " for(int j=0;j!=2;++j)" " for(int i=0;i!=2;++i)" " {" " float xoffs = xo[i]*s;" " float yoffs = yo[j]*s;" " gl_Position = ProjectionMatrix * vec4(" " gl_in[0].gl_Position.x-xoffs," " gl_in[0].gl_Position.y-yoffs," " gl_in[0].gl_Position.z," " 1.0" " );" " EmitVertex();" " }" " EndPrimitive();" "}" ); prog << FragmentShader(ObjectDesc("Draw"), "#version 330\n" "in vec3 geomColor;" "out vec4 fragColor;" "void main(void)" "{" " float a = 1.0/16.0;" " fragColor = vec4(geomColor, a);" "}" ); prog.Link(); return prog; }