The following sections provide a tutorial on how to create a program to render a triangle on the Xbox One dev kit:
- Creating a project
- Adding pixel and vertex shaders
- Adding declarations for the triangle
- Initializing triangle data
- Rendering the triangle
- Building and deploying
Creating a project
First, create a new project for Xbox One by using a template supplied with the XDK.
To create a new project
-
On your development PC create a new Visual C++ project using the Durango platform and Direct3D Game template.
Do not use spaces or other non-alphanumeric characters in the project name
Adding pixel and vertex shaders
Next, add source code for the pixel shader and the vertex shader.
To add source code for a pixel shader
- Select the project in Solution Explorer.
- On the Project menu, click Add New Item.
-
Select HLSL in the tree, and then select Pixel Shader File (.hlsl).
- Click Add.
-
Replace the default code for the pixel shader with the following:
struct Interpolants { float4 position : SV_POSITION0; float4 color : COLOR0; }; struct Pixel { float4 color : SV_TARGET0; }; Pixel main( Interpolants In ) { Pixel Out; Out.color = In.color; return Out; }
- Right-click PixelShader.hlsl in Solution Explorer and choose Properties from the context menu.
-
For Configuration, select All Configurations, and then select HLSL CompilerAll Options in the tree control.
- Set Header File Name to PixelShader.h. 这里的设定表示该hlsl文件会变成那个.h文件;工程在build的时候会自动根据这个hlsl文件生成这个头文件
- Set Header Variable Name to g_ps_main. 在上一步中会生成的头文件中会有一个数组存储shader的代码,这个变量就是这个数组的名字,后面根据的CreateResource()函数,创建shader(CreateVertexShader和CreatePixelShader)的时候,会引用这个数组名;
- Clear the value for Object File Name.
-
Set Shader Model to Shader Model 5 (/5_0).
-
Verify your changes, and then click OK.
To add source code for a vertex shader
- Select the project in Solution Explorer.
- On the Project menu, click Add New Item.
-
Select HLSL in the Add New Item dialog box, and select Vertex Shader File (.hlsl).
- Click Add.
-
Replace the default code for the vertex shader code with the following code.
struct Vertex { float4 position : POSITION0; float4 color : COLOR0; }; struct Interpolants { float4 position : SV_POSITION0; float4 color : COLOR0; }; Interpolants main( Vertex In ) { return In; }
- Right-click VertexShader.hlsl in Solution Explorer and choose Properties from the context menu.
- Select All Configurations for Configurations, and then select HLSL CompilerAll Options in the tree control.
- Set Header File Name to VertexShader.h.
- Set Header Variable Name to g_vs_main.
- Clear the value for Object File Name.
- Set Shader Model to Shader Model 5 (/5_0).
-
Verify your changes, and then click OK.
- Add the following
include
statements to game.cpp:#include "PixelShader.h" #include "VertexShader.h"
注意这里虽然新增了两行去包含两个文件,但是在工程中现在这两个文件是不存在的,这两个文件会在全部配置完成后编译工程成功后可以由hlsl shader compiler为durango平台自动生成,比如该工程生成的两个文件的内容是:
#if 0 // // Generated by Microsoft (R) HLSL Shader Compiler For Durango // // // // Input signature: // // Name Index Mask Register SysValue Format Used // -------------------- ----- ------ -------- -------- ------- ------ // POSITION 0 xyzw 0 NONE float xyzw // COLOR 0 xyzw 1 NONE float xyzw // // // Output signature: // // Name Index Mask Register SysValue Format Used // -------------------- ----- ------ -------- -------- ------- ------ // SV_POSITION 0 xyzw 0 POS float xyzw // COLOR 0 xyzw 1 NONE float xyzw // vs_5_0 // Opaque Custom Data - XBOX Precompiled Shader Header // First Precompiled Shader at offset:[32] // Embedded Data: // 0x00000020 - Offset To First Precompiled Shader // 0x00010050 - Original Shader Descriptor // 0x0000001b - Original Shader Size dcl_globalFlags refactoringAllowed | skipOptimization dcl_input v0.xyzw dcl_input v1.xyzw dcl_output_siv o0.xyzw, position dcl_output o1.xyzw // // Initial variable locations: // v0.x <- In.position.x; v0.y <- In.position.y; v0.z <- In.position.z; v0.w <- In.position.w; // v1.x <- In.color.x; v1.y <- In.color.y; v1.z <- In.color.z; v1.w <- In.color.w; // o1.x <- <main return value>.color.x; o1.y <- <main return value>.color.y; o1.z <- <main return value>.color.z; o1.w <- <main return value>.color.w; // o0.x <- <main return value>.position.x; o0.y <- <main return value>.position.y; o0.z <- <main return value>.position.z; o0.w <- <main return value>.position.w // #line 21 "C:UserswangsongweiDesktopDurangoGameDurangoGameVertexShader1.hlsl" mov o0.xyzw, v0.xyzw mov o1.xyzw, v1.xyzw ret // Opaque Custom Data - XBOX Precompiled Shader Object // Offset:[32], bundle is:[126] DWORDS. // Disassembled Precompiled Shader Object: // Shader Type : (VS) // Shader Hw Stage : (VS) // HLSL Source Filename : C:UserswangsongweiDesktopDurangoGameDurangoGameVertexShader1.hlsl // HLSL Entrypoint Name : main // __XBOX_SHADER_UNIQUE_HASH : 0x9fc1b81a // Shader Semantic Hash : 0xa4ddca041f70c960 // // Shader Data: // ----------------- VS Data ------------------------ ; Input Semantic Mappings ; [0] generic, usageIdx 0, channelMask 15, v[4:7] ; [1] generic, usageIdx 1, channelMask 15, v[8:11] ; Output Semantic Mappings ; [0] generic, usageIdx 1, paramIdx 0, paramSlot 0 codeLenInByte = 28 bytes; ; launchModeFlags = 0x0000000E srdTableSupport = TRUE immediateSupportExt = TRUE srdTableSupportExt = TRUE userElementCount = 2; ; userElements[0] = PTR_VERTEX_BUFFER_TABLE, Element=1 dwords, s[2:3] ; userElements[1] = SUB_PTR_FETCH_SHADER, s[2:3] extUserElementCount = 0; NumVgprs = 12; NumSgprs = 5; FloatMode = 192; IeeeMode = 0; FlatPtr32 = 0; ScratchSize = 0 dwords/thread; LDSByteSize = 0 bytes/workgroup (compile time only); ScratchWaveOffsetReg = s65535; ; texSamplerUsage = 0x00000000 ; constBufUsage = 0x00000000 ; SPI_SHADER_PGM_RSRC2_VS = 0x00000008 SSPRV:USER_SGPR = 4 ; PA_CL_VS_OUT_CNTL = 0x00000000 ; SPI_VS_OUT_CONFIG = 0x00000000 SVOC:VS_EXPORT_COUNT = 0 SVOC:VS_HALF_PACK = 0 SVOC:VS_EXPORTS_FOG = 0 SVOC:VS_OUT_FOG_VEC_ADDR = 0 ; SPI_SHADER_POS_FORMAT = 0x00000004 SSPF:POS0_EXPORT_FORMAT = 4 SSPF:POS1_EXPORT_FORMAT = 0 SSPF:POS2_EXPORT_FORMAT = 0 SSPF:POS3_EXPORT_FORMAT = 0 VGT_STRMOUT_CONFIG = 0x0 VGT_STRMOUT_CONFIG:RAST_STREAM = 0 VGT_STRMOUT_CONFIG:STREAMOUT_0_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_1_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_2_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_3_EN = 0 ; exportVertexSize = 4 // Shader Instructions: shader main asic(CI) type(VS) s_swappc_b64 s[2:3], s[2:3] // 000000000000: BE822102 s_waitcnt vmcnt(0) & lgkmcnt(15) // 000000000004: BF8C0F70 exp pos0, v4, v5, v6, v7 done // 000000000008: F80008CF 07060504 exp param0, v8, v9, v10, v11 // 000000000010: F800020F 0B0A0908 s_endpgm // 000000000018: BF810000 end // Shader Statistics: // NumActiveWaveFront: 40 // WorkgroupSize: 64 // NumVRegs: 12 // NumSRegs: 4 // NumActiveWaveFrontByVReg: 40 // NumActiveWaveFrontBySReg: 40 // NumActiveWaveFrontByLDS: 40 // CycleSim: 82 // CycleSimNWaves: 82 // Code Size: 28 //Assembler Counts: // num_32bit_ops : 3 // num_64bit_ops : 2 // num_alu : 0 // num_fc : 3 // num_inst_exp : 2 // num_inst_ldsgds : 0 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 0 // num_inst_sopp : 2 // num_inst_sopc : 0 // num_inst_sop1 : 1 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 0 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 0 // constant_literal_operands : 0 // num_tex : 2 // num_vmem_group : 0 // vmem_grouping_started : 0 // Shader Statistics: // m_uNumActiveWaveFront : 40 // m_uVRegLimit : 256 // m_uSRegLimit : 512 // m_uNumVRegs : 12 // m_uNumSRegs : 4 // m_uSCCodeSize : 28 // m_uWorkgroupSize : 64 // m_uNumActiveWaveFrontByVReg : 40 // m_uNumActiveWaveFrontBySReg : 40 // m_uNumActiveWaveFrontByLDS : 40 // m_uCycleSim1Wave : 82 // m_uCycleSimNWaves : 82 // Assembler Counts: // num_32bit_ops : 3 // num_64bit_ops : 2 // num_alu : 0 // num_fc : 3 // num_inst_exp : 2 // num_inst_ldsgds : 0 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 0 // num_inst_sopp : 2 // num_inst_sopc : 0 // num_inst_sop1 : 1 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 0 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 0 // num_inst_smem : 0 // constant_literal_operands : 0 // num_tex : 2 // num_vmem_group : 0 // vmem_grouping_started : 0 // m_fFullPrecompilePromise : 0 // m_fIntegerDivideExpansion : 0 // m_fIntegerModExpansion : 0 // m_fDoubleDivideExpansion : 0 // m_fUsesScratch : 0 // m_fPerThreadIndirection : 0 // m_fUsingTableLoads : 0 // m_fUsingSHmemLoads : 0 // fSemanticHashFlagOverrideApplied : 0 // fPSHasNoUAVWrite : 0 // version : 2 // fCodePatched : 0 // Opaque Custom Data - XBOX Precompiled Shader Object // Offset:[160], bundle is:[137] DWORDS. // Disassembled Precompiled Shader Object: // Shader Type : (VS) // Shader Hw Stage : (ES) // HLSL Source Filename : C:UserswangsongweiDesktopDurangoGameDurangoGameVertexShader1.hlsl // HLSL Entrypoint Name : main // __XBOX_SHADER_UNIQUE_HASH : 0x9fc1b81a // Shader Semantic Hash : 0xa4ddca041f70c960 // // Shader Data: // ----------------- VS Data ------------------------ ; Input Semantic Mappings ; [0] generic, usageIdx 0, channelMask 15, v[4:7] ; [1] generic, usageIdx 1, channelMask 15, v[8:11] ; Output Semantic Mappings ; [0] position, usageIdx 0, paramIdx 0, paramSlot 0 ; [1] generic, usageIdx 1, paramIdx 1, paramSlot 0 codeLenInByte = 80 bytes; ; launchModeFlags = 0x0000000E srdTableSupport = TRUE immediateSupportExt = TRUE srdTableSupportExt = TRUE userElementCount = 4; ; userElements[0] = PTR_VERTEX_BUFFER_TABLE, Element=1 dwords, s[2:3] ; userElements[1] = PTR_EXTENDED_USER_DATA, ReferencedExtUserElements=1, s[2:3] ; userElements[2] = PTR_INTERNAL_GLOBAL_TABLE, Element=1 dwords, s[2:3] ; userElements[3] = SUB_PTR_FETCH_SHADER, s[2:3] extUserElementCount = 0; NumVgprs = 12; NumSgprs = 6; FloatMode = 192; IeeeMode = 0; FlatPtr32 = 0; ScratchSize = 0 dwords/thread; LDSByteSize = 0 bytes/workgroup (compile time only); ScratchWaveOffsetReg = s65535; ; texSamplerUsage = 0x00000000 ; constBufUsage = 0x00000000 ; SPI_SHADER_PGM_RSRC2_VS = 0x00000008 SSPRV:USER_SGPR = 4 ; PA_CL_VS_OUT_CNTL = 0x00000000 ; SPI_VS_OUT_CONFIG = 0x00000000 SVOC:VS_EXPORT_COUNT = 0 SVOC:VS_HALF_PACK = 0 SVOC:VS_EXPORTS_FOG = 0 SVOC:VS_OUT_FOG_VEC_ADDR = 0 ; SPI_SHADER_POS_FORMAT = 0x00000004 SSPF:POS0_EXPORT_FORMAT = 4 SSPF:POS1_EXPORT_FORMAT = 0 SSPF:POS2_EXPORT_FORMAT = 0 SSPF:POS3_EXPORT_FORMAT = 0 VGT_STRMOUT_CONFIG = 0x0 VGT_STRMOUT_CONFIG:RAST_STREAM = 0 VGT_STRMOUT_CONFIG:STREAMOUT_0_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_1_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_2_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_3_EN = 0 ; exportVertexSize = 8 ; esGsRingItemSize = 8 // Shader Instructions: shader main asic(CI) type(VS) s_swappc_b64 s[2:3], s[2:3] // 000000000000: BE822102 s_load_dwordx4 s[0:3], s[2:3], 0x08 // 000000000004: C0800308 s_waitcnt vmcnt(0) & lgkmcnt(0) // 000000000008: BF8C0070 buffer_store_dword v4, v0, s[0:3], s4 glc slc // 00000000000C: E0704000 04400400 buffer_store_dword v5, v0, s[0:3], s4 offset:4 glc slc // 000000000014: E0704004 04400500 buffer_store_dword v6, v0, s[0:3], s4 offset:8 glc slc // 00000000001C: E0704008 04400600 buffer_store_dword v7, v0, s[0:3], s4 offset:12 glc slc // 000000000024: E070400C 04400700 buffer_store_dword v8, v0, s[0:3], s4 offset:16 glc slc // 00000000002C: E0704010 04400800 buffer_store_dword v9, v0, s[0:3], s4 offset:20 glc slc // 000000000034: E0704014 04400900 buffer_store_dword v10, v0, s[0:3], s4 offset:24 glc slc // 00000000003C: E0704018 04400A00 buffer_store_dword v11, v0, s[0:3], s4 offset:28 glc slc // 000000000044: E070401C 04400B00 s_endpgm // 00000000004C: BF810000 end // Shader Statistics: // NumActiveWaveFront: 40 // WorkgroupSize: 64 // NumVRegs: 12 // NumSRegs: 5 // NumActiveWaveFrontByVReg: 40 // NumActiveWaveFrontBySReg: 40 // NumActiveWaveFrontByLDS: 40 // CycleSim: 85 // CycleSimNWaves: 85 // Code Size: 80 //Assembler Counts: // num_32bit_ops : 4 // num_64bit_ops : 8 // num_alu : 0 // num_fc : 4 // num_inst_exp : 0 // num_inst_ldsgds : 0 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 8 // num_inst_sopp : 2 // num_inst_sopc : 0 // num_inst_sop1 : 1 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 0 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 0 // constant_literal_operands : 0 // num_tex : 8 // num_vmem_group : 1 // vmem_grouping_started : 0 // Shader Statistics: // m_uNumActiveWaveFront : 40 // m_uVRegLimit : 256 // m_uSRegLimit : 512 // m_uNumVRegs : 12 // m_uNumSRegs : 5 // m_uSCCodeSize : 80 // m_uWorkgroupSize : 64 // m_uNumActiveWaveFrontByVReg : 40 // m_uNumActiveWaveFrontBySReg : 40 // m_uNumActiveWaveFrontByLDS : 40 // m_uCycleSim1Wave : 85 // m_uCycleSimNWaves : 85 // Assembler Counts: // num_32bit_ops : 4 // num_64bit_ops : 8 // num_alu : 0 // num_fc : 4 // num_inst_exp : 0 // num_inst_ldsgds : 0 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 8 // num_inst_sopp : 2 // num_inst_sopc : 0 // num_inst_sop1 : 1 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 0 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 0 // num_inst_smem : 1 // constant_literal_operands : 0 // num_tex : 8 // num_vmem_group : 1 // vmem_grouping_started : 0 // m_fFullPrecompilePromise : 0 // m_fIntegerDivideExpansion : 0 // m_fIntegerModExpansion : 0 // m_fDoubleDivideExpansion : 0 // m_fUsesScratch : 0 // m_fPerThreadIndirection : 0 // m_fUsingTableLoads : 0 // m_fUsingSHmemLoads : 0 // fSemanticHashFlagOverrideApplied : 0 // fPSHasNoUAVWrite : 0 // version : 2 // fCodePatched : 0 // Opaque Custom Data - XBOX Precompiled Shader Object // Offset:[299], bundle is:[132] DWORDS. // Disassembled Precompiled Shader Object: // Shader Type : (VS) // Shader Hw Stage : (LS) // HLSL Source Filename : C:UserswangsongweiDesktopDurangoGameDurangoGameVertexShader1.hlsl // HLSL Entrypoint Name : main // __XBOX_SHADER_UNIQUE_HASH : 0x9fc1b81a // Shader Semantic Hash : 0xa4ddca041f70c960 // // Shader Data: // ----------------- VS Data ------------------------ ; Input Semantic Mappings ; [0] generic, usageIdx 0, channelMask 15, v[4:7] ; [1] generic, usageIdx 1, channelMask 15, v[8:11] ; Output Semantic Mappings ; [0] position, usageIdx 0, paramIdx 0, paramSlot 0 ; [1] generic, usageIdx 1, paramIdx 1, paramSlot 0 codeLenInByte = 56 bytes; ; launchModeFlags = 0x0000000E srdTableSupport = TRUE immediateSupportExt = TRUE srdTableSupportExt = TRUE userElementCount = 2; ; userElements[0] = PTR_VERTEX_BUFFER_TABLE, Element=1 dwords, s[2:3] ; userElements[1] = SUB_PTR_FETCH_SHADER, s[2:3] extUserElementCount = 0; NumVgprs = 12; NumSgprs = 5; FloatMode = 192; IeeeMode = 0; FlatPtr32 = 0; ScratchSize = 0 dwords/thread; LDSByteSize = 0 bytes/workgroup (compile time only); ScratchWaveOffsetReg = s65535; ; texSamplerUsage = 0x00000000 ; constBufUsage = 0x00000000 ; SPI_SHADER_PGM_RSRC2_VS = 0x00000008 SSPRV:USER_SGPR = 4 ; PA_CL_VS_OUT_CNTL = 0x00000000 ; SPI_VS_OUT_CONFIG = 0x00000000 SVOC:VS_EXPORT_COUNT = 0 SVOC:VS_HALF_PACK = 0 SVOC:VS_EXPORTS_FOG = 0 SVOC:VS_OUT_FOG_VEC_ADDR = 0 ; SPI_SHADER_POS_FORMAT = 0x00000004 SSPF:POS0_EXPORT_FORMAT = 4 SSPF:POS1_EXPORT_FORMAT = 0 SSPF:POS2_EXPORT_FORMAT = 0 SSPF:POS3_EXPORT_FORMAT = 0 VGT_STRMOUT_CONFIG = 0x0 VGT_STRMOUT_CONFIG:RAST_STREAM = 0 VGT_STRMOUT_CONFIG:STREAMOUT_0_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_1_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_2_EN = 0 VGT_STRMOUT_CONFIG:STREAMOUT_3_EN = 0 ; vgprCompCnt = 1 ; exportVertexSize = 8 ; lsStride = 32 // Shader Instructions: shader main asic(CI) type(VS) s_swappc_b64 s[2:3], s[2:3] // 000000000000: BE822102 s_mov_b32 m0, 0x00010000 // 000000000004: BEFC03FF 00010000 s_waitcnt vmcnt(0) & lgkmcnt(15) // 00000000000C: BF8C0F70 v_lshlrev_b32 v0, 5, v1 // 000000000010: 34000285 ds_write2_b32 v0, v4, v5 offset1:1 // 000000000014: D8380100 00050400 ds_write2_b32 v0, v6, v7 offset0:2 offset1:3 // 00000000001C: D8380302 00070600 ds_write2_b32 v0, v8, v9 offset0:4 offset1:5 // 000000000024: D8380504 00090800 ds_write2_b32 v0, v10, v11 offset0:6 offset1:7 // 00000000002C: D8380706 000B0A00 s_endpgm // 000000000034: BF810000 end // Shader Statistics: // NumActiveWaveFront: 40 // WorkgroupSize: 64 // NumVRegs: 12 // NumSRegs: 4 // NumActiveWaveFrontByVReg: 40 // NumActiveWaveFrontBySReg: 40 // NumActiveWaveFrontByLDS: 40 // CycleSim: 80 // CycleSimNWaves: 80 // Code Size: 56 //Assembler Counts: // num_32bit_ops : 5 // num_64bit_ops : 4 // num_alu : 1 // num_fc : 4 // num_inst_exp : 0 // num_inst_ldsgds : 4 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 0 // num_inst_sopp : 2 // num_inst_sopc : 0 // num_inst_sop1 : 2 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 1 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 0 // constant_literal_operands : 0 // num_tex : 4 // num_vmem_group : 0 // vmem_grouping_started : 0 // Shader Statistics: // m_uNumActiveWaveFront : 40 // m_uVRegLimit : 256 // m_uSRegLimit : 512 // m_uNumVRegs : 12 // m_uNumSRegs : 4 // m_uSCCodeSize : 56 // m_uWorkgroupSize : 64 // m_uNumActiveWaveFrontByVReg : 40 // m_uNumActiveWaveFrontBySReg : 40 // m_uNumActiveWaveFrontByLDS : 40 // m_uCycleSim1Wave : 80 // m_uCycleSimNWaves : 80 // Assembler Counts: // num_32bit_ops : 5 // num_64bit_ops : 4 // num_alu : 1 // num_fc : 4 // num_inst_exp : 0 // num_inst_ldsgds : 4 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 0 // num_inst_sopp : 2 // num_inst_sopc : 0 // num_inst_sop1 : 2 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 1 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 0 // num_inst_smem : 0 // constant_literal_operands : 0 // num_tex : 4 // num_vmem_group : 0 // vmem_grouping_started : 0 // m_fFullPrecompilePromise : 0 // m_fIntegerDivideExpansion : 0 // m_fIntegerModExpansion : 0 // m_fDoubleDivideExpansion : 0 // m_fUsesScratch : 0 // m_fPerThreadIndirection : 0 // m_fUsingTableLoads : 0 // m_fUsingSHmemLoads : 0 // fSemanticHashFlagOverrideApplied : 0 // fPSHasNoUAVWrite : 0 // version : 2 // fCodePatched : 0 // Approximately 3 instruction slots used #endif const BYTE g_vs_main[] = { 68, 88, 66, 67, 69, 20, 129, 136, 215, 73, 53, 15, 169, 15, 129, 169, 105, 218, 127, 10, 1, 0, 0, 0, 212, 118, 0, 0, 7, 0, 0, 0, 60, 0, 0, 0, 176, 0, 0, 0, 0, 1, 。。。 。。。 }
#if 0 // // Generated by Microsoft (R) HLSL Shader Compiler For Durango // // // // Input signature: // // Name Index Mask Register SysValue Format Used // -------------------- ----- ------ -------- -------- ------- ------ // SV_POSITION 0 xyzw 0 POS float // COLOR 0 xyzw 1 NONE float xyzw // // // Output signature: // // Name Index Mask Register SysValue Format Used // -------------------- ----- ------ -------- -------- ------- ------ // SV_TARGET 0 xyzw 0 TARGET float xyzw // ps_5_0 // Opaque Custom Data - XBOX Precompiled Shader Header // First Precompiled Shader at offset:[27] // Embedded Data: // 0x0000001b - Offset To First Precompiled Shader // 0x00000050 - Original Shader Descriptor // 0x00000016 - Original Shader Size dcl_globalFlags refactoringAllowed | skipOptimization dcl_input_ps linear v1.xyzw dcl_output o0.xyzw dcl_temps 1 // // Initial variable locations: // v0.x <- In.position.x; v0.y <- In.position.y; v0.z <- In.position.z; v0.w <- In.position.w; // v1.x <- In.color.x; v1.y <- In.color.y; v1.z <- In.color.z; v1.w <- In.color.w; // o0.x <- <main return value>.color.x; o0.y <- <main return value>.color.y; o0.z <- <main return value>.color.z; o0.w <- <main return value>.color.w // #line 21 "C:UserswangsongweiDesktopDurangoGameDurangoGamePixelShader1.hlsl" mov r0.xyzw, v1.xyzw // r0.x <- Out.color.x; r0.y <- Out.color.y; r0.z <- Out.color.z; r0.w <- Out.color.w mov o0.xyzw, r0.xyzw ret // Opaque Custom Data - XBOX Precompiled Shader Object // Offset:[27], bundle is:[130] DWORDS. // Disassembled Precompiled Shader Object: // Shader Type : (PS) // Shader Hw Stage : (PS) // HLSL Source Filename : C:UserswangsongweiDesktopDurangoGameDurangoGamePixelShader1.hlsl // HLSL Entrypoint Name : main // __XBOX_SHADER_UNIQUE_HASH : 0x8ff4423e // Shader Semantic Hash : 0xb9c70b8dba049c28 // // Shader Data: // ----------------- PS Data ------------------------ ; Input Semantic Mappings ; [0] generic, usageIdx 1, channelMask 15, param0, paramSlot 1, DefaultVal={0,0,0,0} codeLenInByte = 56 bytes; ; launchModeFlags = 0x0000000E srdTableSupport = TRUE immediateSupportExt = TRUE srdTableSupportExt = TRUE userElementCount = 0; extUserElementCount = 0; NumVgprs = 5; NumSgprs = 4; FloatMode = 192; IeeeMode = 0; FlatPtr32 = 0; ScratchSize = 0 dwords/thread; LDSByteSize = 0 bytes/workgroup (compile time only); ScratchWaveOffsetReg = s65535; ; texSamplerUsage = 0x00000000 ; constBufUsage = 0x00000000 ; SPI_SHADER_PGM_RSRC2_PS = 0x00000004 SSPRP:SCRATCH_EN = 0 SSPRP:USER_SGPR = 2 SSPRP:TRAP_PRESENT = 0 SSPRP:WAVE_CNT_EN = 0 SSPRP:EXTRA_LDS_SIZE = 0 SSPRP:EXCP_EN = 0 ; SPI_SHADER_Z_FORMAT = 0x00000000 SPZF:Z_EXPORT_FORMAT = 0; SPI_SHADER_ZERO ; SPI_PS_IN_CONTROL = 0x00000001 SPIC:NUM_INTERP = 1 SPIC:PARAM_GEN = 0 SPIC:FOG_ADDR = 0 SPIC:BC_OPTIMIZE_DISABLE = 0 SPIC:PASS_FOG_THROUGH_PS = 0 ; SPI_PS_INPUT_ADDR = 0x00000002 SPIA:PERSP_CENTER_ENA = 1 ; DB_SHADER_CONTROL = 0x00000010 DB:Z_ORDER = 1 DB:CONSERVATIVE_Z_EXPORT = 0; EXPORT_ANY_Z ; CB_SHADER_MASK = 0x0000000F CB:OUTPUT0_ENABLE = 15 // Shader Instructions: shader main asic(CI) type(PS) // s_ps_state in s0 s_mov_b32 m0, s2 // 000000000000: BEFC0302 v_interp_p1_f32 v2, v0, attr0.x // 000000000004: C8080000 v_interp_p1_f32 v3, v0, attr0.y // 000000000008: C80C0100 v_interp_p1_f32 v4, v0, attr0.z // 00000000000C: C8100200 v_interp_p1_f32 v0, v0, attr0.w // 000000000010: C8000300 v_interp_p2_f32 v2, v1, attr0.x // 000000000014: C8090001 v_interp_p2_f32 v3, v1, attr0.y // 000000000018: C80D0101 v_interp_p2_f32 v4, v1, attr0.z // 00000000001C: C8110201 v_interp_p2_f32 v0, v1, attr0.w // 000000000020: C8010301 v_cvt_pkrtz_f16_f32 v1, v2, v3 // 000000000024: 5E020702 v_cvt_pkrtz_f16_f32 v0, v4, v0 // 000000000028: 5E000104 exp mrt0, v1, v1, v0, v0 done compr vm // 00000000002C: F8001C0F 00000001 s_endpgm // 000000000034: BF810000 end // Shader Statistics: // NumActiveWaveFront: 40 // WorkgroupSize: 64 // NumVRegs: 5 // NumSRegs: 3 // NumActiveWaveFrontByVReg: 40 // NumActiveWaveFrontBySReg: 40 // NumActiveWaveFrontByLDS: 40 // CycleSim: 82 // CycleSimNWaves: 82 // Code Size: 56 //Assembler Counts: // num_32bit_ops : 12 // num_64bit_ops : 1 // num_alu : 10 // num_fc : 2 // num_inst_exp : 1 // num_inst_ldsgds : 0 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 0 // num_inst_sopp : 1 // num_inst_sopc : 0 // num_inst_sop1 : 1 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 2 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 8 // constant_literal_operands : 0 // num_tex : 1 // num_vmem_group : 0 // vmem_grouping_started : 0 // //Compilation Notes: // // PS has no UAV writes, consider forcing REZ/LATEZ. // Shader Statistics: // m_uNumActiveWaveFront : 40 // m_uVRegLimit : 256 // m_uSRegLimit : 512 // m_uNumVRegs : 5 // m_uNumSRegs : 3 // m_uSCCodeSize : 56 // m_uWorkgroupSize : 64 // m_uNumActiveWaveFrontByVReg : 40 // m_uNumActiveWaveFrontBySReg : 40 // m_uNumActiveWaveFrontByLDS : 40 // m_uCycleSim1Wave : 82 // m_uCycleSimNWaves : 82 // Assembler Counts: // num_32bit_ops : 12 // num_64bit_ops : 1 // num_alu : 10 // num_fc : 2 // num_inst_exp : 1 // num_inst_ldsgds : 0 // num_inst_mimg : 0 // num_inst_mtbuf : 0 // num_inst_mubuf : 0 // num_inst_sopp : 1 // num_inst_sopc : 0 // num_inst_sop1 : 1 // num_inst_sop2 : 0 // num_inst_smrd : 0 // num_inst_sopk : 0 // num_inst_vop1 : 0 // num_inst_vop2 : 2 // num_inst_vop3 : 0 // num_inst_vopc : 0 // num_inst_vintrp : 8 // num_inst_smem : 0 // constant_literal_operands : 0 // num_tex : 1 // num_vmem_group : 0 // vmem_grouping_started : 0 // m_fFullPrecompilePromise : 0 // m_fIntegerDivideExpansion : 0 // m_fIntegerModExpansion : 0 // m_fDoubleDivideExpansion : 0 // m_fUsesScratch : 0 // m_fPerThreadIndirection : 0 // m_fUsingTableLoads : 0 // m_fUsingSHmemLoads : 0 // fSemanticHashFlagOverrideApplied : 0 // fPSHasNoUAVWrite : 1 // version : 2 // fCodePatched : 0 // Approximately 3 instruction slots used #endif const BYTE g_ps_main[] = { 68, 88, 66, 67, 62, 154, 8, 223, 12, 158, 224, 177, 58, 26, 76, 41, 172, 172, 35, 60, 1, 0, 0, 0, 112, 82, 0, 0, 7, 0, 0, 0, 60, 0, 0, 0, 176, 0, 0, 0, 4, 1, 0, 0, 56, 1, 0, 0, 188, 3, 0, 0, 88, 4, 0, 0, 96, 82, 0, 0, ... ... }
Adding declarations for the triangle
Next, add declarations to the header file to draw a triangle.
To add declarations to the header file to draw a triangle
- Open Game.h in the source editor.
-
Add the following declarations to the private section of the Game class.
// Declarations for drawing a triangle struct s_Vertex { float vPostion[4]; float vColor[4]; }; Microsoft::WRL::ComPtr<ID3D11InputLayout> m_InputLayout; Microsoft::WRL::ComPtr<ID3D11Buffer> m_VertexBuffer; Microsoft::WRL::ComPtr<ID3D11VertexShader> m_VertexShader; Microsoft::WRL::ComPtr<ID3D11PixelShader> m_PixelShader;
Shown here for reference, the following code lists the whole class declaration.
ref class Game sealed { public: Game(); // Initialization and management void Initialize(Windows::UI::Core::CoreWindow^ window); // Basic game loop void Tick(); void Update(float totalTime, float elapsedTime); void Render(); // Rendering helpers void Clear(); void Present(); private: void CreateDevice(); void CreateResources(); // Core Application state Platform::Agile<Windows::UI::Core::CoreWindow> m_window; Windows::Foundation::Rect m_windowBounds; // Direct3D Objects D3D_FEATURE_LEVEL m_featureLevel; Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice; Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_d3dContext; // Rendering resources Microsoft::WRL::ComPtr<IDXGISwapChain1> m_swapChain; Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_renderTargetView; Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_depthStencilView; Microsoft::WRL::ComPtr<ID3D11Texture2D> m_depthStencil; // Game state INT64 m_frame; BasicTimer^ m_timer; // Declarations for drawing a triangle struct s_Vertex { float vPostion[4]; float vColor[4]; }; Microsoft::WRL::ComPtr<ID3D11InputLayout> m_InputLayout; Microsoft::WRL::ComPtr<ID3D11Buffer> m_VertexBuffer; Microsoft::WRL::ComPtr<ID3D11VertexShader> m_VertexShader; Microsoft::WRL::ComPtr<ID3D11PixelShader> m_PixelShader; };
Initializing triangle data
Next, initialize triangle data by adding source code to Game.cpp.
To add triangle data to Game.cpp
- Open Game.cpp.
- Locate the definition for the function CreateResources.
-
Add the following code at the end of
CreateResources()
:// The compiled shader bytecodes are contained in constant buffers, // g_vs_main and g_ps_main defined in the auto-generated header files. DX::ThrowIfFailed( m_d3dDevice->CreateVertexShader( g_vs_main, sizeof( g_vs_main ), NULL, m_VertexShader.GetAddressOf() ) ); DX::ThrowIfFailed( m_d3dDevice->CreatePixelShader( g_ps_main, sizeof( g_ps_main ), NULL, m_PixelShader.GetAddressOf() ) ); // Create input layout (must match declaration of s_Vertex) const D3D11_INPUT_ELEMENT_DESC InputElementDesc[] = { { "POSITION", // LPCSTR SemanticName; 0, // UINT SemanticIndex; DXGI_FORMAT_R32G32B32A32_FLOAT, // DXGI_FORMAT Format; 0, // UINT InputSlot; 0, // UINT AlignedByteOffset; D3D11_INPUT_PER_VERTEX_DATA, // InputSlotClass; 0, // UINT InstanceDataStepRate; }, { "COLOR", // LPCSTR SemanticName; 0, // UINT SemanticIndex; DXGI_FORMAT_R32G32B32A32_FLOAT, // DXGI_FORMAT Format; 0, // UINT InputSlot; D3D11_APPEND_ALIGNED_ELEMENT, // UINT AlignedByteOffset; D3D11_INPUT_PER_VERTEX_DATA, // InputSlotClass; 0, // UINT InstanceDataStepRate; }, }; DX::ThrowIfFailed( m_d3dDevice->CreateInputLayout( InputElementDesc, _countof( InputElementDesc ), g_vs_main, sizeof( g_vs_main ), m_InputLayout.GetAddressOf() ) ); // Create vertex buffer containing a single triangle s_Vertex VertexData[3] = { { { 0.0f, 0.5f, 0.5f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f }, }, { { 0.5f, -0.5f, 0.5f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, }, { { -0.5f, -0.5f, 0.5f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }, }, }; D3D11_SUBRESOURCE_DATA InitialData = { VertexData, // const void *pSysMem; 0, // UINT SysMemPitch; 0, // UINT SysMemSlicePitch; }; D3D11_BUFFER_DESC BufferDesc = { sizeof( VertexData ), // UINT ByteWidth; D3D11_USAGE_IMMUTABLE, // D3D11_USAGE Usage; D3D11_BIND_VERTEX_BUFFER, // UINT BindFlags; 0, // UINT CPUAccessFlags; 0, // UINT MiscFlags; sizeof( VertexData[0] ), // UINT StructureByteStride; }; DX::ThrowIfFailed( m_d3dDevice->CreateBuffer( &BufferDesc, &InitialData, m_VertexBuffer.GetAddressOf() ) );
Rendering the triangle
Next, add source code to Game.cpp that will render the triangle.
To add source code to render the triangle
-
Locate the definition of the method Game::Render, in Game.cpp.
Look for the following comment:
// TODO: Add your rendering code here
-
Replace the comment with the following code:
// Set input assembler state m_d3dContext->IASetInputLayout( m_InputLayout.Get() ); UINT Strides[1] = { sizeof( s_Vertex ), }; UINT Offsets[1] = { 0, }; m_d3dContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); m_d3dContext->IASetVertexBuffers( 0, 1, m_VertexBuffer.GetAddressOf(), Strides, Offsets ); // Set shaders m_d3dContext->VSSetShader( m_VertexShader.Get(), NULL, 0 ); //m_d3dContext->GSSetShader( NULL, NULL, 0 ); m_d3dContext->PSSetShader( m_PixelShader.Get(), NULL, 0 ); // Draw triangle m_d3dContext->Draw( 3, 0 );
Building and deploying
If you've completed the preceding steps in this tutorial, you can now build and deploy your first app to the dev kit.
To build and deploy your app
-
Use Connect (xbconnect.exe) to set the default remote console, or set the IP address of the console in the project properties.
-
On the Build menu, click Build Solution.
Wait for building to finish.
-
On the Build menu, click Deploy Solution.
-
Navigate to the All Apps page on the dev kit using the Xbox controller, and select DurangoGame.
Note that it can take some time for the app to both deploy and run.
-
如果利用XDK自带的manager工具进行deplay到kit上,deplay以下路径即可(这里的.cso文件是在VS里面shader文件右键属性中的“ObjectFileName”一栏保留默认设置“$(OutDir)%(Filename).cso”以生成的shader的编译结果文件):