Move shader files to Shaders group
This commit is contained in:
parent
cce59a45dc
commit
8e2a873edf
3 changed files with 10 additions and 2 deletions
61
Terrain2/Shaders/Shaders.metal
Normal file
61
Terrain2/Shaders/Shaders.metal
Normal file
|
@ -0,0 +1,61 @@
|
|||
//
|
||||
// Shaders.metal
|
||||
// Terrain2
|
||||
//
|
||||
// Created by Eryn Wells on 11/3/18.
|
||||
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
// File for Metal kernel and shader functions
|
||||
|
||||
#include <metal_stdlib>
|
||||
#include <simd/simd.h>
|
||||
|
||||
// Including header shared between this Metal shader code and Swift/C code executing Metal API commands
|
||||
#import "ShaderTypes.h"
|
||||
|
||||
using namespace metal;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float3 position [[attribute(VertexAttributePosition)]];
|
||||
float2 texCoord [[attribute(VertexAttributeTexcoord)]];
|
||||
uint2 gridCoord [[attribute(VertexAttributeGridCoord)]];
|
||||
} Vertex;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float4 position [[position]];
|
||||
float2 texCoord;
|
||||
} ColorInOut;
|
||||
|
||||
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||
texture2d<float> heights [[texture(0)]],
|
||||
constant Uniforms & uniforms [[buffer(BufferIndexUniforms)]])
|
||||
{
|
||||
constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
|
||||
|
||||
ColorInOut out;
|
||||
|
||||
float4 height = heights.sample(s, in.texCoord);
|
||||
|
||||
// Replace the y coordinate with the height we read from the texture.
|
||||
float4 position(in.position.x, height.r, in.position.z, 1.0);
|
||||
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * position;
|
||||
out.texCoord = in.texCoord;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
fragment float4 fragmentShader(ColorInOut in [[stage_in]],
|
||||
constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]],
|
||||
texture2d<half> colorMap [[ texture(TextureIndexColor) ]])
|
||||
{
|
||||
constexpr sampler colorSampler(mip_filter::linear,
|
||||
mag_filter::linear,
|
||||
min_filter::linear);
|
||||
|
||||
half4 colorSample = colorMap.sample(colorSampler, in.texCoord.xy);
|
||||
|
||||
return float4(1.0);
|
||||
}
|
62
Terrain2/Shaders/TerrainAlgorithms.metal
Normal file
62
Terrain2/Shaders/TerrainAlgorithms.metal
Normal file
|
@ -0,0 +1,62 @@
|
|||
//
|
||||
// TerrainAlgorithms.metal
|
||||
// Terrain2
|
||||
//
|
||||
// Created by Eryn Wells on 11/4/18.
|
||||
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
#include <metal_stdlib>
|
||||
#include <metal_types>
|
||||
#include "ShaderTypes.h"
|
||||
using namespace metal;
|
||||
|
||||
/// A pseudo-random number generator providing several algorithms.
|
||||
/// - http://reedbeta.com/blog/quick-and-easy-gpu-random-numbers-in-d3d11/
|
||||
struct PRNG {
|
||||
PRNG(uint seed) : mSeed(wangHash(seed)) { }
|
||||
|
||||
/// Generate a random unsigned integer using a linear congruential generator.
|
||||
uint lcg() {
|
||||
mSeed = 1664525 * mSeed + 1013904223;
|
||||
return mSeed;
|
||||
}
|
||||
|
||||
/// Generate a random unsigned integer using the Xorshift algorithm from George Marsaglia's paper.
|
||||
uint xorShift() {
|
||||
mSeed ^= (mSeed << 13);
|
||||
mSeed ^= (mSeed >> 17);
|
||||
mSeed ^= (mSeed << 5);
|
||||
return mSeed;
|
||||
}
|
||||
|
||||
uint wangHash(uint seed) {
|
||||
seed = (seed ^ 61) ^ (seed >> 16);
|
||||
seed *= 9;
|
||||
seed = seed ^ (seed >> 4);
|
||||
seed *= 0x27d4eb2d;
|
||||
seed = seed ^ (seed >> 15);
|
||||
return seed;
|
||||
}
|
||||
|
||||
private:
|
||||
uint mSeed;
|
||||
};
|
||||
|
||||
kernel void zeroKernel(texture2d<float, access::write> outTexture [[texture(GeneratorTextureIndexOut)]],
|
||||
uint2 tid [[thread_position_in_grid]])
|
||||
{
|
||||
outTexture.write(0, tid);
|
||||
}
|
||||
|
||||
#pragma mark - RandomAlgorithm
|
||||
|
||||
kernel void randomKernel(texture2d<float, access::write> outTexture [[texture(GeneratorTextureIndexOut)]],
|
||||
constant RandomAlgorithmUniforms &uniforms [[buffer(0)]],
|
||||
uint2 tid [[thread_position_in_grid]])
|
||||
{
|
||||
PRNG rng(uniforms.randoms[(tid.x * tid.y) % kRandomAlgorithmUniforms_RandomCount]);
|
||||
uint r = rng.xorShift();
|
||||
float x = float(r * (1.0 / float(UINT_MAX))) * 0.5f;
|
||||
outTexture.write(x, tid);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue