A header-only C-like shading language compiler that writes Metal, HLSL, GLSL

Related tags

Graphics gpuc
Overview

GPUC

A generic shading language compiler that writes metal, HLSL, and GLSL

GPUC is a work in progress, not ready for prime time.

The primary motivation was to create a header-only shader language translator that has negligible effect on compile times. Including this should not add more than a fraction of a second to your compile time, unlike other shader translators I tried.

Only the metal backend is actually useful right now, and probably only for simple shaders. The HLSL and GLSL backends have rotted a bit due to some recent changes in the source language.

I initially wrote this over a two week holiday vacation in December of 2017. I was amazed how easy it was to continue building upon when I returned to it many months later. For me, this was the first reasonably useful thing I implemented in C, and since then I have switched to C for the majority of my hobby projects.

Usage

Header-only library

// include API declarations as needed
#include "gpuc/gpuc.h"
// include implementation in one translation unit
#include "gpuc/gpuc.inl"

Stand-alone compiler

» sh gpuc.c --help
usage:

  gpuc [options] <source>

options:

  --help           display this usage summary
  --ast            print AST to stdout
  --debug          print debug summary to stdout
  --test           run tests
  --glsl           translate GPUC to GLSL
  --hlsl           translate GPUC to HLSL
  --metal          translate GPUC to Metal
  --frag <file>    write output to <file>, or '-' for stdout
  --vert <file>    write output to <file>, or '-' for stdout
» sh gpuc.c sample.gpuc --vert - --frag - --metal
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

float4 sample(const texture2d<float> t, float2 uv) {
    constexpr sampler s(filter::nearest);
    return t.sample(s, uv);
}

float4 sample(const texture2d<float> t, float2 uv, const sampler s) {
    return t.sample(s, uv);
}

struct gpuc {

    // float4 sample(texture2d, float2);

    // float4 sample(texture2d, float2, sampler);

    struct Camera {
        float4x4 mvp;
        float4x4 mvn;
    };

    struct Vertex {
        float3 position [[attribute(0)]];
        float4 color [[attribute(1)]];
        float4 texcoords [[attribute(2)]];
    };

    struct Fragment {
        float4 position [[position]];
        float4 color;
        float4 texcoords;
    };

    struct Sample {
        float4 color;
    };

    constant const Camera& cam;

    const texture2d<float> color;

    const sampler samp;

    Fragment vert(const Vertex v) const {
        Fragment f;
        f.position = cam.mvp * float4(v.position, 1);
        f.color = v.color;
        f.texcoords = v.texcoords;
        return f;
    }

};

vertex gpuc::Fragment vert(
    constant const gpuc::Camera& cam [[buffer(0)]],
    texture2d
    color [[texture(
   0)]],
    sampler samp [[sampler(
   0)]],
    
   const gpuc::Vertex v [[stage_in]]
) {
    
   return gpuc{cam, color, samp}.
   vert(v);
}
#
   include 
   <metal_stdlib>
#
   include 
   <simd/simd.h>

using namespace metal;

float4 
   sample(
   const texture2d<
   float> t, float2 uv) {
    constexpr sampler 
   s(filter::nearest);
    
   return t.
   sample(s, uv);
}

float4 
   sample(
   const texture2d<
   float> t, float2 uv, 
   const sampler s) {
    
   return t.
   sample(s, uv);
}


   struct gpuc {

    
   // float4 sample(texture2d, float2);

    
   // float4 sample(texture2d, float2, sampler);

    
   struct Camera {
        float4x4 mvp;
        float4x4 mvn;
    };

    
   struct Vertex {
        float3 position;
        float4 color;
        float4 texcoords;
    };

    
   struct Fragment {
        float4 position [[position]];
        float4 color;
        float4 texcoords;
    };

    
   struct Sample {
        float4 color;
    };

    constant 
   const Camera& cam;

    
   const texture2d<
   float> color;

    
   const sampler samp;

    Sample 
   frag(
   const Fragment f) 
   const {
        Sample s;
        s.
   color = 
   sample(color, f.
   texcoords.
   st, samp);
        
   return s;
    }

};

fragment gpuc::Sample 
   frag(
    constant 
   const gpuc::Camera& cam [[buffer(
   0)]],
    texture2d
   
     color [[texture(
    0)]],
    sampler samp [[sampler(
    0)]],
    
    const gpuc::Fragment f [[stage_in]]
) {
    
    return gpuc{cam, color, samp}.
    frag(f);
}
   
  
You might also like...
Efficiently spawn and move high amounts of objects like bullets for bullet hells, particles and more.
Efficiently spawn and move high amounts of objects like bullets for bullet hells, particles and more.

Godot Native Bullets Efficiently spawn and move high amounts of objects like bullets for bullet hells, particles and more. This is a GDNative plugin,

This Project Implement an interactive camera for 3D model using Quaternion. It have some advantages over eulerian camera like no gimbal lock and faster to compute.
This Project Implement an interactive camera for 3D model using Quaternion. It have some advantages over eulerian camera like no gimbal lock and faster to compute.

Quaternion-Camera This Project Implement an interactive camera for 3D model using Quaternion. It have some advantages over eulerian camera like no gim

A scratch built kernel original planed to only render a Utah-teapot
A scratch built kernel original planed to only render a Utah-teapot

teapot-OS (sine wave animation) (first 3d renderer, with mouse controll) Current progress Bootloader enter 32 bit protected mode and run c code Switch

Single header C library for rendering truetype text to the screen
Single header C library for rendering truetype text to the screen

kc_truetypeassembler.h Single header C library for assembling textured quads for text rendering using a graphics API. It generates a vertices and text

This is a single-header, multithreaded C++ library for simulating the effect of hydraulic erosion on height maps.
This is a single-header, multithreaded C++ library for simulating the effect of hydraulic erosion on height maps.

TinyErode This is a single-header, multithreaded C++ library for simulating the effect of hydraulic erosion on height maps. The algorithm is based on

A modern, feature-rich single header C++ interface system for GLFW
A modern, feature-rich single header C++ interface system for GLFW

A modern, feature-rich single header C++ interface system for GLFW

Single header KTX/DDS reader

dds-ktx: Portable single header DDS/KTX reader for C/C++ @septag Parses from memory blob. No allocations No dependencies Single-header for easy integr

Single-header single-function C/C++ immediate-mode camera for your graphics demos
Single-header single-function C/C++ immediate-mode camera for your graphics demos

Single-header single-function C/C++ immediate-mode camera for your graphics demos

glslcc: Cross-compiler for GLSL shader language (GLSL-HLSL,METAL,GLES,GLSLv3)

glslcc: Cross-compiler for GLSL shader language (GLSL-HLSL,METAL,GLES,GLSLv3) @septag glslcc is a command line tool that converts GLSL code to HLSL,

⚔️ A tool for cross compiling shaders. Convert between GLSL, HLSL, Metal Shader Language, or older versions of GLSL.
⚔️ A tool for cross compiling shaders. Convert between GLSL, HLSL, Metal Shader Language, or older versions of GLSL.

A cross compiler for shader languages. Convert between SPIR-V, GLSL / GLSL ES, HLSL, Metal Shader Language, or older versions of a given language. Cross Shader wraps glslang and SPIRV-Cross, exposing a simpler interface to transpile shaders.

HLSL Parser and Translator for HLSL, GLSL, and MSL.

HLSLParser This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (usin

HLSL Parser and Translator for HLSL, GLSL, and MSL.

HLSLParser This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (usin

The DirectX Shader Compiler project includes a compiler and related tools used to compile High-Level Shader Language (HLSL) programs into DirectX Intermediate Language (DXIL) representation

DirectX Shader Compiler The DirectX Shader Compiler project includes a compiler and related tools used to compile High-Level Shader Language (HLSL) pr

Metal-cpp is a low-overhead C++ interface for Metal that helps developers add Metal functionality to graphics apps, games, and game engines that are written in C++.

About metal-cpp is a low overhead and header only C++ interface for Metal that helps developers add Metal functionality to graphics applications that

A Visual Studio extension that provides enhanced support for editing High Level Shading Language (HLSL) files
A Visual Studio extension that provides enhanced support for editing High Level Shading Language (HLSL) files

HLSL Tools for Visual Studio This extension is for Visual Studio 2017 / 2019. Go here for the Visual Studio Code extension. HLSL Tools is a Visual Stu

Shader cross compiler to translate HLSL (Shader Model 4 and 5) to GLSL
Shader cross compiler to translate HLSL (Shader Model 4 and 5) to GLSL

XShaderCompiler ("Cross Shader Compiler") Features Cross compiles HLSL shader code (Shader Model 4 and 5) into GLSL Simple to integrate into other pro

GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.

GLSL optimizer ⚠️ As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a different shader compilati

ShaderConductor is a tool designed for cross-compiling HLSL to other shading languages

ShaderConductor ShaderConductor is a tool designed for cross-compiling HLSL to other shading languages. Features Converts HLSL to readable, usable and

HLSL to GLSL language translator based on ATI's HLSL2GLSL. Used in Unity.

HLSL to GLSL shader language translator ⚠️ As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a d

Owner
Garett Bass
an unsophisticated sophont, on an excellent quest for the necessary items. he/him. experienced game mechanic.
Garett Bass
Metal-cpp is a low-overhead C++ interface for Metal that helps developers add Metal functionality to graphics apps, games, and game engines that are written in C++.

About metal-cpp is a low overhead and header only C++ interface for Metal that helps developers add Metal functionality to graphics applications that

Бранимир Караџић 164 Dec 31, 2022
GLSL and ESSL are Khronos high-level shading languages.

GLSL GLSL and ESSL are Khronos high-level shading languages. Khronos Registries are available for OpenGL OpenGL ES Vulkan Extension specifications in

The Khronos Group 241 Dec 9, 2022
Blend text in a HLSL shader and have it look like native DirectWrite

dwrite-hlsl This project demonstrates how to blend text in a HLSL shader and have it look like native DirectWrite. License This project is an extract

Leonard Hecker 11 May 24, 2022
After Effects Plug-in to write GLSL with a format of glslCanvas

glslCanvas4AE After Effects plug-in to run codes written at The Book of Shaders Editor. NOTE: This codes is working in progress and has many problems

Baku 麦 176 Dec 19, 2022
Low Level Graphics Library (LLGL) is a thin abstraction layer for the modern graphics APIs OpenGL, Direct3D, Vulkan, and Metal

Low Level Graphics Library (LLGL) Documentation NOTE: This repository receives bug fixes only, but no major updates. Pull requests may still be accept

Lukas Hermanns 1.5k Jan 8, 2023
🎮 C Bindings/Wrappers for Apple's METAL framework

Apple's Metal for C C Wrapper for Apple's METAL framework. This library is C bindings of Metal API (MetalGL). Since OpenGL is deprecated, this library

Recep Aslantas 116 Dec 30, 2022
OpenGL 4.6 on Metal

MGL OpenGL 4.6 on Metal This is a start for porting OpenGL 4.6 on top of Metal, most of it is functional and has been tested. The tests are functional

null 550 Dec 29, 2022
CXXGraph is a small library, header only, that manages the Graph and it's algorithms in C++.

CXXGraph is a small library, header only, that manages the Graph and it's algorithms in C++.

ZigRazor 184 Dec 28, 2022
2D Vector Graphics Engine Powered by a JIT Compiler

Blend2D 2D Vector Graphics Powered by a JIT Compiler. Official Home Page (blend2d.com) Official Repository (blend2d/blend2d) Public Chat Channel Zlib

Blend2D 1.2k Dec 27, 2022
DirectX shader bytecode cross compiler

HLSLcc DirectX shader bytecode cross compiler. Originally based on https://github.com/James-Jones/HLSLCrossCompiler. This library takes DirectX byteco

Unity Technologies 742 Dec 29, 2022