diff --git a/samples/content/pos_col_shader.gsh b/samples/content/pos_col_shader.gsh new file mode 100644 index 0000000..e1c4694 Binary files /dev/null and b/samples/content/pos_col_shader.gsh differ diff --git a/samples/content/pos_col_shader.psh b/samples/content/pos_col_shader.psh new file mode 100644 index 0000000..75c6832 --- /dev/null +++ b/samples/content/pos_col_shader.psh @@ -0,0 +1,7 @@ +; $MODE = "UniformRegister" +; $NUM_SPI_PS_INPUT_CNTL = 1 +; $SPI_PS_INPUT_CNTL[0].SEMANTIC = 0 +; $SPI_PS_INPUT_CNTL[0].DEFAULT_VAL = 1 + +00 EXP_DONE: PIX0, R0.xyzw +END_OF_PROGRAM diff --git a/samples/content/pos_col_shader.vsh b/samples/content/pos_col_shader.vsh new file mode 100644 index 0000000..8ca0dea --- /dev/null +++ b/samples/content/pos_col_shader.vsh @@ -0,0 +1,14 @@ +; $MODE = "UniformRegister" +; $ATTRIB_VARS[0].name = "aColour" +; $ATTRIB_VARS[0].type = "Float4" +; $ATTRIB_VARS[0].location = 0 +; $ATTRIB_VARS[1].name = "aPosition" +; $ATTRIB_VARS[1].type = "Float4" +; $ATTRIB_VARS[1].location = 1 +; $NUM_SPI_VS_OUT_ID = 1 +; $SPI_VS_OUT_ID[0].SEMANTIC_0 = 0 + +00 CALL_FS NO_BARRIER +01 EXP_DONE: POS0, R2.xyzw +02 EXP_DONE: PARAM0, R1.xyzw NO_BARRIER +END_OF_PROGRAM diff --git a/samples/gx2_triangle/CMakeLists.txt b/samples/gx2_triangle/CMakeLists.txt new file mode 100644 index 0000000..231f3d5 --- /dev/null +++ b/samples/gx2_triangle/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.2) +project(gx2_triangle C) +include("${WUT_ROOT}/share/wut.cmake" REQUIRED) + +add_executable(gx2_triangle + main.c) + +target_link_libraries(gx2_triangle + whb + gfd + gx2 + proc_ui + nsysnet + sysapp) + +wut_create_rpx(gx2_triangle.rpx gx2_triangle) + +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/gx2_triangle.rpx" + DESTINATION "${CMAKE_INSTALL_PREFIX}") diff --git a/samples/gx2_triangle/main.c b/samples/gx2_triangle/main.c new file mode 100644 index 0000000..4802b72 --- /dev/null +++ b/samples/gx2_triangle/main.c @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static const float sPositionData[] = +{ + 1.0f, -1.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 1.0f, 1.0f, +}; + +static const float sColourData[] = +{ + 1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, 1.0f, +}; + +int main(int argc, char **argv) +{ + GX2RBuffer positionBuffer = { 0 }; + GX2RBuffer colourBuffer = { 0 }; + WHBGfxShaderGroup group = { 0 }; + void *buffer = NULL; + char *gshFileData = NULL; + char *sdRootPath = NULL; + char path[256]; + int result = 0; + + WHBLogUdpInit(); + WHBProcInit(); + WHBGfxInit(); + + if (!WHBMountSdCard()) { + result = -1; + goto exit; + } + + sdRootPath = WHBGetSdCardMountPath(); + sprintf(path, "%s/wut/pos_col_shader.gsh", sdRootPath); + + gshFileData = WHBReadWholeFile(path, NULL); + if (!gshFileData) { + result = -1; + WHBLogPrintf("WHBReadWholeFile(%s) returned NULL", path); + goto exit; + } + + if (!WHBGfxLoadGFDShaderGroup(&group, 0, gshFileData)) { + result = -1; + WHBLogPrintf("WHBGfxLoadGFDShaderGroup returned FALSE"); + goto exit; + } + + WHBGfxInitShaderAttribute(&group, "aPosition", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32); + WHBGfxInitShaderAttribute(&group, "aColour", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32); + WHBGfxInitFetchShader(&group); + + WHBFreeWholeFile(gshFileData); + gshFileData = NULL; + + // Set vertex position + positionBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER | + GX2R_RESOURCE_USAGE_CPU_READ | + GX2R_RESOURCE_USAGE_CPU_WRITE | + GX2R_RESOURCE_USAGE_GPU_READ; + positionBuffer.elemSize = 4 * 4; + positionBuffer.elemCount = 3; + GX2RCreateBuffer(&positionBuffer); + buffer = GX2RLockBufferEx(&positionBuffer, 0); + memcpy(buffer, sPositionData, positionBuffer.elemSize * positionBuffer.elemCount); + GX2RUnlockBufferEx(&positionBuffer, 0); + + // Set vertex colour + colourBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER | + GX2R_RESOURCE_USAGE_CPU_READ | + GX2R_RESOURCE_USAGE_CPU_WRITE | + GX2R_RESOURCE_USAGE_GPU_READ; + colourBuffer.elemSize = 4 * 4; + colourBuffer.elemCount = 3; + GX2RCreateBuffer(&colourBuffer); + buffer = GX2RLockBufferEx(&colourBuffer, 0); + memcpy(buffer, sColourData, colourBuffer.elemSize * colourBuffer.elemCount); + GX2RUnlockBufferEx(&colourBuffer, 0); + + WHBLogPrintf("Begin rendering..."); + while (WHBProcIsRunning()) { + // Animate colours... + float *colours = (float *)GX2RLockBufferEx(&colourBuffer, 0); + colours[0] = 1.0f; + colours[1] = colours[1] >= 1.0f ? 0.0f : (colours[1] + 0.01f); + colours[2] = colours[2] >= 1.0f ? 0.0f : (colours[2] + 0.01f); + colours[3] = 1.0f; + + colours[4] = colours[4] >= 1.0f ? 0.0f : (colours[4] + 0.01f); + colours[5] = 1.0f; + colours[6] = colours[6] >= 1.0f ? 0.0f : (colours[6] + 0.01f); + colours[7] = 1.0f; + + colours[8] = colours[8] >= 1.0f ? 0.0f : (colours[8] + 0.01f); + colours[9] = colours[9] >= 1.0f ? 0.0f : (colours[9] + 0.01f); + colours[10] = 1.0f; + colours[11] = 1.0f; + GX2RUnlockBufferEx(&colourBuffer, 0); + + // Render! + WHBGfxBeginRender(); + + WHBGfxBeginRenderTV(); + WHBGfxClearColor(0.0f, 0.0f, 1.0f, 1.0f); + GX2SetFetchShader(&group.fetchShader); + GX2SetVertexShader(group.vertexShader); + GX2SetPixelShader(group.pixelShader); + GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0); + GX2RSetAttributeBuffer(&colourBuffer, 1, colourBuffer.elemSize, 0); + GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1); + WHBGfxFinishRenderTV(); + + WHBGfxBeginRenderDRC(); + WHBGfxClearColor(1.0f, 0.0f, 1.0f, 1.0f); + GX2SetFetchShader(&group.fetchShader); + GX2SetVertexShader(group.vertexShader); + GX2SetPixelShader(group.pixelShader); + GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0); + GX2RSetAttributeBuffer(&colourBuffer, 1, colourBuffer.elemSize, 0); + GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1); + WHBGfxFinishRenderDRC(); + + WHBGfxFinishRender(); + } + +exit: + WHBLogPrintf("Exiting..."); + GX2RDestroyBufferEx(&positionBuffer, 0); + GX2RDestroyBufferEx(&colourBuffer, 0); + WHBUnmountSdCard(); + WHBGfxShutdown(); + WHBProcShutdown(); + WHBLogUdpDeinit(); + return result; +}