samples: Add gx2 triangle sample.

This commit is contained in:
James Benton 2018-05-31 10:20:16 +01:00
parent 4e2099a2e4
commit 9f5066d75a
5 changed files with 197 additions and 0 deletions

Binary file not shown.

View File

@ -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

View File

@ -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

View File

@ -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}")

157
samples/gx2_triangle/main.c Normal file
View File

@ -0,0 +1,157 @@
#include <gfd.h>
#include <gx2/draw.h>
#include <gx2/shaders.h>
#include <gx2/mem.h>
#include <gx2/registers.h>
#include <gx2r/draw.h>
#include <gx2r/buffer.h>
#include <string.h>
#include <stdio.h>
#include <whb/file.h>
#include <whb/proc.h>
#include <whb/sdcard.h>
#include <whb/gfx.h>
#include <whb/log.h>
#include <whb/log_udp.h>
#include <coreinit/systeminfo.h>
#include <coreinit/thread.h>
#include <coreinit/time.h>
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;
}