mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-03 13:11:33 +00:00
separate shader uniforms structures
This commit is contained in:
@ -321,9 +321,7 @@ public class Renderer {
|
|||||||
encoder.setRenderPipelineState(pso)
|
encoder.setRenderPipelineState(pso)
|
||||||
encoder.setDepthStencilState(depthStencilState)
|
encoder.setDepthStencilState(depthStencilState)
|
||||||
encoder.setFragmentTexture(cubeTexture ?? defaultTexture, index: 0)
|
encoder.setFragmentTexture(cubeTexture ?? defaultTexture, index: 0)
|
||||||
encoder.setVertexBuffer(vtxBuffer,
|
encoder.setVertexBuffer(vtxBuffer, offset: 0, index: VertexShaderInputIdx.vertices.rawValue)
|
||||||
offset: 0,
|
|
||||||
index: ShaderInputIdx.vertices.rawValue)
|
|
||||||
|
|
||||||
self._encoder = encoder
|
self._encoder = encoder
|
||||||
frameFunc(self)
|
frameFunc(self)
|
||||||
@ -342,31 +340,30 @@ public class Renderer {
|
|||||||
|
|
||||||
func batch(instances: [Instance], camera: Camera) {
|
func batch(instances: [Instance], camera: Camera) {
|
||||||
assert(self._encoder != nil, "batch can't be called outside of a frame being rendered")
|
assert(self._encoder != nil, "batch can't be called outside of a frame being rendered")
|
||||||
assert(instances.count < 52)
|
assert(instances.count < 28)
|
||||||
|
|
||||||
var uniforms = ShaderUniforms(
|
var vertUniforms = VertexShaderUniforms(projView: camera.viewProjection)
|
||||||
projView: camera.viewProjection,
|
var fragUniforms = FragmentShaderUniforms(directionalLight: normalize(.init(0.75, -1, 0.5)))
|
||||||
directionalLight: normalize(.init(0.75, -1, 0.5)))
|
let instances = instances.map { (instance: Instance) -> VertexShaderInstance in
|
||||||
let instances = instances.map { (instance: Instance) -> ShaderInstance in
|
|
||||||
let model =
|
let model =
|
||||||
.translate(instance.position) *
|
.translate(instance.position) *
|
||||||
matrix_float4x4(instance.rotation) *
|
matrix_float4x4(instance.rotation) *
|
||||||
.scale(instance.scale)
|
.scale(instance.scale)
|
||||||
return ShaderInstance(
|
return VertexShaderInstance(
|
||||||
model: model, normalModel: model.inverse.transpose,
|
model: model, normalModel: model.inverse.transpose,
|
||||||
color: SIMD4(Color<UInt8>(instance.color)))
|
color: SIMD4(Color<UInt8>(instance.color)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ideal as long as our uniforms total 4 KB or less
|
// Ideal as long as our uniforms total 4 KB or less
|
||||||
self._encoder.setVertexBytes(instances,
|
self._encoder.setVertexBytes(instances,
|
||||||
length: instances.count * MemoryLayout<ShaderInstance>.stride,
|
length: instances.count * MemoryLayout<VertexShaderInstance>.stride,
|
||||||
index: ShaderInputIdx.instance.rawValue)
|
index: VertexShaderInputIdx.instance.rawValue)
|
||||||
self._encoder.setVertexBytes(&uniforms,
|
self._encoder.setVertexBytes(&vertUniforms,
|
||||||
length: MemoryLayout<ShaderUniforms>.stride,
|
length: MemoryLayout<VertexShaderUniforms>.stride,
|
||||||
index: ShaderInputIdx.uniforms.rawValue)
|
index: VertexShaderInputIdx.uniforms.rawValue)
|
||||||
self._encoder.setFragmentBytes(&uniforms,
|
self._encoder.setFragmentBytes(&fragUniforms,
|
||||||
length: MemoryLayout<ShaderUniforms>.stride,
|
length: MemoryLayout<FragmentShaderUniforms>.stride,
|
||||||
index: ShaderInputIdx.uniforms.rawValue)
|
index: FragmentShaderInputIdx.uniforms.rawValue)
|
||||||
|
|
||||||
self._encoder.drawIndexedPrimitives(
|
self._encoder.drawIndexedPrimitives(
|
||||||
type: .triangle,
|
type: .triangle,
|
||||||
|
@ -12,9 +12,9 @@ struct FragmentInput {
|
|||||||
vertex FragmentInput vertexMain(
|
vertex FragmentInput vertexMain(
|
||||||
uint vertexID [[vertex_id]],
|
uint vertexID [[vertex_id]],
|
||||||
uint instanceID [[instance_id]],
|
uint instanceID [[instance_id]],
|
||||||
device const ShaderVertex* vtx [[buffer(ShaderInputIdxVertices)]],
|
device const ShaderVertex* vtx [[buffer(VertexShaderInputIdxVertices)]],
|
||||||
device const ShaderInstance* i [[buffer(ShaderInputIdxInstance)]],
|
device const VertexShaderInstance* i [[buffer(VertexShaderInputIdxInstance)]],
|
||||||
constant ShaderUniforms& u [[buffer(ShaderInputIdxUniforms)]]
|
constant VertexShaderUniforms& u [[buffer(VertexShaderInputIdxUniforms)]]
|
||||||
) {
|
) {
|
||||||
auto position = vtx[vertexID].position;
|
auto position = vtx[vertexID].position;
|
||||||
auto world = i[instanceID].model * position;
|
auto world = i[instanceID].model * position;
|
||||||
@ -31,7 +31,7 @@ vertex FragmentInput vertexMain(
|
|||||||
fragment half4 fragmentMain(
|
fragment half4 fragmentMain(
|
||||||
FragmentInput in [[stage_in]],
|
FragmentInput in [[stage_in]],
|
||||||
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
||||||
constant ShaderUniforms& u [[buffer(ShaderInputIdxUniforms)]]
|
constant FragmentShaderUniforms& u [[buffer(FragmentShaderInputIdxUniforms)]]
|
||||||
) {
|
) {
|
||||||
constexpr metal::sampler sampler(metal::address::repeat, metal::filter::nearest);
|
constexpr metal::sampler sampler(metal::address::repeat, metal::filter::nearest);
|
||||||
auto normal = metal::normalize(in.normal);
|
auto normal = metal::normalize(in.normal);
|
||||||
|
@ -10,10 +10,10 @@
|
|||||||
|
|
||||||
#include <simd/simd.h>
|
#include <simd/simd.h>
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger, ShaderInputIdx) {
|
typedef NS_ENUM(NSInteger, VertexShaderInputIdx) {
|
||||||
ShaderInputIdxVertices = 0,
|
VertexShaderInputIdxVertices = 0,
|
||||||
ShaderInputIdxInstance = 1,
|
VertexShaderInputIdxInstance = 1,
|
||||||
ShaderInputIdxUniforms = 2
|
VertexShaderInputIdxUniforms = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -26,11 +26,18 @@ typedef struct {
|
|||||||
matrix_float4x4 model;
|
matrix_float4x4 model;
|
||||||
matrix_float4x4 normalModel;
|
matrix_float4x4 normalModel;
|
||||||
vector_uchar4 color;
|
vector_uchar4 color;
|
||||||
} ShaderInstance;
|
} VertexShaderInstance;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
matrix_float4x4 projView;
|
matrix_float4x4 projView;
|
||||||
|
} VertexShaderUniforms;
|
||||||
|
|
||||||
|
typedef NS_ENUM(NSInteger, FragmentShaderInputIdx) {
|
||||||
|
FragmentShaderInputIdxUniforms = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
vector_float3 directionalLight;
|
vector_float3 directionalLight;
|
||||||
} ShaderUniforms;
|
} FragmentShaderUniforms;
|
||||||
|
|
||||||
#endif//SHADERTYPES_H
|
#endif//SHADERTYPES_H
|
||||||
|
Reference in New Issue
Block a user