forked from Crementif/WiiU-GX2-Shader-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_triangle.cpp
123 lines (102 loc) · 4.65 KB
/
example_triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "common.h"
#include "example_triangle.h"
constexpr const char* s_vertexShader = R"(
#version 400 core
layout(location = 0) in vec4 aColor;
layout(location = 1) in vec4 aPosition;
out vec4 vColor;
void main()
{
gl_Position = vec4(aPosition);
vColor = aColor;
}
)";
constexpr const char* s_fragmentShader = R"(
#version 400 core
in vec4 vColor;
out vec4 FragColor;
void main()
{
FragColor = vColor;
}
)";
ExampleTriangle::ExampleTriangle() {
// create shader group
std::string errorLog(1024, '\0');
GX2VertexShader* vertexShader = GLSL_CompileVertexShader(s_vertexShader, errorLog.data(), (int)errorLog.size(), GLSL_COMPILER_FLAG_NONE);
if (!vertexShader) {
WHBLogPrintf("Vertex shader compilation failed for triangle example: %s", errorLog.data());
return;
}
GX2PixelShader* pixelShader = GLSL_CompilePixelShader(s_fragmentShader, errorLog.data(), (int)errorLog.size(), GLSL_COMPILER_FLAG_NONE);
if (!pixelShader) {
WHBLogPrintf("Pixel shader compilation failed for triangle example: %s", errorLog.data());
return;
}
memset(&s_shaderGroup, 0, sizeof(WHBGfxShaderGroup));
s_shaderGroup.vertexShader = vertexShader;
s_shaderGroup.pixelShader = pixelShader;
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, s_shaderGroup.vertexShader->program, s_shaderGroup.vertexShader->size);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, s_shaderGroup.pixelShader->program, s_shaderGroup.pixelShader->size);
WHBGfxInitShaderAttribute(&s_shaderGroup, "aPosition", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32);
WHBGfxInitShaderAttribute(&s_shaderGroup, "aColor", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32);
WHBGfxInitFetchShader(&s_shaderGroup);
// upload vertex position
s_positionBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER | GX2R_RESOURCE_USAGE_CPU_READ | GX2R_RESOURCE_USAGE_CPU_WRITE | GX2R_RESOURCE_USAGE_GPU_READ;
s_positionBuffer.elemSize = 4 * 4;
s_positionBuffer.elemCount = 3;
GX2RCreateBuffer(&s_positionBuffer);
void *posUploadBuffer = GX2RLockBufferEx(&s_positionBuffer, GX2R_RESOURCE_BIND_NONE);
memcpy(posUploadBuffer, s_positionData, s_positionBuffer.elemSize * s_positionBuffer.elemCount);
GX2RUnlockBufferEx(&s_positionBuffer, GX2R_RESOURCE_BIND_NONE);
// upload vertex color
s_colorBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER | GX2R_RESOURCE_USAGE_CPU_READ | GX2R_RESOURCE_USAGE_CPU_WRITE | GX2R_RESOURCE_USAGE_GPU_READ;
s_colorBuffer.elemSize = 4 * 4;
s_colorBuffer.elemCount = 3;
GX2RCreateBuffer(&s_colorBuffer);
void* colUploadBuffer = GX2RLockBufferEx(&s_colorBuffer, GX2R_RESOURCE_BIND_NONE);
memcpy(colUploadBuffer, s_colorData, s_colorBuffer.elemSize * s_colorBuffer.elemCount);
GX2RUnlockBufferEx(&s_colorBuffer, GX2R_RESOURCE_BIND_NONE);
}
void ExampleTriangle::Draw() {
// animate triangle colors
float *colors = (float *)GX2RLockBufferEx(&s_colorBuffer, GX2R_RESOURCE_BIND_NONE);
colors[0] = 1.0f;
colors[1] = colors[1] >= 1.0f ? 0.0f : (colors[1] + 0.01f);
colors[2] = colors[2] >= 1.0f ? 0.0f : (colors[2] + 0.01f);
colors[3] = 1.0f;
colors[4] = colors[4] >= 1.0f ? 0.0f : (colors[4] + 0.01f);
colors[5] = 1.0f;
colors[6] = colors[6] >= 1.0f ? 0.0f : (colors[6] + 0.01f);
colors[7] = 1.0f;
colors[8] = colors[8] >= 1.0f ? 0.0f : (colors[8] + 0.01f);
colors[9] = colors[9] >= 1.0f ? 0.0f : (colors[9] + 0.01f);
colors[10] = 1.0f;
colors[11] = 1.0f;
GX2RUnlockBufferEx(&s_colorBuffer, GX2R_RESOURCE_BIND_NONE);
// render triangle
WHBGfxBeginRender();
WHBGfxBeginRenderTV();
WHBGfxClearColor(0.0f, 0.0f, 1.0f, 1.0f);
GX2SetFetchShader(&s_shaderGroup.fetchShader);
GX2SetVertexShader(s_shaderGroup.vertexShader);
GX2SetPixelShader(s_shaderGroup.pixelShader);
GX2RSetAttributeBuffer(&s_positionBuffer, 0, s_positionBuffer.elemSize, 0);
GX2RSetAttributeBuffer(&s_colorBuffer, 1, s_colorBuffer.elemSize, 0);
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1);
WHBGfxFinishRenderTV();
WHBGfxBeginRenderDRC();
WHBGfxClearColor(1.0f, 0.0f, 1.0f, 1.0f);
GX2SetFetchShader(&s_shaderGroup.fetchShader);
GX2SetVertexShader(s_shaderGroup.vertexShader);
GX2SetPixelShader(s_shaderGroup.pixelShader);
GX2RSetAttributeBuffer(&s_positionBuffer, 0, s_positionBuffer.elemSize, 0);
GX2RSetAttributeBuffer(&s_colorBuffer, 1, s_colorBuffer.elemSize, 0);
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1);
WHBGfxFinishRenderDRC();
WHBGfxFinishRender();
}
ExampleTriangle::~ExampleTriangle() {
GX2RDestroyBufferEx(&s_positionBuffer, GX2R_RESOURCE_BIND_NONE);
GX2RDestroyBufferEx(&s_colorBuffer, GX2R_RESOURCE_BIND_NONE);
}