-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69159b1
commit 4ce1b23
Showing
6 changed files
with
232 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "ResourceManager.h" | ||
|
||
#include <fstream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
bool ResourceManager::loadGeometry( | ||
const std::filesystem::path& path, | ||
std::vector<float>& pointData, | ||
std::vector<uint16_t>& indexData | ||
) { | ||
std::ifstream file(path); | ||
if (!file.is_open()) { | ||
return false; | ||
} | ||
|
||
pointData.clear(); | ||
indexData.clear(); | ||
|
||
enum class Section { | ||
None, | ||
Points, | ||
Indices, | ||
}; | ||
Section currentSection = Section::None; | ||
|
||
float value; | ||
uint16_t index; | ||
std::string line; | ||
while (!file.eof()) { | ||
getline(file, line); | ||
|
||
// overcome the `CRLF` problem | ||
if (!line.empty() && line.back() == '\r') { | ||
line.pop_back(); | ||
} | ||
|
||
if (line == "[points]") { | ||
currentSection = Section::Points; | ||
} | ||
else if (line == "[indices]") { | ||
currentSection = Section::Indices; | ||
} | ||
else if (line[0] == '#' || line.empty()) { | ||
// Do nothing, this is a comment | ||
} | ||
else if (currentSection == Section::Points) { | ||
std::istringstream iss(line); | ||
// Get x, y, r, g, b | ||
for (int i = 0; i < 5; ++i) { | ||
iss >> value; | ||
pointData.push_back(value); | ||
} | ||
} | ||
else if (currentSection == Section::Indices) { | ||
std::istringstream iss(line); | ||
// Get corners #0 #1 and #2 | ||
for (int i = 0; i < 3; ++i) { | ||
iss >> index; | ||
indexData.push_back(index); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
WGPUShaderModule ResourceManager::loadShaderModule(const std::filesystem::path& path, WGPUDevice device) { | ||
std::ifstream file(path); | ||
if (!file.is_open()) { | ||
return nullptr; | ||
} | ||
file.seekg(0, std::ios::end); | ||
size_t size = file.tellg(); | ||
std::string shaderSource(size, ' '); | ||
file.seekg(0); | ||
file.read(shaderSource.data(), size); | ||
|
||
WGPUShaderModuleWGSLDescriptor shaderCodeDesc{}; | ||
shaderCodeDesc.chain.next = nullptr; | ||
shaderCodeDesc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; | ||
shaderCodeDesc.code = shaderSource.c_str(); | ||
|
||
WGPUShaderModuleDescriptor shaderDesc{}; | ||
shaderDesc.nextInChain = nullptr; | ||
#ifdef WEBGPU_BACKEND_WGPU | ||
shaderDesc.hintCount = 0; | ||
shaderDesc.hints = nullptr; | ||
#endif | ||
shaderDesc.nextInChain = &shaderCodeDesc.chain; | ||
return wgpuDeviceCreateShaderModule(device, &shaderDesc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
#include <vector> | ||
#include <filesystem> | ||
#include <webgpu/webgpu.h> | ||
|
||
class ResourceManager { | ||
public: | ||
/** | ||
* Load a file from `path` using our ad-hoc format and populate the `pointData` | ||
* and `indexData` vectors. | ||
*/ | ||
static bool loadGeometry( | ||
const std::filesystem::path& path, | ||
std::vector<float>& pointData, | ||
std::vector<uint16_t>& indexData | ||
); | ||
|
||
/** | ||
* Create a shader module for a given WebGPU `device` from a WGSL shader source | ||
* loaded from file `path`. | ||
*/ | ||
static WGPUShaderModule loadShaderModule( | ||
const std::filesystem::path& path, | ||
WGPUDevice device | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* A structure with fields labeled with vertex attribute locations can be used | ||
* as input to the entry point of a shader. | ||
*/ | ||
struct VertexInput { | ||
@location(0) position: vec2f, | ||
@location(1) color: vec3f, | ||
}; | ||
|
||
/** | ||
* A structure with fields labeled with builtins and locations can also be used | ||
* as *output* of the vertex shader, which is also the input of the fragment | ||
* shader. | ||
*/ | ||
struct VertexOutput { | ||
@builtin(position) position: vec4f, | ||
// The location here does not refer to a vertex attribute, it just means | ||
// that this field must be handled by the rasterizer. | ||
// (It can also refer to another field of another struct that would be used | ||
// as input to the fragment shader.) | ||
@location(0) color: vec3f, | ||
}; | ||
|
||
@vertex | ||
fn vs_main(in: VertexInput) -> VertexOutput { | ||
// ^^^^^^^^^^^^ We return a custom struct | ||
var out: VertexOutput; // create the output struct | ||
let ratio = 640.0 / 480.0; // The width and height of the target surface | ||
let offset = vec2f(-0.6875, -0.463); // The offset that we want to apply to the position | ||
out.position = vec4f(in.position.x + offset.x, (in.position.y + offset.y) * ratio, 0.0, 1.0); | ||
out.color = in.color; // forward the color attribute to the fragment shader | ||
return out; | ||
} | ||
|
||
@fragment | ||
fn fs_main(in: VertexOutput) -> @location(0) vec4f { | ||
// We apply a gamma-correction to the color | ||
// We need to convert our input sRGB color into linear before the target | ||
// surface converts it back to sRGB. | ||
let linear_color = pow(in.color, vec3f(2.2)); | ||
return vec4f(linear_color, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[points] | ||
# x y r g b | ||
|
||
0.5 0.0 0.0 0.353 0.612 | ||
1.0 0.866 0.0 0.353 0.612 | ||
0.0 0.866 0.0 0.353 0.612 | ||
|
||
0.75 0.433 0.0 0.4 0.7 | ||
1.25 0.433 0.0 0.4 0.7 | ||
1.0 0.866 0.0 0.4 0.7 | ||
|
||
1.0 0.0 0.0 0.463 0.8 | ||
1.25 0.433 0.0 0.463 0.8 | ||
0.75 0.433 0.0 0.463 0.8 | ||
|
||
1.25 0.433 0.0 0.525 0.91 | ||
1.375 0.65 0.0 0.525 0.91 | ||
1.125 0.65 0.0 0.525 0.91 | ||
|
||
1.125 0.65 0.0 0.576 1.0 | ||
1.375 0.65 0.0 0.576 1.0 | ||
1.25 0.866 0.0 0.576 1.0 | ||
|
||
[indices] | ||
0 1 2 | ||
3 4 5 | ||
6 7 8 | ||
9 10 11 | ||
12 13 14 |