MoonWorks-docs/content/Graphics/Resources/ShaderModule.md

32 lines
1.8 KiB
Markdown

---
title: "Shader Module"
date: 2021-01-26T17:41:14-08:00
weight: 4
---
Shaders are, to perhaps oversimplify a bit, the procedures that are used in rendering. A shader is a parallel program that your GPU will run thousands or tens of thousands of times simultaneously to process your data.
Explaining everything about shaders is beyond the scope of this documentation and there are plenty of resources for learning shader programming out there, so I will stick to a simple explanation and you can take it from there.
Vertex shaders are programs that transform vertex data. A vertex shader runs once per vertex and outputs some vertex-related data.
The graphics pipeline "rasterizes" your vertex data to produce pixel data, and this pixel data is then further transformed by your pixel, or "fragment" shader.
We'll talk more about this when we get to graphics pipelines. For now, let's explain how to create a MoonWorks shader module.
MoonWorks shader modules expect input in a format called "SPIR-V bytecode." Unlike the bad old days where you were locked into whatever shader language your engine required, SPIR-V is an intermediate representation. That means it can compile from higher level shader languages like GLSL or HLSL. Personally I prefer GLSL, but you can use any shader language you want that outputs SPIR-V!
To compile a GLSL shader into SPIR-V, you can use the [glslangValidator](https://github.com/KhronosGroup/glslang) program like this:
```sh
glslangValidator -V myCoolShader.frag -o myCoolShaderFrag.spv
```
This will output a file named "myCoolShaderFrag.spv". Now we can put that shader module into MoonWorks.
```cs
var myShaderModule = new ShaderModule(GraphicsDevice, "myCoolShaderFrag.spv");
```
Shader modules don't do anything on their own - you use them as part of a graphics pipeline. We'll get into that later.