forked from Crementif/WiiU-GX2-Shader-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_texture.cpp
154 lines (123 loc) · 6.02 KB
/
example_texture.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "common.h"
#include "example_texture.h"
constexpr const char* s_textureVertexShader = R"(
#version 450
layout(location = 0) in vec2 aPos;
layout(location = 1) in vec2 aTexCoord;
layout(location = 0) out vec2 TexCoord;
void main()
{
TexCoord = aTexCoord;
gl_Position = vec4(aPos.x, aPos.y, 0.0f, 1.0f);
}
)";
constexpr const char* s_texturePixelShader = R"(
#version 450
#extension GL_ARB_shading_language_420pack: enable
layout(location = 0) in vec2 TexCoord;
layout(binding = 0) uniform uf_data
{
float uf_time;
};
layout(location = 0) out vec4 FragColor;
layout(binding = 0) uniform sampler2D frogTexture;
void main()
{
FragColor = texture(frogTexture, TexCoord);
FragColor.x = mod(uf_time.x, 1.0f);
}
)";
#include "TGATexture.h"
#include "example_texture.h"
ExampleTexture::ExampleTexture() {
launchTime = OSGetTime();
// create shader group
std::string errorLog(1024, '\0');
GX2VertexShader *vertexShader = GLSL_CompileVertexShader(s_textureVertexShader, errorLog.data(), (int) errorLog.size(), GLSL_COMPILER_FLAG_NONE);
if (!vertexShader) {
WHBLogPrintf("Vertex shader compilation failed for texture example: %s", errorLog.data());
return;
}
GX2PixelShader *pixelShader = GLSL_CompilePixelShader(s_texturePixelShader, errorLog.data(), (int) errorLog.size(), GLSL_COMPILER_FLAG_NONE);
if (!pixelShader) {
WHBLogPrintf("Pixel shader compilation failed for texture 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);
GX2SetShaderMode(GX2_SHADER_MODE_UNIFORM_BLOCK);
WHBGfxInitShaderAttribute(&s_shaderGroup, "aPos", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32);
WHBGfxInitShaderAttribute(&s_shaderGroup, "aTexCoord", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_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 = 2 * sizeof(float);
s_positionBuffer.elemCount = 4;
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 texture coords
s_texCoordBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER | GX2R_RESOURCE_USAGE_CPU_READ | GX2R_RESOURCE_USAGE_CPU_WRITE | GX2R_RESOURCE_USAGE_GPU_READ;
s_texCoordBuffer.elemSize = 2 * sizeof(float);
s_texCoordBuffer.elemCount = 4;
GX2RCreateBuffer(&s_texCoordBuffer);
void *coordsUploadBuffer = GX2RLockBufferEx(&s_texCoordBuffer, GX2R_RESOURCE_BIND_NONE);
memcpy(coordsUploadBuffer, s_texCoords, s_texCoordBuffer.elemSize * s_texCoordBuffer.elemCount);
GX2RUnlockBufferEx(&s_texCoordBuffer, GX2R_RESOURCE_BIND_NONE);
// upload texture
std::ifstream fs("romfs:/texture.tga", std::ios::in | std::ios::binary);
if (!fs.is_open())
return;
std::vector<uint8_t> data((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
s_texture = TGA_LoadTexture((uint8_t *) data.data(), data.size());
if (s_texture == nullptr)
return;
GX2Sampler sampler;
GX2InitSampler(&sampler, GX2_TEX_CLAMP_MODE_CLAMP, GX2_TEX_XY_FILTER_MODE_LINEAR);
}
void ExampleTexture::Draw() {
// render texture
WHBGfxBeginRender();
WHBGfxBeginRenderTV();
WHBGfxClearColor(0.0f, 0.0f, 1.0f, 1.0f);
GX2SetFetchShader(&s_shaderGroup.fetchShader);
GX2SetVertexShader(s_shaderGroup.vertexShader);
GX2SetPixelShader(s_shaderGroup.pixelShader);
GX2SetShaderMode(GX2_SHADER_MODE_UNIFORM_BLOCK);
// update time uniform in uniform block
float time = (float)OSTicksToMilliseconds((OSGetTime() - launchTime));
time *= 0.001f;
s_timePSUniformBlock[0] = _swapF32(time);
GX2SetPixelUniformBlock(0, sizeof(s_timePSUniformBlock), (void*)s_timePSUniformBlock);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU | GX2_INVALIDATE_MODE_UNIFORM_BLOCK, s_timePSUniformBlock, sizeof(s_timePSUniformBlock));
GX2RSetAttributeBuffer(&s_positionBuffer, 0, s_positionBuffer.elemSize, 0);
GX2RSetAttributeBuffer(&s_texCoordBuffer, 1, s_texCoordBuffer.elemSize, 0);
GX2SetPixelTexture(s_texture, s_shaderGroup.pixelShader->samplerVars[0].location);
GX2SetPixelSampler(&s_sampler, s_shaderGroup.pixelShader->samplerVars[0].location);
GX2DrawEx(GX2_PRIMITIVE_MODE_QUADS, 4, 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);
GX2SetShaderMode(GX2_SHADER_MODE_UNIFORM_BLOCK);
GX2SetPixelUniformBlock(0, sizeof(s_timePSUniformBlock), (void*)s_timePSUniformBlock);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU | GX2_INVALIDATE_MODE_UNIFORM_BLOCK, s_timePSUniformBlock, sizeof(s_timePSUniformBlock));
GX2RSetAttributeBuffer(&s_positionBuffer, 0, s_positionBuffer.elemSize, 0);
GX2RSetAttributeBuffer(&s_texCoordBuffer, 1, s_texCoordBuffer.elemSize, 0);
GX2SetPixelTexture(s_texture, s_shaderGroup.pixelShader->samplerVars[0].location);
GX2SetPixelSampler(&s_sampler, s_shaderGroup.pixelShader->samplerVars[0].location);
GX2DrawEx(GX2_PRIMITIVE_MODE_QUADS, 4, 0, 1);
WHBGfxFinishRenderDRC();
WHBGfxFinishRender();
}
ExampleTexture::~ExampleTexture() {
GX2RDestroyBufferEx(&s_positionBuffer, GX2R_RESOURCE_BIND_NONE);
GX2RDestroyBufferEx(&s_texCoordBuffer, GX2R_RESOURCE_BIND_NONE);
TGA_UnloadTexture(s_texture);
}