1 module evael.renderer.gl.gl_pipeline;
2 
3 import evael.renderer.pipeline;
4 import evael.renderer.graphics_buffer;
5 
6 import evael.renderer.gl.gl_shader;
7 import evael.renderer.gl.gl_wrapper;
8 import evael.renderer.gl.gl_uniform_resource;
9 import evael.renderer.gl.gl_texture_resource;
10 import evael.renderer.gl.gl_enum_converter;
11 
12 import evael.renderer.resources.resource;
13 import evael.renderer.texture;
14 
15 import evael.lib.memory;
16 
17 class GLPipeline : Pipeline
18 {
19     @nogc
20     public override void apply() const nothrow
21     {
22         if (this.depthState.enabled)
23         {
24             gl.Enable(GL_DEPTH_TEST);
25             gl.DepthMask(!this.depthState.readOnly);
26         }
27         
28         if (this.blendState.enabled)
29         {
30             gl.Enable(GL_BLEND);
31             gl.BlendFuncSeparate(
32                 GLEnumConverter.blendFactor(this.blendState.sourceRGB),
33                 GLEnumConverter.blendFactor(this.blendState.destinationRGB),
34                 GLEnumConverter.blendFactor(this.blendState.sourceAlpha),
35                 GLEnumConverter.blendFactor(this.blendState.destinationAlpha)
36             );
37 
38             gl.BlendEquationSeparate(
39                 GLEnumConverter.blendFunction(this.blendState.colorFunction),
40                 GLEnumConverter.blendFunction(this.blendState.alphaFunction)
41             );
42         }
43 
44         foreach (resource; this.m_resources)
45         {
46             resource.apply();
47         }
48     }
49 
50     @nogc
51     public override void clear() const nothrow
52     {
53         foreach (resource; this.m_resources)
54         {
55             resource.clear();
56         }
57 
58         if (this.blendState.enabled)
59         {
60             gl.Disable(GL_BLEND);
61         }
62 
63         if (this.depthState.enabled)
64         {
65             gl.Disable(GL_DEPTH_TEST);
66         }
67     }
68 
69     @nogc
70     public override GLTextureResource addTextureResource(Texture texture = null)
71     {
72         auto resource = MemoryHelper.create!GLTextureResource(texture);
73         this.m_resources.insert(resource);
74         return resource;
75     }
76 
77     @nogc
78     public GLUniformResource!T addUniformResource(T)(in string name, T value)
79     {
80         assert(this.shader !is null, "Set a shader before adding an uniform resource.");
81 
82         auto resource = MemoryHelper.create!(GLUniformResource!T)(name, (cast(GLShader) this.shader).programId, value);
83         this.m_resources.insert(resource);
84         return resource;
85     }
86 }