Skip to content

Commit

Permalink
Implement ECS resource interface for materials
Browse files Browse the repository at this point in the history
  • Loading branch information
chances committed Apr 9, 2024
1 parent 94566c1 commit 1bef47e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions source/graphics/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
/// License: 3-Clause BSD License
module teraflop.graphics;

import concepts : implements;
import teraflop.ecs : Resource;
import teraflop.math;
import teraflop.traits : isStruct;
public import teraflop.graphics.color;
public import teraflop.graphics.primitives;
public import wgpu.api : ShaderStage;
import wgpu.api : Adapter, Device;

/// A shaded material for geometry encapsulating its `Shader`s, graphics pipeline state, and optionally a `Texture`.
/// See_Also: `teraflop.systems.rendering.PipelinePreparer`
@implements!(Material, Resource)
struct Material {
import std.typecons : Flag, No, Yes;
import wgpu.api : CullMode, FrontFace;
Expand All @@ -19,7 +23,7 @@ struct Material {
private FrontFace _frontFace = FrontFace.cw;
private CullMode _cullMode = CullMode.back;

package (teraflop) Shader*[] _shaders;
package (teraflop) Shader*[] shaders;
// TODO: package (teraflop) Texture _texture;

/// Initialize a new Material.
Expand All @@ -32,10 +36,10 @@ struct Material {
FrontFace frontFace = FrontFace.cw, CullMode cullMode = CullMode.back,
Flag!"depthTest" depthTest = Yes.depthTest
) {
_shaders = shaders;
shaders = shaders;
_depthTest = depthTest;
_frontFace = frontFace;
_cullMode = cullMode;
_depthTest = depthTest;
}

/// Whether to perform the depth test. If `true`, assumes the render target has a depth buffer attachment.
Expand All @@ -50,6 +54,11 @@ struct Material {
CullMode cullMode() @property const {
return _cullMode;
}

///
void initialize(Adapter adapter, Device device) {
foreach (shader; shaders) shader.initialize(adapter, device);
}
}

///
Expand All @@ -69,6 +78,7 @@ enum SourceLanguage {
}

///
@implements!(Shader, Resource)
struct Shader {
import std.conv : to;
import std.exception : enforce;
Expand Down Expand Up @@ -137,7 +147,7 @@ struct Shader {
}

/// Initialize this Shader.
void initialize(scope Device device) {
void initialize(scope Adapter adapter, scope Device device) {
assert(!initialized);
this.device = device;
this._shaderModule = _language == SourceLanguage.spirv
Expand Down

0 comments on commit 1bef47e

Please sign in to comment.