diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..511d01d --- /dev/null +++ b/.gitignore @@ -0,0 +1,165 @@ +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile +*.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + +#################### +## Sample framework +#################### + +ShaderCache/ +*.exe +*.dll +*.deps + +# Sublime +*.sublime-workspace \ No newline at end of file diff --git a/Content/.gitignore b/Content/.gitignore new file mode 100644 index 0000000..8ea97fe --- /dev/null +++ b/Content/.gitignore @@ -0,0 +1,5 @@ +## Exclusions for content + +!*.obj +*.swatch +*.swatches \ No newline at end of file diff --git a/Content/Models/Columns/Columns.sdkmesh b/Content/Models/Columns/Columns.sdkmesh new file mode 100644 index 0000000..75daee2 Binary files /dev/null and b/Content/Models/Columns/Columns.sdkmesh differ diff --git a/Content/Models/Columns/default.dds b/Content/Models/Columns/default.dds new file mode 100644 index 0000000..9cae349 Binary files /dev/null and b/Content/Models/Columns/default.dds differ diff --git a/Content/Models/Powerplant/Powerplant.sdkmesh b/Content/Models/Powerplant/Powerplant.sdkmesh new file mode 100644 index 0000000..701bcbd Binary files /dev/null and b/Content/Models/Powerplant/Powerplant.sdkmesh differ diff --git a/Content/Models/Powerplant/basket_map.dds b/Content/Models/Powerplant/basket_map.dds new file mode 100644 index 0000000..1e600ea Binary files /dev/null and b/Content/Models/Powerplant/basket_map.dds differ diff --git a/Content/Models/Powerplant/command_a.dds b/Content/Models/Powerplant/command_a.dds new file mode 100644 index 0000000..6b4fdac Binary files /dev/null and b/Content/Models/Powerplant/command_a.dds differ diff --git a/Content/Models/Powerplant/con_edge.dds b/Content/Models/Powerplant/con_edge.dds new file mode 100644 index 0000000..4caf7a0 Binary files /dev/null and b/Content/Models/Powerplant/con_edge.dds differ diff --git a/Content/Models/Powerplant/container_grn_num.dds b/Content/Models/Powerplant/container_grn_num.dds new file mode 100644 index 0000000..8e102d8 Binary files /dev/null and b/Content/Models/Powerplant/container_grn_num.dds differ diff --git a/Content/Models/Powerplant/container_gry_num.dds b/Content/Models/Powerplant/container_gry_num.dds new file mode 100644 index 0000000..0a54924 Binary files /dev/null and b/Content/Models/Powerplant/container_gry_num.dds differ diff --git a/Content/Models/Powerplant/container_hab_num.dds b/Content/Models/Powerplant/container_hab_num.dds new file mode 100644 index 0000000..55db964 Binary files /dev/null and b/Content/Models/Powerplant/container_hab_num.dds differ diff --git a/Content/Models/Powerplant/container_orn_num.dds b/Content/Models/Powerplant/container_orn_num.dds new file mode 100644 index 0000000..d948be5 Binary files /dev/null and b/Content/Models/Powerplant/container_orn_num.dds differ diff --git a/Content/Models/Powerplant/crane_light.dds b/Content/Models/Powerplant/crane_light.dds new file mode 100644 index 0000000..9fbd753 Binary files /dev/null and b/Content/Models/Powerplant/crane_light.dds differ diff --git a/Content/Models/Powerplant/crane_wheel.dds b/Content/Models/Powerplant/crane_wheel.dds new file mode 100644 index 0000000..1539547 Binary files /dev/null and b/Content/Models/Powerplant/crane_wheel.dds differ diff --git a/Content/Models/Powerplant/crn_clamp_a.dds b/Content/Models/Powerplant/crn_clamp_a.dds new file mode 100644 index 0000000..c9ba58c Binary files /dev/null and b/Content/Models/Powerplant/crn_clamp_a.dds differ diff --git a/Content/Models/Powerplant/crn_pan_a.dds b/Content/Models/Powerplant/crn_pan_a.dds new file mode 100644 index 0000000..9f35d3a Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_a.dds differ diff --git a/Content/Models/Powerplant/crn_pan_c.dds b/Content/Models/Powerplant/crn_pan_c.dds new file mode 100644 index 0000000..9bdd083 Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_c.dds differ diff --git a/Content/Models/Powerplant/crn_pan_d.dds b/Content/Models/Powerplant/crn_pan_d.dds new file mode 100644 index 0000000..80b5222 Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_d.dds differ diff --git a/Content/Models/Powerplant/crn_pan_e.dds b/Content/Models/Powerplant/crn_pan_e.dds new file mode 100644 index 0000000..c373107 Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_e.dds differ diff --git a/Content/Models/Powerplant/crn_pan_f.dds b/Content/Models/Powerplant/crn_pan_f.dds new file mode 100644 index 0000000..4f61100 Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_f.dds differ diff --git a/Content/Models/Powerplant/crn_pan_g.dds b/Content/Models/Powerplant/crn_pan_g.dds new file mode 100644 index 0000000..dd018c7 Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_g.dds differ diff --git a/Content/Models/Powerplant/crn_pan_i.dds b/Content/Models/Powerplant/crn_pan_i.dds new file mode 100644 index 0000000..5950258 Binary files /dev/null and b/Content/Models/Powerplant/crn_pan_i.dds differ diff --git a/Content/Models/Powerplant/ele_bot.dds b/Content/Models/Powerplant/ele_bot.dds new file mode 100644 index 0000000..1c5043f Binary files /dev/null and b/Content/Models/Powerplant/ele_bot.dds differ diff --git a/Content/Models/Powerplant/ele_floor.dds b/Content/Models/Powerplant/ele_floor.dds new file mode 100644 index 0000000..f75686d Binary files /dev/null and b/Content/Models/Powerplant/ele_floor.dds differ diff --git a/Content/Models/Powerplant/ele_support.dds b/Content/Models/Powerplant/ele_support.dds new file mode 100644 index 0000000..9d00ab2 Binary files /dev/null and b/Content/Models/Powerplant/ele_support.dds differ diff --git a/Content/Models/Powerplant/ele_track_a.dds b/Content/Models/Powerplant/ele_track_a.dds new file mode 100644 index 0000000..0598140 Binary files /dev/null and b/Content/Models/Powerplant/ele_track_a.dds differ diff --git a/Content/Models/Powerplant/fan_blde.dds b/Content/Models/Powerplant/fan_blde.dds new file mode 100644 index 0000000..8cafa5b Binary files /dev/null and b/Content/Models/Powerplant/fan_blde.dds differ diff --git a/Content/Models/Powerplant/fan_housing_b.dds b/Content/Models/Powerplant/fan_housing_b.dds new file mode 100644 index 0000000..e15731c Binary files /dev/null and b/Content/Models/Powerplant/fan_housing_b.dds differ diff --git a/Content/Models/Powerplant/fan_hub.dds b/Content/Models/Powerplant/fan_hub.dds new file mode 100644 index 0000000..57f4d85 Binary files /dev/null and b/Content/Models/Powerplant/fan_hub.dds differ diff --git a/Content/Models/Powerplant/gr_trans_b.dds b/Content/Models/Powerplant/gr_trans_b.dds new file mode 100644 index 0000000..d0cdce3 Binary files /dev/null and b/Content/Models/Powerplant/gr_trans_b.dds differ diff --git a/Content/Models/Powerplant/gravel_a.dds b/Content/Models/Powerplant/gravel_a.dds new file mode 100644 index 0000000..5847008 Binary files /dev/null and b/Content/Models/Powerplant/gravel_a.dds differ diff --git a/Content/Models/Powerplant/gravel_c.dds b/Content/Models/Powerplant/gravel_c.dds new file mode 100644 index 0000000..8588177 Binary files /dev/null and b/Content/Models/Powerplant/gravel_c.dds differ diff --git a/Content/Models/Powerplant/grt_sm_a.dds b/Content/Models/Powerplant/grt_sm_a.dds new file mode 100644 index 0000000..ba96717 Binary files /dev/null and b/Content/Models/Powerplant/grt_sm_a.dds differ diff --git a/Content/Models/Powerplant/grt_sm_b.dds b/Content/Models/Powerplant/grt_sm_b.dds new file mode 100644 index 0000000..e96e345 Binary files /dev/null and b/Content/Models/Powerplant/grt_sm_b.dds differ diff --git a/Content/Models/Powerplant/hill_mound_b.dds b/Content/Models/Powerplant/hill_mound_b.dds new file mode 100644 index 0000000..5d8e3b0 Binary files /dev/null and b/Content/Models/Powerplant/hill_mound_b.dds differ diff --git a/Content/Models/Powerplant/hz_a.dds b/Content/Models/Powerplant/hz_a.dds new file mode 100644 index 0000000..58a4e44 Binary files /dev/null and b/Content/Models/Powerplant/hz_a.dds differ diff --git a/Content/Models/Powerplant/mav_a_ring.dds b/Content/Models/Powerplant/mav_a_ring.dds new file mode 100644 index 0000000..5154a31 Binary files /dev/null and b/Content/Models/Powerplant/mav_a_ring.dds differ diff --git a/Content/Models/Powerplant/mav_b_body.dds b/Content/Models/Powerplant/mav_b_body.dds new file mode 100644 index 0000000..93bff9f Binary files /dev/null and b/Content/Models/Powerplant/mav_b_body.dds differ diff --git a/Content/Models/Powerplant/mav_b_lit.dds b/Content/Models/Powerplant/mav_b_lit.dds new file mode 100644 index 0000000..bf21fc0 Binary files /dev/null and b/Content/Models/Powerplant/mav_b_lit.dds differ diff --git a/Content/Models/Powerplant/mav_b_lit_NRM.dds b/Content/Models/Powerplant/mav_b_lit_NRM.dds new file mode 100644 index 0000000..786a28c Binary files /dev/null and b/Content/Models/Powerplant/mav_b_lit_NRM.dds differ diff --git a/Content/Models/Powerplant/pad_big_b.dds b/Content/Models/Powerplant/pad_big_b.dds new file mode 100644 index 0000000..9e0f0a4 Binary files /dev/null and b/Content/Models/Powerplant/pad_big_b.dds differ diff --git a/Content/Models/Powerplant/pad_pan_a.dds b/Content/Models/Powerplant/pad_pan_a.dds new file mode 100644 index 0000000..88a3390 Binary files /dev/null and b/Content/Models/Powerplant/pad_pan_a.dds differ diff --git a/Content/Models/Powerplant/pipe_a.dds b/Content/Models/Powerplant/pipe_a.dds new file mode 100644 index 0000000..28dcf1b Binary files /dev/null and b/Content/Models/Powerplant/pipe_a.dds differ diff --git a/Content/Models/Powerplant/pipe_b.dds b/Content/Models/Powerplant/pipe_b.dds new file mode 100644 index 0000000..02cc92b Binary files /dev/null and b/Content/Models/Powerplant/pipe_b.dds differ diff --git a/Content/Models/Powerplant/pipe_d.dds b/Content/Models/Powerplant/pipe_d.dds new file mode 100644 index 0000000..1309d20 Binary files /dev/null and b/Content/Models/Powerplant/pipe_d.dds differ diff --git a/Content/Models/Powerplant/pipe_e.dds b/Content/Models/Powerplant/pipe_e.dds new file mode 100644 index 0000000..98b2fc1 Binary files /dev/null and b/Content/Models/Powerplant/pipe_e.dds differ diff --git a/Content/Models/Powerplant/rail.dds b/Content/Models/Powerplant/rail.dds new file mode 100644 index 0000000..7b255db Binary files /dev/null and b/Content/Models/Powerplant/rail.dds differ diff --git a/Content/Models/Powerplant/re_bar_a.dds b/Content/Models/Powerplant/re_bar_a.dds new file mode 100644 index 0000000..b7ef207 Binary files /dev/null and b/Content/Models/Powerplant/re_bar_a.dds differ diff --git a/Content/Models/Powerplant/re_bar_trans.dds b/Content/Models/Powerplant/re_bar_trans.dds new file mode 100644 index 0000000..2d92f39 Binary files /dev/null and b/Content/Models/Powerplant/re_bar_trans.dds differ diff --git a/Content/Models/Powerplant/red_girder_a.dds b/Content/Models/Powerplant/red_girder_a.dds new file mode 100644 index 0000000..dd1c74a Binary files /dev/null and b/Content/Models/Powerplant/red_girder_a.dds differ diff --git a/Content/Models/Powerplant/ring_a.dds b/Content/Models/Powerplant/ring_a.dds new file mode 100644 index 0000000..acbd849 Binary files /dev/null and b/Content/Models/Powerplant/ring_a.dds differ diff --git a/Content/Models/Powerplant/road_end.dds b/Content/Models/Powerplant/road_end.dds new file mode 100644 index 0000000..3291f45 Binary files /dev/null and b/Content/Models/Powerplant/road_end.dds differ diff --git a/Content/Models/Powerplant/road_end_a.dds b/Content/Models/Powerplant/road_end_a.dds new file mode 100644 index 0000000..cc3bfe9 Binary files /dev/null and b/Content/Models/Powerplant/road_end_a.dds differ diff --git a/Content/Models/Powerplant/road_end_b.dds b/Content/Models/Powerplant/road_end_b.dds new file mode 100644 index 0000000..8f04be5 Binary files /dev/null and b/Content/Models/Powerplant/road_end_b.dds differ diff --git a/Content/Models/Powerplant/road_loop_a.dds b/Content/Models/Powerplant/road_loop_a.dds new file mode 100644 index 0000000..6ab0959 Binary files /dev/null and b/Content/Models/Powerplant/road_loop_a.dds differ diff --git a/Content/Models/Powerplant/road_st_a.dds b/Content/Models/Powerplant/road_st_a.dds new file mode 100644 index 0000000..3f96409 Binary files /dev/null and b/Content/Models/Powerplant/road_st_a.dds differ diff --git a/Content/Models/Powerplant/sm_pan_a.dds b/Content/Models/Powerplant/sm_pan_a.dds new file mode 100644 index 0000000..c04735a Binary files /dev/null and b/Content/Models/Powerplant/sm_pan_a.dds differ diff --git a/Content/Models/Powerplant/tank_side.dds b/Content/Models/Powerplant/tank_side.dds new file mode 100644 index 0000000..eb6de10 Binary files /dev/null and b/Content/Models/Powerplant/tank_side.dds differ diff --git a/Content/Models/Powerplant/tank_top.dds b/Content/Models/Powerplant/tank_top.dds new file mode 100644 index 0000000..959e578 Binary files /dev/null and b/Content/Models/Powerplant/tank_top.dds differ diff --git a/Content/Models/Powerplant/test_ground_b.dds b/Content/Models/Powerplant/test_ground_b.dds new file mode 100644 index 0000000..e253545 Binary files /dev/null and b/Content/Models/Powerplant/test_ground_b.dds differ diff --git a/Content/Models/Powerplant/tow_rib_ a.dds b/Content/Models/Powerplant/tow_rib_ a.dds new file mode 100644 index 0000000..0d856ff Binary files /dev/null and b/Content/Models/Powerplant/tow_rib_ a.dds differ diff --git a/Content/Models/Powerplant/tow_rib_c.dds b/Content/Models/Powerplant/tow_rib_c.dds new file mode 100644 index 0000000..947eb0e Binary files /dev/null and b/Content/Models/Powerplant/tow_rib_c.dds differ diff --git a/Content/Models/Powerplant/trench_b.dds b/Content/Models/Powerplant/trench_b.dds new file mode 100644 index 0000000..2879067 Binary files /dev/null and b/Content/Models/Powerplant/trench_b.dds differ diff --git a/Content/Models/Powerplant/trench_end.dds b/Content/Models/Powerplant/trench_end.dds new file mode 100644 index 0000000..f6ac62e Binary files /dev/null and b/Content/Models/Powerplant/trench_end.dds differ diff --git a/Content/Models/Powerplant/trench_surround.dds b/Content/Models/Powerplant/trench_surround.dds new file mode 100644 index 0000000..708b8cb Binary files /dev/null and b/Content/Models/Powerplant/trench_surround.dds differ diff --git a/Content/Models/Powerplant/truss_a.dds b/Content/Models/Powerplant/truss_a.dds new file mode 100644 index 0000000..254c79c Binary files /dev/null and b/Content/Models/Powerplant/truss_a.dds differ diff --git a/Content/Models/Powerplant/truss_end.dds b/Content/Models/Powerplant/truss_end.dds new file mode 100644 index 0000000..cecb38f Binary files /dev/null and b/Content/Models/Powerplant/truss_end.dds differ diff --git a/Content/Models/Powerplant/tw_tex_a.dds b/Content/Models/Powerplant/tw_tex_a.dds new file mode 100644 index 0000000..c2990a2 Binary files /dev/null and b/Content/Models/Powerplant/tw_tex_a.dds differ diff --git a/Content/Models/Powerplant/walkway_a.dds b/Content/Models/Powerplant/walkway_a.dds new file mode 100644 index 0000000..a362f25 Binary files /dev/null and b/Content/Models/Powerplant/walkway_a.dds differ diff --git a/Content/Models/Powerplant/wall_tex_a.dds b/Content/Models/Powerplant/wall_tex_a.dds new file mode 100644 index 0000000..e5908f7 Binary files /dev/null and b/Content/Models/Powerplant/wall_tex_a.dds differ diff --git a/Content/Models/Powerplant/wall_tex_b.dds b/Content/Models/Powerplant/wall_tex_b.dds new file mode 100644 index 0000000..14572b9 Binary files /dev/null and b/Content/Models/Powerplant/wall_tex_b.dds differ diff --git a/Content/Models/Powerplant/wheel_house.dds b/Content/Models/Powerplant/wheel_house.dds new file mode 100644 index 0000000..eef201d Binary files /dev/null and b/Content/Models/Powerplant/wheel_house.dds differ diff --git a/Content/Models/Powerplant/yellow_girder_a.dds b/Content/Models/Powerplant/yellow_girder_a.dds new file mode 100644 index 0000000..ee84a53 Binary files /dev/null and b/Content/Models/Powerplant/yellow_girder_a.dds differ diff --git a/Content/Models/Soldier/Soldier.sdkmesh b/Content/Models/Soldier/Soldier.sdkmesh new file mode 100644 index 0000000..f9dc6fd Binary files /dev/null and b/Content/Models/Soldier/Soldier.sdkmesh differ diff --git a/Content/Models/Soldier/Soldier.sdkmesh_anim b/Content/Models/Soldier/Soldier.sdkmesh_anim new file mode 100644 index 0000000..46c894f Binary files /dev/null and b/Content/Models/Soldier/Soldier.sdkmesh_anim differ diff --git a/Content/Models/Soldier/head_diff.dds b/Content/Models/Soldier/head_diff.dds new file mode 100644 index 0000000..9e935bd Binary files /dev/null and b/Content/Models/Soldier/head_diff.dds differ diff --git a/Content/Models/Soldier/head_norm.dds b/Content/Models/Soldier/head_norm.dds new file mode 100644 index 0000000..cec6798 Binary files /dev/null and b/Content/Models/Soldier/head_norm.dds differ diff --git a/Content/Models/Soldier/jacket_diff.dds b/Content/Models/Soldier/jacket_diff.dds new file mode 100644 index 0000000..4643c1e Binary files /dev/null and b/Content/Models/Soldier/jacket_diff.dds differ diff --git a/Content/Models/Soldier/jacket_norm.dds b/Content/Models/Soldier/jacket_norm.dds new file mode 100644 index 0000000..ac83462 Binary files /dev/null and b/Content/Models/Soldier/jacket_norm.dds differ diff --git a/Content/Models/Soldier/pants_diff.dds b/Content/Models/Soldier/pants_diff.dds new file mode 100644 index 0000000..a924841 Binary files /dev/null and b/Content/Models/Soldier/pants_diff.dds differ diff --git a/Content/Models/Soldier/pants_norm.dds b/Content/Models/Soldier/pants_norm.dds new file mode 100644 index 0000000..76bf7d3 Binary files /dev/null and b/Content/Models/Soldier/pants_norm.dds differ diff --git a/Content/Models/Soldier/upBody_diff.dds b/Content/Models/Soldier/upBody_diff.dds new file mode 100644 index 0000000..56bf886 Binary files /dev/null and b/Content/Models/Soldier/upBody_diff.dds differ diff --git a/Content/Models/Soldier/upbody_norm.dds b/Content/Models/Soldier/upbody_norm.dds new file mode 100644 index 0000000..fa78150 Binary files /dev/null and b/Content/Models/Soldier/upbody_norm.dds differ diff --git a/Content/Models/Tower/Tower.sdkmesh b/Content/Models/Tower/Tower.sdkmesh new file mode 100644 index 0000000..be03901 Binary files /dev/null and b/Content/Models/Tower/Tower.sdkmesh differ diff --git a/Content/Models/Tower/blackDirty.png b/Content/Models/Tower/blackDirty.png new file mode 100644 index 0000000..c2884b8 Binary files /dev/null and b/Content/Models/Tower/blackDirty.png differ diff --git a/Content/Models/Tower/cementDiffuse.png b/Content/Models/Tower/cementDiffuse.png new file mode 100644 index 0000000..ecb6254 Binary files /dev/null and b/Content/Models/Tower/cementDiffuse.png differ diff --git a/Content/Models/Tower/metalSilverDiffuse.png b/Content/Models/Tower/metalSilverDiffuse.png new file mode 100644 index 0000000..29c86d7 Binary files /dev/null and b/Content/Models/Tower/metalSilverDiffuse.png differ diff --git a/Content/Models/Tower/redDirty.png b/Content/Models/Tower/redDirty.png new file mode 100644 index 0000000..6636461 Binary files /dev/null and b/Content/Models/Tower/redDirty.png differ diff --git a/Content/Models/Tower/redRibbonDiffuse.png b/Content/Models/Tower/redRibbonDiffuse.png new file mode 100644 index 0000000..4adb2b8 Binary files /dev/null and b/Content/Models/Tower/redRibbonDiffuse.png differ diff --git a/Content/Models/Tower/road_2k_diff_01.png b/Content/Models/Tower/road_2k_diff_01.png new file mode 100644 index 0000000..64d6736 Binary files /dev/null and b/Content/Models/Tower/road_2k_diff_01.png differ diff --git a/Content/Models/Tower/rubberDirtyDiffuse.png b/Content/Models/Tower/rubberDirtyDiffuse.png new file mode 100644 index 0000000..a6df82b Binary files /dev/null and b/Content/Models/Tower/rubberDirtyDiffuse.png differ diff --git a/Content/Models/Tower/supportShort_2k_diff_01.png b/Content/Models/Tower/supportShort_2k_diff_01.png new file mode 100644 index 0000000..2f44ee8 Binary files /dev/null and b/Content/Models/Tower/supportShort_2k_diff_01.png differ diff --git a/Content/Models/Tower/woodDirty.png b/Content/Models/Tower/woodDirty.png new file mode 100644 index 0000000..7a322dc Binary files /dev/null and b/Content/Models/Tower/woodDirty.png differ diff --git a/Content/Textures/Default.dds b/Content/Textures/Default.dds new file mode 100644 index 0000000..9cae349 Binary files /dev/null and b/Content/Textures/Default.dds differ diff --git a/Externals/.gitignore b/Externals/.gitignore new file mode 100644 index 0000000..f764d74 --- /dev/null +++ b/Externals/.gitignore @@ -0,0 +1,22 @@ +## Exclusions for externals + +![Dd]ebug/ +![Rr]elease/ +!x64/ +!build/ + +![Bb]in/ +![Oo]bj/ +!*.exe +!*.dll +!*.pdb +![Ll]ib/ +![Ii]nclude/ + +boost157/ +sun/source/x64/ + +*.sdf +*.opensdf +*.suo +*.vcproj.user diff --git a/Externals/AntTweakBar/bin/AntTweakBar64.dll b/Externals/AntTweakBar/bin/AntTweakBar64.dll new file mode 100644 index 0000000..276b2c4 Binary files /dev/null and b/Externals/AntTweakBar/bin/AntTweakBar64.dll differ diff --git a/Externals/AntTweakBar/include/AntTweakBar.h b/Externals/AntTweakBar/include/AntTweakBar.h new file mode 100644 index 0000000..a1cf277 --- /dev/null +++ b/Externals/AntTweakBar/include/AntTweakBar.h @@ -0,0 +1,378 @@ +// ---------------------------------------------------------------------------- +// +// @file AntTweakBar.h +// +// @brief AntTweakBar is a light and intuitive graphical user interface +// that can be readily integrated into OpenGL and DirectX +// applications in order to interactively tweak parameters. +// +// @author Philippe Decaudin +// +// @doc http://anttweakbar.sourceforge.net/doc +// +// @license This file is part of the AntTweakBar library. +// AntTweakBar is a free software released under the zlib license. +// For conditions of distribution and use, see License.txt +// +// ---------------------------------------------------------------------------- + + +#if !defined TW_INCLUDED +#define TW_INCLUDED + +#include + +#define TW_VERSION 116 // Version Mmm : M=Major mm=minor (e.g., 102 is version 1.02) + + +#ifdef __cplusplus +# if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable: 4995 4530) +# include +# pragma warning(pop) +# else +# include +# endif + extern "C" { +#endif // __cplusplus + + +// ---------------------------------------------------------------------------- +// OS specific definitions +// ---------------------------------------------------------------------------- + +#if (defined(_WIN32) || defined(_WIN64)) && !defined(TW_STATIC) +# define TW_CALL __stdcall +# define TW_CDECL_CALL __cdecl +# define TW_EXPORT_API __declspec(dllexport) +# define TW_IMPORT_API __declspec(dllimport) +#else +# define TW_CALL +# define TW_CDECL_CALL +# define TW_EXPORT_API +# define TW_IMPORT_API +#endif + +#if defined TW_EXPORTS +# define TW_API TW_EXPORT_API +#elif defined TW_STATIC +# define TW_API +# if defined(_MSC_VER) && !defined(TW_NO_LIB_PRAGMA) +# ifdef _WIN64 +# pragma comment(lib, "AntTweakBarStatic64") +# else +# pragma comment(lib, "AntTweakBarStatic") +# endif +# endif +#else +# define TW_API TW_IMPORT_API +# if defined(_MSC_VER) && !defined(TW_NO_LIB_PRAGMA) +# ifdef _WIN64 +# pragma comment(lib, "AntTweakBar64") +# else +# pragma comment(lib, "AntTweakBar") +# endif +# endif +#endif + + +// ---------------------------------------------------------------------------- +// Bar functions and definitions +// ---------------------------------------------------------------------------- + +typedef struct CTwBar TwBar; // structure CTwBar is not exposed. + +TW_API TwBar * TW_CALL TwNewBar(const char *barName); +TW_API int TW_CALL TwDeleteBar(TwBar *bar); +TW_API int TW_CALL TwDeleteAllBars(); +TW_API int TW_CALL TwSetTopBar(const TwBar *bar); +TW_API TwBar * TW_CALL TwGetTopBar(); +TW_API int TW_CALL TwSetBottomBar(const TwBar *bar); +TW_API TwBar * TW_CALL TwGetBottomBar(); +TW_API const char * TW_CALL TwGetBarName(const TwBar *bar); +TW_API int TW_CALL TwGetBarCount(); +TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex); +TW_API TwBar * TW_CALL TwGetBarByName(const char *barName); +TW_API int TW_CALL TwRefreshBar(TwBar *bar); + +// ---------------------------------------------------------------------------- +// Var functions and definitions +// ---------------------------------------------------------------------------- + +typedef enum ETwType +{ + TW_TYPE_UNDEF = 0, +#ifdef __cplusplus + TW_TYPE_BOOLCPP = 1, +#endif // __cplusplus + TW_TYPE_BOOL8 = 2, + TW_TYPE_BOOL16, + TW_TYPE_BOOL32, + TW_TYPE_CHAR, + TW_TYPE_INT8, + TW_TYPE_UINT8, + TW_TYPE_INT16, + TW_TYPE_UINT16, + TW_TYPE_INT32, + TW_TYPE_UINT32, + TW_TYPE_FLOAT, + TW_TYPE_DOUBLE, + TW_TYPE_COLOR32, // 32 bits color. Order is RGBA if API is OpenGL or Direct3D10, and inversed if API is Direct3D9 (can be modified by defining 'colorOrder=...', see doc) + TW_TYPE_COLOR3F, // 3 floats color. Order is RGB. + TW_TYPE_COLOR4F, // 4 floats color. Order is RGBA. + TW_TYPE_CDSTRING, // Null-terminated C Dynamic String (pointer to an array of char dynamically allocated with malloc/realloc/strdup) +#ifdef __cplusplus +# if defined(_MSC_VER) && (_MSC_VER == 1600) + TW_TYPE_STDSTRING = (0x2ffe0000+sizeof(std::string)), // VS2010 C++ STL string (std::string) +# else + TW_TYPE_STDSTRING = (0x2fff0000+sizeof(std::string)), // C++ STL string (std::string) +# endif +#endif // __cplusplus + TW_TYPE_QUAT4F = TW_TYPE_CDSTRING+2, // 4 floats encoding a quaternion {qx,qy,qz,qs} + TW_TYPE_QUAT4D, // 4 doubles encoding a quaternion {qx,qy,qz,qs} + TW_TYPE_DIR3F, // direction vector represented by 3 floats + TW_TYPE_DIR3D // direction vector represented by 3 doubles +} TwType; +#define TW_TYPE_CSSTRING(n) ((TwType)(0x30000000+((n)&0xfffffff))) // Null-terminated C Static String of size n (defined as char[n], with n<2^28) + +typedef void (TW_CALL * TwSetVarCallback)(const void *value, void *clientData); +typedef void (TW_CALL * TwGetVarCallback)(void *value, void *clientData); +typedef void (TW_CALL * TwButtonCallback)(void *clientData); + +TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def); +TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def); +TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def); +TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def); +TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def); +TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name); +TW_API int TW_CALL TwRemoveAllVars(TwBar *bar); + +typedef struct CTwEnumVal +{ + int Value; + const char * Label; +} TwEnumVal; +typedef struct CTwStructMember +{ + const char * Name; + TwType Type; + size_t Offset; + const char * DefString; +} TwStructMember; +typedef void (TW_CALL * TwSummaryCallback)(char *summaryString, size_t summaryMaxLength, const void *value, void *clientData); + +TW_API int TW_CALL TwDefine(const char *def); +TW_API TwType TW_CALL TwDefineEnum(const char *name, const TwEnumVal *enumValues, unsigned int nbValues); +TW_API TwType TW_CALL TwDefineEnumFromString(const char *name, const char *enumString); +TW_API TwType TW_CALL TwDefineStruct(const char *name, const TwStructMember *structMembers, unsigned int nbMembers, size_t structSize, TwSummaryCallback summaryCallback, void *summaryClientData); + +typedef void (TW_CALL * TwCopyCDStringToClient)(char **destinationClientStringPtr, const char *sourceString); +TW_API void TW_CALL TwCopyCDStringToClientFunc(TwCopyCDStringToClient copyCDStringFunc); +TW_API void TW_CALL TwCopyCDStringToLibrary(char **destinationLibraryStringPtr, const char *sourceClientString); +#ifdef __cplusplus +typedef void (TW_CALL * TwCopyStdStringToClient)(std::string& destinationClientString, const std::string& sourceString); +TW_API void TW_CALL TwCopyStdStringToClientFunc(TwCopyStdStringToClient copyStdStringToClientFunc); +TW_API void TW_CALL TwCopyStdStringToLibrary(std::string& destinationLibraryString, const std::string& sourceClientString); +#endif // __cplusplus + +typedef enum ETwParamValueType +{ + TW_PARAM_INT32, + TW_PARAM_FLOAT, + TW_PARAM_DOUBLE, + TW_PARAM_CSTRING // Null-terminated array of char (ie, c-string) +} TwParamValueType; +TW_API int TW_CALL TwGetParam(TwBar *bar, const char *varName, const char *paramName, TwParamValueType paramValueType, unsigned int outValueMaxCount, void *outValues); +TW_API int TW_CALL TwSetParam(TwBar *bar, const char *varName, const char *paramName, TwParamValueType paramValueType, unsigned int inValueCount, const void *inValues); + + +// ---------------------------------------------------------------------------- +// Management functions and definitions +// ---------------------------------------------------------------------------- + +typedef enum ETwGraphAPI +{ + TW_OPENGL = 1, + TW_DIRECT3D9 = 2, + TW_DIRECT3D10 = 3, + TW_DIRECT3D11 = 4, + TW_OPENGL_CORE = 5 +} TwGraphAPI; + +TW_API int TW_CALL TwInit(TwGraphAPI graphAPI, void *device); +TW_API int TW_CALL TwTerminate(); + +TW_API int TW_CALL TwDraw(); +TW_API int TW_CALL TwWindowSize(int width, int height); + +TW_API int TW_CALL TwSetCurrentWindow(int windowID); // multi-windows support +TW_API int TW_CALL TwGetCurrentWindow(); +TW_API int TW_CALL TwWindowExists(int windowID); + +typedef enum ETwKeyModifier +{ + TW_KMOD_NONE = 0x0000, // same codes as SDL keysym.mod + TW_KMOD_SHIFT = 0x0003, + TW_KMOD_CTRL = 0x00c0, + TW_KMOD_ALT = 0x0100, + TW_KMOD_META = 0x0c00 +} TwKeyModifier; +typedef enum EKeySpecial +{ + TW_KEY_BACKSPACE = '\b', + TW_KEY_TAB = '\t', + TW_KEY_CLEAR = 0x0c, + TW_KEY_RETURN = '\r', + TW_KEY_PAUSE = 0x13, + TW_KEY_ESCAPE = 0x1b, + TW_KEY_SPACE = ' ', + TW_KEY_DELETE = 0x7f, + TW_KEY_UP = 273, // same codes and order as SDL 1.2 keysym.sym + TW_KEY_DOWN, + TW_KEY_RIGHT, + TW_KEY_LEFT, + TW_KEY_INSERT, + TW_KEY_HOME, + TW_KEY_END, + TW_KEY_PAGE_UP, + TW_KEY_PAGE_DOWN, + TW_KEY_F1, + TW_KEY_F2, + TW_KEY_F3, + TW_KEY_F4, + TW_KEY_F5, + TW_KEY_F6, + TW_KEY_F7, + TW_KEY_F8, + TW_KEY_F9, + TW_KEY_F10, + TW_KEY_F11, + TW_KEY_F12, + TW_KEY_F13, + TW_KEY_F14, + TW_KEY_F15, + TW_KEY_LAST +} TwKeySpecial; + +TW_API int TW_CALL TwKeyPressed(int key, int modifiers); +TW_API int TW_CALL TwKeyTest(int key, int modifiers); + +typedef enum ETwMouseAction +{ + TW_MOUSE_RELEASED, + TW_MOUSE_PRESSED +} TwMouseAction; +typedef enum ETwMouseButtonID +{ + TW_MOUSE_LEFT = 1, // same code as SDL_BUTTON_LEFT + TW_MOUSE_MIDDLE = 2, // same code as SDL_BUTTON_MIDDLE + TW_MOUSE_RIGHT = 3 // same code as SDL_BUTTON_RIGHT +} TwMouseButtonID; + +TW_API int TW_CALL TwMouseButton(TwMouseAction action, TwMouseButtonID button); +TW_API int TW_CALL TwMouseMotion(int mouseX, int mouseY); +TW_API int TW_CALL TwMouseWheel(int pos); + +TW_API const char * TW_CALL TwGetLastError(); +typedef void (TW_CALL * TwErrorHandler)(const char *errorMessage); +TW_API void TW_CALL TwHandleErrors(TwErrorHandler errorHandler); + + +// ---------------------------------------------------------------------------- +// Helper functions to translate events from some common window management +// frameworks to AntTweakBar. +// They call TwKeyPressed, TwMouse* and TwWindowSize for you (implemented in +// files TwEventWin.c TwEventSDL*.c TwEventGLFW.c TwEventGLUT.c) +// ---------------------------------------------------------------------------- + +// For Windows message proc +#ifndef _W64 // Microsoft specific (detection of 64 bits portability issues) +# define _W64 +#endif // _W64 +#ifdef _WIN64 + TW_API int TW_CALL TwEventWin(void *wnd, unsigned int msg, unsigned __int64 _W64 wParam, __int64 _W64 lParam); +#else + TW_API int TW_CALL TwEventWin(void *wnd, unsigned int msg, unsigned int _W64 wParam, int _W64 lParam); +#endif +#define TwEventWin32 TwEventWin // For compatibility with AntTweakBar versions prior to 1.11 + +// For libSDL event loop +TW_API int TW_CALL TwEventSDL(const void *sdlEvent, unsigned char sdlMajorVersion, unsigned char sdlMinorVersion); + +// For GLFW event callbacks +// You should define GLFW_CDECL before including AntTweakBar.h if your version of GLFW uses cdecl calling convensions +#ifdef GLFW_CDECL + TW_API int TW_CDECL_CALL TwEventMouseButtonGLFWcdecl(int glfwButton, int glfwAction); + TW_API int TW_CDECL_CALL TwEventKeyGLFWcdecl(int glfwKey, int glfwAction); + TW_API int TW_CDECL_CALL TwEventCharGLFWcdecl(int glfwChar, int glfwAction); + TW_API int TW_CDECL_CALL TwEventMousePosGLFWcdecl(int mouseX, int mouseY); + TW_API int TW_CDECL_CALL TwEventMouseWheelGLFWcdecl(int wheelPos); +# define TwEventMouseButtonGLFW TwEventMouseButtonGLFWcdecl +# define TwEventKeyGLFW TwEventKeyGLFWcdecl +# define TwEventCharGLFW TwEventCharGLFWcdecl +# define TwEventMousePosGLFW TwEventMousePosGLFWcdecl +# define TwEventMouseWheelGLFW TwEventMouseWheelGLFWcdecl +#else + TW_API int TW_CALL TwEventMouseButtonGLFW(int glfwButton, int glfwAction); + TW_API int TW_CALL TwEventKeyGLFW(int glfwKey, int glfwAction); + TW_API int TW_CALL TwEventCharGLFW(int glfwChar, int glfwAction); +# define TwEventMousePosGLFW TwMouseMotion +# define TwEventMouseWheelGLFW TwMouseWheel +#endif + +// For GLUT event callbacks (Windows calling convention for GLUT callbacks is cdecl) +#if defined(_WIN32) || defined(_WIN64) +# define TW_GLUT_CALL TW_CDECL_CALL +#else +# define TW_GLUT_CALL +#endif +TW_API int TW_GLUT_CALL TwEventMouseButtonGLUT(int glutButton, int glutState, int mouseX, int mouseY); +TW_API int TW_GLUT_CALL TwEventMouseMotionGLUT(int mouseX, int mouseY); +TW_API int TW_GLUT_CALL TwEventKeyboardGLUT(unsigned char glutKey, int mouseX, int mouseY); +TW_API int TW_GLUT_CALL TwEventSpecialGLUT(int glutKey, int mouseX, int mouseY); +TW_API int TW_CALL TwGLUTModifiersFunc(int (TW_CALL *glutGetModifiersFunc)(void)); +typedef void (TW_GLUT_CALL *GLUTmousebuttonfun)(int glutButton, int glutState, int mouseX, int mouseY); +typedef void (TW_GLUT_CALL *GLUTmousemotionfun)(int mouseX, int mouseY); +typedef void (TW_GLUT_CALL *GLUTkeyboardfun)(unsigned char glutKey, int mouseX, int mouseY); +typedef void (TW_GLUT_CALL *GLUTspecialfun)(int glutKey, int mouseX, int mouseY); + +// For SFML event loop +TW_API int TW_CALL TwEventSFML(const void *sfmlEvent, unsigned char sfmlMajorVersion, unsigned char sfmlMinorVersion); + +// For X11 event loop +#if defined(_UNIX) + TW_API int TW_CDECL_CALL TwEventX11(void *xevent); +#endif + +// ---------------------------------------------------------------------------- +// Make sure the types have the right sizes +// ---------------------------------------------------------------------------- + +#define TW_COMPILE_TIME_ASSERT(name, x) typedef int TW_DUMMY_ ## name[(x) * 2 - 1] + +TW_COMPILE_TIME_ASSERT(TW_CHAR, sizeof(char) == 1); +TW_COMPILE_TIME_ASSERT(TW_SHORT, sizeof(short) == 2); +TW_COMPILE_TIME_ASSERT(TW_INT, sizeof(int) == 4); +TW_COMPILE_TIME_ASSERT(TW_FLOAT, sizeof(float) == 4); +TW_COMPILE_TIME_ASSERT(TW_DOUBLE, sizeof(double) == 8); + +// Check pointer size on Windows +#if !defined(_WIN64) && defined(_WIN32) + // If the following assert failed, the platform is not 32-bit and _WIN64 is not defined. + // When targetting 64-bit Windows platform, _WIN64 must be defined. + TW_COMPILE_TIME_ASSERT(TW_PTR32, sizeof(void*) == 4); +#elif defined(_WIN64) + // If the following assert failed, _WIN64 is defined but the targeted platform is not 64-bit. + TW_COMPILE_TIME_ASSERT(TW_PTR64, sizeof(void*) == 8); +#endif + +// --------------------------------------------------------------------------- + + +#ifdef __cplusplus + } // extern "C" +#endif // __cplusplus + + +#endif // !defined TW_INCLUDED diff --git a/Externals/AntTweakBar/lib/AntTweakBar64.lib b/Externals/AntTweakBar/lib/AntTweakBar64.lib new file mode 100644 index 0000000..2d39b32 Binary files /dev/null and b/Externals/AntTweakBar/lib/AntTweakBar64.lib differ diff --git a/Externals/d3dcompiler_46.dll b/Externals/d3dcompiler_46.dll new file mode 100644 index 0000000..552389b Binary files /dev/null and b/Externals/d3dcompiler_46.dll differ diff --git a/README.md b/README.md index 5c90b1f..70b437a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # Shadows -A sample app that demonstrates several techniques for rendering real-time shadow maps + +This is a D3D11 sample app that demonstrates several techniques for rendering real-time shadow maps. The following techniques are implemented: + +* Cascaded Shadow Maps +* Stabilized Cascaded Shadow Maps +* Automatic Cascade Fitting based on depth buffer analysis, as in [Sample Distribution Shadow Maps](https://software.intel.com/en-us/articles/sample-distribution-shadow-maps). +* Various forms of Percentage Closer Filtering +* [Variance Shadow Maps](http://www.punkuser.net/vsm/vsm_paper.pdf) +* [Exponential Variance Shadow Maps](http://www.punkuser.net/lvsm/lvsm_web.pdf) (EVSM) +* [Moment Shadow Maps](http://cg.cs.uni-bonn.de/en/publications/paper-details/peters-2015-msm/) + +This code sample was done as part of 2 articles for my blog, which you can find here: + +* [https://mynameismjp.wordpress.com/2013/09/10/shadow-maps/](https://mynameismjp.wordpress.com/2013/09/10/shadow-maps/) +* [https://mynameismjp.wordpress.com/2015/02/18/shadow-sample-update/](https://mynameismjp.wordpress.com/2015/02/18/shadow-sample-update/) + +# Build Instructions + +The repository contains a Visual Studio 2012 project and solution file that's ready to build and run on Windows 7, 8, or 10. All external dependencies are included in the repository. diff --git a/Shadows/AppSettings.cpp b/Shadows/AppSettings.cpp new file mode 100644 index 0000000..2878ca5 --- /dev/null +++ b/Shadows/AppSettings.cpp @@ -0,0 +1,354 @@ +#include "PCH.h" +#include "AppSettings.h" + +using namespace SampleFramework11; + +static const char* SceneLabels[3] = +{ + "PowerPlant", + "Tower", + "Columns", +}; + +static const char* ShadowModeLabels[9] = +{ + "Fixed Size PCF", + "Grid PCF", + "Random Disc PCF", + "Optimized PCF", + "VSM", + "EVSM 2 Component", + "EVSM 4 Component", + "MSM Hamburger", + "MSM Hausdorff", +}; + +static const char* ShadowMapSizeLabels[3] = +{ + "512x512", + "1024x1024", + "2048x2048", +}; + +static const char* FixedFilterSizeLabels[5] = +{ + "2x2", + "3x3", + "5x5", + "7x7", + "9x9", +}; + +static const char* PartitionModeLabels[3] = +{ + "Manual", + "Logarithmic", + "PSSM", +}; + +static const char* ShadowMSAALabels[4] = +{ + "None", + "2x", + "4x", + "8x", +}; + +static const char* SMFormatLabels[2] = +{ + "16-bit", + "32-bit", +}; + +static const char* ShadowAnisotropyLabels[5] = +{ + "1x", + "2x", + "4x", + "8x", + "16x", +}; + +namespace AppSettings +{ + SceneSetting CurrentScene; + BoolSetting AnimateLight; + DirectionSetting LightDirection; + ColorSetting LightColor; + OrientationSetting CharacterOrientation; + BoolSetting EnableAlbedoMap; + ShadowModeSetting ShadowMode; + ShadowMapSizeSetting ShadowMapSize; + FixedFilterSizeSetting FixedFilterSize; + FloatSetting FilterSize; + BoolSetting VisualizeCascades; + BoolSetting StabilizeCascades; + BoolSetting FilterAcrossCascades; + BoolSetting RandomizeDiscOffsets; + IntSetting NumDiscSamples; + BoolSetting AutoComputeDepthBounds; + IntSetting ReadbackLatency; + BoolSetting GPUSceneSubmission; + FloatSetting MinCascadeDistance; + FloatSetting MaxCascadeDistance; + PartitionModeSetting PartitionMode; + FloatSetting SplitDistance0; + FloatSetting SplitDistance1; + FloatSetting SplitDistance2; + FloatSetting SplitDistance3; + FloatSetting PSSMLambda; + BoolSetting UsePlaneDepthBias; + FloatSetting Bias; + FloatSetting VSMBias; + FloatSetting OffsetScale; + ShadowMSAASetting ShadowMSAA; + SMFormatSetting SMFormat; + ShadowAnisotropySetting ShadowAnisotropy; + BoolSetting EnableShadowMips; + FloatSetting PositiveExponent; + FloatSetting NegativeExponent; + FloatSetting LightBleedingReduction; + FloatSetting MSMDepthBias; + FloatSetting MSMMomentBias; + FloatSetting BloomThreshold; + FloatSetting BloomMagnitude; + FloatSetting BloomBlurSigma; + FloatSetting KeyValue; + FloatSetting AdaptationRate; + + ConstantBuffer CBuffer; + + void Initialize(ID3D11Device* device) + { + TwBar* tweakBar = Settings.TweakBar(); + + CurrentScene.Initialize(tweakBar, "CurrentScene", "SceneControls", "Current Scene", "The scene to render", Scene::PowerPlant, 3, SceneLabels); + Settings.AddSetting(&CurrentScene); + + AnimateLight.Initialize(tweakBar, "AnimateLight", "SceneControls", "Animate Light", "Automatically rotates the light about the Y axis", false); + Settings.AddSetting(&AnimateLight); + + LightDirection.Initialize(tweakBar, "LightDirection", "SceneControls", "Light Direction", "The direction of the light", Float3(1.0000f, 1.0000f, 1.0000f)); + Settings.AddSetting(&LightDirection); + + LightColor.Initialize(tweakBar, "LightColor", "SceneControls", "Light Color", "The color of the light", Float3(10.0000f, 8.0000f, 5.0000f), true, 0.0000f, 20.0000f, 0.1000f); + Settings.AddSetting(&LightColor); + + CharacterOrientation.Initialize(tweakBar, "CharacterOrientation", "SceneControls", "Character Orientation", "The orientation of the character model", Quaternion(0.0000f, 0.0000f, 0.0000f, 0.0000f)); + Settings.AddSetting(&CharacterOrientation); + + EnableAlbedoMap.Initialize(tweakBar, "EnableAlbedoMap", "SceneControls", "Enable Albedo Map", "Enables using albedo maps when rendering the scene", true); + Settings.AddSetting(&EnableAlbedoMap); + + ShadowMode.Initialize(tweakBar, "ShadowMode", "Shadows", "Shadow Mode", "The shadow mapping technique to use", ShadowMode::FixedSizePCF, 9, ShadowModeLabels); + Settings.AddSetting(&ShadowMode); + + ShadowMapSize.Initialize(tweakBar, "ShadowMapSize", "Shadows", "Shadow Map Size", "The size of the shadow map", ShadowMapSize::SMSize2048, 3, ShadowMapSizeLabels); + Settings.AddSetting(&ShadowMapSize); + + FixedFilterSize.Initialize(tweakBar, "FixedFilterSize", "Shadows", "Fixed Filter Size", "Size of the PCF kernel used for Fixed Sized PCF shadow mode", FixedFilterSize::Filter2x2, 5, FixedFilterSizeLabels); + Settings.AddSetting(&FixedFilterSize); + + FilterSize.Initialize(tweakBar, "FilterSize", "Shadows", "Filter Size", "Width of the filter kernel used for PCF or VSM filtering", 0.0000f, 0.0000f, 100.0000f, 0.1000f); + Settings.AddSetting(&FilterSize); + + VisualizeCascades.Initialize(tweakBar, "VisualizeCascades", "Shadows", "Visualize Cascades", "Colors each cascade a different color to visualize their start and end points", false); + Settings.AddSetting(&VisualizeCascades); + + StabilizeCascades.Initialize(tweakBar, "StabilizeCascades", "Shadows", "Stabilize Cascades", "Keeps consistent sizes for each cascade, and snaps each cascade so that they move in texel-sized increments. Reduces temporal aliasing artifacts, but reduces the effective resolution of the cascades", false); + Settings.AddSetting(&StabilizeCascades); + + FilterAcrossCascades.Initialize(tweakBar, "FilterAcrossCascades", "Shadows", "Filter Across Cascades", "Enables blending across cascade boundaries to reduce the appearance of seams", false); + Settings.AddSetting(&FilterAcrossCascades); + + RandomizeDiscOffsets.Initialize(tweakBar, "RandomizeDiscOffsets", "Shadows", "Randomize Disc Offsets", "Applies a per-pixel random rotation to the sample locations when using disc PCF", false); + Settings.AddSetting(&RandomizeDiscOffsets); + + NumDiscSamples.Initialize(tweakBar, "NumDiscSamples", "Shadows", "Num Disc Samples", "Number of samples to take when using randomized disc PCF", 16, 1, 64); + Settings.AddSetting(&NumDiscSamples); + + AutoComputeDepthBounds.Initialize(tweakBar, "AutoComputeDepthBounds", "Shadows", "Auto-Compute Depth Bounds", "Automatically fits the cascades to the min and max depth of the scene based on the contents of the depth buffer", false); + Settings.AddSetting(&AutoComputeDepthBounds); + + ReadbackLatency.Initialize(tweakBar, "ReadbackLatency", "Shadows", "Depth Bounds Readback Latency", "Number of frames to wait before reading back the depth reduction results", 1, 0, 3); + Settings.AddSetting(&ReadbackLatency); + + GPUSceneSubmission.Initialize(tweakBar, "GPUSceneSubmission", "Shadows", "GPU Scene Submission", "Uses compute shaders to handle shadow setup and mesh batching to minimize draw calls and avoid depth readback latency", false); + Settings.AddSetting(&GPUSceneSubmission); + + MinCascadeDistance.Initialize(tweakBar, "MinCascadeDistance", "Shadows", "Min Cascade Distance", "The closest depth that is covered by the shadow cascades", 0.0000f, 0.0000f, 0.1000f, 0.0010f); + Settings.AddSetting(&MinCascadeDistance); + + MaxCascadeDistance.Initialize(tweakBar, "MaxCascadeDistance", "Shadows", "Max Cascade Distance", "The furthest depth that is covered by the shadow cascades", 1.0000f, 0.0000f, 1.0000f, 0.0100f); + Settings.AddSetting(&MaxCascadeDistance); + + PartitionMode.Initialize(tweakBar, "PartitionMode", "Shadows", "CSM Partition Model", "Controls how the viewable depth range is partitioned into cascades", PartitionMode::Manual, 3, PartitionModeLabels); + Settings.AddSetting(&PartitionMode); + + SplitDistance0.Initialize(tweakBar, "SplitDistance0", "Shadows", "Split Distance 0", "Normalized distance to the end of the first cascade split", 0.0500f, 0.0000f, 1.0000f, 0.0100f); + Settings.AddSetting(&SplitDistance0); + + SplitDistance1.Initialize(tweakBar, "SplitDistance1", "Shadows", "Split Distance 1", "Normalized distance to the end of the first cascade split", 0.1500f, 0.0000f, 1.0000f, 0.0100f); + Settings.AddSetting(&SplitDistance1); + + SplitDistance2.Initialize(tweakBar, "SplitDistance2", "Shadows", "Split Distance 2", "Normalized distance to the end of the first cascade split", 0.5000f, 0.0000f, 1.0000f, 0.0100f); + Settings.AddSetting(&SplitDistance2); + + SplitDistance3.Initialize(tweakBar, "SplitDistance3", "Shadows", "Split Distance 3", "Normalized distance to the end of the first cascade split", 1.0000f, 0.0000f, 1.0000f, 0.0100f); + Settings.AddSetting(&SplitDistance3); + + PSSMLambda.Initialize(tweakBar, "PSSMLambda", "Shadows", "PSSM Lambda", "Lambda parameter used when PSSM mode is used for generated, blends between a linear and a logarithmic distribution", 1.0000f, 0.0000f, 1.0000f, 0.0100f); + Settings.AddSetting(&PSSMLambda); + + UsePlaneDepthBias.Initialize(tweakBar, "UsePlaneDepthBias", "Shadows", "Use Receiver Plane Depth Bias", "Automatically computes a bias value based on the slope of the receiver", true); + Settings.AddSetting(&UsePlaneDepthBias); + + Bias.Initialize(tweakBar, "Bias", "Shadows", "Bias", "Bias used for shadow map depth comparisons", 0.0050f, 0.0000f, 0.0100f, 0.0001f); + Settings.AddSetting(&Bias); + + VSMBias.Initialize(tweakBar, "VSMBias", "Shadows", "VSM Bias (x100)", "Bias used for VSM evaluation", 0.0100f, 0.0000f, 100.0000f, 0.0010f); + Settings.AddSetting(&VSMBias); + + OffsetScale.Initialize(tweakBar, "OffsetScale", "Shadows", "Offset Scale", "Shadow receiver offset along the surface normal direction", 0.0000f, 0.0000f, 100.0000f, 0.1000f); + Settings.AddSetting(&OffsetScale); + + ShadowMSAA.Initialize(tweakBar, "ShadowMSAA", "Shadows", "Shadow MSAA", "MSAA mode to use for VSM or MSM shadow maps", ShadowMSAA::MSAANone, 4, ShadowMSAALabels); + Settings.AddSetting(&ShadowMSAA); + + SMFormat.Initialize(tweakBar, "SMFormat", "Shadows", "VSM/MSM Format", "Texture format to use for VSM or SceneSetting; + +enum class ShadowMode +{ + FixedSizePCF = 0, + GridPCF = 1, + RandomDiscPCF = 2, + OptimizedPCF = 3, + VSM = 4, + EVSM2 = 5, + EVSM4 = 6, + MSMHamburger = 7, + MSMHausdorff = 8, + + NumValues +}; + +typedef EnumSettingT ShadowModeSetting; + +enum class ShadowMapSize +{ + SMSize512 = 0, + SMSize1024 = 1, + SMSize2048 = 2, + + NumValues +}; + +typedef EnumSettingT ShadowMapSizeSetting; + +enum class FixedFilterSize +{ + Filter2x2 = 0, + Filter3x3 = 1, + Filter5x5 = 2, + Filter7x7 = 3, + Filter9x9 = 4, + + NumValues +}; + +typedef EnumSettingT FixedFilterSizeSetting; + +enum class PartitionMode +{ + Manual = 0, + Logarithmic = 1, + PSSM = 2, + + NumValues +}; + +typedef EnumSettingT PartitionModeSetting; + +enum class ShadowMSAA +{ + MSAANone = 0, + MSAA2x = 1, + MSAA4x = 2, + MSAA8x = 3, + + NumValues +}; + +typedef EnumSettingT ShadowMSAASetting; + +enum class SMFormat +{ + SM16Bit = 0, + SM32Bit = 1, + + NumValues +}; + +typedef EnumSettingT SMFormatSetting; + +enum class ShadowAnisotropy +{ + Anisotropy1x = 0, + Anisotropy2x = 1, + Anisotropy4x = 2, + Anisotropy8x = 3, + Anisotropy16x = 4, + + NumValues +}; + +typedef EnumSettingT ShadowAnisotropySetting; + +namespace AppSettings +{ + extern SceneSetting CurrentScene; + extern BoolSetting AnimateLight; + extern DirectionSetting LightDirection; + extern ColorSetting LightColor; + extern OrientationSetting CharacterOrientation; + extern BoolSetting EnableAlbedoMap; + extern ShadowModeSetting ShadowMode; + extern ShadowMapSizeSetting ShadowMapSize; + extern FixedFilterSizeSetting FixedFilterSize; + extern FloatSetting FilterSize; + extern BoolSetting VisualizeCascades; + extern BoolSetting StabilizeCascades; + extern BoolSetting FilterAcrossCascades; + extern BoolSetting RandomizeDiscOffsets; + extern IntSetting NumDiscSamples; + extern BoolSetting AutoComputeDepthBounds; + extern IntSetting ReadbackLatency; + extern BoolSetting GPUSceneSubmission; + extern FloatSetting MinCascadeDistance; + extern FloatSetting MaxCascadeDistance; + extern PartitionModeSetting PartitionMode; + extern FloatSetting SplitDistance0; + extern FloatSetting SplitDistance1; + extern FloatSetting SplitDistance2; + extern FloatSetting SplitDistance3; + extern FloatSetting PSSMLambda; + extern BoolSetting UsePlaneDepthBias; + extern FloatSetting Bias; + extern FloatSetting VSMBias; + extern FloatSetting OffsetScale; + extern ShadowMSAASetting ShadowMSAA; + extern SMFormatSetting SMFormat; + extern ShadowAnisotropySetting ShadowAnisotropy; + extern BoolSetting EnableShadowMips; + extern FloatSetting PositiveExponent; + extern FloatSetting NegativeExponent; + extern FloatSetting LightBleedingReduction; + extern FloatSetting MSMDepthBias; + extern FloatSetting MSMMomentBias; + extern FloatSetting BloomThreshold; + extern FloatSetting BloomMagnitude; + extern FloatSetting BloomBlurSigma; + extern FloatSetting KeyValue; + extern FloatSetting AdaptationRate; + + struct AppSettingsCBuffer + { + Float3 LightDirection; + Float4Align Float3 LightColor; + bool32 EnableAlbedoMap; + int32 ShadowMapSize; + float FilterSize; + bool32 StabilizeCascades; + int32 NumDiscSamples; + bool32 AutoComputeDepthBounds; + float MinCascadeDistance; + float MaxCascadeDistance; + int32 PartitionMode; + float SplitDistance0; + float SplitDistance1; + float SplitDistance2; + float SplitDistance3; + float PSSMLambda; + float Bias; + float VSMBias; + float OffsetScale; + int32 SMFormat; + float PositiveExponent; + float NegativeExponent; + float LightBleedingReduction; + float MSMDepthBias; + float MSMMomentBias; + float BloomThreshold; + float BloomMagnitude; + float BloomBlurSigma; + float KeyValue; + float AdaptationRate; + }; + + extern ConstantBuffer CBuffer; + + void Initialize(ID3D11Device* device); + void UpdateCBuffer(ID3D11DeviceContext* context); +}; + +// ================================================================================================ + +namespace AppSettings +{ + static const uint64 NumFilterableShadowModes = uint64(ShadowMode::NumValues) - uint64(ShadowMode::VSM); + + inline uint32 FixedFilterKernelSize(uint32 value) + { + static const uint32 KernelSizes[] = { 2, 3, 5, 7, 9 }; + StaticAssert_(_countof(KernelSizes) >= uint64(FixedFilterSize::NumValues)); + return KernelSizes[value]; + } + + inline uint32 FixedFilterKernelSize() + { + return FixedFilterKernelSize(FixedFilterSize); + } + + inline uint32 ShadowMapResolution(uint32 value) + { + static const uint32 Sizes[] = { 512, 1024, 2048 }; + StaticAssert_(_countof(Sizes) == uint64(ShadowMapSize::NumValues)); + return Sizes[value]; + } + + inline uint32 ShadowMapResolution() + { + return ShadowMapResolution(ShadowMapSize); + } + + inline bool UseVSM(uint32 value) + { + return value >= uint32(ShadowMode::VSM) && + value <= uint32(ShadowMode::EVSM4); + } + + inline bool UseVSM() + { + return UseVSM(ShadowMode); + } + + inline bool UseEVSM(uint32 value) + { + return value == uint32(ShadowMode::EVSM2) || + value == uint32(ShadowMode::EVSM4); + } + + inline bool UseEVSM() + { + return UseEVSM(ShadowMode); + } + + inline bool UseMSM(uint32 value) + { + return value == uint32(ShadowMode::MSMHamburger) || + value == uint32(ShadowMode::MSMHausdorff); + } + + inline bool UseMSM() + { + return UseMSM(ShadowMode); + } + + inline bool UseFilterableShadows(uint32 value) + { + return UseVSM(value) || UseMSM(value); + } + + inline bool UseFilterableShadows() + { + return UseFilterableShadows(ShadowMode); + } + + inline uint32 MSAASamples(uint32 value) + { + static const uint32 Samples[] = { 1, 2, 4, 8 }; + StaticAssert_(_countof(Samples) == uint64(ShadowMSAA::NumValues)); + return Samples[value]; + } + + inline uint32 MSAASamples() + { + return MSAASamples(ShadowMSAA); + } + + inline uint32 NumAnisotropicSamples(uint32 value) + { + const uint32 NumSamples[] = { 1, 2, 4, 8, 16 }; + StaticAssert_(_countof(NumSamples) == uint64(ShadowAnisotropy::NumValues)); + return NumSamples[value]; + } + + inline uint32 NumAnisotropicSamples() + { + return NumAnisotropicSamples(ShadowAnisotropy); + } + + void Update(); +} \ No newline at end of file diff --git a/Shadows/AppSettings.hlsl b/Shadows/AppSettings.hlsl new file mode 100644 index 0000000..cbed84a --- /dev/null +++ b/Shadows/AppSettings.hlsl @@ -0,0 +1,76 @@ +cbuffer AppSettings : register(b7) +{ + float3 LightDirection; + float3 LightColor; + bool EnableAlbedoMap; + int ShadowMapSize; + float FilterSize; + bool StabilizeCascades; + int NumDiscSamples; + bool AutoComputeDepthBounds; + float MinCascadeDistance; + float MaxCascadeDistance; + int PartitionMode; + float SplitDistance0; + float SplitDistance1; + float SplitDistance2; + float SplitDistance3; + float PSSMLambda; + float Bias; + float VSMBias; + float OffsetScale; + int SMFormat; + float PositiveExponent; + float NegativeExponent; + float LightBleedingReduction; + float MSMDepthBias; + float MSMMomentBias; + float BloomThreshold; + float BloomMagnitude; + float BloomBlurSigma; + float KeyValue; + float AdaptationRate; +} + +static const int Scene_PowerPlant = 0; +static const int Scene_Tower = 1; +static const int Scene_Columns = 2; + +static const int ShadowMode_FixedSizePCF = 0; +static const int ShadowMode_GridPCF = 1; +static const int ShadowMode_RandomDiscPCF = 2; +static const int ShadowMode_OptimizedPCF = 3; +static const int ShadowMode_VSM = 4; +static const int ShadowMode_EVSM2 = 5; +static const int ShadowMode_EVSM4 = 6; +static const int ShadowMode_MSMHamburger = 7; +static const int ShadowMode_MSMHausdorff = 8; + +static const int ShadowMapSize_SMSize512 = 0; +static const int ShadowMapSize_SMSize1024 = 1; +static const int ShadowMapSize_SMSize2048 = 2; + +static const int FixedFilterSize_Filter2x2 = 0; +static const int FixedFilterSize_Filter3x3 = 1; +static const int FixedFilterSize_Filter5x5 = 2; +static const int FixedFilterSize_Filter7x7 = 3; +static const int FixedFilterSize_Filter9x9 = 4; + +static const int PartitionMode_Manual = 0; +static const int PartitionMode_Logarithmic = 1; +static const int PartitionMode_PSSM = 2; + +static const int ShadowMSAA_MSAANone = 0; +static const int ShadowMSAA_MSAA2x = 1; +static const int ShadowMSAA_MSAA4x = 2; +static const int ShadowMSAA_MSAA8x = 3; + +static const int SMFormat_SM16Bit = 0; +static const int SMFormat_SM32Bit = 1; + +static const int ShadowAnisotropy_Anisotropy1x = 0; +static const int ShadowAnisotropy_Anisotropy2x = 1; +static const int ShadowAnisotropy_Anisotropy4x = 2; +static const int ShadowAnisotropy_Anisotropy8x = 3; +static const int ShadowAnisotropy_Anisotropy16x = 4; + diff --git a/Shadows/DepthOnly.hlsl b/Shadows/DepthOnly.hlsl new file mode 100644 index 0000000..bc89108 --- /dev/null +++ b/Shadows/DepthOnly.hlsl @@ -0,0 +1,27 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +// ================================================================================================ +// Constant buffers +// ================================================================================================ +cbuffer VSConstants : register(cb0) +{ + float4x4 World; + float4x4 ViewProjection; +} + +// ================================================================================================ +// Vertex Shader +// ================================================================================================ +float4 VS(in float3 PositionOS : POSITION) : SV_Position +{ + float3 positionWS = mul(float4(PositionOS, 1.0f), World).xyz; + return mul(float4(positionWS, 1.0f), ViewProjection); +} \ No newline at end of file diff --git a/Shadows/DepthReduction.hlsl b/Shadows/DepthReduction.hlsl new file mode 100644 index 0000000..2787b1a --- /dev/null +++ b/Shadows/DepthReduction.hlsl @@ -0,0 +1,238 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Includes +//================================================================================================= +#include "SharedConstants.h" + +//================================================================================================= +// Constants +//================================================================================================= +#define MSAA_ 1 +static const uint NumThreads = ReductionTGSize * ReductionTGSize; + +//================================================================================================= +// Resources +//================================================================================================= +#if MSAA_ + Texture2DMS DepthMap : register(t0); +#else + Texture2D DepthMap : register(t0); +#endif + +Texture2D ReductionMap : register(t0); + +SamplerState LinearClampSampler : register(s0); + +RWTexture2D OutputMap : register(u0); + +cbuffer ReductionConstants : register(b0) +{ + float4x4 Projection; + float NearClip; + float FarClip; +} + +#if CS_ + // -- shared memory + groupshared float2 depthSamples[NumThreads]; +#endif + +// ------------------------------------------------------------------------------------------------ +// Returns the min of 4 values +// ------------------------------------------------------------------------------------------------ +float Min4(in float4 values) +{ + return min(min(values.x, values.y), min(values.z, values.w)); +} + +// ------------------------------------------------------------------------------------------------ +// Returns the max of 4 values +// ------------------------------------------------------------------------------------------------ +float Max4(in float4 values) +{ + return max(max(values.x, values.y), max(values.z, values.w)); +} + +// First pass of the depth reduction +float2 DepthReductionInitialPS(in float4 PositionSS : SV_Position, + in float2 TexCoord : TEXCOORD0) : SV_Target0 +{ + float minDepth = 1.0f; + float maxDepth = 0.0f; + + #if MSAA_ + uint w, h, numSamples; + DepthMap.GetDimensions(w, h, numSamples); + + for(uint sIdx = 0; sIdx < numSamples; ++sIdx) + { + [unroll] + for(uint y = 0; y < 2; ++y) + { + [unroll] + for(uint x = 0; x < 2; ++x) + { + uint2 samplePos = min(uint2(PositionSS.xy) * 2 + uint2(x, y), uint2(w - 1, h - 1)); + float depthSample = DepthMap.Load(samplePos, sIdx); + + if(depthSample < 1.0f) + { + // Convert to linear Z + depthSample = Projection._43 / (depthSample - Projection._33); + depthSample = saturate((depthSample - NearClip) / (FarClip - NearClip)); + minDepth = min(minDepth, depthSample); + maxDepth = max(maxDepth, depthSample); + } + } + } + } + #else + float4 depthSamples = DepthMap.GatherRed(LinearClampSampler, TexCoord); + depthSamples = Projection._43 / (depthSamples - Projection._33); + depthSamples = saturate((depthSamples - NearClip) / (FarClip - NearClip)); + float minDepth = Min4(depthSamples); + float maxDepth = Max4(depthSamples); + #endif + + return float2(minDepth, maxDepth); +} + +// Subsequent passes of the depth reduction +float2 DepthReductionPS(in float4 PositionSS : SV_Position, + in float2 TexCoord : TEXCOORD0) : SV_Target0 +{ + float4 minSamples = ReductionMap.GatherRed(LinearClampSampler, TexCoord); + float4 maxSamples = ReductionMap.GatherGreen(LinearClampSampler, TexCoord); + float minDepth = Min4(minSamples); + float maxDepth = Max4(maxSamples); + + return float2(minDepth, maxDepth); +} + +#if CS_ + +// First pass of the depth reduction +[numthreads(ReductionTGSize, ReductionTGSize, 1)] +void DepthReductionInitialCS(in uint3 GroupID : SV_GroupID, + in uint3 GroupThreadID : SV_GroupThreadID, + uint ThreadIndex : SV_GroupIndex) +{ + float minDepth = 1.0f; + float maxDepth = 0.0f; + + #if MSAA_ + uint2 textureSize; + uint numSamples; + DepthMap.GetDimensions(textureSize.x, textureSize.y, numSamples); + #else + uint2 textureSize; + DepthMap.GetDimensions(textureSize.x, textureSize.y); + #endif + + uint2 samplePos = GroupID.xy * ReductionTGSize + GroupThreadID.xy; + samplePos = min(samplePos, textureSize - 1); + + #if MSAA_ + for(uint sIdx = 0; sIdx < numSamples; ++sIdx) + { + float depthSample = DepthMap.Load(samplePos, sIdx); + + if(depthSample < 1.0f) + { + // Convert to linear Z + depthSample = Projection._43 / (depthSample - Projection._33); + depthSample = saturate((depthSample - NearClip) / (FarClip - NearClip)); + minDepth = min(minDepth, depthSample); + maxDepth = max(maxDepth, depthSample); + } + } + #else + float depthSample = DepthMap[samplePos]; + + if(depthSample < 1.0f) + { + // Convert to linear Z + depthSample = Projection._43 / (depthSample - Projection._33); + depthSample = saturate((depthSample - NearClip) / (FarClip - NearClip)); + minDepth = min(minDepth, depthSample); + maxDepth = max(maxDepth, depthSample); + } + #endif + + // Store in shared memory + depthSamples[ThreadIndex] = float2(minDepth, maxDepth); + GroupMemoryBarrierWithGroupSync(); + + // Reduce + [unroll] + for(uint s = NumThreads / 2; s > 0; s >>= 1) + { + if(ThreadIndex < s) + { + depthSamples[ThreadIndex].x = min(depthSamples[ThreadIndex].x, depthSamples[ThreadIndex + s].x); + depthSamples[ThreadIndex].y = max(depthSamples[ThreadIndex].y, depthSamples[ThreadIndex + s].y); + } + + GroupMemoryBarrierWithGroupSync(); + } + + if(ThreadIndex == 0) + { + minDepth = depthSamples[0].x; + maxDepth = depthSamples[0].y; + OutputMap[GroupID.xy] = float2(minDepth, maxDepth); + } +} + +// Subsequent passes of the depth reduction +[numthreads(ReductionTGSize, ReductionTGSize, 1)] +void DepthReductionCS(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID, + in uint ThreadIndex : SV_GroupIndex) +{ + uint2 textureSize; + ReductionMap.GetDimensions(textureSize.x, textureSize.y); + + uint2 samplePos = GroupID.xy * ReductionTGSize + GroupThreadID.xy; + samplePos = min(samplePos, textureSize - 1); + + float minDepth = ReductionMap[samplePos].x; + float maxDepth = ReductionMap[samplePos].y; + + if(minDepth == 0.0f) + minDepth = 1.0f; + + // Store in shared memory + depthSamples[ThreadIndex] = float2(minDepth, maxDepth); + GroupMemoryBarrierWithGroupSync(); + + // Reduce + [unroll] + for(uint s = NumThreads / 2; s > 0; s >>= 1) + { + if(ThreadIndex < s) + { + depthSamples[ThreadIndex].x = min(depthSamples[ThreadIndex].x, depthSamples[ThreadIndex + s].x); + depthSamples[ThreadIndex].y = max(depthSamples[ThreadIndex].y, depthSamples[ThreadIndex + s].y); + } + + GroupMemoryBarrierWithGroupSync(); + } + + if(ThreadIndex == 0) + { + minDepth = depthSamples[0].x; + maxDepth = depthSamples[0].y; + OutputMap[GroupID.xy] = float2(minDepth, maxDepth); + } +} + +#endif // CS_ \ No newline at end of file diff --git a/Shadows/GPUBatch.hlsl b/Shadows/GPUBatch.hlsl new file mode 100644 index 0000000..dc24ee4 --- /dev/null +++ b/Shadows/GPUBatch.hlsl @@ -0,0 +1,106 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Includes +//================================================================================================= +#include "SharedConstants.h" +#include "AppSettings.hlsl" + +//================================================================================================= +// Constant buffers +//================================================================================================= +cbuffer Constants : register(b0) +{ + float4 FrustumPlanes[6]; + uint NumDrawCalls; + bool CullNearZ; +} + +//================================================================================================= +// Resources +//================================================================================================= +StructuredBuffer DrawCalls : register(t0); +RWByteAddressBuffer DrawArgsBuffer : SV_GroupIndex : register(u0); +AppendStructuredBuffer CulledDrawsOutput : register(u1); + +StructuredBuffer CulledDraws : register(t0); +StructuredBuffer Indices : register(t1); +RWBuffer CulledIndices : register(u0); + +// Clears an indirect args buffer to default values +[numthreads(1, 1, 1)] +void ClearArgsBuffer() +{ + DrawArgsBuffer.Store4(0, uint4(0, 1, 0, 0)); + DrawArgsBuffer.Store(16, 0); +} + +// Checks if the bounding sphere of a draw call intersects with the view frustum +static bool IsVisible(in DrawCall drawCall) +{ + bool inFrustum = true; + + [unroll] + for(uint i = 0; i < 5; ++i) + { + float d = dot(FrustumPlanes[i], float4(drawCall.SphereCenter, 1.0f)); + inFrustum = inFrustum && (d >= -drawCall.SphereRadius); + } + + if(CullNearZ) + { + float d = dot(FrustumPlanes[5], float4(drawCall.SphereCenter, 1.0f)); + inFrustum = inFrustum && (d >= -drawCall.SphereRadius); + } + + return inFrustum; +} + +// Frustum culls each draw call, and allocates space for visible indices in the +// culled indices buffer. This uses one thread per draw call. +[numthreads(CullTGSize, 1, 1)] +void CullDrawCalls(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID, + uint ThreadIndex : SV_GroupIndex) +{ + const uint drawIdx = CullTGSize * GroupID.x + ThreadIndex; + if(drawIdx >= NumDrawCalls) + return; + + DrawCall drawCall = DrawCalls[drawIdx]; + + if(IsVisible(drawCall)) + { + CulledDraw culledDraw; + culledDraw.SrcIndexStart = drawCall.StartIndex; + culledDraw.NumIndices = drawCall.NumIndices; + DrawArgsBuffer.InterlockedAdd(0, drawCall.NumIndices, culledDraw.DstIndexStart); + + CulledDrawsOutput.Append(culledDraw); + } +} + +// Copies indices from visible draw calls to the combined index buffer. Thus uses one +// thread group per draw call, where the indices of a draw call are copied in parallel +// to the output buffer using the threads belonging to the thread group +[numthreads(CullTGSize, 1, 1)] +void BatchDrawCalls(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID, + uint ThreadIndex : SV_GroupIndex) +{ + const uint drawIdx = GroupID.x; + CulledDraw drawCall = CulledDraws[drawIdx]; + + for(uint currIdx = ThreadIndex; currIdx < drawCall.NumIndices; currIdx += CullTGSize) + { + const uint srcIdx = currIdx + drawCall.SrcIndexStart; + const uint dstIdx = currIdx + drawCall.DstIndexStart; + CulledIndices[dstIdx] = Indices[srcIdx]; + } +} \ No newline at end of file diff --git a/Shadows/Icon.ico b/Shadows/Icon.ico new file mode 100644 index 0000000..c46c658 Binary files /dev/null and b/Shadows/Icon.ico differ diff --git a/Shadows/MSM.hlsl b/Shadows/MSM.hlsl new file mode 100644 index 0000000..0365c21 --- /dev/null +++ b/Shadows/MSM.hlsl @@ -0,0 +1,147 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#if ShadowMode_ == ShadowModeMSMHamburger_ || ShadowMode_ == ShadowModeMSMHausdorff_ + #define UseMSM_ 1 +#else + #define UseMSM_ 0 +#endif + +float4 GetOptimizedMoments(in float depth) +{ + float square = depth * depth; + float4 moments = float4(depth, square, square * depth, square * square); + float4 optimized = mul(moments, float4x4(-2.07224649f, 13.7948857237f, 0.105877704f, 9.7924062118f, + 32.23703778f, -59.4683975703f, -1.9077466311f, -33.7652110555f, + -68.571074599f, 82.0359750338f, 9.3496555107f, 47.9456096605f, + 39.3703274134f,-35.364903257f, -6.6543490743f, -23.9728048165f)); + optimized[0] += 0.035955884801f; + return optimized; +} + +float4 ConvertOptimizedMoments(in float4 optimizedMoments) +{ + optimizedMoments[0] -= 0.035955884801f; + return mul(optimizedMoments, float4x4(0.2227744146f, 0.1549679261f, 0.1451988946f, 0.163127443f, + 0.0771972861f, 0.1394629426f, 0.2120202157f, 0.2591432266f, + 0.7926986636f, 0.7963415838f, 0.7258694464f, 0.6539092497f, + 0.0319417555f,-0.1722823173f,-0.2758014811f,-0.3376131734f)); +} + +float ComputeMSMHamburger(in float4 moments, in float fragmentDepth , in float depthBias, in float momentBias) +{ + // Bias input data to avoid artifacts + float4 b = lerp(moments, float4(0.5f, 0.5f, 0.5f, 0.5f), momentBias); + float3 z; + z[0] = fragmentDepth - depthBias; + + // Compute a Cholesky factorization of the Hankel matrix B storing only non- + // trivial entries or related products + float L32D22 = mad(-b[0], b[1], b[2]); + float D22 = mad(-b[0], b[0], b[1]); + float squaredDepthVariance = mad(-b[1], b[1], b[3]); + float D33D22 = dot(float2(squaredDepthVariance, -L32D22), float2(D22, L32D22)); + float InvD22 = 1.0f / D22; + float L32 = L32D22 * InvD22; + + // Obtain a scaled inverse image of bz = (1,z[0],z[0]*z[0])^T + float3 c = float3(1.0f, z[0], z[0] * z[0]); + + // Forward substitution to solve L*c1=bz + c[1] -= b.x; + c[2] -= b.y + L32 * c[1]; + + // Scaling to solve D*c2=c1 + c[1] *= InvD22; + c[2] *= D22 / D33D22; + + // Backward substitution to solve L^T*c3=c2 + c[1] -= L32 * c[2]; + c[0] -= dot(c.yz, b.xy); + + // Solve the quadratic equation c[0]+c[1]*z+c[2]*z^2 to obtain solutions + // z[1] and z[2] + float p = c[1] / c[2]; + float q = c[0] / c[2]; + float D = (p * p * 0.25f) - q; + float r = sqrt(D); + z[1] =- p * 0.5f - r; + z[2] =- p * 0.5f + r; + + // Compute the shadow intensity by summing the appropriate weights + float4 switchVal = (z[2] < z[0]) ? float4(z[1], z[0], 1.0f, 1.0f) : + ((z[1] < z[0]) ? float4(z[0], z[1], 0.0f, 1.0f) : + float4(0.0f,0.0f,0.0f,0.0f)); + float quotient = (switchVal[0] * z[2] - b[0] * (switchVal[0] + z[2]) + b[1])/((z[2] - switchVal[1]) * (z[0] - z[1])); + float shadowIntensity = switchVal[2] + switchVal[3] * quotient; + return 1.0f - saturate(shadowIntensity); +} + +float ComputeMSMHausdorff(in float4 moments, in float fragmentDepth, in float depthBias, in float momentBias) +{ + // Bias input data to avoid artifacts + float4 b = lerp(moments, float4(0.5f, 0.5f, 0.5f, 0.5f), momentBias); + float3 z; + z[0] = fragmentDepth - depthBias; + + // Compute a Cholesky factorization of the Hankel matrix B storing only non- + // trivial entries or related products + float L32D22 = mad(-b[0], b[1], b[2]); + float D22 = mad(-b[0], b[0], b[1]); + float squaredDepthVariance = mad(-b[1], b[1], b[3]); + float D33D22 = dot(float2(squaredDepthVariance, -L32D22), float2(D22, L32D22)); + float InvD22 = 1.0f / D22; + float L32 = L32D22 * InvD22; + + // Obtain a scaled inverse image of bz=(1,z[0],z[0]*z[0])^T + float3 c = float3(1.0f, z[0], z[0] * z[0]); + + // Forward substitution to solve L*c1=bz + c[1] -= b.x; + c[2] -= b.y + L32 * c[1]; + + // Scaling to solve D*c2=c1 + c[1] *= InvD22; + c[2] *= D22 / D33D22; + + // Backward substitution to solve L^T*c3=c2 + c[1] -= L32 * c[2]; + c[0] -= dot(c.yz, b.xy); + + // Solve the quadratic equation c[0]+c[1]*z+c[2]*z^2 to obtain solutions z[1] + // and z[2] + float p = c[1] / c[2]; + float q = c[0] / c[2]; + float D = ((p * p) / 4.0f) - q; + float r = sqrt(D); + z[1] =- (p / 2.0f) - r; + z[2] =- (p / 2.0f) + r; + + float shadowIntensity = 1.0f; + + // Use a solution made of four deltas if the solution with three deltas is invalid + if(z[1] < 0.0f || z[2] > 1.0f) + { + float zFree = ((b[2] - b[1]) * z[0] + b[2] - b[3]) / ((b[1] - b[0]) * z[0] + b[1] - b[2]); + float w1Factor = (z[0] > zFree) ? 1.0f : 0.0f; + shadowIntensity = (b[1] - b[0] + (b[2] - b[0] - (zFree + 1.0f) * (b[1] - b[0])) * (zFree - w1Factor - z[0]) + /(z[0] * (z[0] - zFree))) / (zFree - w1Factor) + 1.0f - b[0]; + } + // Use the solution with three deltas + else{ + float4 switchVal = (z[2] < z[0]) ? float4(z[1], z[0], 1.0f, 1.0f) : + ((z[1] < z[0]) ? float4(z[0], z[1], 0.0f, 1.0f) : + float4(0.0f, 0.0f, 0.0f, 0.0f)); + float quotient = (switchVal[0] * z[2] - b[0] * (switchVal[0] + z[2]) + b[1]) / ((z[2] - switchVal[1]) * (z[0] - z[1])); + shadowIntensity = switchVal[2] + switchVal[3] * quotient; + } + + return 1.0f - saturate(shadowIntensity); +} \ No newline at end of file diff --git a/Shadows/Mesh.hlsl b/Shadows/Mesh.hlsl new file mode 100644 index 0000000..be329af --- /dev/null +++ b/Shadows/Mesh.hlsl @@ -0,0 +1,803 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Includes +//================================================================================================= +#include "SharedConstants.h" +#include "PCFKernels.hlsl" +#include "VSM.hlsl" +#include "MSM.hlsl" +#include "AppSettings.hlsl" + +//================================================================================================= +// Constant buffers +//================================================================================================= +cbuffer VSConstants : register(b0) +{ + float4x4 World; + float4x4 ViewProjection; +} + +cbuffer PSConstants : register(b0) +{ + float3 CameraPosWS; + float4x4 ShadowMatrix; + float4 CascadeSplits; + float4 CascadeOffsets[NumCascades]; + float4 CascadeScales[NumCascades]; +} + +//================================================================================================= +// Resources +//================================================================================================= +Texture2D DiffuseMap : register(t0); +Texture2DArray ShadowMap : register(t1); +Texture2D RandomRotations : register(t2); + +SamplerState AnisoSampler : register(s0); +SamplerComparisonState ShadowSampler : register(s1); +SamplerComparisonState ShadowSamplerPCF : register(s2); +SamplerState VSMSampler : register(s3); + +//================================================================================================= +// Input/Output structs +//================================================================================================= +struct VSInput +{ + float3 PositionOS : POSITION; + float3 NormalOS : NORMAL; + float2 TexCoord : TEXCOORD0; +}; + +struct VSOutput +{ + float4 PositionCS : SV_Position; + float3 PositionWS : POSITIONWS; + float3 NormalWS : NORMALWS; + float2 TexCoord : TEXCOORD; + float DepthVS : DEPTHVS; +}; + +struct PSInput +{ + float4 PositionSS : SV_Position; + float3 PositionWS : POSITIONWS; + float3 NormalWS : NORMALWS; + float2 TexCoord : TEXCOORD; + float DepthVS : DEPTHVS; +}; + +//================================================================================================= +// Vertex Shader +//================================================================================================= +VSOutput VS(in VSInput input) +{ + VSOutput output; + + // Calc the world-space position + output.PositionWS = mul(float4(input.PositionOS, 1.0f), World).xyz; + + // Calc the clip-space position + output.PositionCS = mul(float4(output.PositionWS, 1.0f), ViewProjection); + + output.DepthVS = output.PositionCS.w; + + // Rotate the normal into world space + output.NormalWS = normalize(mul(input.NormalOS, (float3x3)World)); + + // Pass along the texture coordinate + output.TexCoord = input.TexCoord; + + return output; +} + +float2 ComputeReceiverPlaneDepthBias(float3 texCoordDX, float3 texCoordDY) +{ + float2 biasUV; + biasUV.x = texCoordDY.y * texCoordDX.z - texCoordDX.y * texCoordDY.z; + biasUV.y = texCoordDX.x * texCoordDY.z - texCoordDY.x * texCoordDX.z; + biasUV *= 1.0f / ((texCoordDX.x * texCoordDY.y) - (texCoordDX.y * texCoordDY.x)); + return biasUV; +} + +//------------------------------------------------------------------------------------------------- +// Samples the shadow map with a fixed-size PCF kernel optimized with GatherCmp. Uses code +// from "Fast Conventional Shadow Filtering" by Holger Gruen, in GPU Pro. +//------------------------------------------------------------------------------------------------- +float SampleShadowMapFixedSizePCF(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, in uint cascadeIdx) { + float2 shadowMapSize; + float numSlices; + ShadowMap.GetDimensions(shadowMapSize.x, shadowMapSize.y, numSlices); + + float lightDepth = shadowPos.z; + + const float bias = Bias; + + #if UsePlaneDepthBias_ + float2 texelSize = 1.0f / shadowMapSize; + + float2 receiverPlaneDepthBias = ComputeReceiverPlaneDepthBias(shadowPosDX, shadowPosDY); + + // Static depth biasing to make up for incorrect fractional sampling on the shadow map grid + float fractionalSamplingError = dot(float2(1.0f, 1.0f) * texelSize, abs(receiverPlaneDepthBias)); + lightDepth -= min(fractionalSamplingError, 0.01f); + #else + lightDepth -= bias; + #endif + + #if FilterSize_ == 2 + return ShadowMap.SampleCmpLevelZero(ShadowSamplerPCF, float3(shadowPos.xy, cascadeIdx), lightDepth); + #else + const int FS_2 = FilterSize_ / 2; + + float2 tc = shadowPos.xy; + + float4 s = 0.0f; + float2 stc = (shadowMapSize * tc.xy) + float2(0.5f, 0.5f); + float2 tcs = floor(stc); + float2 fc; + int row; + int col; + float w = 0.0f; + float4 v1[FS_2 + 1]; + float2 v0[FS_2 + 1]; + + fc.xy = stc - tcs; + tc.xy = tcs / shadowMapSize; + + for(row = 0; row < FilterSize_; ++row) + for(col = 0; col < FilterSize_; ++col) + w += W[row][col]; + + // -- loop over the rows + [unroll] + for(row = -FS_2; row <= FS_2; row += 2) + { + [unroll] + for(col = -FS_2; col <= FS_2; col += 2) + { + float value = W[row + FS_2][col + FS_2]; + + if(col > -FS_2) + value += W[row + FS_2][col + FS_2 - 1]; + + if(col < FS_2) + value += W[row + FS_2][col + FS_2 + 1]; + + if(row > -FS_2) { + value += W[row + FS_2 - 1][col + FS_2]; + + if(col < FS_2) + value += W[row + FS_2 - 1][col + FS_2 + 1]; + + if(col > -FS_2) + value += W[row + FS_2 - 1][col + FS_2 - 1]; + } + + if(value != 0.0f) + { + float sampleDepth = lightDepth; + + #if UsePlaneDepthBias_ + // Compute offset and apply planar depth bias + float2 offset = float2(col, row) * texelSize; + sampleDepth += dot(offset, receiverPlaneDepthBias); + #endif + + v1[(col + FS_2) / 2] = ShadowMap.GatherCmp(ShadowSampler, float3(tc.xy, cascadeIdx), + sampleDepth, int2(col, row)); + } + else + v1[(col + FS_2) / 2] = 0.0f; + + if(col == -FS_2) + { + s.x += (1.0f - fc.y) * (v1[0].w * (W[row + FS_2][col + FS_2] + - W[row + FS_2][col + FS_2] * fc.x) + + v1[0].z * (fc.x * (W[row + FS_2][col + FS_2] + - W[row + FS_2][col + FS_2 + 1.0f]) + + W[row + FS_2][col + FS_2 + 1])); + s.y += fc.y * (v1[0].x * (W[row + FS_2][col + FS_2] + - W[row + FS_2][col + FS_2] * fc.x) + + v1[0].y * (fc.x * (W[row + FS_2][col + FS_2] + - W[row + FS_2][col + FS_2 + 1]) + + W[row + FS_2][col + FS_2 + 1])); + if(row > -FS_2) + { + s.z += (1.0f - fc.y) * (v0[0].x * (W[row + FS_2 - 1][col + FS_2] + - W[row + FS_2 - 1][col + FS_2] * fc.x) + + v0[0].y * (fc.x * (W[row + FS_2 - 1][col + FS_2] + - W[row + FS_2 - 1][col + FS_2 + 1]) + + W[row + FS_2 - 1][col + FS_2 + 1])); + s.w += fc.y * (v1[0].w * (W[row + FS_2 - 1][col + FS_2] + - W[row + FS_2 - 1][col + FS_2] * fc.x) + + v1[0].z * (fc.x * (W[row + FS_2 - 1][col + FS_2] + - W[row + FS_2 - 1][col + FS_2 + 1]) + + W[row + FS_2 - 1][col + FS_2 + 1])); + } + } + else if(col == FS_2) + { + s.x += (1 - fc.y) * (v1[FS_2].w * (fc.x * (W[row + FS_2][col + FS_2 - 1] + - W[row + FS_2][col + FS_2]) + W[row + FS_2][col + FS_2]) + + v1[FS_2].z * fc.x * W[row + FS_2][col + FS_2]); + s.y += fc.y * (v1[FS_2].x * (fc.x * (W[row + FS_2][col + FS_2 - 1] + - W[row + FS_2][col + FS_2] ) + W[row + FS_2][col + FS_2]) + + v1[FS_2].y * fc.x * W[row + FS_2][col + FS_2]); + if(row > -FS_2) { + s.z += (1 - fc.y) * (v0[FS_2].x * (fc.x * (W[row + FS_2 - 1][col + FS_2 - 1] + - W[row + FS_2 - 1][col + FS_2]) + + W[row + FS_2 - 1][col + FS_2]) + + v0[FS_2].y * fc.x * W[row + FS_2 - 1][col + FS_2]); + s.w += fc.y * (v1[FS_2].w * (fc.x * (W[row + FS_2 - 1][col + FS_2 - 1] + - W[row + FS_2 - 1][col + FS_2]) + + W[row + FS_2 - 1][col + FS_2]) + + v1[FS_2].z * fc.x * W[row + FS_2 - 1][col + FS_2]); + } + } + else + { + s.x += (1 - fc.y) * (v1[(col + FS_2) / 2].w * (fc.x * (W[row + FS_2][col + FS_2 - 1] + - W[row + FS_2][col + FS_2 + 0] ) + W[row + FS_2][col + FS_2 + 0]) + + v1[(col + FS_2) / 2].z * (fc.x * (W[row + FS_2][col + FS_2 - 0] + - W[row + FS_2][col + FS_2 + 1]) + W[row + FS_2][col + FS_2 + 1])); + s.y += fc.y * (v1[(col + FS_2) / 2].x * (fc.x * (W[row + FS_2][col + FS_2-1] + - W[row + FS_2][col + FS_2 + 0]) + W[row + FS_2][col + FS_2 + 0]) + + v1[(col + FS_2) / 2].y * (fc.x * (W[row + FS_2][col + FS_2 - 0] + - W[row + FS_2][col + FS_2 + 1]) + W[row + FS_2][col + FS_2 + 1])); + if(row > -FS_2) { + s.z += (1 - fc.y) * (v0[(col + FS_2) / 2].x * (fc.x * (W[row + FS_2 - 1][col + FS_2 - 1] + - W[row + FS_2 - 1][col + FS_2 + 0]) + W[row + FS_2 - 1][col + FS_2 + 0]) + + v0[(col + FS_2) / 2].y * (fc.x * (W[row + FS_2 - 1][col + FS_2 - 0] + - W[row + FS_2 - 1][col + FS_2 + 1]) + W[row + FS_2 - 1][col + FS_2 + 1])); + s.w += fc.y * (v1[(col + FS_2) / 2].w * (fc.x * (W[row + FS_2 - 1][col + FS_2 - 1] + - W[row + FS_2 - 1][col + FS_2 + 0]) + W[row + FS_2 - 1][col + FS_2 + 0]) + + v1[(col + FS_2) / 2].z * (fc.x * (W[row + FS_2 - 1][col + FS_2 - 0] + - W[row + FS_2 - 1][col + FS_2 + 1]) + W[row + FS_2 - 1][col + FS_2 + 1])); + } + } + + if(row != FS_2) + v0[(col + FS_2) / 2] = v1[(col + FS_2) / 2].xy; + } + } + + return dot(s, 1.0f) / w; + #endif +} + +//-------------------------------------------------------------------------------------- +// Samples the shadow map using a PCF kernel made up from random points on a disc +//-------------------------------------------------------------------------------------- +float SampleShadowMapRandomDiscPCF(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, in uint cascadeIdx, + in uint2 screenPos) +{ + float2 maxFilterSize = MaxKernelSize / abs(CascadeScales[0].xy); + float2 filterSize = clamp(min(FilterSize.xx, maxFilterSize) * abs(CascadeScales[cascadeIdx].xy), 1.0f, MaxKernelSize); + + float result = 1.0f; + + // Get the size of the shadow map + uint2 shadowMapSize; + uint numSlices; + ShadowMap.GetDimensions(shadowMapSize.x, shadowMapSize.y, numSlices); + + #if UsePlaneDepthBias_ + float2 texelSize = 1.0f / shadowMapSize; + + float2 receiverPlaneDepthBias = ComputeReceiverPlaneDepthBias(shadowPosDX, shadowPosDY); + + // Static depth biasing to make up for incorrect fractional sampling on the shadow map grid + float fractionalSamplingError = dot(float2(1.0f, 1.0f) * texelSize, abs(receiverPlaneDepthBias)); + float shadowDepth = shadowPos.z - min(fractionalSamplingError, 0.01f); + #else + float shadowDepth = shadowPos.z - Bias; + #endif + + [branch] + if(filterSize.x > 1.0f || filterSize.y > 1.0f) + { + #if RandomizeOffsets_ + // Get a value to randomly rotate the kernel by + uint2 randomRotationsSize; + RandomRotations.GetDimensions(randomRotationsSize.x, randomRotationsSize.y); + uint2 randomSamplePos = screenPos % randomRotationsSize; + float theta = RandomRotations[randomSamplePos] * Pi2; + float2x2 randomRotationMatrix = float2x2(float2(cos(theta), -sin(theta)), + float2(sin(theta), cos(theta))); + #endif + + float2 sampleScale = (0.5f * filterSize) / shadowMapSize; + + float sum = 0.0f; + for(uint i = 0; i < uint(NumDiscSamples); ++i) + { + #if RandomizeOffsets_ + float2 sampleOffset = mul(PoissonSamples[i], randomRotationMatrix) * sampleScale; + #else + float2 sampleOffset = PoissonSamples[i] * sampleScale; + #endif + + float2 samplePos = shadowPos.xy + sampleOffset; + + #if UsePlaneDepthBias_ + // Compute offset and apply planar depth bias + float sampleDepth = shadowDepth + dot(sampleOffset, receiverPlaneDepthBias); + #else + float sampleDepth = shadowDepth; + #endif + + sum += ShadowMap.SampleCmpLevelZero(ShadowSamplerPCF, float3(samplePos, cascadeIdx), sampleDepth); + } + + result = sum / NumDiscSamples; + } + else + { + result = ShadowMap.SampleCmpLevelZero(ShadowSamplerPCF, float3(shadowPos.xy, cascadeIdx), shadowDepth); + } + + return result; +} + +//-------------------------------------------------------------------------------------- +// Samples the shadow map grid-sampled PCF +//-------------------------------------------------------------------------------------- +float SampleShadowMapGridPCF(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, in uint cascadeIdx) +{ + float2 maxFilterSize = MaxKernelSize / abs(CascadeScales[0].xy); + float2 filterSize = clamp(min(FilterSize.xx, maxFilterSize) * abs(CascadeScales[cascadeIdx].xy), 1.0f, MaxKernelSize); + + float result = 0.0f; + + // Get the size of the shadow map + uint2 shadowMapSize; + uint numSlices; + ShadowMap.GetDimensions(shadowMapSize.x, shadowMapSize.y, numSlices); + + float2 texelSize = 1.0f / shadowMapSize; + + #if UsePlaneDepthBias_ + float2 receiverPlaneDepthBias = ComputeReceiverPlaneDepthBias(shadowPosDX, shadowPosDY); + + // Static depth biasing to make up for incorrect fractional sampling on the shadow map grid + float fractionalSamplingError = dot(float2(1.0f, 1.0f) * texelSize, abs(receiverPlaneDepthBias)); + float shadowDepth = shadowPos.z - min(fractionalSamplingError, 0.01f); + #else + float shadowDepth = shadowPos.z - Bias; + #endif + + [branch] + if(filterSize.x > 1.0f || filterSize.y > 1.0f) + { + // Get the texel that will be sampled + float2 shadowTexel = shadowPos.xy * shadowMapSize; + float2 texelFraction = frac(shadowTexel); + + const float2 Radius = filterSize / 2.0f; + + int2 minOffset = int2(floor(texelFraction - Radius)); + int2 maxOffset = int2(texelFraction + Radius); + + float weightSum = 0.0f; + + [loop] + for(int y = minOffset.y; y <= maxOffset.y; ++y) + { + float yWeight = 1.0f; + if(y == minOffset.y) + yWeight = saturate((Radius.y - texelFraction.y) + 1.0f + y); + else if(y == maxOffset.y) + yWeight = saturate(Radius.y + texelFraction.y - y); + + [loop] + for(int x = minOffset.x; x <= maxOffset.x; ++x) + { + float2 sampleOffset = texelSize * float2(x, y); + float2 samplePos = shadowPos.xy + sampleOffset; + + #if UsePlaneDepthBias_ + // Compute offset and apply planar depth bias + float sampleDepth = shadowDepth + dot(sampleOffset, receiverPlaneDepthBias); + #else + float sampleDepth = shadowDepth; + #endif + + float sample = ShadowMap.SampleCmpLevelZero(ShadowSampler, float3(samplePos.xy, cascadeIdx), sampleDepth); + + float xWeight = 1.0f; + if(x == minOffset.x) + xWeight = saturate((Radius.x - texelFraction.x) + 1.0f + x); + else if(x == maxOffset.x) + xWeight = saturate(Radius.x + texelFraction.x - x); + + float2 sampleCoverage = float2(xWeight, yWeight); + + float sampleWeight = sampleCoverage.x * sampleCoverage.y; + weightSum += sampleWeight; + + result += sample * sampleWeight; + } + } + + result /= (filterSize.x * filterSize.y); + } + else + { + result = ShadowMap.SampleCmpLevelZero(ShadowSamplerPCF, float3(shadowPos.xy, cascadeIdx), shadowDepth); + } + + return result; +} + +//------------------------------------------------------------------------------------------------- +// Samples the VSM shadow map +//------------------------------------------------------------------------------------------------- +float SampleShadowMapVSM(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, uint cascadeIdx) +{ + float depth = shadowPos.z; + + float2 occluder = ShadowMap.SampleGrad(VSMSampler, float3(shadowPos.xy, cascadeIdx), + shadowPosDX.xy, shadowPosDY.xy).xy; + + return ChebyshevUpperBound(occluder, depth, VSMBias * 0.01, LightBleedingReduction); +} + +//------------------------------------------------------------------------------------------------- +// Samples the EVSM shadow map +//------------------------------------------------------------------------------------------------- +float SampleShadowMapEVSM(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, uint cascadeIdx) +{ + float2 exponents = GetEVSMExponents(PositiveExponent, NegativeExponent, SMFormat); + float2 warpedDepth = WarpDepth(shadowPos.z, exponents); + + float4 occluder = ShadowMap.SampleGrad(VSMSampler, float3(shadowPos.xy, cascadeIdx), + shadowPosDX.xy, shadowPosDY.xy); + + // Derivative of warping at depth + float2 depthScale = VSMBias * 0.01f * exponents * warpedDepth; + float2 minVariance = depthScale * depthScale; + + #if ShadowMode_ == ShadowModeEVSM4_ + float posContrib = ChebyshevUpperBound(occluder.xz, warpedDepth.x, minVariance.x, LightBleedingReduction); + float negContrib = ChebyshevUpperBound(occluder.yw, warpedDepth.y, minVariance.y, LightBleedingReduction); + return min(posContrib, negContrib); + #else + // Positive only + return ChebyshevUpperBound(occluder.xy, warpedDepth.x, minVariance.x, LightBleedingReduction); + #endif +} + +//------------------------------------------------------------------------------------------------- +// Samples the MSM shadow map +//------------------------------------------------------------------------------------------------- +float SampleShadowMapMSM(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, uint cascadeIdx) +{ + float depth = shadowPos.z; + float4 moments = ShadowMap.SampleGrad(VSMSampler, float3(shadowPos.xy, cascadeIdx), + shadowPosDX.xy, shadowPosDY.xy); + if(SMFormat == SMFormat_SM16Bit) + moments = ConvertOptimizedMoments(moments); + + #if ShadowMode_ == ShadowModeMSMHausdorff_ + float result = ComputeMSMHausdorff(moments, depth, MSMDepthBias * 0.001f, MSMMomentBias * 0.001f); + #else + float result = ComputeMSMHamburger(moments, depth, MSMDepthBias * 0.001f, MSMMomentBias * 0.001f); + #endif + + return ReduceLightBleeding(result, LightBleedingReduction); +} + + +//------------------------------------------------------------------------------------------------- +// Helper function for SampleShadowMapOptimizedPCF +//------------------------------------------------------------------------------------------------- +float SampleShadowMap(in float2 base_uv, in float u, in float v, in float2 shadowMapSizeInv, + in uint cascadeIdx, in float depth, in float2 receiverPlaneDepthBias) { + + float2 uv = base_uv + float2(u, v) * shadowMapSizeInv; + + #if UsePlaneDepthBias_ + float z = depth + dot(float2(u, v) * shadowMapSizeInv, receiverPlaneDepthBias); + #else + float z = depth; + #endif + + return ShadowMap.SampleCmpLevelZero(ShadowSamplerPCF, float3(uv, cascadeIdx), z); +} + +//------------------------------------------------------------------------------------------------- +// The method used in The Witness +//------------------------------------------------------------------------------------------------- +float SampleShadowMapOptimizedPCF(in float3 shadowPos, in float3 shadowPosDX, + in float3 shadowPosDY, in uint cascadeIdx) { + float2 shadowMapSize; + float numSlices; + ShadowMap.GetDimensions(shadowMapSize.x, shadowMapSize.y, numSlices); + + float lightDepth = shadowPos.z; + + const float bias = Bias; + + #if UsePlaneDepthBias_ + float2 texelSize = 1.0f / shadowMapSize; + + float2 receiverPlaneDepthBias = ComputeReceiverPlaneDepthBias(shadowPosDX, shadowPosDY); + + // Static depth biasing to make up for incorrect fractional sampling on the shadow map grid + float fractionalSamplingError = 2 * dot(float2(1.0f, 1.0f) * texelSize, abs(receiverPlaneDepthBias)); + lightDepth -= min(fractionalSamplingError, 0.01f); + #else + float2 receiverPlaneDepthBias; + lightDepth -= bias; + #endif + + float2 uv = shadowPos.xy * shadowMapSize; // 1 unit - 1 texel + + float2 shadowMapSizeInv = 1.0 / shadowMapSize; + + float2 base_uv; + base_uv.x = floor(uv.x + 0.5); + base_uv.y = floor(uv.y + 0.5); + + float s = (uv.x + 0.5 - base_uv.x); + float t = (uv.y + 0.5 - base_uv.y); + + base_uv -= float2(0.5, 0.5); + base_uv *= shadowMapSizeInv; + + float sum = 0; + + #if FilterSize_ == 2 + return ShadowMap.SampleCmpLevelZero(ShadowSamplerPCF, float3(shadowPos.xy, cascadeIdx), lightDepth); + #elif FilterSize_ == 3 + + float uw0 = (3 - 2 * s); + float uw1 = (1 + 2 * s); + + float u0 = (2 - s) / uw0 - 1; + float u1 = s / uw1 + 1; + + float vw0 = (3 - 2 * t); + float vw1 = (1 + 2 * t); + + float v0 = (2 - t) / vw0 - 1; + float v1 = t / vw1 + 1; + + sum += uw0 * vw0 * SampleShadowMap(base_uv, u0, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw0 * SampleShadowMap(base_uv, u1, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw0 * vw1 * SampleShadowMap(base_uv, u0, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw1 * SampleShadowMap(base_uv, u1, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + return sum * 1.0f / 16; + + #elif FilterSize_ == 5 + + float uw0 = (4 - 3 * s); + float uw1 = 7; + float uw2 = (1 + 3 * s); + + float u0 = (3 - 2 * s) / uw0 - 2; + float u1 = (3 + s) / uw1; + float u2 = s / uw2 + 2; + + float vw0 = (4 - 3 * t); + float vw1 = 7; + float vw2 = (1 + 3 * t); + + float v0 = (3 - 2 * t) / vw0 - 2; + float v1 = (3 + t) / vw1; + float v2 = t / vw2 + 2; + + sum += uw0 * vw0 * SampleShadowMap(base_uv, u0, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw0 * SampleShadowMap(base_uv, u1, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw0 * SampleShadowMap(base_uv, u2, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + sum += uw0 * vw1 * SampleShadowMap(base_uv, u0, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw1 * SampleShadowMap(base_uv, u1, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw1 * SampleShadowMap(base_uv, u2, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + sum += uw0 * vw2 * SampleShadowMap(base_uv, u0, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw2 * SampleShadowMap(base_uv, u1, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw2 * SampleShadowMap(base_uv, u2, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + return sum * 1.0f / 144; + + #else // FilterSize_ == 7 + + float uw0 = (5 * s - 6); + float uw1 = (11 * s - 28); + float uw2 = -(11 * s + 17); + float uw3 = -(5 * s + 1); + + float u0 = (4 * s - 5) / uw0 - 3; + float u1 = (4 * s - 16) / uw1 - 1; + float u2 = -(7 * s + 5) / uw2 + 1; + float u3 = -s / uw3 + 3; + + float vw0 = (5 * t - 6); + float vw1 = (11 * t - 28); + float vw2 = -(11 * t + 17); + float vw3 = -(5 * t + 1); + + float v0 = (4 * t - 5) / vw0 - 3; + float v1 = (4 * t - 16) / vw1 - 1; + float v2 = -(7 * t + 5) / vw2 + 1; + float v3 = -t / vw3 + 3; + + sum += uw0 * vw0 * SampleShadowMap(base_uv, u0, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw0 * SampleShadowMap(base_uv, u1, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw0 * SampleShadowMap(base_uv, u2, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw3 * vw0 * SampleShadowMap(base_uv, u3, v0, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + sum += uw0 * vw1 * SampleShadowMap(base_uv, u0, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw1 * SampleShadowMap(base_uv, u1, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw1 * SampleShadowMap(base_uv, u2, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw3 * vw1 * SampleShadowMap(base_uv, u3, v1, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + sum += uw0 * vw2 * SampleShadowMap(base_uv, u0, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw2 * SampleShadowMap(base_uv, u1, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw2 * SampleShadowMap(base_uv, u2, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw3 * vw2 * SampleShadowMap(base_uv, u3, v2, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + sum += uw0 * vw3 * SampleShadowMap(base_uv, u0, v3, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw1 * vw3 * SampleShadowMap(base_uv, u1, v3, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw2 * vw3 * SampleShadowMap(base_uv, u2, v3, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + sum += uw3 * vw3 * SampleShadowMap(base_uv, u3, v3, shadowMapSizeInv, cascadeIdx, lightDepth, receiverPlaneDepthBias); + + return sum * 1.0f / 2704; + + #endif +} + +//------------------------------------------------------------------------------------------------- +// Samples the appropriate shadow map cascade +//------------------------------------------------------------------------------------------------- +float3 SampleShadowCascade(in float3 shadowPosition, in float3 shadowPosDX, + in float3 shadowPosDY, in uint cascadeIdx, + in uint2 screenPos) +{ + shadowPosition += CascadeOffsets[cascadeIdx].xyz; + shadowPosition *= CascadeScales[cascadeIdx].xyz; + + shadowPosDX *= CascadeScales[cascadeIdx].xyz; + shadowPosDY *= CascadeScales[cascadeIdx].xyz; + + float3 cascadeColor = 1.0f; + + #if VisualizeCascades_ + const float3 CascadeColors[NumCascades] = + { + float3(1.0f, 0.0, 0.0f), + float3(0.0f, 1.0f, 0.0f), + float3(0.0f, 0.0f, 1.0f), + float3(1.0f, 1.0f, 0.0f) + }; + + cascadeColor = CascadeColors[cascadeIdx]; + #endif + + #if UseEVSM_ + float shadow = SampleShadowMapEVSM(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx); + #elif UseMSM_ + float shadow = SampleShadowMapMSM(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx); + #elif ShadowMode_ == ShadowModeVSM_ + float shadow = SampleShadowMapVSM(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx); + #elif ShadowMode_ == ShadowModeFixedSizePCF_ + float shadow = SampleShadowMapFixedSizePCF(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx); + #elif ShadowMode_ == ShadowModeGridPCF_ + float shadow = SampleShadowMapGridPCF(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx); + #elif ShadowMode_ == ShadowModeRandomDiscPCF_ + float shadow = SampleShadowMapRandomDiscPCF(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx, screenPos); + #else //if ShadowMode_ == SampleShadowMapOptimizedPCF_ + float shadow = SampleShadowMapOptimizedPCF(shadowPosition, shadowPosDX, shadowPosDY, cascadeIdx); + #endif + + return shadow * cascadeColor; +} + +//------------------------------------------------------------------------------------------------- +// Calculates the offset to use for sampling the shadow map, based on the surface normal +//------------------------------------------------------------------------------------------------- +float3 GetShadowPosOffset(in float nDotL, in float3 normal) +{ + float2 shadowMapSize; + float numSlices; + ShadowMap.GetDimensions(shadowMapSize.x, shadowMapSize.y, numSlices); + float texelSize = 2.0f / shadowMapSize.x; + float nmlOffsetScale = saturate(1.0f - nDotL); + return texelSize * OffsetScale * nmlOffsetScale * normal; +} + +//------------------------------------------------------------------------------------------------- +// Computes the visibility term by performing the shadow test +//------------------------------------------------------------------------------------------------- +float3 ShadowVisibility(in float3 positionWS, in float depthVS, in float nDotL, in float3 normal, + in uint2 screenPos) +{ + float3 shadowVisibility = 1.0f; + uint cascadeIdx = 0; + + // Figure out which cascade to sample from + [unroll] + for(uint i = 0; i < NumCascades - 1; ++i) + { + [flatten] + if(depthVS > CascadeSplits[i]) + cascadeIdx = i + 1; + } + + // Apply offset + float3 offset = GetShadowPosOffset(nDotL, normal) / abs(CascadeScales[cascadeIdx].z); + + // Project into shadow space + float3 samplePos = positionWS + offset; + float3 shadowPosition = mul(float4(samplePos, 1.0f), ShadowMatrix).xyz; + float3 shadowPosDX = ddx_fine(shadowPosition); + float3 shadowPosDY = ddy_fine(shadowPosition); + + shadowVisibility = SampleShadowCascade(shadowPosition, shadowPosDX, shadowPosDY, + cascadeIdx, screenPos); + + #if FilterAcrossCascades_ + // Sample the next cascade, and blend between the two results to + // smooth the transition + const float BlendThreshold = 0.1f; + float nextSplit = CascadeSplits[cascadeIdx]; + float splitSize = cascadeIdx == 0 ? nextSplit : nextSplit - CascadeSplits[cascadeIdx - 1]; + float splitDist = (nextSplit - depthVS) / splitSize; + + [branch] + if(splitDist <= BlendThreshold && cascadeIdx != NumCascades - 1) + { + float3 nextSplitVisibility = SampleShadowCascade(shadowPosition, shadowPosDX, + shadowPosDY, cascadeIdx + 1, + screenPos); + float lerpAmt = smoothstep(0.0f, BlendThreshold, splitDist); + shadowVisibility = lerp(nextSplitVisibility, shadowVisibility, lerpAmt); + } + #endif + + return shadowVisibility; +} + +//================================================================================================= +// Pixel Shader +//================================================================================================= +float4 PS(in PSInput input) : SV_Target0 +{ + // Normalize after interpolation + float3 normalWS = normalize(input.NormalWS); + + float3 diffuseAlbedo = 1.0f; + if(EnableAlbedoMap) + diffuseAlbedo = DiffuseMap.Sample(AnisoSampler, input.TexCoord).xyz; + + float nDotL = saturate(dot(normalWS, LightDirection)); + uint2 screenPos = uint2(input.PositionSS.xy); + float3 shadowVisibility = ShadowVisibility(input.PositionWS, input.DepthVS, nDotL, + normalWS, screenPos); + + float3 lighting = 0.0f; + + // Add in the primary directional light + lighting += nDotL * LightColor * diffuseAlbedo * (1.0f / 3.14159f) * shadowVisibility; + + lighting += float3(0.2f, 0.5f, 1.0f) * 0.1f * diffuseAlbedo; + + return float4(max(lighting, 0.0001f), 1.0f); +} \ No newline at end of file diff --git a/Shadows/MeshRenderer.cpp b/Shadows/MeshRenderer.cpp new file mode 100644 index 0000000..6d10361 --- /dev/null +++ b/Shadows/MeshRenderer.cpp @@ -0,0 +1,1600 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "MeshRenderer.h" +#include "AppSettings.h" +#include "SharedConstants.h" + +#include "SampleFramework11/Exceptions.h" +#include "SampleFramework11/Utility.h" +#include "SampleFramework11/ShaderCompilation.h" +#include "SampleFramework11/App.h" +#include "SampleFramework11/Profiler.h" +#include "SampleFramework11/Settings.h" + +// Constants +static const float ShadowNearClip = 1.0f; +static const bool UseComputeReduction = true; + +// Finds the approximate smallest enclosing bounding sphere for a set of points. Based on +// "An Efficient Bounding Sphere", by Jack Ritter. +static Sphere ComputeBoundingSphereFromPoints(const XMFLOAT3* points, uint32 numPoints, uint32 stride) +{ + Sphere sphere; + + Assert_(numPoints > 0); + Assert_(points); + + // Find the points with minimum and maximum x, y, and z + XMVECTOR MinX, MaxX, MinY, MaxY, MinZ, MaxZ; + + MinX = MaxX = MinY = MaxY = MinZ = MaxZ = XMLoadFloat3(points); + + for(uint32 i = 1; i < numPoints; i++) + { + XMVECTOR Point = XMLoadFloat3((XMFLOAT3*)((BYTE*)points + i * stride)); + + float px = XMVectorGetX(Point); + float py = XMVectorGetY(Point); + float pz = XMVectorGetZ(Point); + + if(px < XMVectorGetX(MinX)) + MinX = Point; + + if(px > XMVectorGetX(MaxX)) + MaxX = Point; + + if(py < XMVectorGetY(MinY)) + MinY = Point; + + if(py > XMVectorGetY(MaxY)) + MaxY = Point; + + if(pz < XMVectorGetZ(MinZ)) + MinZ = Point; + + if(pz > XMVectorGetZ(MaxZ)) + MaxZ = Point; + } + + // Use the min/max pair that are farthest apart to form the initial sphere. + XMVECTOR DeltaX = MaxX - MinX; + XMVECTOR DistX = XMVector3Length(DeltaX); + + XMVECTOR DeltaY = MaxY - MinY; + XMVECTOR DistY = XMVector3Length(DeltaY); + + XMVECTOR DeltaZ = MaxZ - MinZ; + XMVECTOR DistZ = XMVector3Length(DeltaZ); + + XMVECTOR Center; + XMVECTOR Radius; + + if(XMVector3Greater(DistX, DistY)) + { + if(XMVector3Greater(DistX, DistZ)) + { + // Use min/max x. + Center = (MaxX + MinX) * 0.5f; + Radius = DistX * 0.5f; + } + else + { + // Use min/max z. + Center = (MaxZ + MinZ) * 0.5f; + Radius = DistZ * 0.5f; + } + } + else // Y >= X + { + if(XMVector3Greater(DistY, DistZ)) + { + // Use min/max y. + Center = (MaxY + MinY) * 0.5f; + Radius = DistY * 0.5f; + } + else + { + // Use min/max z. + Center = (MaxZ + MinZ) * 0.5f; + Radius = DistZ * 0.5f; + } + } + + // Add any points not inside the sphere. + for(uint32 i = 0; i < numPoints; i++) + { + XMVECTOR Point = XMLoadFloat3((XMFLOAT3*)((BYTE*)points + i * stride)); + + XMVECTOR Delta = Point - Center; + + XMVECTOR Dist = XMVector3Length(Delta); + + if(XMVector3Greater(Dist, Radius)) + { + // Adjust sphere to include the new point. + Radius = (Radius + Dist) * 0.5f; + Center += (XMVectorReplicate(1.0f) - Radius * XMVectorReciprocal(Dist)) * Delta; + } + } + + XMStoreFloat3(&sphere.Center, Center); + XMStoreFloat(&sphere.Radius, Radius); + + return sphere; +} + +// Calculates the frustum planes given a view * projection matrix +static void ComputeFrustum(const XMMATRIX& viewProj, Frustum& frustum) +{ + XMVECTOR det; + XMMATRIX invViewProj = XMMatrixInverse(&det, viewProj); + + // Corners in homogeneous clip space + XMVECTOR corners[8] = + { // 7--------6 + XMVectorSet( 1.0f, -1.0f, 0.0f, 1.0f), // /| /| + XMVectorSet(-1.0f, -1.0f, 0.0f, 1.0f), // Y ^ / | / | + XMVectorSet( 1.0f, 1.0f, 0.0f, 1.0f), // | _ 3--------2 | + XMVectorSet(-1.0f, 1.0f, 0.0f, 1.0f), // | /' Z | | | | + XMVectorSet( 1.0f, -1.0f, 1.0f, 1.0f), // |/ | 5-----|--4 + XMVectorSet(-1.0f, -1.0f, 1.0f, 1.0f), // + ---> X | / | / + XMVectorSet( 1.0f, 1.0f, 1.0f, 1.0f), // |/ |/ + XMVectorSet(-1.0f, 1.0f, 1.0f, 1.0f), // 1--------0 + }; + + // Convert to world space + for(uint32 i = 0; i < 8; ++i) + corners[i] = XMVector3TransformCoord(corners[i], invViewProj); + + // Calculate the 6 planes + frustum.Planes[0] = XMPlaneFromPoints(corners[0], corners[4], corners[2]); + frustum.Planes[1] = XMPlaneFromPoints(corners[1], corners[3], corners[5]); + frustum.Planes[2] = XMPlaneFromPoints(corners[3], corners[2], corners[7]); + frustum.Planes[3] = XMPlaneFromPoints(corners[1], corners[5], corners[0]); + frustum.Planes[4] = XMPlaneFromPoints(corners[5], corners[7], corners[4]); + frustum.Planes[5] = XMPlaneFromPoints(corners[1], corners[0], corners[3]); +} + +// Tests a frustum for intersection with a sphere +static uint32 TestFrustumSphere(const Frustum& frustum, const Sphere& sphere, bool ignoreNearZ) +{ + XMVECTOR sphereCenter = XMLoadFloat3(&sphere.Center); + + uint32 result = 1; + uint32 numPlanes = ignoreNearZ ? 5 : 6; + for(uint32 i = 0; i < numPlanes; i++) { + float distance = XMVectorGetX(XMPlaneDotCoord(frustum.Planes[i], sphereCenter)); + + if (distance < -sphere.Radius) + return 0; + else if (distance < sphere.Radius) + result = 1; + } + + return result; +} + +// Calculates the bounding sphere for each MeshPart +static void ComputeBoundingSpheres(ID3D11Device* device, ID3D11DeviceContext* context, + const Float4x4& world, Model* model, + std::vector& boundingSpheres) +{ + boundingSpheres.clear(); + + for(uint32 meshIdx = 0; meshIdx < model->Meshes().size(); ++meshIdx) + { + Mesh& mesh = model->Meshes()[meshIdx]; + + // Create staging buffers for copying the vertex/index data to + ID3D11BufferPtr stagingVB; + ID3D11BufferPtr stagingIB; + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.BindFlags = 0; + bufferDesc.ByteWidth = mesh.NumVertices() * mesh.VertexStride(); + bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + bufferDesc.Usage = D3D11_USAGE_STAGING; + DXCall(device->CreateBuffer(&bufferDesc, nullptr, &stagingVB)); + + uint32 indexSize = mesh.IndexBufferType() == Mesh::Index16Bit ? 2 : 4; + bufferDesc.ByteWidth = mesh.NumIndices() * indexSize; + DXCall(device->CreateBuffer(&bufferDesc, nullptr, &stagingIB)); + + context->CopyResource(stagingVB, mesh.VertexBuffer()); + context->CopyResource(stagingIB, mesh.IndexBuffer()); + + D3D11_MAPPED_SUBRESOURCE mapped; + context->Map(stagingVB, 0, D3D11_MAP_READ, 0, &mapped); + const BYTE* verts = reinterpret_cast(mapped.pData); + uint32 stride = mesh.VertexStride(); + + context->Map(stagingIB, 0, D3D11_MAP_READ, 0, &mapped); + const BYTE* indices = reinterpret_cast(mapped.pData); + const uint32* indices32 = reinterpret_cast(mapped.pData); + const WORD* indices16 = reinterpret_cast(mapped.pData); + + for(uint32 partIdx = 0; partIdx < mesh.MeshParts().size(); ++partIdx) + { + const MeshPart& part = mesh.MeshParts()[partIdx]; + + std::vector points; + for(uint32 i = 0; i < part.IndexCount; ++i) + { + uint32 index = indexSize == 2 ? indices16[part.IndexStart + i] : indices32[part.IndexStart + i]; + Float3 point = *reinterpret_cast(verts + (index * stride)); + point = Float3::Transform(point, world); + points.push_back(point); + } + + Sphere sphere = ComputeBoundingSphereFromPoints(&points[0], static_cast(points.size()), sizeof(XMFLOAT3)); + boundingSpheres.push_back(sphere); + } + + context->Unmap(stagingVB, 0); + context->Unmap(stagingIB, 0); + } +} + +// Makes the "global" shadow matrix used as the reference point for the cascades +static Float4x4 MakeGlobalShadowMatrix(const Camera& camera) +{ + // Get the 8 points of the view frustum in world space + Float3 frustumCorners[8] = + { + Float3(-1.0f, 1.0f, 0.0f), + Float3( 1.0f, 1.0f, 0.0f), + Float3( 1.0f, -1.0f, 0.0f), + Float3(-1.0f, -1.0f, 0.0f), + Float3(-1.0f, 1.0f, 1.0f), + Float3( 1.0f, 1.0f, 1.0f), + Float3( 1.0f, -1.0f, 1.0f), + Float3(-1.0f, -1.0f, 1.0f), + }; + + Float4x4 invViewProj = Float4x4::Invert(camera.ViewProjectionMatrix()); + Float3 frustumCenter = 0.0f; + for(uint64 i = 0; i < 8; ++i) + { + frustumCorners[i] = Float3::Transform(frustumCorners[i], invViewProj); + frustumCenter += frustumCorners[i]; + } + + frustumCenter /= 8.0f; + + // Pick the up vector to use for the light camera + Float3 upDir = camera.Right(); + + // This needs to be constant for it to be stable + if(AppSettings::StabilizeCascades) + upDir = Float3(0.0f, 1.0f, 0.0f); + + // Create a temporary view matrix for the light + Float3 lightCameraPos = frustumCenter; + Float3 lookAt = frustumCenter - AppSettings::LightDirection; + Float4x4 lightView = XMMatrixLookAtLH(lightCameraPos.ToSIMD(), lookAt.ToSIMD(), upDir.ToSIMD()); + + // Get position of the shadow camera + Float3 shadowCameraPos = frustumCenter + AppSettings::LightDirection.Value() * -0.5f; + + // Come up with a new orthographic camera for the shadow caster + OrthographicCamera shadowCamera(-0.5f, -0.5f, 0.5f, + 0.5f, 0.0f, 1.0f); + shadowCamera.SetLookAt(shadowCameraPos, frustumCenter, upDir); + + Float4x4 texScaleBias = Float4x4::ScaleMatrix(Float3(0.5f, -0.5f, 1.0f)); + texScaleBias.SetTranslation(Float3(0.5f, 0.5f, 0.0f)); + return shadowCamera.ViewProjectionMatrix() * texScaleBias; +} + +MeshRenderer::MeshRenderer() : currFrame(0) +{ +} + +static PixelShaderPtr CompileMeshPS(ID3D11Device* device) +{ + CompileOptions opts; + opts.Add("VisualizeCascades_", AppSettings::VisualizeCascades); + opts.Add("UsePlaneDepthBias_", AppSettings::UsePlaneDepthBias); + opts.Add("FilterAcrossCascades_", AppSettings::FilterAcrossCascades); + opts.Add("FilterSize_", AppSettings::FixedFilterKernelSize()); + opts.Add("ShadowMode_", uint32(AppSettings::ShadowMode)); + opts.Add("RandomizeOffsets_", AppSettings::RandomizeDiscOffsets); + return CompilePSFromFile(device, L"Mesh.hlsl", "PS", "ps_5_0", opts); +} + +// Loads all shaders +void MeshRenderer::LoadShaders() +{ + // Load the mesh shaders + meshDepthVS = CompileVSFromFile(device, L"DepthOnly.hlsl", "VS", "vs_5_0"); + meshVS = CompileVSFromFile(device, L"Mesh.hlsl", "VS", "vs_5_0"); + meshPS = CompileMeshPS(device); + + fullScreenVS = CompileVSFromFile(device, L"VSMConvert.hlsl", "FullScreenVS"); + for(uint32 shadowMode = uint32(ShadowMode::VSM); shadowMode < uint32(ShadowMode::NumValues); ++shadowMode) + { + for(uint32 msaaMode = 0; msaaMode < uint32(ShadowMSAA::NumValues); ++msaaMode) + { + PixelShaderPtr& shader = vsmConvertPS[shadowMode - uint32(ShadowMode::VSM)][msaaMode]; + + CompileOptions opts; + opts.Add("ShadowMode_", shadowMode); + opts.Add("MSAASamples_", AppSettings::MSAASamples(msaaMode)); + shader = CompilePSFromFile(device, L"VSMConvert.hlsl", "ConvertToVSM", "ps_5_0", opts); + } + } + + for(uint32 i = 0; i <= MaxBlurRadius; ++i) + { + + CompileOptions opts; + opts.Add("Horizontal_", 1); + opts.Add("Vertical_", 0); + opts.Add("SampleRadius_", i); + opts.Add("GPUSceneSubmission_", 0); + vsmBlurH[i] = CompilePSFromFile(device, L"VSMConvert.hlsl", "BlurVSM", "ps_5_0", opts); + + + opts.Reset(); + opts.Add("Horizontal_", 0); + opts.Add("Vertical_", 1); + opts.Add("SampleRadius_", i); + opts.Add("GPUSceneSubmission_", 0); + vsmBlurV[i] = CompilePSFromFile(device, L"VSMConvert.hlsl", "BlurVSM", "ps_5_0", opts); + } + + + CompileOptions opts; + opts.Add("Horizontal_", 1); + opts.Add("Vertical_", 0); + opts.Add("GPUSceneSubmission_", 1); + vsmBlurGPUH = CompilePSFromFile(device, L"VSMConvert.hlsl", "BlurVSM", "ps_5_0", opts); + + + opts.Reset(); + opts.Add("Horizontal_", 0); + opts.Add("Vertical_", 1); + opts.Add("GPUSceneSubmission_", 1); + vsmBlurGPUV = CompilePSFromFile(device, L"VSMConvert.hlsl", "BlurVSM", "ps_5_0", opts); + + + depthReductionInitialPS = CompilePSFromFile(device, L"DepthReduction.hlsl", "DepthReductionInitialPS"); + depthReductionPS = CompilePSFromFile(device, L"DepthReduction.hlsl", "DepthReductionPS"); + + + opts.Reset(); + opts.Add("CS_", 1); + depthReductionInitialCS = CompileCSFromFile(device, L"DepthReduction.hlsl", "DepthReductionInitialCS", + "cs_5_0", opts); + + depthReductionCS = CompileCSFromFile(device, L"DepthReduction.hlsl", "DepthReductionCS", + "cs_5_0", opts); + + clearArgsBuffer = CompileCSFromFile(device, L"GPUBatch.hlsl", "ClearArgsBuffer"); + cullDrawCalls = CompileCSFromFile(device, L"GPUBatch.hlsl", "CullDrawCalls"); + batchDrawCalls = CompileCSFromFile(device, L"GPUBatch.hlsl", "BatchDrawCalls"); + + setupCascades = CompileCSFromFile(device, L"SetupShadows.hlsl", "SetupCascades"); +} + +// Creates shadow map render targets and depth targets +void MeshRenderer::CreateShadowMaps() +{ + // Create the shadow map as a texture atlas + const uint32 ShadowMapSize = AppSettings::ShadowMapResolution(); + + if(AppSettings::UseFilterableShadows()) + { + uint32 msaaSamples = AppSettings::MSAASamples(); + shadowMap.Initialize(device, ShadowMapSize, ShadowMapSize, DXGI_FORMAT_D32_FLOAT, true, msaaSamples, 0, 1); + + DXGI_FORMAT smFmt; + if(AppSettings::ShadowMode == ShadowMode::EVSM4) + { + if(AppSettings::SMFormat == SMFormat::SM16Bit) + smFmt = DXGI_FORMAT_R16G16B16A16_FLOAT; + else + smFmt = DXGI_FORMAT_R32G32B32A32_FLOAT; + } + else if(AppSettings::ShadowMode == ShadowMode::EVSM2) + { + if(AppSettings::SMFormat == SMFormat::SM16Bit) + smFmt = DXGI_FORMAT_R16G16_FLOAT; + else + smFmt = DXGI_FORMAT_R32G32_FLOAT; + } + else if(AppSettings::ShadowMode == ShadowMode::MSMHamburger || AppSettings::ShadowMode == ShadowMode::MSMHausdorff) + { + if(AppSettings::SMFormat == SMFormat::SM16Bit) + smFmt = DXGI_FORMAT_R16G16B16A16_UNORM; + else + smFmt = DXGI_FORMAT_R32G32B32A32_FLOAT; + } + else + { + if(AppSettings::SMFormat == SMFormat::SM16Bit) + smFmt = DXGI_FORMAT_R16G16_UNORM; + else + smFmt = DXGI_FORMAT_R32G32_FLOAT; + } + + uint32 numMips = AppSettings::EnableShadowMips ? 0 : 1; + varianceShadowMap.Initialize(device, ShadowMapSize, ShadowMapSize, smFmt, numMips, 1, 0, + AppSettings::EnableShadowMips, false, NumCascades, false); + + tempVSM.Initialize(device, ShadowMapSize, ShadowMapSize, smFmt, 1, 1, 0, false, false, 1, false); + } + else + { + shadowMap.Initialize(device, ShadowMapSize, ShadowMapSize, DXGI_FORMAT_D32_FLOAT, true, 1, 0, NumCascades); + varianceShadowMap = RenderTarget2D(); + } +} + +// Creates bounding spheres for a mesh, and creates resources used for GPU batching +static void SetupMesh(ID3D11Device* device, ID3D11DeviceContext* context, Model* model, + MeshData& meshData, const Float4x4& world, VertexShaderPtr meshVS, + VertexShaderPtr meshDepthVS) +{ + meshData.Model = model; + + ComputeBoundingSpheres(device, context, world, model, meshData.BoundingSpheres); + + std::vector positions; + std::vector indices; + std::vector drawCalls; + uint64 drawIdx = 0; + for(uint64 meshIdx = 0; meshIdx < model->Meshes().size(); ++meshIdx) + { + const Mesh& mesh = model->Meshes()[meshIdx]; + const uint8* verts = mesh.Vertices(); + + const uint32 vtxOffset = uint32(positions.size()); + for(uint64 i = 0; i < mesh.NumVertices(); ++i) + { + positions.push_back(*reinterpret_cast(verts)); + verts += mesh.VertexStride(); + } + + const uint32 idxOffset = uint32(indices.size()); + for(uint32 i = 0; i < mesh.NumIndices(); ++i) + { + uint32 idx = GetIndex(mesh.Indices(), i, mesh.IndexSize()); + idx += vtxOffset; + indices.push_back(idx); + } + + for(uint64 partIdx = 0; partIdx < mesh.MeshParts().size(); ++partIdx) + { + const MeshPart& part = mesh.MeshParts()[partIdx]; + DrawCall drawCall; + drawCall.StartIndex = part.IndexStart + idxOffset; + drawCall.NumIndices = part.IndexCount; + drawCall.SphereCenter = meshData.BoundingSpheres[drawIdx].Center; + drawCall.SphereRadius= meshData.BoundingSpheres[drawIdx].Radius; + drawCalls.push_back(drawCall); + + ++drawIdx; + } + } + + meshData.Indices.Initialize(device, sizeof(uint32), uint32(indices.size()), false, false, false, indices.data()); + meshData.CulledIndices.Initialize(device, DXGI_FORMAT_R32_UINT, 4, uint32(indices.size()), false, false, true); + meshData.DrawCalls.Initialize(device, sizeof(DrawCall), uint32(drawCalls.size()), false, false, false, drawCalls.data()); + meshData.CulledDraws.Initialize(device, sizeof(CulledDraw), uint32(drawCalls.size()), true, true, false, nullptr); + + D3D11_BUFFER_DESC vbDesc; + vbDesc.Usage = D3D11_USAGE_IMMUTABLE; + vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + vbDesc.ByteWidth = uint32(positions.size() * sizeof(Float3)); + vbDesc.CPUAccessFlags = 0; + vbDesc.MiscFlags = 0; + vbDesc.StructureByteStride = 0; + D3D11_SUBRESOURCE_DATA vbInitData = { positions.data(), 0, 0 }; + DXCall(device->CreateBuffer(&vbDesc, &vbInitData, &meshData.PositionsVB)); + + meshData.InputLayouts.clear(); + meshData.DepthInputLayouts.clear(); + + for(uint32 i = 0; i < model->Meshes().size(); ++i) + { + Mesh& mesh = model->Meshes()[i]; + ID3D11InputLayoutPtr inputLayout; + DXCall(device->CreateInputLayout(mesh.InputElements(), mesh.NumInputElements(), + meshVS->ByteCode->GetBufferPointer(), + meshVS->ByteCode->GetBufferSize(), &inputLayout)); + meshData.InputLayouts.push_back(inputLayout); + + DXCall(device->CreateInputLayout(mesh.InputElements(), mesh.NumInputElements(), + meshDepthVS->ByteCode->GetBufferPointer(), + meshDepthVS->ByteCode->GetBufferSize(), &inputLayout)); + meshData.DepthInputLayouts.push_back(inputLayout); + } +} + +void MeshRenderer::SetSceneMesh(ID3D11DeviceContext* context, Model* model, const Float4x4& world) +{ + SetupMesh(device, context, model, scene, world, meshVS, meshDepthVS); +} + +void MeshRenderer::SetCharacterMesh(ID3D11DeviceContext* context, Model* model, const Float4x4& world) +{ + SetupMesh(device, context, model, character, world, meshVS, meshDepthVS); +} + +// Loads resources +void MeshRenderer::Initialize(ID3D11Device* device, ID3D11DeviceContext* context) +{ + this->device = device; + + blendStates.Initialize(device); + rasterizerStates.Initialize(device); + depthStencilStates.Initialize(device); + samplerStates.Initialize(device); + + depthOnlyConstants.Initialize(device, true); + meshVSConstants.Initialize(device); + meshPSConstants.Initialize(device, true); + vsmConstants.Initialize(device, true); + reductionConstants.Initialize(device); + gpuBatchConstants.Initialize(device, true); + shadowSetupConstants.Initialize(device); + + tempViewProjBuffer.Initialize(device); + tempFrustumPlanesBuffer.Initialize(device); + + defaultTexture = LoadTexture(device, L"..\\Content\\Textures\\Default.dds"); + + LoadShaders(); + + D3D11_RASTERIZER_DESC rsDesc = RasterizerStates::NoCullDesc(); + rsDesc.DepthClipEnable = FALSE; + DXCall(device->CreateRasterizerState(&rsDesc, &shadowRSState)); + + for(uint32 anisotropy = 0; anisotropy < uint32(ShadowAnisotropy::NumValues); ++anisotropy) + { + D3D11_SAMPLER_DESC sampDesc = SamplerStates::AnisotropicDesc(); + sampDesc.MaxAnisotropy = AppSettings::NumAnisotropicSamples(anisotropy); + DXCall(device->CreateSamplerState(&sampDesc, &evsmSamplers[anisotropy])); + } + + // Initialize a 64x64 texture containing random rotation values + static const uint32 RandomTextureSize = 64; + BYTE randomValues[RandomTextureSize * RandomTextureSize]; + srand(0); + + for(uint32 i = 0; i < RandomTextureSize * RandomTextureSize; ++i) + randomValues[i] = static_cast(RandFloat() * 255.0f); + + D3D11_TEXTURE2D_DESC texDesc; + texDesc.Width = RandomTextureSize; + texDesc.Height = RandomTextureSize; + texDesc.Format = DXGI_FORMAT_R8_UNORM; + texDesc.ArraySize = 1; + texDesc.MipLevels = 1; + texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + texDesc.Usage = D3D11_USAGE_IMMUTABLE; + texDesc.MiscFlags = 0; + texDesc.SampleDesc.Count = 1; + texDesc.SampleDesc.Quality = 0; + texDesc.CPUAccessFlags = 0; + + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = randomValues; + initData.SysMemPitch = RandomTextureSize; + initData.SysMemSlicePitch = 0; + + ID3D11Texture2DPtr randomValuesTexture; + DXCall(device->CreateTexture2D(&texDesc, &initData, &randomValuesTexture)); + DXCall(device->CreateShaderResourceView(randomValuesTexture, nullptr, &randomRotations)); + + // Create the staging textures for reading back the reduced depth buffer + for(uint32 i = 0; i < MaxReadbackLatency; ++i) + reductionStagingTextures[i].Initialize(device, 1, 1, DXGI_FORMAT_R16G16_UNORM); + + // Create resources needed for GPU draw call batching + uint32 drawArgsInit[5] = { 0, 1, 0, 0, 0 }; + drawArgsBuffer.Initialize(device, DXGI_FORMAT_R32_TYPELESS, 4, 5, true, false, false, true, drawArgsInit); + + uint32 dispatchArgsInit[4] = { 1, 1, 1, 0 }; + batchDispatchArgs.Initialize(device, DXGI_FORMAT_R32_TYPELESS, 4, 4, true, false, false, true, dispatchArgsInit); + + D3D11_INPUT_ELEMENT_DESC inputElements[1]; + inputElements[0].AlignedByteOffset = 0; + inputElements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; + inputElements[0].InputSlot = 0; + inputElements[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + inputElements[0].InstanceDataStepRate = 0; + inputElements[0].SemanticName = "POSITION"; + inputElements[0].SemanticIndex = 0; + DXCall(device->CreateInputLayout(inputElements, 1, meshDepthVS->ByteCode->GetBufferPointer(), + meshDepthVS->ByteCode->GetBufferSize(), &depthGPUInputLayout)); + + // Create resources for GPU cascade setup + cascadeMatrixBuffer.Initialize(device, sizeof(Float4), NumCascades * 4, true); + cascadeSplitBuffer.Initialize(device, sizeof(float), NumCascades, true); + cascadeOffsetBuffer.Initialize(device, sizeof(Float4), NumCascades, true); + cascadeScaleBuffer.Initialize(device, sizeof(Float4), NumCascades, true); + cascadePlanesBuffer.Initialize(device, sizeof(Float4), NumCascades * 6, true); + + CreateShadowMaps(); +} + +// Performs frustum/sphere intersection tests for all MeshPart's +static void DoFrustumTests(const Camera& camera, bool ignoreNearZ, MeshData& mesh) +{ + mesh.FrustumTests.clear(); + mesh.NumSuccessfulTests = 0; + + Frustum frustum; + ComputeFrustum(camera.ViewProjectionMatrix().ToSIMD(), frustum); + + for(uint32 i = 0; i < mesh.BoundingSpheres.size(); ++i) + { + const Sphere& sphere = mesh.BoundingSpheres[i]; + uint32 test = TestFrustumSphere(frustum, sphere, ignoreNearZ); + mesh.FrustumTests.push_back(test); + mesh.NumSuccessfulTests += test; + } +} + +void MeshRenderer::Update() +{ + if(AppSettings::ShadowMapSize.Changed() || AppSettings::ShadowMode.Changed() + || AppSettings::ShadowMSAA.Changed() || AppSettings::SMFormat.Changed() + || AppSettings::EnableShadowMips.Changed()) + CreateShadowMaps(); + + if(AppSettings::VisualizeCascades.Changed() || AppSettings::UsePlaneDepthBias.Changed() + || AppSettings::FilterAcrossCascades.Changed() || AppSettings::FixedFilterSize.Changed() + || AppSettings::ShadowMode.Changed() || AppSettings::RandomizeDiscOffsets.Changed()) + meshPS = CompileMeshPS(device); + + if(AppSettings::AutoComputeDepthBounds && AppSettings::GPUSceneSubmission == false) + { + AppSettings::MinCascadeDistance.SetValue(reductionDepth.x); + AppSettings::MaxCascadeDistance.SetValue(reductionDepth.y); + } + else + currFrame = 0; +} + +// Creates the chain of render targets used for computing min/max depth +void MeshRenderer::CreateReductionTargets(uint32 width, uint32 height) +{ + depthReductionTargets.clear(); + + if(UseComputeReduction) + { + uint32 w = width; + uint32 h = height; + + while(w > 1 || h > 1) + { + w = DispatchSize(ReductionTGSize, w); + h = DispatchSize(ReductionTGSize, h); + + RenderTarget2D rt; + rt.Initialize(device, w, h, DXGI_FORMAT_R16G16_UNORM, 1, 1, 0, FALSE, TRUE); + depthReductionTargets.push_back(rt); + } + } + else + { + // Basically create a mip chain + uint32 w = width; + uint32 h = height; + + while(w > 1 || h > 1) + { + w = static_cast(Round(w / 2.0f)); + h = static_cast(Round(h / 2.0f)); + + w = std::max(w, 1); + h = std::max(h, 1); + + RenderTarget2D rt; + rt.Initialize(device, w, h, DXGI_FORMAT_R16G16_UNORM); + depthReductionTargets.push_back(rt); + } + } +} + +// Computes the min and max depth from the depth buffer using a parallel reduction +void MeshRenderer::ReduceDepth(ID3D11DeviceContext* context, ID3D11ShaderResourceView* depthTarget, + const Camera& camera) +{ + PIXEvent event(L"Depth Reduction"); + ProfileBlock block(L"Depth Reduction"); + + reductionConstants.Data.Projection = Float4x4::Transpose(camera.ProjectionMatrix()); + reductionConstants.Data.NearClip = camera.NearClip(); + reductionConstants.Data.FarClip = camera.FarClip(); + reductionConstants.ApplyChanges(context); + + if(UseComputeReduction) + { + reductionConstants.SetCS(context, 0); + + ID3D11RenderTargetView* rtvs[1] = { nullptr }; + context->OMSetRenderTargets(1, rtvs, nullptr); + + ID3D11UnorderedAccessView* uavs[1] = { depthReductionTargets[0].UAView }; + context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); + + ID3D11ShaderResourceView* srvs[1] = { depthTarget }; + context->CSSetShaderResources(0, 1, srvs); + + ID3D11SamplerState* samplers[1] = { samplerStates.LinearClamp() }; + context->CSSetSamplers(0, 1, samplers); + + context->CSSetShader(depthReductionInitialCS, nullptr, 0); + + uint32 dispatchX = depthReductionTargets[0].Width; + uint32 dispatchY = depthReductionTargets[0].Height; + context->Dispatch(dispatchX, dispatchY, 1); + + uavs[0] = nullptr; + context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); + + srvs[0] = nullptr; + context->CSSetShaderResources(0, 1, srvs); + + context->CSSetShader(depthReductionCS, nullptr, 0); + + for(uint32 i = 1; i < depthReductionTargets.size(); ++i) + { + uavs[0] = depthReductionTargets[i].UAView; + context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); + + srvs[0] = depthReductionTargets[i - 1].SRView; + context->CSSetShaderResources(0, 1, srvs); + + dispatchX = depthReductionTargets[i].Width; + dispatchY = depthReductionTargets[i].Height; + context->Dispatch(dispatchX, dispatchY, 1); + + uavs[0] = nullptr; + context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); + + srvs[0] = nullptr; + context->CSSetShaderResources(0, 1, srvs); + } + } + else + { + float blendFactor[4] = {1, 1, 1, 1}; + context->OMSetBlendState(blendStates.BlendDisabled(), blendFactor, 0xFFFFFFFF); + context->RSSetState(rasterizerStates.NoCull()); + context->OMSetDepthStencilState(depthStencilStates.DepthDisabled(), 0); + ID3D11Buffer* vbs[1] = { nullptr }; + uint32 strides[1] = { 0 }; + uint32 offsets[1] = { 0 }; + context->IASetVertexBuffers(0, 1, vbs, strides, offsets); + context->IASetIndexBuffer(nullptr, DXGI_FORMAT_R32_UINT, 0); + context->IASetInputLayout(nullptr); + + reductionConstants.SetPS(context, 0); + + context->VSSetShader(fullScreenVS, nullptr, 0); + + context->PSSetShader(depthReductionInitialPS, nullptr, 0); + + ID3D11RenderTargetView* rtvs[1] = { depthReductionTargets[0].RTView }; + context->OMSetRenderTargets(1, rtvs, nullptr); + + ID3D11ShaderResourceView* srvs[1] = { depthTarget }; + context->PSSetShaderResources(0, 1, srvs); + + ID3D11SamplerState* samplers[1] = { samplerStates.LinearClamp() }; + context->PSSetSamplers(0, 1, samplers); + + D3D11_VIEWPORT vp; + vp.TopLeftX = 0.0f; + vp.TopLeftY = 0.0f; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.Width = static_cast(depthReductionTargets[0].Width); + vp.Height = static_cast(depthReductionTargets[0].Height); + context->RSSetViewports(1, &vp); + + context->Draw(3, 0); + + srvs[0] = nullptr; + context->PSSetShaderResources(0, 1, srvs); + + context->PSSetShader(depthReductionPS, nullptr, 0); + + for(uint32 i = 1; i < depthReductionTargets.size(); ++i) + { + rtvs[0] = depthReductionTargets[i].RTView; + context->OMSetRenderTargets(1, rtvs, nullptr); + + srvs[0] = depthReductionTargets[i - 1].SRView; + context->PSSetShaderResources(0, 1, srvs); + + vp.Width = static_cast(depthReductionTargets[i].Width); + vp.Height = static_cast(depthReductionTargets[i].Height); + context->RSSetViewports(1, &vp); + + context->Draw(3, 0); + + srvs[0] = nullptr; + context->PSSetShaderResources(0, 1, srvs); + } + } + + // Don't read back the depth when doing GPU batching, because that would default the purpose! + if(AppSettings::GPUSceneSubmission) + return; + + // Copy to a staging texture + const uint32 latency = uint32(AppSettings::ReadbackLatency + 1); + ID3D11Texture2D* lastTarget = depthReductionTargets[depthReductionTargets.size() - 1].Texture; + context->CopyResource(reductionStagingTextures[currFrame % latency].Texture, lastTarget); + + ++currFrame; + + if(currFrame >= latency) + { + CPUProfileBlock cpuBlock(L"Depth Readback"); + + StagingTexture2D& stagingTexture = reductionStagingTextures[currFrame % latency]; + + uint32 pitch; + const uint16* texData = reinterpret_cast(stagingTexture.Map(context, 0, pitch)); + reductionDepth.x = texData[0] / static_cast(0xffff); + reductionDepth.y = texData[1] / static_cast(0xffff); + + stagingTexture.Unmap(context, 0); + } + else + { + reductionDepth = Float2(0.0f, 1.0f); + } +} + +// Convert to a VSM map +void MeshRenderer::ConvertToVSM(ID3D11DeviceContext* context, uint32 cascadeIdx, + Float3 cascadeScale, Float3 cascade0Scale) +{ + PIXEvent event(L"VSM Conversion"); + + float blendFactor[4] = {1, 1, 1, 1}; + context->OMSetBlendState(blendStates.BlendDisabled(), blendFactor, 0xFFFFFFFF); + context->RSSetState(rasterizerStates.NoCull()); + context->OMSetDepthStencilState(depthStencilStates.DepthDisabled(), 0); + ID3D11Buffer* vbs[1] = { nullptr }; + uint32 strides[1] = { 0 }; + uint32 offsets[1] = { 0 }; + context->IASetVertexBuffers(0, 1, vbs, strides, offsets); + context->IASetIndexBuffer(nullptr, DXGI_FORMAT_R32_UINT, 0); + context->IASetInputLayout(nullptr); + + D3D11_VIEWPORT vp; + vp.TopLeftX = 0.0f; + vp.TopLeftY = 0.0f; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.Width = static_cast(varianceShadowMap.Width); + vp.Height = static_cast(varianceShadowMap.Height); + context->RSSetViewports(1, &vp); + + vsmConstants.Data.CascadeScale = Float4(cascadeScale, 1.0f); + vsmConstants.Data.Cascade0Scale = Float4(cascade0Scale, 1.0f); + vsmConstants.Data.ShadowMapSize = Float2(float(varianceShadowMap.Width), float(varianceShadowMap.Height)); + vsmConstants.ApplyChanges(context); + vsmConstants.SetPS(context, 0); + + if(AppSettings::GPUSceneSubmission) + { + // Copy the cascade scale from the GPU buffer + CopyBufferRegion(context, vsmConstants.Buffer, cascadeScaleBuffer.Buffer, 0, + sizeof(Float4) * cascadeIdx, sizeof(vsmConstants.Data.CascadeScale)); + + CopyBufferRegion(context, vsmConstants.Buffer, cascadeScaleBuffer.Buffer, sizeof(Float4), + sizeof(Float4) * 0, sizeof(vsmConstants.Data.CascadeScale)); + } + + context->VSSetShader(fullScreenVS, nullptr, 0); + + ID3D11PixelShader* ps = vsmConvertPS[AppSettings::ShadowMode - uint32(ShadowMode::VSM)][AppSettings::ShadowMSAA]; + context->PSSetShader(ps, nullptr, 0); + + ID3D11RenderTargetView* rtvs[1] = { varianceShadowMap.RTVArraySlices[cascadeIdx] }; + context->OMSetRenderTargets(1, rtvs, nullptr); + + ID3D11ShaderResourceView* srvs[1] = { shadowMap.SRView }; + context->PSSetShaderResources(0, 1, srvs); + + context->Draw(3, 0); + + srvs[0] = nullptr; + context->PSSetShaderResources(0, 1, srvs); + + float maxFilterSizeU = MaxKernelSize / std::abs(cascade0Scale.x); + float maxFilterSizeV = MaxKernelSize / std::abs(cascade0Scale.y); + const float FilterSizeU = Clamp(std::min(AppSettings::FilterSize, maxFilterSizeU) * std::abs(cascadeScale.x), 1.0f, MaxKernelSize); + const float FilterSizeV = Clamp(std::min(AppSettings::FilterSize, maxFilterSizeV) * std::abs(cascadeScale.y), 1.0f, MaxKernelSize); + + // For GPU-driven submission, we always run the blur passes and use a dynamic loop + // in the shader. For CPU submission, we figure out the minimum sample radius and + // switch shader permutations. This could also be done with GPU submission, + // by using DrawIndirect or something similar. + if((FilterSizeU > 1.0f || FilterSizeV > 1.0f) || AppSettings::GPUSceneSubmission) + { + // Horizontal pass + uint32 sampleRadiusU = static_cast((FilterSizeU / 2) + 0.499f); + + rtvs[0] = tempVSM.RTView; + context->OMSetRenderTargets(1, rtvs, nullptr); + + srvs[0] = varianceShadowMap.SRVArraySlices[cascadeIdx]; + context->PSSetShaderResources(0, 1, srvs); + + if(AppSettings::GPUSceneSubmission) + context->PSSetShader(vsmBlurGPUH, nullptr, 0); + else + context->PSSetShader(vsmBlurH[sampleRadiusU], nullptr, 0); + + context->Draw(3, 0); + + srvs[0] = nullptr; + context->PSSetShaderResources(0, 1, srvs); + + // Vertical pass + uint32 sampleRadiusV = static_cast((FilterSizeV / 2) + 0.499f); + + rtvs[0] = varianceShadowMap.RTVArraySlices[cascadeIdx]; + context->OMSetRenderTargets(1, rtvs, nullptr); + + srvs[0] = tempVSM.SRView; + context->PSSetShaderResources(0, 1, srvs); + + if(AppSettings::GPUSceneSubmission) + context->PSSetShader(vsmBlurGPUV, nullptr, 0); + else + context->PSSetShader(vsmBlurV[sampleRadiusV], nullptr, 0); + + context->Draw(3, 0); + + srvs[0] = nullptr; + context->PSSetShaderResources(0, 1, srvs); + } + + if(AppSettings::EnableShadowMips && cascadeIdx == NumCascades - 1) + context->GenerateMips(varianceShadowMap.SRView); +} + +// Renders the main pass for all meshes in both scenes (assumes shadow maps are already rendered) +void MeshRenderer::Render(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + const Float4x4& characterWorld) +{ + PIXEvent event(L"Mesh Rendering"); + ProfileBlock block(L"Mesh Rendering"); + + DoFrustumTests(camera, false, scene); + DoFrustumTests(camera, false, character); + + // Set states + float blendFactor[4] = {1, 1, 1, 1}; + context->OMSetBlendState(blendStates.BlendDisabled(), blendFactor, 0xFFFFFFFF); + context->OMSetDepthStencilState(depthStencilStates.DepthEnabled(), 0); + context->RSSetState(rasterizerStates.BackFaceCull()); + + ID3D11SamplerState* sampStates[4] = { + samplerStates.Anisotropic(), + samplerStates.ShadowMap(), + samplerStates.ShadowMapPCF(), + evsmSamplers[AppSettings::ShadowAnisotropy], + }; + + context->PSSetSamplers(0, 4, sampStates); + + // Set shaders + context->DSSetShader(nullptr, nullptr, 0); + context->HSSetShader(nullptr, nullptr, 0); + context->GSSetShader(nullptr, nullptr, 0); + context->VSSetShader(meshVS , nullptr, 0); + + /*uint32 filterSize = (AppSettings::ShadowMode == ShadowMode::FixedSizePCF || AppSettings::ShadowMode == ShadowMode::OptimizedPCF) ? AppSettings::FixedFilterSize : 0; + uint32 randomizeOffsets = AppSettings::ShadowMode == ShadowMode::RandomDiscPCF ? AppSettings::RandomizeDiscOffsets : 0; + uint32 usePlaneBias = AppSettings::UseFilterableShadows() ? 0 : AppSettings::UsePlaneDepthBias; + ID3D11PixelShader* ps = meshPS[AppSettings::VisualizeCascades][usePlaneBias][AppSettings::FilterAcrossCascades] + [randomizeOffsets][filterSize][AppSettings::ShadowMode];*/ + context->PSSetShader(meshPS, nullptr, 0); + + { + PIXEvent event_(L"Static Mesh Rendering"); + + RenderModel(context, camera, world, scene); + } + + { + PIXEvent event_(L"Character Mesh Rendering"); + + RenderModel(context, camera, characterWorld, character); + } + + ID3D11ShaderResourceView* nullSRVs[3] = { nullptr }; + context->PSSetShaderResources(0, 3, nullSRVs); +} + +// Renders one of the models, either the scene or the character +void MeshRenderer::RenderModel(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + MeshData& meshData) +{ + // Set constant buffers + meshVSConstants.Data.World = Float4x4::Transpose(world); + meshVSConstants.Data.ViewProjection = Float4x4::Transpose(camera.ViewProjectionMatrix()); + meshVSConstants.ApplyChanges(context); + meshVSConstants.SetVS(context, 0); + + meshPSConstants.Data.CameraPosWS = camera.Position(); + meshPSConstants.ApplyChanges(context); + meshPSConstants.SetPS(context, 0); + + if(AppSettings::GPUSceneSubmission) + { + // Copy the computed cascade info to the constant buffer + D3D11_BOX srcBox; + srcBox.left = 0; + srcBox.right = sizeof(float) * NumCascades; + srcBox.top = 0; + srcBox.bottom = 1; + srcBox.front = 0; + srcBox.back = 1; + uint32 dstOffset = offsetof(MeshPSConstants, CascadeSplits); + context->CopySubresourceRegion(meshPSConstants.Buffer, 0, dstOffset, 0, 0, + cascadeSplitBuffer.Buffer, 0, &srcBox); + + srcBox.right = sizeof(Float4) * NumCascades; + dstOffset = offsetof(MeshPSConstants, CascadeOffsets); + context->CopySubresourceRegion(meshPSConstants.Buffer, 0, dstOffset, 0, 0, + cascadeOffsetBuffer.Buffer, 0, &srcBox); + + srcBox.right = sizeof(Float4) * NumCascades; + dstOffset = offsetof(MeshPSConstants, CascadeScales); + context->CopySubresourceRegion(meshPSConstants.Buffer, 0, dstOffset, 0, 0, + cascadeScaleBuffer.Buffer, 0, &srcBox); + } + + // Draw all meshes + Model* model = meshData.Model; + uint32 partCount = 0; + for(uint32 meshIdx = 0; meshIdx < model->Meshes().size(); ++meshIdx) + { + Mesh& mesh = model->Meshes()[meshIdx]; + + // Set the vertices and indices + ID3D11Buffer* vertexBuffers[1] = { mesh.VertexBuffer() }; + uint32 vertexStrides[1] = { mesh.VertexStride() }; + uint32 offsets[1] = { 0 }; + context->IASetVertexBuffers(0, 1, vertexBuffers, vertexStrides, offsets); + context->IASetIndexBuffer(mesh.IndexBuffer(), mesh.IndexBufferFormat(), 0); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Set the input layout + context->IASetInputLayout(meshData.InputLayouts[meshIdx]); + + // Draw all parts + for(uint64 partIdx = 0; partIdx < mesh.MeshParts().size(); ++partIdx) + { + if(meshData.FrustumTests[partCount++]) + { + const MeshPart& part = mesh.MeshParts()[partIdx]; + const MeshMaterial& material = model->Materials()[part.MaterialIdx]; + + // Set the textures + ID3D11ShaderResourceView* psTextures[3] = + { + material.DiffuseMap, + shadowMap.SRView, + randomRotations, + }; + + if(psTextures[0] == nullptr) + psTextures[0] = defaultTexture; + + if(AppSettings::UseFilterableShadows()) + psTextures[1] = varianceShadowMap.SRView; + + context->PSSetShaderResources(0, 3, psTextures); + context->DrawIndexed(part.IndexCount, part.IndexStart, 0); + } + } + } +} + +// Renders all meshes using depth-only rendering, using CPU-driven submission +void MeshRenderer::RenderDepthCPU(ID3D11DeviceContext* context, const Camera& camera, + const Float4x4& world, const Float4x4& characterWorld, + bool shadowRendering) +{ + PIXEvent event(L"Mesh Depth Rendering"); + + DoFrustumTests(camera, shadowRendering, scene); + DoFrustumTests(camera, shadowRendering, character); + + SetupRenderDepthState(context, shadowRendering); + + { + PIXEvent event_(L"Static Mesh Rendering"); + RenderModelDepthCPU(context, camera, world, scene); + } + + { + PIXEvent event_(L"Character Mesh Rendering"); + RenderModelDepthCPU(context, camera, characterWorld, character); + } +} + +// Renders all meshes using depth-only rendering, using GPU-driven submission +void MeshRenderer::RenderDepthGPU(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + const Float4x4& characterWorld, bool shadowRendering) +{ + tempViewProjBuffer.Data = Float4x4::Transpose(camera.ViewProjectionMatrix()); + tempViewProjBuffer.ApplyChanges(context); + + Frustum frustum; + ComputeFrustum(camera.ViewProjectionMatrix().ToSIMD(), frustum); + for(uint64 i = 0; i < 6; ++i) + tempFrustumPlanesBuffer.Data.FrustumPlanes[i] = frustum.Planes[i]; + tempFrustumPlanesBuffer.ApplyChanges(context); + + RenderDepthGPU(context, world, characterWorld, shadowRendering, tempViewProjBuffer.Buffer, 0, + tempFrustumPlanesBuffer.Buffer, 0); +} + +// Renders all meshes using depth-only rendering, using GPU-driven submission +void MeshRenderer::RenderDepthGPU(ID3D11DeviceContext* context, const Float4x4& world, + const Float4x4& characterWorld, bool shadowRendering, + ID3D11Buffer* viewProj, uint32 viewProjOffset, + ID3D11Buffer* frustumPlanes, uint32 planesOffset) +{ + PIXEvent event(L"Mesh Depth Rendering"); + + SetupRenderDepthState(context, shadowRendering); + + { + PIXEvent event_(L"Static Mesh Rendering"); + RenderModelDepthGPU(context, scene, shadowRendering, world, viewProj, viewProjOffset, + frustumPlanes, planesOffset); + } + + { + PIXEvent event_(L"Character Mesh Rendering"); + RenderModelDepthGPU(context, character, shadowRendering, characterWorld, viewProj, viewProjOffset, + frustumPlanes, planesOffset); + } +} + +// Sets render states used for depth-only rendering +void MeshRenderer::SetupRenderDepthState(ID3D11DeviceContext* context, bool shadowRendering) +{ + // Set states + float blendFactor[4] = {1, 1, 1, 1}; + context->OMSetBlendState(blendStates.ColorWriteDisabled(), blendFactor, 0xFFFFFFFF); + context->OMSetDepthStencilState(depthStencilStates.DepthWriteEnabled(), 0); + + if(shadowRendering) + context->RSSetState(shadowRSState); + else + context->RSSetState(rasterizerStates.BackFaceCull()); + + // Set shaders + context->VSSetShader(meshDepthVS , nullptr, 0); + context->PSSetShader(nullptr, nullptr, 0); + context->GSSetShader(nullptr, nullptr, 0); + context->DSSetShader(nullptr, nullptr, 0); + context->HSSetShader(nullptr, nullptr, 0); +} + +// Renders depth-only for a model using CPU-driven submission +void MeshRenderer::RenderModelDepthCPU(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + MeshData& meshData) +{ + // Set constant buffers + depthOnlyConstants.Data.World = Float4x4::Transpose(world); + depthOnlyConstants.Data.ViewProjection = Float4x4::Transpose(camera.ViewProjectionMatrix()); + depthOnlyConstants.ApplyChanges(context); + depthOnlyConstants.SetVS(context, 0); + + Model* model = meshData.Model; + + uint32 partCount = 0; + for (uint64 meshIdx = 0; meshIdx < model->Meshes().size(); ++meshIdx) + { + Mesh& mesh = model->Meshes()[meshIdx]; + + // Set the vertices and indices + ID3D11Buffer* vertexBuffers[1] = { mesh.VertexBuffer() }; + uint32 vertexStrides[1] = { mesh.VertexStride() }; + uint32 offsets[1] = { 0 }; + context->IASetVertexBuffers(0, 1, vertexBuffers, vertexStrides, offsets); + context->IASetIndexBuffer(mesh.IndexBuffer(), mesh.IndexBufferFormat(), 0); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Set the input layout + context->IASetInputLayout(meshData.DepthInputLayouts[meshIdx]); + + // Draw all parts + for (uint64 partIdx = 0; partIdx < mesh.MeshParts().size(); ++partIdx) + { + if(meshData.FrustumTests[partCount++]) + { + const MeshPart& part = mesh.MeshParts()[partIdx]; + context->DrawIndexed(part.IndexCount, part.IndexStart, 0); + } + } + } +} + +// Renders depth-only for a model using GPU-driven submission +void MeshRenderer::RenderModelDepthGPU(ID3D11DeviceContext* context, MeshData& meshData, bool shadowRendering, + const Float4x4& world, ID3D11Buffer* viewProj, uint32 viewProjOffset, + ID3D11Buffer* frustumPlanes, uint32 planeOffset) +{ + // Prepare the constant buffer + gpuBatchConstants.Data.NumDrawCalls = meshData.DrawCalls.NumElements; + gpuBatchConstants.Data.CullNearZ = shadowRendering == false; + gpuBatchConstants.ApplyChanges(context); + gpuBatchConstants.SetCS(context, 0); + + CopyBufferRegion(context, gpuBatchConstants.Buffer, frustumPlanes, 0, planeOffset, sizeof(Float4) * 6); + + // Clear the IndirectDraw args buffer + SetCSShader(context, clearArgsBuffer); + SetCSOutputs(context, drawArgsBuffer.UAView); + context->Dispatch(1, 1, 1); + ClearCSOutputs(context); + + // Cull the draw calls + SetCSShader(context, cullDrawCalls); + SetCSInputs(context, meshData.DrawCalls.SRView); + ID3D11UnorderedAccessView* uavs[2] = { drawArgsBuffer.UAView, meshData.CulledDraws.UAView }; + uint32 counts[2] = { 0, 0 }; + context->CSSetUnorderedAccessViews(0, 2, uavs, counts); + context->Dispatch(DispatchSize(CullTGSize, meshData.DrawCalls.NumElements), 1, 1); + ClearCSOutputs(context); + ClearCSInputs(context); + + // Copy the number of culled draws to a DispatchIndirect args buffer. + // We'll do 1 thread group per draw call + context->CopyStructureCount(batchDispatchArgs.Buffer, 0, meshData.CulledDraws.UAView); + + // Copy culled indices to the batched index buffer + SetCSShader(context, batchDrawCalls); + SetCSInputs(context, meshData.CulledDraws.SRView, meshData.Indices.SRView); + SetCSOutputs(context, meshData.CulledIndices.UAView); + context->DispatchIndirect(batchDispatchArgs.Buffer, 0); + ClearCSOutputs(context); + ClearCSInputs(context); + + // Setup the constant buffer for mesh rendering + depthOnlyConstants.Data.World = Float4x4::Transpose(world); + depthOnlyConstants.ApplyChanges(context); + depthOnlyConstants.SetVS(context, 0); + + CopyBufferRegion(context, depthOnlyConstants.Buffer, viewProj, sizeof(Float4x4), viewProjOffset, sizeof(Float4x4)); + + // Set the vertices and indices + ID3D11Buffer* vertexBuffers[1] = { meshData.PositionsVB }; + uint32 vertexStrides[1] = { sizeof(Float3) }; + uint32 offsets[1] = { 0 }; + context->IASetVertexBuffers(0, 1, vertexBuffers, vertexStrides, offsets); + context->IASetIndexBuffer(meshData.CulledIndices.Buffer, DXGI_FORMAT_R32_UINT, 0); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Set the input layout + context->IASetInputLayout(depthGPUInputLayout); + + // Draw the batch + context->DrawIndexedInstancedIndirect(drawArgsBuffer.Buffer, 0); +} + +// Renders the shadow map for all cascades, and performs VSM conversion if necessary. +// This uses CPU-driven shadow map setup and scene submission +void MeshRenderer::RenderShadowMap(ID3D11DeviceContext* context, const Camera& camera, + const Float4x4& world, const Float4x4& characterWorld) +{ + PIXEvent event(L"Mesh Shadow Map Rendering"); + ProfileBlock block(L"Shadow Map Rendering"); + + const uint32 ShadowMapSize = AppSettings::ShadowMapResolution(); + const float sMapSize = static_cast(ShadowMapSize); + + const float MinDistance = AppSettings::AutoComputeDepthBounds ? reductionDepth.x + : AppSettings::MinCascadeDistance; + const float MaxDistance = AppSettings::AutoComputeDepthBounds ? reductionDepth.y + : AppSettings::MaxCascadeDistance; + + // Compute the split distances based on the partitioning mode + float CascadeSplits[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + + if(AppSettings::PartitionMode == PartitionMode::Manual) + { + CascadeSplits[0] = MinDistance + AppSettings::SplitDistance0 * MaxDistance; + CascadeSplits[1] = MinDistance + AppSettings::SplitDistance1 * MaxDistance; + CascadeSplits[2] = MinDistance + AppSettings::SplitDistance2 * MaxDistance; + CascadeSplits[3] = MinDistance + AppSettings::SplitDistance3 * MaxDistance; + } + else if(AppSettings::PartitionMode == PartitionMode::Logarithmic + || AppSettings::PartitionMode == PartitionMode::PSSM) + { + float lambda = 1.0f; + if(AppSettings::PartitionMode == PartitionMode::PSSM) + lambda = AppSettings::PSSMLambda; + + float nearClip = camera.NearClip(); + float farClip = camera.FarClip(); + float clipRange = farClip - nearClip; + + float minZ = nearClip + MinDistance * clipRange; + float maxZ = nearClip + MaxDistance * clipRange; + + float range = maxZ - minZ; + float ratio = maxZ / minZ; + + for(uint32 i = 0; i < NumCascades; ++i) + { + float p = (i + 1) / static_cast(NumCascades); + float log = minZ * std::pow(ratio, p); + float uniform = minZ + range * p; + float d = lambda * (log - uniform) + uniform; + CascadeSplits[i] = (d - nearClip) / clipRange; + } + } + + Float4x4 globalShadowMatrix = MakeGlobalShadowMatrix(camera); + meshPSConstants.Data.ShadowMatrix = Float4x4::Transpose(globalShadowMatrix); + + // Render the meshes to each cascade + for(uint32 cascadeIdx = 0; cascadeIdx < NumCascades; ++cascadeIdx) + { + PIXEvent cascadeEvent((L"Rendering Shadow Map Cascade " + ToString(cascadeIdx)).c_str()); + + // Set the viewport + D3D11_VIEWPORT viewport; + viewport.TopLeftX = 0.0f; + viewport.TopLeftY = 0.0f; + viewport.Width = sMapSize; + viewport.Height = sMapSize; + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; + context->RSSetViewports(1, &viewport); + + // Set the shadow map as the depth target + ID3D11DepthStencilView* dsv = shadowMap.DSView; + if(AppSettings::UseFilterableShadows() == false) + dsv = shadowMap.ArraySlices[cascadeIdx]; + ID3D11RenderTargetView* nullRenderTargets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT] = { nullptr }; + context->OMSetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, nullRenderTargets, dsv); + context->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0); + + // Get the 8 points of the view frustum in world space + Float3 frustumCornersWS[8] = + { + Float3(-1.0f, 1.0f, 0.0f), + Float3( 1.0f, 1.0f, 0.0f), + Float3( 1.0f, -1.0f, 0.0f), + Float3(-1.0f, -1.0f, 0.0f), + Float3(-1.0f, 1.0f, 1.0f), + Float3( 1.0f, 1.0f, 1.0f), + Float3( 1.0f, -1.0f, 1.0f), + Float3(-1.0f, -1.0f, 1.0f), + }; + + float prevSplitDist = cascadeIdx == 0 ? MinDistance : CascadeSplits[cascadeIdx - 1]; + float splitDist = CascadeSplits[cascadeIdx]; + + Float4x4 invViewProj = Float4x4::Invert(camera.ViewProjectionMatrix()); + for(uint32 i = 0; i < 8; ++i) + frustumCornersWS[i] = Float3::Transform(frustumCornersWS[i], invViewProj); + + // Get the corners of the current cascade slice of the view frustum + for(uint32 i = 0; i < 4; ++i) + { + Float3 cornerRay = frustumCornersWS[i + 4] - frustumCornersWS[i]; + Float3 nearCornerRay = cornerRay * prevSplitDist; + Float3 farCornerRay = cornerRay * splitDist; + frustumCornersWS[i + 4] = frustumCornersWS[i] + farCornerRay; + frustumCornersWS[i] = frustumCornersWS[i] + nearCornerRay; + } + + // Calculate the centroid of the view frustum slice + Float3 frustumCenter = 0.0f; + for(uint32 i = 0; i < 8; ++i) + frustumCenter = frustumCenter + frustumCornersWS[i]; + frustumCenter *= 1.0f / 8.0f; + + // Pick the up vector to use for the light camera + Float3 upDir = camera.Right(); + + Float3 minExtents; + Float3 maxExtents; + if(AppSettings::StabilizeCascades) + { + // This needs to be constant for it to be stable + upDir = Float3(0.0f, 1.0f, 0.0f); + + // Calculate the radius of a bounding sphere surrounding the frustum corners + float sphereRadius = 0.0f; + for(uint32 i = 0; i < 8; ++i) + { + float dist = Float3::Length(frustumCornersWS[i] - frustumCenter); + sphereRadius = std::max(sphereRadius, dist); + } + + sphereRadius = std::ceil(sphereRadius * 16.0f) / 16.0f; + + maxExtents = Float3(sphereRadius, sphereRadius, sphereRadius); + minExtents = -maxExtents; + } + else + { + // Create a temporary view matrix for the light + Float3 lightCameraPos = frustumCenter; + Float3 lookAt = frustumCenter - AppSettings::LightDirection; + Float4x4 lightView = XMMatrixLookAtLH(lightCameraPos.ToSIMD(), lookAt.ToSIMD(), upDir.ToSIMD()); + + // Calculate an AABB around the frustum corners + Float3 mins = FLT_MAX; + Float3 maxes = -FLT_MAX; + for(uint32 i = 0; i < 8; ++i) + { + Float3 corner = Float3::Transform(frustumCornersWS[i], lightView); + mins = XMVectorMin(mins.ToSIMD(), corner.ToSIMD()); + maxes = XMVectorMax(maxes.ToSIMD(), corner.ToSIMD()); + } + + minExtents = mins; + maxExtents = maxes; + + // Adjust the min/max to accommodate the filtering size + float scale = (ShadowMapSize + AppSettings::FixedFilterKernelSize()) / static_cast(ShadowMapSize); + minExtents.x *= scale; + minExtents.y *= scale; + maxExtents.x *= scale; + maxExtents.y *= scale; + } + + Float3 cascadeExtents = maxExtents - minExtents; + + // Get position of the shadow camera + Float3 shadowCameraPos = frustumCenter + AppSettings::LightDirection.Value() * -minExtents.z; + + // Come up with a new orthographic camera for the shadow caster + OrthographicCamera shadowCamera(minExtents.x, minExtents.y, maxExtents.x, + maxExtents.y, 0.0f, cascadeExtents.z); + shadowCamera.SetLookAt(shadowCameraPos, frustumCenter, upDir); + + if(AppSettings::StabilizeCascades) + { + // Create the rounding matrix, by projecting the world-space origin and determining + // the fractional offset in texel space + XMMATRIX shadowMatrix = shadowCamera.ViewProjectionMatrix().ToSIMD(); + XMVECTOR shadowOrigin = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); + shadowOrigin = XMVector4Transform(shadowOrigin, shadowMatrix); + shadowOrigin = XMVectorScale(shadowOrigin, sMapSize / 2.0f); + + XMVECTOR roundedOrigin = XMVectorRound(shadowOrigin); + XMVECTOR roundOffset = XMVectorSubtract(roundedOrigin, shadowOrigin); + roundOffset = XMVectorScale(roundOffset, 2.0f / sMapSize); + roundOffset = XMVectorSetZ(roundOffset, 0.0f); + roundOffset = XMVectorSetW(roundOffset, 0.0f); + + XMMATRIX shadowProj = shadowCamera.ProjectionMatrix().ToSIMD(); + shadowProj.r[3] = XMVectorAdd(shadowProj.r[3], roundOffset); + shadowCamera.SetProjection(shadowProj); + } + + // Draw the mesh with depth only, using the new shadow camera + RenderDepthCPU(context, shadowCamera, world, characterWorld, true); + + // Apply the scale/offset matrix, which transforms from [-1,1] + // post-projection space to [0,1] UV space + XMMATRIX texScaleBias; + texScaleBias.r[0] = XMVectorSet(0.5f, 0.0f, 0.0f, 0.0f); + texScaleBias.r[1] = XMVectorSet(0.0f, -0.5f, 0.0f, 0.0f); + texScaleBias.r[2] = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); + texScaleBias.r[3] = XMVectorSet(0.5f, 0.5f, 0.0f, 1.0f); + XMMATRIX shadowMatrix = shadowCamera.ViewProjectionMatrix().ToSIMD(); + shadowMatrix = XMMatrixMultiply(shadowMatrix, texScaleBias); + + // Store the split distance in terms of view space depth + const float clipDist = camera.FarClip() - camera.NearClip(); + meshPSConstants.Data.CascadeSplits[cascadeIdx] = camera.NearClip() + splitDist * clipDist; + + // Calculate the position of the lower corner of the cascade partition, in the UV space + // of the first cascade partition + Float4x4 invCascadeMat = Float4x4::Invert(shadowMatrix); + Float3 cascadeCorner = Float3::Transform(Float3(0.0f, 0.0f, 0.0f), invCascadeMat); + cascadeCorner = Float3::Transform(cascadeCorner, globalShadowMatrix); + + // Do the same for the upper corner + Float3 otherCorner = Float3::Transform(Float3(1.0f, 1.0f, 1.0f), invCascadeMat); + otherCorner = Float3::Transform(otherCorner, globalShadowMatrix); + + // Calculate the scale and offset + Float3 cascadeScale = Float3(1.0f, 1.0f, 1.0f) / (otherCorner - cascadeCorner); + meshPSConstants.Data.CascadeOffsets[cascadeIdx] = Float4(-cascadeCorner, 0.0f); + meshPSConstants.Data.CascadeScales[cascadeIdx] = Float4(cascadeScale, 1.0f); + + if(AppSettings::UseFilterableShadows()) + ConvertToVSM(context, cascadeIdx, meshPSConstants.Data.CascadeScales[cascadeIdx].To3D(), + meshPSConstants.Data.CascadeScales[0].To3D()); + } +} + +// Renders the shadow map for all cascades using GPU batching, and performs VSM conversion if necessary +void MeshRenderer::RenderShadowMapGPU(ID3D11DeviceContext* context, const Camera& camera, + const Float4x4& world, const Float4x4& characterWorld) +{ + PIXEvent event(L"Mesh Shadow Map Rendering(GPU)"); + ProfileBlock block(L"Shadow Map Rendering/Setup"); + + Float4x4 shadowMatrix = MakeGlobalShadowMatrix(camera); + + meshPSConstants.Data.ShadowMatrix = Float4x4::Transpose(shadowMatrix); + + // Run the cascade setup shader on the GPU + shadowSetupConstants.Data.GlobalShadowMatrix = Float4x4::Transpose(shadowMatrix); + shadowSetupConstants.Data.ViewProjInv = Float4x4::Transpose(Float4x4::Invert(camera.ViewProjectionMatrix())); + shadowSetupConstants.Data.CameraRight = camera.WorldMatrix().Right(); + shadowSetupConstants.Data.CameraNearClip = camera.NearClip(); + shadowSetupConstants.Data.CameraFarClip = camera.FarClip(); + shadowSetupConstants.ApplyChanges(context); + shadowSetupConstants.SetCS(context, 0); + + SetCSShader(context, setupCascades); + SetCSInputs(context, depthReductionTargets[depthReductionTargets.size() - 1].SRView); + SetCSOutputs(context, cascadeMatrixBuffer.UAView, cascadeSplitBuffer.UAView, + cascadeOffsetBuffer.UAView, cascadeScaleBuffer.UAView, + cascadePlanesBuffer.UAView); + context->Dispatch(1, 1, 1); + ClearCSInputs(context); + ClearCSOutputs(context); + + const uint32 ShadowMapSize = AppSettings::ShadowMapResolution(); + const float sMapSize = static_cast(ShadowMapSize); + + // Set the viewport + D3D11_VIEWPORT viewport; + viewport.TopLeftX = 0.0f; + viewport.TopLeftY = 0.0f; + viewport.Width = sMapSize; + viewport.Height = sMapSize; + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; + context->RSSetViewports(1, &viewport); + + // Render each cascade + for(uint32 cascadeIdx = 0; cascadeIdx < NumCascades; ++cascadeIdx) + { + ID3D11DepthStencilView* dsv = shadowMap.DSView; + if(AppSettings::UseFilterableShadows() == false) + dsv = shadowMap.ArraySlices[cascadeIdx]; + ID3D11RenderTargetView* nullRenderTargets[8] = { nullptr }; + context->OMSetRenderTargets(8, nullRenderTargets, dsv); + context->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0); + + RenderDepthGPU(context, world, characterWorld, true, cascadeMatrixBuffer.Buffer, sizeof(Float4x4) * cascadeIdx, + cascadePlanesBuffer.Buffer, sizeof(Float4) * 6 * cascadeIdx); + + if(AppSettings::UseFilterableShadows()) + ConvertToVSM(context, cascadeIdx, Float3(1.0f, 1.0f, 1.0f), Float3(1.0f, 1.0f, 1.0f)); + } +} \ No newline at end of file diff --git a/Shadows/MeshRenderer.h b/Shadows/MeshRenderer.h new file mode 100644 index 0000000..d369ccb --- /dev/null +++ b/Shadows/MeshRenderer.h @@ -0,0 +1,247 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "SampleFramework11/PCH.h" + +#include "SampleFramework11/Model.h" +#include "SampleFramework11/GraphicsTypes.h" +#include "SampleFramework11/DeviceStates.h" +#include "SampleFramework11/Camera.h" +#include "SampleFramework11/SH.h" +#include "SampleFramework11/ShaderCompilation.h" + +#include "AppSettings.h" +#include "SharedConstants.h" + +using namespace SampleFramework11; + +// Represents a bouncing sphere for a MeshPart +struct Sphere +{ + XMFLOAT3 Center; + float Radius; +}; + +// Represents the 6 planes of a frustum +Float4Align struct Frustum +{ + XMVECTOR Planes[6]; +}; + +struct MeshData +{ + Model* Model; + ID3D11BufferPtr PositionsVB; + StructuredBuffer Indices; + StructuredBuffer DrawCalls; + StructuredBuffer CulledDraws; + RWBuffer CulledIndices; + + std::vector BoundingSpheres; + std::vector FrustumTests; + uint32 NumSuccessfulTests; + + std::vector InputLayouts; + std::vector DepthInputLayouts; + + MeshData() : Model(NULL), NumSuccessfulTests(0) {} +}; + +class MeshRenderer +{ + +protected: + + // Constants + static const uint32 MaxReadbackLatency = 4; + static const uint32 MaxBlurRadius = 4; + +public: + + MeshRenderer(); + + void Initialize(ID3D11Device* device, ID3D11DeviceContext* context); + void SetSceneMesh(ID3D11DeviceContext* context, Model* model, const Float4x4& world); + void SetCharacterMesh(ID3D11DeviceContext* context, Model* model, const Float4x4& world); + + void RenderDepthCPU(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + const Float4x4& characterWorld, bool shadowRendering); + void RenderDepthGPU(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + const Float4x4& characterWorld, bool shadowRendering); + + void Render(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + const Float4x4& characterWorld); + + void RenderShadowMap(ID3D11DeviceContext* context, const Camera& camera, + const Float4x4& world, const Float4x4& characterWorld); + void RenderShadowMapGPU(ID3D11DeviceContext* context, const Camera& camera, + const Float4x4& world, const Float4x4& characterWorld); + + void Update(); + + void CreateReductionTargets(uint32 width, uint32 height); + + void ReduceDepth(ID3D11DeviceContext* context, ID3D11ShaderResourceView* depthTarget, + const Camera& camera); + + DepthStencilBuffer& ShadowMap() { return shadowMap; } + +protected: + + void LoadShaders(); + void CreateShadowMaps(); + void ConvertToVSM(ID3D11DeviceContext* context, uint32 cascadeIdx, + Float3 cascadeScale, Float3 cascade0Scale); + + void SetupRenderDepthState(ID3D11DeviceContext* context, bool shadowRendering); + + void RenderDepthGPU(ID3D11DeviceContext* context, const Float4x4& world, + const Float4x4& characterWorld, bool shadowRendering, + ID3D11Buffer* viewProj, uint32 viewProjOffset, + ID3D11Buffer* frustumPlanes, uint32 planesOffset); + + void RenderModelDepthGPU(ID3D11DeviceContext* context, MeshData& meshData, bool shadowRendering, + const Float4x4& world, ID3D11Buffer* viewProj, uint32 viewProjOffset, + ID3D11Buffer* frustumPlanes, uint32 planeOffset); + void RenderModelDepthCPU(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + MeshData& meshData); + + void RenderModel(ID3D11DeviceContext* context, const Camera& camera, const Float4x4& world, + MeshData& meshData); + + ID3D11DevicePtr device; + + BlendStates blendStates; + RasterizerStates rasterizerStates; + DepthStencilStates depthStencilStates; + SamplerStates samplerStates; + + MeshData scene; + MeshData character; + + ID3D11ShaderResourceViewPtr defaultTexture; + + DepthStencilBuffer shadowMap; + RenderTarget2D varianceShadowMap; + RenderTarget2D tempVSM; + + ID3D11ShaderResourceViewPtr randomRotations; + + ID3D11RasterizerStatePtr shadowRSState; + ID3D11SamplerStatePtr evsmSamplers[uint64(ShadowAnisotropy::NumValues)]; + + VertexShaderPtr meshVS; + PixelShaderPtr meshPS; + + VertexShaderPtr meshDepthVS; + + VertexShaderPtr fullScreenVS; + PixelShaderPtr vsmConvertPS[AppSettings::NumFilterableShadowModes][uint64(ShadowMSAA::NumValues)]; + PixelShaderPtr vsmBlurH[MaxBlurRadius + 1]; + PixelShaderPtr vsmBlurV[MaxBlurRadius + 1]; + PixelShaderPtr vsmBlurGPUH; + PixelShaderPtr vsmBlurGPUV; + + PixelShaderPtr depthReductionInitialPS; + PixelShaderPtr depthReductionPS; + ComputeShaderPtr depthReductionInitialCS; + ComputeShaderPtr depthReductionCS; + std::vector depthReductionTargets; + StagingTexture2D reductionStagingTextures[MaxReadbackLatency]; + uint32 currFrame; + + Float2 reductionDepth; + + ComputeShaderPtr clearArgsBuffer; + ComputeShaderPtr cullDrawCalls; + ComputeShaderPtr batchDrawCalls; + RWBuffer drawArgsBuffer; + ID3D11InputLayoutPtr depthGPUInputLayout; + RWBuffer batchDispatchArgs; + + ComputeShaderPtr setupCascades; + StructuredBuffer cascadeMatrixBuffer; + StructuredBuffer cascadeSplitBuffer; + StructuredBuffer cascadeOffsetBuffer; + StructuredBuffer cascadeScaleBuffer; + StructuredBuffer cascadePlanesBuffer; + + // Constant buffers + struct DepthOnlyConstants + { + Float4x4 World; + Float4x4 ViewProjection; + }; + + struct MeshVSConstants + { + Float4x4 World; + Float4x4 ViewProjection; + }; + + struct MeshPSConstants + { + Float4Align Float3 CameraPosWS; + + Float4Align Float4x4 ShadowMatrix; + Float4Align float CascadeSplits[NumCascades]; + + Float4Align Float4 CascadeOffsets[NumCascades]; + Float4Align Float4 CascadeScales[NumCascades]; + }; + + struct VSMConstants + { + Float4 CascadeScale; + Float4 Cascade0Scale; + Float2 ShadowMapSize; + }; + + struct ReductionConstants + { + Float4x4 Projection; + float NearClip; + float FarClip; + }; + + struct GPUBatchConstants + { + Float4 FrustumPlanes[6]; + uint32 NumDrawCalls; + bool32 CullNearZ; + }; + + struct ShadowSetupConstants + { + Float4x4 GlobalShadowMatrix; + Float4x4 ViewProjInv; + Float3 CameraRight; + float CameraNearClip; + float CameraFarClip; + }; + + struct FrustumConstants + { + Float4 FrustumPlanes[6]; + }; + + ConstantBuffer depthOnlyConstants; + ConstantBuffer meshVSConstants; + ConstantBuffer meshPSConstants; + ConstantBuffer vsmConstants; + ConstantBuffer reductionConstants; + ConstantBuffer gpuBatchConstants; + ConstantBuffer shadowSetupConstants; + + ConstantBuffer tempViewProjBuffer; + ConstantBuffer tempFrustumPlanesBuffer; +}; \ No newline at end of file diff --git a/Shadows/PCFKernels.hlsl b/Shadows/PCFKernels.hlsl new file mode 100644 index 0000000..93b7a45 --- /dev/null +++ b/Shadows/PCFKernels.hlsl @@ -0,0 +1,133 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#ifndef FilterSize_ + #define FilterSize_ 2 +#endif + +#if FilterSize_ == 3 + +static const float W[3][3] = +{ + { 0.5,1.0,0.5, }, + { 1.0,1.0,1.0, }, + { 0.5,1.0,0.5, } +}; + +#elif FilterSize_ == 5 + +static const float W[5][5] = +{ + { 0.0,0.5,1.0,0.5,0.0 }, + { 0.5,1.0,1.0,1.0,0.5 }, + { 1.0,1.0,1.0,1.0,1.0 }, + { 0.5,1.0,1.0,1.0,0.5 }, + { 0.0,0.5,1.0,0.5,0.0 } +}; + +#elif FilterSize_ == 7 + +// -- 7x7 disc kernel +static const float W[FilterSize_][FilterSize_] = +{ + { 0.0,0.0,0.5,1.0,0.5,0.0,0.0 }, + { 0.0,1.0,1.0,1.0,1.0,1.0,0.0 }, + { 0.5,1.0,1.0,1.0,1.0,1.0,0.5 }, + { 1.0,1.0,1.0,1.0,1.0,1.0,1.0 }, + { 0.5,1.0,1.0,1.0,1.0,1.0,0.5 }, + { 0.0,1.0,1.0,1.0,1.0,1.0,0.0 }, + { 0.0,0.0,0.5,1.0,0.5,0.0,0.0 } +}; + +#elif FilterSize_ == 9 + +static const float W[9][9] = +{ + { 0.0,0.0,0.0,0.5,1.0,0.5,0.0,0.0,0.0 }, + { 0.0,0.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0 }, + { 0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0 }, + { 0.5,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.5 }, + { 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 }, + { 0.5,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.5 }, + { 0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0 }, + { 0.0,0.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0 }, + { 0.0,0.0,0.0,0.5,1.0,0.5,0.0,0.0,0.0 } +}; + +#endif + +// For Poisson Disk PCF sampling +static const float2 PoissonSamples[64] = +{ + float2(-0.5119625f, -0.4827938f), + float2(-0.2171264f, -0.4768726f), + float2(-0.7552931f, -0.2426507f), + float2(-0.7136765f, -0.4496614f), + float2(-0.5938849f, -0.6895654f), + float2(-0.3148003f, -0.7047654f), + float2(-0.42215f, -0.2024607f), + float2(-0.9466816f, -0.2014508f), + float2(-0.8409063f, -0.03465778f), + float2(-0.6517572f, -0.07476326f), + float2(-0.1041822f, -0.02521214f), + float2(-0.3042712f, -0.02195431f), + float2(-0.5082307f, 0.1079806f), + float2(-0.08429877f, -0.2316298f), + float2(-0.9879128f, 0.1113683f), + float2(-0.3859636f, 0.3363545f), + float2(-0.1925334f, 0.1787288f), + float2(0.003256182f, 0.138135f), + float2(-0.8706837f, 0.3010679f), + float2(-0.6982038f, 0.1904326f), + float2(0.1975043f, 0.2221317f), + float2(0.1507788f, 0.4204168f), + float2(0.3514056f, 0.09865579f), + float2(0.1558783f, -0.08460935f), + float2(-0.0684978f, 0.4461993f), + float2(0.3780522f, 0.3478679f), + float2(0.3956799f, -0.1469177f), + float2(0.5838975f, 0.1054943f), + float2(0.6155105f, 0.3245716f), + float2(0.3928624f, -0.4417621f), + float2(0.1749884f, -0.4202175f), + float2(0.6813727f, -0.2424808f), + float2(-0.6707711f, 0.4912741f), + float2(0.0005130528f, -0.8058334f), + float2(0.02703013f, -0.6010728f), + float2(-0.1658188f, -0.9695674f), + float2(0.4060591f, -0.7100726f), + float2(0.7713396f, -0.4713659f), + float2(0.573212f, -0.51544f), + float2(-0.3448896f, -0.9046497f), + float2(0.1268544f, -0.9874692f), + float2(0.7418533f, -0.6667366f), + float2(0.3492522f, 0.5924662f), + float2(0.5679897f, 0.5343465f), + float2(0.5663417f, 0.7708698f), + float2(0.7375497f, 0.6691415f), + float2(0.2271994f, -0.6163502f), + float2(0.2312844f, 0.8725659f), + float2(0.4216993f, 0.9002838f), + float2(0.4262091f, -0.9013284f), + float2(0.2001408f, -0.808381f), + float2(0.149394f, 0.6650763f), + float2(-0.09640376f, 0.9843736f), + float2(0.7682328f, -0.07273844f), + float2(0.04146584f, 0.8313184f), + float2(0.9705266f, -0.1143304f), + float2(0.9670017f, 0.1293385f), + float2(0.9015037f, -0.3306949f), + float2(-0.5085648f, 0.7534177f), + float2(0.9055501f, 0.3758393f), + float2(0.7599946f, 0.1809109f), + float2(-0.2483695f, 0.7942952f), + float2(-0.4241052f, 0.5581087f), + float2(-0.1020106f, 0.6724468f), +}; \ No newline at end of file diff --git a/Shadows/PostProcessing.hlsl b/Shadows/PostProcessing.hlsl new file mode 100644 index 0000000..2b55a3c --- /dev/null +++ b/Shadows/PostProcessing.hlsl @@ -0,0 +1,194 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "SampleFramework11/Shaders/PPIncludes.hlsl" +#include "SharedConstants.h" + +//================================================================================================= +// Constants +//================================================================================================= +cbuffer PPConstants : register(b1) +{ + float BloomThreshold; + float BloomMagnitude; + float BloomBlurSigma; + float Tau; + float TimeDelta; + float KeyValue; +}; + +//================================================================================================= +// Helper Functions +//================================================================================================= + +// Approximates luminance from an RGB value +float CalcLuminance(float3 color) +{ + return max(dot(color, float3(0.299f, 0.587f, 0.114f)), 0.0001f); +} + +// Retrieves the log-average luminance from the texture +float GetAvgLuminance(Texture2D lumTex) +{ + return lumTex.Load(uint3(0, 0, 0)).x; +} + +// Applies the filmic curve from John Hable's presentation +float3 ToneMapFilmicALU(float3 color) +{ + color = max(0, color - 0.004f); + color = (color * (6.2f * color + 0.5f)) / (color * (6.2f * color + 1.7f)+ 0.06f); + + // result has 1/2.2 baked in + return pow(color, 2.2f); +} + +// Determines the color based on exposure settings +float3 CalcExposedColor(float3 color, float avgLuminance, float threshold, out float exposure) +{ + // Use geometric mean + avgLuminance = max(avgLuminance, 0.001f); + float keyValue = KeyValue; + float linearExposure = (KeyValue / avgLuminance); + exposure = log2(max(linearExposure, 0.0001f)); + exposure -= threshold; + return exp2(exposure) * color; +} + +// Applies exposure and tone mapping to the specific color, and applies +// the threshold to the exposure value. +float3 ToneMap(float3 color, float avgLuminance, float threshold, out float exposure) +{ + float pixelLuminance = CalcLuminance(color); + color = CalcExposedColor(color, avgLuminance, threshold, exposure); + color = ToneMapFilmicALU(color); + return color; +} + +// Calculates the gaussian blur weight for a given distance and sigmas +float CalcGaussianWeight(int sampleDist, float sigma) +{ + float g = 1.0f / sqrt(2.0f * 3.14159 * sigma * sigma); + return (g * exp(-(sampleDist * sampleDist) / (2 * sigma * sigma))); +} + +// Performs a gaussian blur in one direction +float4 Blur(in PSInput input, float2 texScale, float sigma) +{ + float4 color = 0; + for (int i = -6; i < 6; i++) + { + float weight = CalcGaussianWeight(i, sigma); + float2 texCoord = input.TexCoord; + texCoord += (i / InputSize0) * texScale; + float4 sample = InputTexture0.Sample(PointSampler, texCoord); + color += sample * weight; + } + + return color; +} + +// ================================================================================================ +// Shader Entry Points +// ================================================================================================ + +// Uses a lower exposure to produce a value suitable for a bloom pass +float4 Threshold(in PSInput input) : SV_Target +{ + float3 color = 0; + + color = InputTexture0.Sample(LinearSampler, input.TexCoord).rgb; + + // Tone map it to threshold + float avgLuminance = GetAvgLuminance(InputTexture1); + float exposure = 0; + float pixelLuminance = CalcLuminance(color); + color = CalcExposedColor(color, avgLuminance, BloomThreshold, exposure); + + if(dot(color, 0.333f) <= 0.001f) + color = 0.0f; + + return float4(color, 1.0f); +} + +// Uses hw bilinear filtering for upscaling or downscaling +float4 Scale(in PSInput input) : SV_Target +{ + return InputTexture0.Sample(LinearSampler, input.TexCoord); +} + +// Horizontal gaussian blur +float4 BloomBlurH(in PSInput input) : SV_Target +{ + return Blur(input, float2(1, 0), BloomBlurSigma); +} + +// Vertical gaussian blur +float4 BloomBlurV(in PSInput input) : SV_Target +{ + return Blur(input, float2(0, 1), BloomBlurSigma); +} + +// Applies exposure and tone mapping to the input, and combines it with the +// results of the bloom pass +float4 Composite(in PSInput input) : SV_Target0 +{ + // Tone map the primary input + float avgLuminance = GetAvgLuminance(InputTexture1); + float3 color = InputTexture0.Sample(PointSampler, input.TexCoord).rgb; + + float exposure = 0; + color = ToneMap(color, avgLuminance, 0, exposure); + + // Sample the bloom + float3 bloom = InputTexture2.Sample(LinearSampler, input.TexCoord).rgb; + bloom *= BloomMagnitude; + + color = color + bloom; + + return float4(color, 1.0f); +} + +// Creates the luminance map for the scene +float4 LuminanceMap(in PSInput input) : SV_Target +{ + // Sample the input + float3 color = InputTexture0.Sample(LinearSampler, input.TexCoord).rgb; + + // calculate the luminance using a weighted average + float luminance = log(max(CalcLuminance(color), 0.00001f)); + + return float4(luminance, 1.0f, 1.0f, 1.0f); +} + +// Slowly adjusts the scene luminance based on the previous scene luminance +float4 AdaptLuminance(in PSInput input) : SV_Target +{ + float lastLum = InputTexture0.Load(uint3(0, 0, 0)).x; + float currentLum = exp(InputTexture1.SampleLevel(PointSampler, float2(0.5f, 0.5f), 10.0f).x); + + // Adapt the luminance using Pattanaik's technique + float adaptedLum = lastLum + (currentLum - lastLum) * (1 - exp(-TimeDelta * Tau)); + + return float4(adaptedLum, 1.0f, 1.0f, 1.0f); +} + +Texture2D DepthTexture : register(t0); +Texture2DMS DepthTextureMSAA : register(t0); + +float4 DrawDepth(in PSInput input) : SV_Target +{ + return float4(saturate(DepthTexture[input.PositionSS.xy].xxx - 0.95f) * 20.0f, 1.0f); +} + +float4 DrawDepthMSAA(in PSInput input) : SV_Target +{ + return float4(saturate(DepthTextureMSAA.Load(input.PositionSS.xy, 0).xxx - 0.95f) * 20.0f, 1.0f); +} diff --git a/Shadows/PostProcessor.cpp b/Shadows/PostProcessor.cpp new file mode 100644 index 0000000..9493349 --- /dev/null +++ b/Shadows/PostProcessor.cpp @@ -0,0 +1,154 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "SampleFramework11/ShaderCompilation.h" +#include "SampleFramework11/Profiler.h" + +#include "PostProcessor.h" +#include "SharedConstants.h" + +// Constants +static const uint32 TGSize = 16; +static const uint32 LumMapSize = 1024; + +void PostProcessor::Initialize(ID3D11Device* device) +{ + PostProcessorBase::Initialize(device); + + constantBuffer.Initialize(device); + + // Load the shaders + bloomThreshold = CompilePSFromFile(device, L"PostProcessing.hlsl", "Threshold"); + bloomBlurH = CompilePSFromFile(device, L"PostProcessing.hlsl", "BloomBlurH"); + bloomBlurV = CompilePSFromFile(device, L"PostProcessing.hlsl", "BloomBlurV"); + luminanceMap = CompilePSFromFile(device, L"PostProcessing.hlsl", "LuminanceMap"); + composite = CompilePSFromFile(device, L"PostProcessing.hlsl", "Composite"); + scale = CompilePSFromFile(device, L"PostProcessing.hlsl", "Scale"); + adaptLuminance = CompilePSFromFile(device, L"PostProcessing.hlsl", "AdaptLuminance"); + drawDepth = CompilePSFromFile(device, L"PostProcessing.hlsl", "DrawDepth"); + drawDepthMSAA = CompilePSFromFile(device, L"PostProcessing.hlsl", "DrawDepthMSAA"); + + // Create average luminance calculation targets + currLumTarget = 0; + adaptedLuminance[0].Initialize(device, 1, 1, DXGI_FORMAT_R32_FLOAT); + adaptedLuminance[1].Initialize(device, 1, 1, DXGI_FORMAT_R32_FLOAT); +} + +void PostProcessor::AfterReset(uint32 width, uint32 height) +{ + PostProcessorBase::AfterReset(width, height); +} + +void PostProcessor::SetConstants(const Constants& constants) +{ + constantBuffer.Data = constants; +} + +void PostProcessor::Render(ID3D11DeviceContext* deviceContext, ID3D11ShaderResourceView* input, + ID3D11RenderTargetView* output) +{ + PostProcessorBase::Render(deviceContext, input, output); + + constantBuffer.ApplyChanges(deviceContext); + constantBuffer.SetPS(deviceContext, 1); + + // Calculate the average luminance first + CalcAvgLuminance(input); + + // Now do the bloom + TempRenderTarget* bloom = NULL; + Bloom(input, bloom); + + // Apply tone mapping + ToneMap(input, bloom->SRView, output); + + bloom->InUse = false; + + currLumTarget = !currLumTarget; +} + +void PostProcessor::CalcAvgLuminance(ID3D11ShaderResourceView* input) +{ + TempRenderTarget* initialLuminance = GetTempRenderTarget(LumMapSize, LumMapSize, DXGI_FORMAT_R32_FLOAT, 1, 0, 0, TRUE); + + // Luminance mapping + PostProcess(input, initialLuminance->RTView, luminanceMap, L"Luminance Map Generation"); + + // Generate the mip chain + context->GenerateMips(initialLuminance->SRView); + + // Adaptation + inputs.push_back(adaptedLuminance[!currLumTarget].SRView); + inputs.push_back(initialLuminance->SRView); + outputs.push_back(adaptedLuminance[currLumTarget].RTView); + PostProcess(adaptLuminance, L"Luminance Adaptation"); + + initialLuminance->InUse = FALSE; +} + +void PostProcessor::Bloom(ID3D11ShaderResourceView* input, TempRenderTarget*& bloomTarget) +{ + // Downscale + TempRenderTarget* bloomSrc = GetTempRenderTarget(inputWidth, inputHeight, DXGI_FORMAT_R11G11B10_FLOAT); + + inputs.push_back(input); + inputs.push_back(adaptedLuminance[currLumTarget].SRView); + outputs.push_back(bloomSrc->RTView); + PostProcess(bloomThreshold, L"Bloom Threshold"); + + TempRenderTarget* downscale1 = GetTempRenderTarget(inputWidth / 2, inputHeight / 2, DXGI_FORMAT_R11G11B10_FLOAT); + PostProcess(bloomSrc->SRView, downscale1->RTView, scale, L"Bloom Downscale"); + bloomSrc->InUse = false; + + TempRenderTarget* downscale2 = GetTempRenderTarget(inputWidth / 4, inputHeight / 4, DXGI_FORMAT_R11G11B10_FLOAT); + PostProcess(downscale1->SRView, downscale2->RTView, scale, L"Bloom Downscale"); + + TempRenderTarget* downscale3 = GetTempRenderTarget(inputWidth / 8, inputHeight / 8, DXGI_FORMAT_R11G11B10_FLOAT); + PostProcess(downscale2->SRView, downscale3->RTView, scale, L"Bloom Downscale"); + + // Blur it + for (uint64 i = 0; i < 4; ++i) + { + TempRenderTarget* blurTemp = GetTempRenderTarget(inputWidth / 8, inputHeight / 8, DXGI_FORMAT_R11G11B10_FLOAT); + PostProcess(downscale3->SRView, blurTemp->RTView, bloomBlurH, L"Horizontal Bloom Blur"); + + PostProcess(blurTemp->SRView, downscale3->RTView, bloomBlurV, L"Vertical Bloom Blur"); + blurTemp->InUse = false; + } + + PostProcess(downscale3->SRView, downscale2->RTView, scale, L"Bloom Upscale"); + downscale3->InUse = false; + + PostProcess(downscale2->SRView, downscale1->RTView, scale, L"Bloom Upscale"); + downscale2->InUse = false; + + bloomTarget = downscale1; +} + +void PostProcessor::ToneMap(ID3D11ShaderResourceView* input, ID3D11ShaderResourceView* bloom, + ID3D11RenderTargetView* output) +{ + // Composite the bloom with the original image, and apply tone-mapping + inputs.push_back(input); + inputs.push_back(adaptedLuminance[currLumTarget].SRView); + inputs.push_back(bloom); + outputs.push_back(output); + PostProcess(composite, L"Tone Map And Composite"); +} + +void PostProcessor::DrawDepthBuffer(DepthStencilBuffer& depthBuffer, ID3D11RenderTargetView* rt) { + Assert_(depthBuffer.SRView != nullptr); + if(depthBuffer.MultiSamples > 1) + PostProcess(depthBuffer.SRView, rt, drawDepthMSAA, L"Draw Depth"); + else + PostProcess(depthBuffer.SRView, rt, drawDepth, L"Draw Depth"); +} \ No newline at end of file diff --git a/Shadows/PostProcessor.h b/Shadows/PostProcessor.h new file mode 100644 index 0000000..efd4406 --- /dev/null +++ b/Shadows/PostProcessor.h @@ -0,0 +1,71 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "SampleFramework11/PCH.h" +#include "SampleFramework11/GraphicsTypes.h" +#include "SampleFramework11/PostProcessorBase.h" +#include "SampleFramework11/DeviceStates.h" + +#include "AppSettings.h" + +using namespace SampleFramework11; + +class PostProcessor : public PostProcessorBase +{ + +public: + + struct Constants + { + float BloomThreshold; + float BloomMagnitude; + float BloomBlurSigma; + float Tau; + float TimeDelta; + float KeyValue; + }; + + void Initialize(ID3D11Device* device); + + void SetConstants(const Constants& constants); + void Render(ID3D11DeviceContext* deviceContext, ID3D11ShaderResourceView* input, + ID3D11RenderTargetView* output); + void AfterReset(uint32 width, uint32 height); + + void DrawDepthBuffer(DepthStencilBuffer& depthBuffer, ID3D11RenderTargetView* rt); + + const Constants& GetConstants() const { return constantBuffer.Data; } + +protected: + + void CalcAvgLuminance(ID3D11ShaderResourceView* input); + void Bloom(ID3D11ShaderResourceView* input, TempRenderTarget*& bloomTarget); + void ToneMap(ID3D11ShaderResourceView* input, ID3D11ShaderResourceView* bloom, + ID3D11RenderTargetView* output); + + PixelShaderPtr bloomThreshold; + PixelShaderPtr bloomBlurH; + PixelShaderPtr bloomBlurV; + PixelShaderPtr luminanceMap; + PixelShaderPtr composite; + PixelShaderPtr scale; + PixelShaderPtr adaptLuminance; + PixelShaderPtr drawDepth; + PixelShaderPtr drawDepthMSAA; + + RenderTarget2D adaptedLuminance[2]; + RenderTarget2D exposureMap; + + uint64 currLumTarget; + + ConstantBuffer constantBuffer; +}; \ No newline at end of file diff --git a/Shadows/SampleFramework11/App.cpp b/Shadows/SampleFramework11/App.cpp new file mode 100644 index 0000000..130b7ac --- /dev/null +++ b/Shadows/SampleFramework11/App.cpp @@ -0,0 +1,274 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "App.h" +#include "Exceptions.h" +#include "Profiler.h" +#include "GUIObject.h" +#include "Math.h" +#include "LodePNG/lodepng.h" +#include "FileIO.h" +#include "Settings.h" +#include "TwHelper.h" + +namespace SampleFramework11 +{ + +App* GlobalApp = nullptr; + +App::App(const wchar* appName, const wchar* iconResource) : window(NULL, appName, WS_OVERLAPPEDWINDOW, + WS_EX_APPWINDOW, 1280, 720, iconResource, iconResource), + currentTimeDeltaSample(0), + fps(0), tweakBar(nullptr), applicationName(appName) +{ + GlobalApp = this; + for(uint32 i = 0; i < NumTimeDeltaSamples; ++i) + timeDeltaBuffer[i] = 0; +} + +App::~App() +{ + +} + +void App::Run() +{ + try + { + Win32Call(AllocConsole()); + Win32Call(SetConsoleTitle(applicationName.c_str())); + FILE* consoleFile = nullptr; + freopen_s(&consoleFile, "CONOUT$", "wb", stdout); + + window.ShowWindow(); + + deviceManager.Initialize(window); + + blendStates.Initialize(deviceManager.Device()); + rasterizerStates.Initialize(deviceManager.Device()); + depthStencilStates.Initialize(deviceManager.Device()); + samplerStates.Initialize(deviceManager.Device()); + + // Create a font + SpriteRenderer + font.Initialize(L"Arial", 18, SpriteFont::Regular, true, deviceManager.Device()); + spriteRenderer.Initialize(deviceManager.Device()); + + Profiler::GlobalProfiler.Initialize(deviceManager.Device(), deviceManager.ImmediateContext()); + + GUIObject::InitGlobalResources(deviceManager.Device()); + + window.RegisterMessageCallback(WM_SIZE, OnWindowResized, this); + + // Initialize AntTweakBar + TwCall(TwInit(TW_DIRECT3D11, deviceManager.Device())); + + // Create a tweak bar + tweakBar = TwNewBar("Settings"); + TwCall(TwDefine(" GLOBAL help='MJPs sample framework for DX11' ")); + TwCall(TwDefine(" GLOBAL fontsize=3 ")); + + Settings.Initialize(tweakBar); + + Initialize(); + + AfterReset(); + + while(window.IsAlive()) + { + if(!window.IsMinimized()) + { + timer.Update(); + Settings.Update(); + + CalculateFPS(); + + Update(timer); + + UpdateShaders(deviceManager.Device()); + + Render(timer); + + // Render the profiler text + spriteRenderer.Begin(deviceManager.ImmediateContext(), SpriteRenderer::Point); + Profiler::GlobalProfiler.EndFrame(spriteRenderer, font); + spriteRenderer.End(); + + // Render the TweakBar UI + TwCall(TwDraw()); + + deviceManager.Present(); + } + + window.MessageLoop(); + } + } + catch (SampleFramework11::Exception exception) + { + exception.ShowErrorMessage(); + } + + ShutdownShaders(); + + TwCall(TwTerminate()); + + fclose(stdout); + FreeConsole(); +} + +void App::CalculateFPS() +{ + timeDeltaBuffer[currentTimeDeltaSample] = timer.DeltaSecondsF(); + currentTimeDeltaSample = (currentTimeDeltaSample + 1) % NumTimeDeltaSamples; + + float averageDelta = 0; + for(UINT i = 0; i < NumTimeDeltaSamples; ++i) + averageDelta += timeDeltaBuffer[i]; + averageDelta /= NumTimeDeltaSamples; + + fps = static_cast(std::floor((1.0f / averageDelta) + 0.5f)); +} + +LRESULT App::OnWindowResized(void* context, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + App* app = reinterpret_cast(context); + + if(!app->deviceManager.FullScreen() && wParam != SIZE_MINIMIZED) + { + int width, height; + app->window.GetClientArea(width, height); + + if(width != app->deviceManager.BackBufferWidth() || height != app->deviceManager.BackBufferHeight()) + { + app->BeforeReset(); + + app->deviceManager.SetBackBufferWidth(width); + app->deviceManager.SetBackBufferHeight(height); + app->deviceManager.Reset(); + + app->AfterReset(); + } + } + + return 0; +} + +void App::Exit() +{ + window.Destroy(); +} + +void App::Initialize() +{ +} + +void App::BeforeReset() +{ +} + +void App::AfterReset() +{ + const uint32 width = deviceManager.BackBufferWidth(); + const uint32 height = deviceManager.BackBufferHeight(); + captureTexture.Initialize(deviceManager.Device(), width, height, deviceManager.BackBufferFormat(), 1, + deviceManager.BackBufferMSCount(), deviceManager.BackBufferMSQuality(), 1); + + TwHelper::SetSize(tweakBar, 375, deviceManager.BackBufferHeight()); + TwHelper::SetPosition(tweakBar, deviceManager.BackBufferWidth() - 375, 0); +} + +void App::ToggleFullScreen(bool fullScreen) +{ + if(fullScreen != deviceManager.FullScreen()) + { + BeforeReset(); + + deviceManager.SetFullScreen(fullScreen); + deviceManager.Reset(); + + AfterReset(); + } +} + +void App::RenderText(const std::wstring& text, Float2 pos) +{ + ID3D11DeviceContext* context = deviceManager.ImmediateContext(); + + // Set the backbuffer + ID3D11RenderTargetView* rtvs[1] = { deviceManager.BackBuffer() }; + context->OMSetRenderTargets(1, rtvs, NULL); + + float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + context->ClearRenderTargetView(rtvs[0], clearColor); + + // Draw the text + Float4x4 transform; + transform.SetTranslation(Float3(pos.x, pos.y,0.0f)); + spriteRenderer.Begin(context, SpriteRenderer::Point); + spriteRenderer.RenderText(font, text.c_str(), transform.ToSIMD()); + spriteRenderer.End(); + + // Present + deviceManager.SwapChain()->Present(0, 0); + + // Pump the message loop + window.MessageLoop(); +} + +void App::RenderCenteredText(const std::wstring& text) +{ + + // Measure the text + Float2 textSize = font.MeasureText(text.c_str()); + + // Position it in the middle + Float2 textPos; + textPos.x = Round((deviceManager.BackBufferWidth() / 2.0f) - (textSize.x / 2.0f)); + textPos.y = Round((deviceManager.BackBufferHeight() / 2.0f) - (textSize.y / 2.0f)); + + RenderText(text, textPos); +} + +void App::SaveScreenshot(const wchar* filePath) +{ + ID3D11DeviceContext* context = deviceManager.ImmediateContext(); + context->CopyResource(captureTexture.Texture, deviceManager.BackBufferTexture()); + + const uint32 w = deviceManager.BackBufferWidth(); + const uint32 h = deviceManager.BackBufferHeight(); + std::vector imageData(w * h * 4); + + uint32 pitch = 0; + const uint8* srcData = reinterpret_cast(captureTexture.Map(context, 0, pitch)); + uint32* dstData = reinterpret_cast(imageData.data()); + for(uint32 y = 0; y < h; ++y) + { + memcpy(dstData, srcData, w * 4); + + // Set the alpha to 0xFF + for(uint32 x = 0; x < w; ++x) + dstData[x] |= 0xFF000000; + + srcData += pitch; + dstData += w; + } + + captureTexture.Unmap(context, 0); + + std::vector fileData; + uint32 result = lodepng::encode(fileData, imageData, w, h, LCT_RGBA, 8); + if(result != 0) + throw Exception(AnsiToWString(lodepng_error_text(result))); + + File pngFile(filePath, File::OpenWrite); + pngFile.Write(fileData.size(), fileData.data()); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/App.h b/Shadows/SampleFramework11/App.h new file mode 100644 index 0000000..6713e86 --- /dev/null +++ b/Shadows/SampleFramework11/App.h @@ -0,0 +1,89 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Window.h" +#include "DeviceManager.h" +#include "DeviceStates.h" +#include "Timer.h" +#include "SpriteFont.h" +#include "SpriteRenderer.h" + +namespace SampleFramework11 +{ + +class App +{ + +public: + + App(const wchar* appName, const wchar* iconResource = NULL); + virtual ~App(); + + void Run(); + + void RenderText(const std::wstring& text, Float2 pos); + void RenderCenteredText(const std::wstring& text); + +protected: + + virtual void Initialize(); + virtual void Update(const Timer& timer) = 0; + virtual void Render(const Timer& timer) = 0; + + virtual void BeforeReset(); + virtual void AfterReset(); + + void Exit(); + void ToggleFullScreen(bool fullScreen); + void CalculateFPS(); + + void SaveScreenshot(const wchar* filePath); + + static LRESULT OnWindowResized(void* context, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); + + Window window; + DeviceManager deviceManager; + Timer timer; + + BlendStates blendStates; + RasterizerStates rasterizerStates; + DepthStencilStates depthStencilStates; + SamplerStates samplerStates; + + SpriteFont font; + SpriteRenderer spriteRenderer; + + static const uint32 NumTimeDeltaSamples = 64; + float timeDeltaBuffer[NumTimeDeltaSamples]; + uint32 currentTimeDeltaSample; + uint32 fps; + + StagingTexture2D captureTexture; + + TwBar* tweakBar; + + std::wstring applicationName; + +public: + + // Accessors + Window& Window() { return window; } + DeviceManager& DeviceManager() { return deviceManager; } + SpriteFont& Font() { return font; } + SpriteRenderer& SpriteRenderer() { return spriteRenderer; } + TwBar* TweakBar() { return tweakBar; } +}; + +extern App* GlobalApp; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Assert.cpp b/Shadows/SampleFramework11/Assert.cpp new file mode 100644 index 0000000..169c669 --- /dev/null +++ b/Shadows/SampleFramework11/Assert.cpp @@ -0,0 +1,100 @@ +/* +* Copyright (c) 2008, Power of Two Games LLC +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of Power of Two Games LLC nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY POWER OF TWO GAMES LLC ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL POWER OF TWO GAMES LLC BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "PCH.h" + +#include "Assert.h" + +namespace pow2 +{ + +namespace +{ + +Assert::FailBehavior DefaultHandler(const char* condition, + const char* msg, + const char* file, + const int line) +{ + const uint64 BufferSize = 2048; + char buffer[BufferSize]; + sprintf_s(buffer, BufferSize, "%s(%d): Assert Failure: ", file, line); + + if(condition != NULL) + sprintf_s(buffer, BufferSize, "%s'%s' ", buffer, condition); + + if(msg != NULL) + sprintf_s(buffer, BufferSize, "%s%s", buffer, msg); + + sprintf_s(buffer, BufferSize, "%s\n", buffer); + + OutputDebugStringA(buffer); + + return Assert::Halt; +} + +Assert::Handler& GetAssertHandlerInstance() +{ + static Assert::Handler s_handler = &DefaultHandler; + return s_handler; +} + +} + +Assert::Handler Assert::GetHandler() +{ + return GetAssertHandlerInstance(); +} + +void Assert::SetHandler(Assert::Handler newHandler) +{ + GetAssertHandlerInstance() = newHandler; +} + +Assert::FailBehavior Assert::ReportFailure(const char* condition, + const char* file, + const int line, + const char* msg, ...) +{ + const char* message = NULL; + if (msg != NULL) + { + char messageBuffer[1024]; + { + va_list args; + va_start(args, msg); + vsnprintf_s(messageBuffer, 1024, 1024, msg, args); + va_end(args); + } + + message = messageBuffer; + } + + return GetAssertHandlerInstance()(condition, message, file, line); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Assert.h b/Shadows/SampleFramework11/Assert.h new file mode 100644 index 0000000..d23e0db --- /dev/null +++ b/Shadows/SampleFramework11/Assert.h @@ -0,0 +1,127 @@ +/* +* Copyright (c) 2008, Power of Two Games LLC +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of Power of Two Games LLC nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY POWER OF TWO GAMES LLC ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL POWER OF TWO GAMES LLC BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#pragma once + +#define POW2_ASSERTS_ENABLED + +namespace pow2 { namespace Assert +{ + enum FailBehavior + { + Halt, + Continue, + }; + + typedef FailBehavior (*Handler)(const char* condition, + const char* msg, + const char* file, + int line); + + Handler GetHandler(); + void SetHandler(Handler newHandler); + + FailBehavior ReportFailure(const char* condition, + const char* file, + int line, + const char* msg, ...); +}} + +#define POW2_HALT() __debugbreak() +#define POW2_UNUSED(x) do { (void)sizeof(x); } while(0) + +#ifdef POW2_ASSERTS_ENABLED + #define POW2_ASSERT(cond) \ + do \ + { \ + if (!(cond)) \ + { \ + if (pow2::Assert::ReportFailure(#cond, __FILE__, __LINE__, 0) == \ + pow2::Assert::Halt) \ + POW2_HALT(); \ + } \ + } while(0) + + #define POW2_ASSERT_MSG(cond, msg, ...) \ + do \ + { \ + if (!(cond)) \ + { \ + if (pow2::Assert::ReportFailure(#cond, __FILE__, __LINE__, (msg), __VA_ARGS__) == \ + pow2::Assert::Halt) \ + POW2_HALT(); \ + } \ + } while(0) + + #define POW2_ASSERT_FAIL(msg, ...) \ + do \ + { \ + if (pow2::Assert::ReportFailure(0, __FILE__, __LINE__, (msg), __VA_ARGS__) == \ + pow2::Assert::Halt) \ + POW2_HALT(); \ + } while(0) + + #define POW2_VERIFY(cond) POW2_ASSERT(cond) + #define POW2_VERIFY_MSG(cond, msg, ...) POW2_ASSERT_MSG(cond, msg, ##__VA_ARGS__) +#else + #define POW2_ASSERT(condition) \ + do { POW2_UNUSED(condition); } while(0) + #define POW2_ASSERT_MSG(condition, msg, ...) \ + do { POW2_UNUSED(condition); POW2_UNUSED(msg); } while(0) + #define POW2_ASSERT_FAIL(msg, ...) \ + do { POW2_UNUSED(msg); } while(0) + #define POW2_VERIFY(cond) (void)(cond) + #define POW2_VERIFY_MSG(cond, msg, ...) \ + do { (void)(cond); POW2_UNUSED(msg); } while(0) +#endif + +// SampleFramework11 Asserts +#ifdef _DEBUG + #define UseAsserts_ 1 +#else + #define UseAsserts_ 0 +#endif + +#if UseAsserts_ + #define Assert_(x) POW2_ASSERT(x) + #define AssertMsg_(x, msg, ...) POW2_ASSERT_MSG(x, msg, __VA_ARGS__) + #define AssertFail_(msg, ...) POW2_ASSERT_FAIL(msg, __VA_ARGS__) +#else + #define Assert_(x) + #define AssertMsg_(x, msg, ...) + #define AssertFail_(msg, ...) +#endif + +#define UseStaticAsserts_ 1 + +#if UseStaticAsserts_ + #define StaticAssert_(x) static_assert(x, #x); + #define StaticAssertMsg_(x, msg) static_assert(x, #x ", " msg); +#else + #define StaticAssert_(x) + #define StaticAssertMsg_(x, msg) +#endif diff --git a/Shadows/SampleFramework11/Camera.cpp b/Shadows/SampleFramework11/Camera.cpp new file mode 100644 index 0000000..ac4fe2a --- /dev/null +++ b/Shadows/SampleFramework11/Camera.cpp @@ -0,0 +1,248 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Camera.h" + +#include "Utility.h" + +namespace SampleFramework11 +{ + +//================================================================================================= +// Camera +//================================================================================================= +Camera::Camera(float nearClip, float farClip) : nearZ(nearClip), + farZ(farClip) +{ + Assert_(nearZ >= 0.0f && nearZ < farZ); + Assert_(farZ >= 0.0f && farZ > nearZ); + + world = XMMatrixIdentity(); + view = XMMatrixIdentity(); + position = Float3(0.0f, 0.0f, 0.0f); + orientation = XMQuaternionIdentity(); +} + +Camera::~Camera() +{ + +} + +void Camera::WorldMatrixChanged() +{ + view = Float4x4::Invert(world); + viewProjection = view * projection; +} + +Float3 Camera::Forward() const +{ + return world.Forward(); +} + +Float3 Camera::Back() const +{ + return world.Back(); +} + +Float3 Camera::Up() const +{ + return world.Up(); +} + +Float3 Camera::Down() const +{ + return world.Down(); +} + +Float3 Camera::Right() const +{ + return world.Right(); +} + +Float3 Camera::Left() const +{ + return world.Left(); +} + +void Camera::SetLookAt(const Float3 &eye, const Float3 &lookAt, const Float3 &up) +{ + view = XMMatrixLookAtLH(eye.ToSIMD(), lookAt.ToSIMD(), up.ToSIMD()); + world = Float4x4::Invert(view); + position = eye; + orientation = XMQuaternionRotationMatrix(world.ToSIMD()); + + WorldMatrixChanged(); +} + +void Camera::SetWorldMatrix(const Float4x4& newWorld) +{ + world = newWorld; + position = world.Translation(); + orientation = XMQuaternionRotationMatrix(world.ToSIMD()); + + WorldMatrixChanged(); +} + +void Camera::SetPosition(const Float3& newPosition) +{ + position = newPosition; + world.SetTranslation(newPosition); + + WorldMatrixChanged(); +} + +void Camera::SetOrientation(const Float4& newOrientation) +{ + world = XMMatrixRotationQuaternion(newOrientation.ToSIMD()); + orientation = newOrientation; + world.SetTranslation(position); + + WorldMatrixChanged(); +} + +void Camera::SetNearClip(float newNearClip) +{ + nearZ = newNearClip; + CreateProjection(); +} + +void Camera::SetFarClip(float newFarClip) +{ + farZ = newFarClip; + CreateProjection(); +} + +void Camera::SetProjection(const Float4x4& newProjection) +{ + projection = newProjection; + viewProjection = view * projection; +} + +//================================================================================================= +// OrthographicCamera +//================================================================================================= + +OrthographicCamera::OrthographicCamera(float minX, float minY, float maxX, + float maxY, float nearClip, float farClip) : Camera(nearClip, farClip), + xMin(minX), + yMin(minY), + xMax(maxX), + yMax(maxY) + +{ + Assert_(xMax > xMin && yMax > yMin); + + CreateProjection(); +} + +OrthographicCamera::~OrthographicCamera() +{ + +} + +void OrthographicCamera::CreateProjection() +{ + projection = XMMatrixOrthographicOffCenterLH(xMin, xMax, yMin, yMax, nearZ, farZ); + viewProjection = view * projection; +} + +void OrthographicCamera::SetMinX(float minX) +{ + xMin = minX; + CreateProjection(); +} + +void OrthographicCamera::SetMinY(float minY) +{ + yMin = minY; + CreateProjection(); +} + +void OrthographicCamera::SetMaxX(float maxX) +{ + xMax = maxX; + CreateProjection(); +} + +void OrthographicCamera::SetMaxY(float maxY) +{ + yMax = maxY; + CreateProjection(); +} + +//================================================================================================= +// PerspectiveCamera +//================================================================================================= + +PerspectiveCamera::PerspectiveCamera(float aspectRatio, float fieldOfView, + float nearClip, float farClip) : Camera(nearClip, farClip), + aspect(aspectRatio), + fov(fieldOfView) +{ + _ASSERT(aspectRatio > 0); + _ASSERT(fieldOfView > 0 && fieldOfView <= 3.14159f); + CreateProjection(); +} + +PerspectiveCamera::~PerspectiveCamera() +{ + +} + +void PerspectiveCamera::SetAspectRatio(float aspectRatio) +{ + aspect = aspectRatio; + CreateProjection(); +} + +void PerspectiveCamera::SetFieldOfView(float fieldOfView) +{ + fov = fieldOfView; + CreateProjection(); +} + +void PerspectiveCamera::CreateProjection() +{ + projection = XMMatrixPerspectiveFovLH(fov, aspect, nearZ, farZ); + viewProjection = view * projection; +} + +//================================================================================================= +// FirstPersonCamera +//================================================================================================= + +FirstPersonCamera::FirstPersonCamera(float aspectRatio, float fieldOfView, + float nearClip, float farClip) : PerspectiveCamera(aspectRatio, fieldOfView, + nearClip, farClip), + xRot(0), + yRot(0) +{ + +} + +FirstPersonCamera::~FirstPersonCamera() +{ + +} + +void FirstPersonCamera::SetXRotation(float xRotation) +{ + xRot = Clamp(xRotation, -XM_PIDIV2, XM_PIDIV2); + SetOrientation(XMQuaternionRotationRollPitchYaw(xRot, yRot, 0)); +} + +void FirstPersonCamera::SetYRotation(float yRotation) +{ + yRot = XMScalarModAngle(yRotation); + SetOrientation(XMQuaternionRotationRollPitchYaw(xRot, yRot, 0)); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Camera.h b/Shadows/SampleFramework11/Camera.h new file mode 100644 index 0000000..a5ea319 --- /dev/null +++ b/Shadows/SampleFramework11/Camera.h @@ -0,0 +1,141 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "Math.h" + +namespace SampleFramework11 +{ + +// Abstract base class for camera types +class Camera +{ + +protected: + + Float4x4 view; + Float4x4 projection; + Float4x4 viewProjection; + + Float4x4 world; + Float3 position; + Float4 orientation; + + float nearZ; + float farZ; + + virtual void CreateProjection() = 0; + void WorldMatrixChanged(); + +public: + + Camera(float nearZ, float farZ); + ~Camera(); + + const Float4x4& ViewMatrix() const { return view; }; + const Float4x4& ProjectionMatrix() const { return projection; }; + const Float4x4& ViewProjectionMatrix() const { return viewProjection; }; + const Float4x4& WorldMatrix() const { return world; }; + const Float3& Position() const { return position; }; + const Float4& Orientation() const { return orientation; }; + const float& NearClip() const { return nearZ; }; + const float& FarClip() const { return farZ; }; + + Float3 Forward() const; + Float3 Back() const; + Float3 Up() const; + Float3 Down() const; + Float3 Right() const; + Float3 Left() const; + + void SetLookAt(const Float3& eye, const Float3& lookAt, const Float3& up); + void SetWorldMatrix(const Float4x4& newWorld); + void SetPosition(const Float3& newPosition); + void SetOrientation(const Float4& newOrientation); + void SetNearClip(float newNearClip); + void SetFarClip(float newFarClip); + void SetProjection(const Float4x4& newProjection); +}; + +// Camera with an orthographic projection +class OrthographicCamera : public Camera +{ + +protected: + + float xMin; + float xMax; + float yMin; + float yMax; + + virtual void CreateProjection(); + +public: + + OrthographicCamera(float minX, float minY, float maxX, float maxY, float nearClip, float farClip); + ~OrthographicCamera(); + + float MinX() const { return xMin; }; + float MinY() const { return yMin; }; + float MaxX() const { return xMax; }; + float MaxY() const { return yMax; }; + + void SetMinX(float minX); + void SetMinY(float minY); + void SetMaxX(float maxX); + void SetMaxY(float maxY); +}; + +// Camera with a perspective projection +class PerspectiveCamera : public Camera +{ + +protected: + + float aspect; + float fov; + + virtual void CreateProjection(); + +public: + + PerspectiveCamera(float aspectRatio, float fieldOfView, float nearClip, float farClip); + ~PerspectiveCamera(); + + float AspectRatio() const { return aspect; }; + float FieldOfView() const { return fov; }; + + void SetAspectRatio(float aspectRatio); + void SetFieldOfView(float fieldOfView); +}; + +// Perspective camera that rotates about Z and Y axes +class FirstPersonCamera : public PerspectiveCamera +{ + +protected: + + float xRot; + float yRot; + +public: + + FirstPersonCamera(float aspectRatio, float fieldOfView, float nearClip, float farClip); + ~FirstPersonCamera(); + + float XRotation() const { return xRot; }; + float YRotation() const { return yRot; }; + + void SetXRotation(float xRotation); + void SetYRotation(float yRotation); +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/DDSTextureLoader.cpp b/Shadows/SampleFramework11/DDSTextureLoader.cpp new file mode 100644 index 0000000..ee97b82 --- /dev/null +++ b/Shadows/SampleFramework11/DDSTextureLoader.cpp @@ -0,0 +1,1414 @@ +//-------------------------------------------------------------------------------------- +// File: DDSTextureLoader.cpp +// +// Function for loading a DDS texture and creating a Direct3D 11 runtime resource for it +// +// Note these functions are useful as a light-weight runtime loader for DDS files. For +// a full-featured DDS file reader, writer, and texture processing pipeline see +// the 'Texconv' sample and the 'DirectXTex' library. +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// http://go.microsoft.com/fwlink/?LinkId=248926 +// http://go.microsoft.com/fwlink/?LinkId=248929 +//-------------------------------------------------------------------------------------- + +#include "PCH.h" +#include "DDSTextureLoader.h" + +namespace SampleFramework11 +{ + +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) && !defined(DXGI_1_2_FORMATS) +#define DXGI_1_2_FORMATS +#endif + +//-------------------------------------------------------------------------------------- +// Macros +//-------------------------------------------------------------------------------------- +#ifndef MAKEFOURCC + #define MAKEFOURCC(ch0, ch1, ch2, ch3) \ + ((uint32_t)(uint8_t)(ch0) | ((uint32_t)(uint8_t)(ch1) << 8) | \ + ((uint32_t)(uint8_t)(ch2) << 16) | ((uint32_t)(uint8_t)(ch3) << 24 )) +#endif /* defined(MAKEFOURCC) */ + +//-------------------------------------------------------------------------------------- +// DDS file structure definitions +// +// See DDS.h in the 'Texconv' sample and the 'DirectXTex' library +//-------------------------------------------------------------------------------------- +#pragma pack(push,1) + +#define DDS_MAGIC 0x20534444 // "DDS " + +struct DDS_PIXELFORMAT +{ + uint32_t size; + uint32_t flags; + uint32_t fourCC; + uint32_t RGBBitCount; + uint32_t RBitMask; + uint32_t GBitMask; + uint32_t BBitMask; + uint32_t ABitMask; +}; + +#define DDS_FOURCC 0x00000004 // DDPF_FOURCC +#define DDS_RGB 0x00000040 // DDPF_RGB +#define DDS_RGBA 0x00000041 // DDPF_RGB | DDPF_ALPHAPIXELS +#define DDS_LUMINANCE 0x00020000 // DDPF_LUMINANCE +#define DDS_LUMINANCEA 0x00020001 // DDPF_LUMINANCE | DDPF_ALPHAPIXELS +#define DDS_ALPHA 0x00000002 // DDPF_ALPHA +#define DDS_PAL8 0x00000020 // DDPF_PALETTEINDEXED8 + +#define DDS_HEADER_FLAGS_TEXTURE 0x00001007 // DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT +#define DDS_HEADER_FLAGS_MIPMAP 0x00020000 // DDSD_MIPMAPCOUNT +#define DDS_HEADER_FLAGS_VOLUME 0x00800000 // DDSD_DEPTH +#define DDS_HEADER_FLAGS_PITCH 0x00000008 // DDSD_PITCH +#define DDS_HEADER_FLAGS_LINEARSIZE 0x00080000 // DDSD_LINEARSIZE + +#define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT +#define DDS_WIDTH 0x00000004 // DDSD_WIDTH + +#define DDS_SURFACE_FLAGS_TEXTURE 0x00001000 // DDSCAPS_TEXTURE +#define DDS_SURFACE_FLAGS_MIPMAP 0x00400008 // DDSCAPS_COMPLEX | DDSCAPS_MIPMAP +#define DDS_SURFACE_FLAGS_CUBEMAP 0x00000008 // DDSCAPS_COMPLEX + +#define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX +#define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX +#define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY +#define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY +#define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ +#define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ + +#define DDS_CUBEMAP_ALLFACES ( DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\ + DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\ + DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ ) + +#define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP + +#define DDS_FLAGS_VOLUME 0x00200000 // DDSCAPS2_VOLUME + +typedef struct +{ + uint32_t size; + uint32_t flags; + uint32_t height; + uint32_t width; + uint32_t pitchOrLinearSize; + uint32_t depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags + uint32_t mipMapCount; + uint32_t reserved1[11]; + DDS_PIXELFORMAT ddspf; + uint32_t caps; + uint32_t caps2; + uint32_t caps3; + uint32_t caps4; + uint32_t reserved2; +} DDS_HEADER; + +typedef struct +{ + DXGI_FORMAT dxgiFormat; + uint32_t resourceDimension; + uint32_t miscFlag; // see D3D11_RESOURCE_MISC_FLAG + uint32_t arraySize; + uint32_t reserved; +} DDS_HEADER_DXT10; + +#pragma pack(pop) + +//--------------------------------------------------------------------------------- +struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } }; + +typedef public std::unique_ptr ScopedHandle; + +inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; } + +//-------------------------------------------------------------------------------------- +static HRESULT LoadTextureDataFromFile( _In_z_ const wchar_t* fileName, + std::unique_ptr& ddsData, + DDS_HEADER** header, + uint8_t** bitData, + size_t* bitSize + ) +{ + if (!header || !bitData || !bitSize) + { + return E_POINTER; + } + + // open the file +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) + ScopedHandle hFile( safe_handle( CreateFile2( fileName, + GENERIC_READ, + FILE_SHARE_READ, + OPEN_EXISTING, + nullptr ) ) ); +#else + ScopedHandle hFile( safe_handle( CreateFileW( fileName, + GENERIC_READ, + FILE_SHARE_READ, + nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr ) ) ); +#endif + + if ( !hFile ) + { + return HRESULT_FROM_WIN32( GetLastError() ); + } + + // Get the file size + LARGE_INTEGER FileSize = { 0 }; + +#if (_WIN32_WINNT >= _WIN32_WINNT_VISTA) + FILE_STANDARD_INFO fileInfo; + if ( !GetFileInformationByHandleEx( hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo) ) ) + { + return HRESULT_FROM_WIN32( GetLastError() ); + } + FileSize = fileInfo.EndOfFile; +#else + GetFileSizeEx( hFile.get(), &FileSize ); +#endif + + // File is too big for 32-bit allocation, so reject read + if (FileSize.HighPart > 0) + { + return E_FAIL; + } + + // Need at least enough data to fill the header and magic number to be a valid DDS + if (FileSize.LowPart < ( sizeof(DDS_HEADER) + sizeof(uint32_t) ) ) + { + return E_FAIL; + } + + // create enough space for the file data + ddsData.reset( new uint8_t[ FileSize.LowPart ] ); + if (!ddsData ) + { + return E_OUTOFMEMORY; + } + + // read the data in + DWORD BytesRead = 0; + if (!ReadFile( hFile.get(), + ddsData.get(), + FileSize.LowPart, + &BytesRead, + nullptr + )) + { + return HRESULT_FROM_WIN32( GetLastError() ); + } + + if (BytesRead < FileSize.LowPart) + { + return E_FAIL; + } + + // DDS files always start with the same magic number ("DDS ") + uint32_t dwMagicNumber = *( const uint32_t* )( ddsData.get() ); + if (dwMagicNumber != DDS_MAGIC) + { + return E_FAIL; + } + + DDS_HEADER* hdr = reinterpret_cast( ddsData.get() + sizeof( uint32_t ) ); + + // Verify header to validate DDS file + if (hdr->size != sizeof(DDS_HEADER) || + hdr->ddspf.size != sizeof(DDS_PIXELFORMAT)) + { + return E_FAIL; + } + + // Check for DX10 extension + bool bDXT10Header = false; + if ((hdr->ddspf.flags & DDS_FOURCC) && + (MAKEFOURCC( 'D', 'X', '1', '0' ) == hdr->ddspf.fourCC)) + { + // Must be long enough for both headers and magic value + if (FileSize.LowPart < ( sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10) ) ) + { + return E_FAIL; + } + + bDXT10Header = true; + } + + // setup the pointers in the process request + *header = hdr; + ptrdiff_t offset = sizeof( uint32_t ) + sizeof( DDS_HEADER ) + + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0); + *bitData = ddsData.get() + offset; + *bitSize = FileSize.LowPart - offset; + + return S_OK; +} + + +//-------------------------------------------------------------------------------------- +// Return the BPP for a particular format +//-------------------------------------------------------------------------------------- +static size_t BitsPerPixel( _In_ DXGI_FORMAT fmt ) +{ + switch( fmt ) + { + case DXGI_FORMAT_R32G32B32A32_TYPELESS: + case DXGI_FORMAT_R32G32B32A32_FLOAT: + case DXGI_FORMAT_R32G32B32A32_UINT: + case DXGI_FORMAT_R32G32B32A32_SINT: + return 128; + + case DXGI_FORMAT_R32G32B32_TYPELESS: + case DXGI_FORMAT_R32G32B32_FLOAT: + case DXGI_FORMAT_R32G32B32_UINT: + case DXGI_FORMAT_R32G32B32_SINT: + return 96; + + case DXGI_FORMAT_R16G16B16A16_TYPELESS: + case DXGI_FORMAT_R16G16B16A16_FLOAT: + case DXGI_FORMAT_R16G16B16A16_UNORM: + case DXGI_FORMAT_R16G16B16A16_UINT: + case DXGI_FORMAT_R16G16B16A16_SNORM: + case DXGI_FORMAT_R16G16B16A16_SINT: + case DXGI_FORMAT_R32G32_TYPELESS: + case DXGI_FORMAT_R32G32_FLOAT: + case DXGI_FORMAT_R32G32_UINT: + case DXGI_FORMAT_R32G32_SINT: + case DXGI_FORMAT_R32G8X24_TYPELESS: + case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: + case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS: + case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT: + return 64; + + case DXGI_FORMAT_R10G10B10A2_TYPELESS: + case DXGI_FORMAT_R10G10B10A2_UNORM: + case DXGI_FORMAT_R10G10B10A2_UINT: + case DXGI_FORMAT_R11G11B10_FLOAT: + case DXGI_FORMAT_R8G8B8A8_TYPELESS: + case DXGI_FORMAT_R8G8B8A8_UNORM: + case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: + case DXGI_FORMAT_R8G8B8A8_UINT: + case DXGI_FORMAT_R8G8B8A8_SNORM: + case DXGI_FORMAT_R8G8B8A8_SINT: + case DXGI_FORMAT_R16G16_TYPELESS: + case DXGI_FORMAT_R16G16_FLOAT: + case DXGI_FORMAT_R16G16_UNORM: + case DXGI_FORMAT_R16G16_UINT: + case DXGI_FORMAT_R16G16_SNORM: + case DXGI_FORMAT_R16G16_SINT: + case DXGI_FORMAT_R32_TYPELESS: + case DXGI_FORMAT_D32_FLOAT: + case DXGI_FORMAT_R32_FLOAT: + case DXGI_FORMAT_R32_UINT: + case DXGI_FORMAT_R32_SINT: + case DXGI_FORMAT_R24G8_TYPELESS: + case DXGI_FORMAT_D24_UNORM_S8_UINT: + case DXGI_FORMAT_R24_UNORM_X8_TYPELESS: + case DXGI_FORMAT_X24_TYPELESS_G8_UINT: + case DXGI_FORMAT_R9G9B9E5_SHAREDEXP: + case DXGI_FORMAT_R8G8_B8G8_UNORM: + case DXGI_FORMAT_G8R8_G8B8_UNORM: + case DXGI_FORMAT_B8G8R8A8_UNORM: + case DXGI_FORMAT_B8G8R8X8_UNORM: + case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM: + case DXGI_FORMAT_B8G8R8A8_TYPELESS: + case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: + case DXGI_FORMAT_B8G8R8X8_TYPELESS: + case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB: + return 32; + + case DXGI_FORMAT_R8G8_TYPELESS: + case DXGI_FORMAT_R8G8_UNORM: + case DXGI_FORMAT_R8G8_UINT: + case DXGI_FORMAT_R8G8_SNORM: + case DXGI_FORMAT_R8G8_SINT: + case DXGI_FORMAT_R16_TYPELESS: + case DXGI_FORMAT_R16_FLOAT: + case DXGI_FORMAT_D16_UNORM: + case DXGI_FORMAT_R16_UNORM: + case DXGI_FORMAT_R16_UINT: + case DXGI_FORMAT_R16_SNORM: + case DXGI_FORMAT_R16_SINT: + case DXGI_FORMAT_B5G6R5_UNORM: + case DXGI_FORMAT_B5G5R5A1_UNORM: + +#ifdef DXGI_1_2_FORMATS + case DXGI_FORMAT_B4G4R4A4_UNORM: +#endif + return 16; + + case DXGI_FORMAT_R8_TYPELESS: + case DXGI_FORMAT_R8_UNORM: + case DXGI_FORMAT_R8_UINT: + case DXGI_FORMAT_R8_SNORM: + case DXGI_FORMAT_R8_SINT: + case DXGI_FORMAT_A8_UNORM: + return 8; + + case DXGI_FORMAT_R1_UNORM: + return 1; + + case DXGI_FORMAT_BC1_TYPELESS: + case DXGI_FORMAT_BC1_UNORM: + case DXGI_FORMAT_BC1_UNORM_SRGB: + case DXGI_FORMAT_BC4_TYPELESS: + case DXGI_FORMAT_BC4_UNORM: + case DXGI_FORMAT_BC4_SNORM: + return 4; + + case DXGI_FORMAT_BC2_TYPELESS: + case DXGI_FORMAT_BC2_UNORM: + case DXGI_FORMAT_BC2_UNORM_SRGB: + case DXGI_FORMAT_BC3_TYPELESS: + case DXGI_FORMAT_BC3_UNORM: + case DXGI_FORMAT_BC3_UNORM_SRGB: + case DXGI_FORMAT_BC5_TYPELESS: + case DXGI_FORMAT_BC5_UNORM: + case DXGI_FORMAT_BC5_SNORM: + case DXGI_FORMAT_BC6H_TYPELESS: + case DXGI_FORMAT_BC6H_UF16: + case DXGI_FORMAT_BC6H_SF16: + case DXGI_FORMAT_BC7_TYPELESS: + case DXGI_FORMAT_BC7_UNORM: + case DXGI_FORMAT_BC7_UNORM_SRGB: + return 8; + + default: + return 0; + } +} + + +//-------------------------------------------------------------------------------------- +// Get surface information for a particular format +//-------------------------------------------------------------------------------------- +static void GetSurfaceInfo( _In_ size_t width, + _In_ size_t height, + _In_ DXGI_FORMAT fmt, + _Out_opt_ size_t* outNumBytes, + _Out_opt_ size_t* outRowBytes, + _Out_opt_ size_t* outNumRows ) +{ + size_t numBytes = 0; + size_t rowBytes = 0; + size_t numRows = 0; + + bool bc = false; + bool packed = false; + size_t bcnumBytesPerBlock = 0; + switch (fmt) + { + case DXGI_FORMAT_BC1_TYPELESS: + case DXGI_FORMAT_BC1_UNORM: + case DXGI_FORMAT_BC1_UNORM_SRGB: + case DXGI_FORMAT_BC4_TYPELESS: + case DXGI_FORMAT_BC4_UNORM: + case DXGI_FORMAT_BC4_SNORM: + bc=true; + bcnumBytesPerBlock = 8; + break; + + case DXGI_FORMAT_BC2_TYPELESS: + case DXGI_FORMAT_BC2_UNORM: + case DXGI_FORMAT_BC2_UNORM_SRGB: + case DXGI_FORMAT_BC3_TYPELESS: + case DXGI_FORMAT_BC3_UNORM: + case DXGI_FORMAT_BC3_UNORM_SRGB: + case DXGI_FORMAT_BC5_TYPELESS: + case DXGI_FORMAT_BC5_UNORM: + case DXGI_FORMAT_BC5_SNORM: + case DXGI_FORMAT_BC6H_TYPELESS: + case DXGI_FORMAT_BC6H_UF16: + case DXGI_FORMAT_BC6H_SF16: + case DXGI_FORMAT_BC7_TYPELESS: + case DXGI_FORMAT_BC7_UNORM: + case DXGI_FORMAT_BC7_UNORM_SRGB: + bc = true; + bcnumBytesPerBlock = 16; + break; + + case DXGI_FORMAT_R8G8_B8G8_UNORM: + case DXGI_FORMAT_G8R8_G8B8_UNORM: + packed = true; + break; + } + + if (bc) + { + size_t numBlocksWide = 0; + if (width > 0) + { + numBlocksWide = std::max( 1, (width + 3) / 4 ); + } + size_t numBlocksHigh = 0; + if (height > 0) + { + numBlocksHigh = std::max( 1, (height + 3) / 4 ); + } + rowBytes = numBlocksWide * bcnumBytesPerBlock; + numRows = numBlocksHigh; + } + else if (packed) + { + rowBytes = ( ( width + 1 ) >> 1 ) * 4; + numRows = height; + } + else + { + size_t bpp = BitsPerPixel( fmt ); + rowBytes = ( width * bpp + 7 ) / 8; // round up to nearest byte + numRows = height; + } + + numBytes = rowBytes * numRows; + if (outNumBytes) + { + *outNumBytes = numBytes; + } + if (outRowBytes) + { + *outRowBytes = rowBytes; + } + if (outNumRows) + { + *outNumRows = numRows; + } +} + + +//-------------------------------------------------------------------------------------- +#define ISBITMASK( r,g,b,a ) ( ddpf.RBitMask == r && ddpf.GBitMask == g && ddpf.BBitMask == b && ddpf.ABitMask == a ) + +static DXGI_FORMAT GetDXGIFormat( const DDS_PIXELFORMAT& ddpf ) +{ + if (ddpf.flags & DDS_RGB) + { + // Note that sRGB formats are written using the "DX10" extended header + + switch (ddpf.RGBBitCount) + { + case 32: + if (ISBITMASK(0x000000ff,0x0000ff00,0x00ff0000,0xff000000)) + { + return DXGI_FORMAT_R8G8B8A8_UNORM; + } + + if (ISBITMASK(0x00ff0000,0x0000ff00,0x000000ff,0xff000000)) + { + return DXGI_FORMAT_B8G8R8A8_UNORM; + } + + if (ISBITMASK(0x00ff0000,0x0000ff00,0x000000ff,0x00000000)) + { + return DXGI_FORMAT_B8G8R8X8_UNORM; + } + + // No DXGI format maps to ISBITMASK(0x000000ff,0x0000ff00,0x00ff0000,0x00000000) aka D3DFMT_X8B8G8R8 + + // Note that many common DDS reader/writers (including D3DX) swap the + // the RED/BLUE masks for 10:10:10:2 formats. We assumme + // below that the 'backwards' header mask is being used since it is most + // likely written by D3DX. The more robust solution is to use the 'DX10' + // header extension and specify the DXGI_FORMAT_R10G10B10A2_UNORM format directly + + // For 'correct' writers, this should be 0x000003ff,0x000ffc00,0x3ff00000 for RGB data + if (ISBITMASK(0x3ff00000,0x000ffc00,0x000003ff,0xc0000000)) + { + return DXGI_FORMAT_R10G10B10A2_UNORM; + } + + // No DXGI format maps to ISBITMASK(0x000003ff,0x000ffc00,0x3ff00000,0xc0000000) aka D3DFMT_A2R10G10B10 + + if (ISBITMASK(0x0000ffff,0xffff0000,0x00000000,0x00000000)) + { + return DXGI_FORMAT_R16G16_UNORM; + } + + if (ISBITMASK(0xffffffff,0x00000000,0x00000000,0x00000000)) + { + // Only 32-bit color channel format in D3D9 was R32F + return DXGI_FORMAT_R32_FLOAT; // D3DX writes this out as a FourCC of 114 + } + break; + + case 24: + // No 24bpp DXGI formats aka D3DFMT_R8G8B8 + break; + + case 16: + if (ISBITMASK(0x7c00,0x03e0,0x001f,0x8000)) + { + return DXGI_FORMAT_B5G5R5A1_UNORM; + } + if (ISBITMASK(0xf800,0x07e0,0x001f,0x0000)) + { + return DXGI_FORMAT_B5G6R5_UNORM; + } + + // No DXGI format maps to ISBITMASK(0x7c00,0x03e0,0x001f,0x0000) aka D3DFMT_X1R5G5B5 + +#ifdef DXGI_1_2_FORMATS + if (ISBITMASK(0x0f00,0x00f0,0x000f,0xf000)) + { + return DXGI_FORMAT_B4G4R4A4_UNORM; + } + + // No DXGI format maps to ISBITMASK(0x0f00,0x00f0,0x000f,0x0000) aka D3DFMT_X4R4G4B4 +#endif + + // No 3:3:2, 3:3:2:8, or paletted DXGI formats aka D3DFMT_A8R3G3B2, D3DFMT_R3G3B2, D3DFMT_P8, D3DFMT_A8P8, etc. + break; + } + } + else if (ddpf.flags & DDS_LUMINANCE) + { + if (8 == ddpf.RGBBitCount) + { + if (ISBITMASK(0x000000ff,0x00000000,0x00000000,0x00000000)) + { + return DXGI_FORMAT_R8_UNORM; // D3DX10/11 writes this out as DX10 extension + } + + // No DXGI format maps to ISBITMASK(0x0f,0x00,0x00,0xf0) aka D3DFMT_A4L4 + } + + if (16 == ddpf.RGBBitCount) + { + if (ISBITMASK(0x0000ffff,0x00000000,0x00000000,0x00000000)) + { + return DXGI_FORMAT_R16_UNORM; // D3DX10/11 writes this out as DX10 extension + } + if (ISBITMASK(0x000000ff,0x00000000,0x00000000,0x0000ff00)) + { + return DXGI_FORMAT_R8G8_UNORM; // D3DX10/11 writes this out as DX10 extension + } + } + } + else if (ddpf.flags & DDS_ALPHA) + { + if (8 == ddpf.RGBBitCount) + { + return DXGI_FORMAT_A8_UNORM; + } + } + else if (ddpf.flags & DDS_FOURCC) + { + if (MAKEFOURCC( 'D', 'X', 'T', '1' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC1_UNORM; + } + if (MAKEFOURCC( 'D', 'X', 'T', '3' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC2_UNORM; + } + if (MAKEFOURCC( 'D', 'X', 'T', '5' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC3_UNORM; + } + + // While pre-mulitplied alpha isn't directly supported by the DXGI formats, + // they are basically the same as these BC formats so they can be mapped + if (MAKEFOURCC( 'D', 'X', 'T', '2' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC2_UNORM; + } + if (MAKEFOURCC( 'D', 'X', 'T', '4' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC3_UNORM; + } + + if (MAKEFOURCC( 'A', 'T', 'I', '1' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC4_UNORM; + } + if (MAKEFOURCC( 'B', 'C', '4', 'U' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC4_UNORM; + } + if (MAKEFOURCC( 'B', 'C', '4', 'S' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC4_SNORM; + } + + if (MAKEFOURCC( 'A', 'T', 'I', '2' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC5_UNORM; + } + if (MAKEFOURCC( 'B', 'C', '5', 'U' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC5_UNORM; + } + if (MAKEFOURCC( 'B', 'C', '5', 'S' ) == ddpf.fourCC) + { + return DXGI_FORMAT_BC5_SNORM; + } + + // BC6H and BC7 are written using the "DX10" extended header + + if (MAKEFOURCC( 'R', 'G', 'B', 'G' ) == ddpf.fourCC) + { + return DXGI_FORMAT_R8G8_B8G8_UNORM; + } + if (MAKEFOURCC( 'G', 'R', 'G', 'B' ) == ddpf.fourCC) + { + return DXGI_FORMAT_G8R8_G8B8_UNORM; + } + + // Check for D3DFORMAT enums being set here + switch( ddpf.fourCC ) + { + case 36: // D3DFMT_A16B16G16R16 + return DXGI_FORMAT_R16G16B16A16_UNORM; + + case 110: // D3DFMT_Q16W16V16U16 + return DXGI_FORMAT_R16G16B16A16_SNORM; + + case 111: // D3DFMT_R16F + return DXGI_FORMAT_R16_FLOAT; + + case 112: // D3DFMT_G16R16F + return DXGI_FORMAT_R16G16_FLOAT; + + case 113: // D3DFMT_A16B16G16R16F + return DXGI_FORMAT_R16G16B16A16_FLOAT; + + case 114: // D3DFMT_R32F + return DXGI_FORMAT_R32_FLOAT; + + case 115: // D3DFMT_G32R32F + return DXGI_FORMAT_R32G32_FLOAT; + + case 116: // D3DFMT_A32B32G32R32F + return DXGI_FORMAT_R32G32B32A32_FLOAT; + } + } + + return DXGI_FORMAT_UNKNOWN; +} + + +//-------------------------------------------------------------------------------------- +static HRESULT FillInitData( _In_ size_t width, + _In_ size_t height, + _In_ size_t depth, + _In_ size_t mipCount, + _In_ size_t arraySize, + _In_ DXGI_FORMAT format, + _In_ size_t maxsize, + _In_ size_t bitSize, + _In_bytecount_(bitSize) const uint8_t* bitData, + _Out_ size_t& twidth, + _Out_ size_t& theight, + _Out_ size_t& tdepth, + _Out_ size_t& skipMip, + _Out_cap_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData ) +{ + if ( !bitData || !initData ) + return E_POINTER; + + skipMip = 0; + twidth = 0; + theight = 0; + tdepth = 0; + + size_t NumBytes = 0; + size_t RowBytes = 0; + size_t NumRows = 0; + const uint8_t* pSrcBits = bitData; + const uint8_t* pEndBits = bitData + bitSize; + + size_t index = 0; + for( size_t j = 0; j < arraySize; j++ ) + { + size_t w = width; + size_t h = height; + size_t d = depth; + for( size_t i = 0; i < mipCount; i++ ) + { + GetSurfaceInfo( w, + h, + format, + &NumBytes, + &RowBytes, + &NumRows + ); + + if ( (mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize) ) + { + if ( !twidth ) + { + twidth = w; + theight = h; + tdepth = d; + } + + initData[index].pSysMem = ( const void* )pSrcBits; + initData[index].SysMemPitch = static_cast( RowBytes ); + initData[index].SysMemSlicePitch = static_cast( NumBytes ); + ++index; + } + else + ++skipMip; + + if (pSrcBits + (NumBytes*d) > pEndBits) + { + return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF ); + } + + pSrcBits += NumBytes * d; + + w = w >> 1; + h = h >> 1; + d = d >> 1; + if (w == 0) + { + w = 1; + } + if (h == 0) + { + h = 1; + } + if (d == 0) + { + d = 1; + } + } + } + + return (index > 0) ? S_OK : E_FAIL; +} + + +//-------------------------------------------------------------------------------------- +static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice, + _In_ uint32_t resDim, + _In_ size_t width, + _In_ size_t height, + _In_ size_t depth, + _In_ size_t mipCount, + _In_ size_t arraySize, + _In_ DXGI_FORMAT format, + _In_ bool isCubeMap, + _In_count_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView ) +{ + if ( !d3dDevice || !initData ) + return E_POINTER; + + HRESULT hr = E_FAIL; + + switch ( resDim ) + { + case D3D11_RESOURCE_DIMENSION_TEXTURE1D: + { + D3D11_TEXTURE1D_DESC desc; + desc.Width = static_cast( width ); + desc.MipLevels = static_cast( mipCount ); + desc.ArraySize = static_cast( arraySize ); + desc.Format = format; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + desc.MiscFlags = 0; + + ID3D11Texture1D* tex = nullptr; + hr = d3dDevice->CreateTexture1D( &desc, + initData, + &tex + ); + if (SUCCEEDED( hr ) && tex != 0) + { + if (textureView != 0) + { + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; + memset( &SRVDesc, 0, sizeof( SRVDesc ) ); + SRVDesc.Format = format; + + if (arraySize > 1) + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY; + SRVDesc.Texture1DArray.MipLevels = desc.MipLevels; + SRVDesc.Texture1DArray.ArraySize = static_cast( arraySize ); + } + else + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D; + SRVDesc.Texture1D.MipLevels = desc.MipLevels; + } + + hr = d3dDevice->CreateShaderResourceView( tex, + &SRVDesc, + textureView + ); + if ( FAILED(hr) ) + { + tex->Release(); + return hr; + } + } + + if (texture != 0) + { + *texture = tex; + } + else + { +#if defined(_DEBUG) || defined(PROFILE) + tex->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("DDSTextureLoader")-1, + "DDSTextureLoader" ); +#endif + tex->Release(); + } + } + } + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE2D: + { + D3D11_TEXTURE2D_DESC desc; + desc.Width = static_cast( width ); + desc.Height = static_cast( height ); + desc.MipLevels = static_cast( mipCount ); + desc.ArraySize = static_cast( arraySize ); + desc.Format = format; + desc.SampleDesc.Count = 1; + desc.SampleDesc.Quality = 0; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + desc.MiscFlags = (isCubeMap) ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0; + + ID3D11Texture2D* tex = nullptr; + hr = d3dDevice->CreateTexture2D( &desc, + initData, + &tex + ); + if (SUCCEEDED( hr ) && tex != 0) + { + if (textureView != 0) + { + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; + memset( &SRVDesc, 0, sizeof( SRVDesc ) ); + SRVDesc.Format = format; + + if (isCubeMap) + { + if (arraySize > 6) + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY; + SRVDesc.TextureCubeArray.MipLevels = desc.MipLevels; + + // Earlier we set arraySize to (NumCubes * 6) + SRVDesc.TextureCubeArray.NumCubes = static_cast( arraySize / 6 ); + } + else + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; + SRVDesc.TextureCube.MipLevels = desc.MipLevels; + } + } + else if (arraySize > 1) + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + SRVDesc.Texture2DArray.MipLevels = desc.MipLevels; + SRVDesc.Texture2DArray.ArraySize = static_cast( arraySize ); + } + else + { + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + SRVDesc.Texture2D.MipLevels = desc.MipLevels; + } + + hr = d3dDevice->CreateShaderResourceView( tex, + &SRVDesc, + textureView + ); + if ( FAILED(hr) ) + { + tex->Release(); + return hr; + } + } + + if (texture != 0) + { + *texture = tex; + } + else + { +#if defined(_DEBUG) || defined(PROFILE) + tex->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("DDSTextureLoader")-1, + "DDSTextureLoader" + ); +#endif + tex->Release(); + } + } + } + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + { + D3D11_TEXTURE3D_DESC desc; + desc.Width = static_cast( width ); + desc.Height = static_cast( height ); + desc.Depth = static_cast( depth ); + desc.MipLevels = static_cast( mipCount ); + desc.Format = format; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + desc.MiscFlags = 0; + + ID3D11Texture3D* tex = nullptr; + hr = d3dDevice->CreateTexture3D( &desc, + initData, + &tex + ); + if (SUCCEEDED( hr ) && tex != 0) + { + if (textureView != 0) + { + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; + memset( &SRVDesc, 0, sizeof( SRVDesc ) ); + SRVDesc.Format = format; + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; + SRVDesc.Texture3D.MipLevels = desc.MipLevels; + + hr = d3dDevice->CreateShaderResourceView( tex, + &SRVDesc, + textureView + ); + if ( FAILED(hr) ) + { + tex->Release(); + return hr; + } + } + + if (texture != 0) + { + *texture = tex; + } + else + { +#if defined(_DEBUG) || defined(PROFILE) + tex->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("DDSTextureLoader")-1, + "DDSTextureLoader" ); +#endif + tex->Release(); + } + } + } + break; + } + + return hr; +} + + +//-------------------------------------------------------------------------------------- +static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice, + _In_ const DDS_HEADER* header, + _In_bytecount_(bitSize) const uint8_t* bitData, + _In_ size_t bitSize, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize ) +{ + HRESULT hr = S_OK; + + size_t width = header->width; + size_t height = header->height; + size_t depth = header->depth; + + uint32_t resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN; + size_t arraySize = 1; + DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN; + bool isCubeMap = false; + + size_t mipCount = header->mipMapCount; + if (0 == mipCount) + { + mipCount = 1; + } + + if ((header->ddspf.flags & DDS_FOURCC) && + (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC )) + { + const DDS_HEADER_DXT10* d3d10ext = reinterpret_cast( (const char*)header + sizeof(DDS_HEADER) ); + + arraySize = d3d10ext->arraySize; + if (arraySize == 0) + { + return HRESULT_FROM_WIN32( ERROR_INVALID_DATA ); + } + + if (BitsPerPixel( d3d10ext->dxgiFormat ) == 0) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + + format = d3d10ext->dxgiFormat; + + switch ( d3d10ext->resourceDimension ) + { + case D3D11_RESOURCE_DIMENSION_TEXTURE1D: + // D3DX writes 1D textures with a fixed Height of 1 + if ((header->flags & DDS_HEIGHT) && height != 1) + { + return HRESULT_FROM_WIN32( ERROR_INVALID_DATA ); + } + height = depth = 1; + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE2D: + if (d3d10ext->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE) + { + arraySize *= 6; + isCubeMap = true; + } + depth = 1; + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + if (!(header->flags & DDS_HEADER_FLAGS_VOLUME)) + { + return HRESULT_FROM_WIN32( ERROR_INVALID_DATA ); + } + + if (arraySize > 1) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + break; + + default: + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + + resDim = d3d10ext->resourceDimension; + } + else + { + format = GetDXGIFormat( header->ddspf ); + + if (format == DXGI_FORMAT_UNKNOWN) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + + if (header->flags & DDS_HEADER_FLAGS_VOLUME) + { + resDim = D3D11_RESOURCE_DIMENSION_TEXTURE3D; + } + else + { + if (header->caps2 & DDS_CUBEMAP) + { + // We require all six faces to be defined + if ((header->caps2 & DDS_CUBEMAP_ALLFACES ) != DDS_CUBEMAP_ALLFACES) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + + arraySize = 6; + isCubeMap = true; + } + + depth = 1; + resDim = D3D11_RESOURCE_DIMENSION_TEXTURE2D; + + // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture + } + + assert( BitsPerPixel( format ) != 0 ); + } + + // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements) + if (mipCount > D3D11_REQ_MIP_LEVELS) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + + switch ( resDim ) + { + case D3D11_RESOURCE_DIMENSION_TEXTURE1D: + if ((arraySize > D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION) || + (width > D3D11_REQ_TEXTURE1D_U_DIMENSION) ) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE2D: + if (isCubeMap) + { + // This is the right bound because we set arraySize to (NumCubes*6) above + if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || + (width > D3D11_REQ_TEXTURECUBE_DIMENSION) || + (height > D3D11_REQ_TEXTURECUBE_DIMENSION)) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + } + else if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) || + (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) || + (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + break; + + case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + if ((arraySize > 1) || + (width > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) || + (height > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) || + (depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ) + { + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + break; + } + + // Create the texture + std::unique_ptr initData( new D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] ); + if ( !initData ) + { + return E_OUTOFMEMORY; + } + + size_t skipMip = 0; + size_t twidth = 0; + size_t theight = 0; + size_t tdepth = 0; + hr = FillInitData( width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, + twidth, theight, tdepth, skipMip, initData.get() ); + + if ( SUCCEEDED(hr) ) + { + hr = CreateD3DResources( d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize, format, isCubeMap, initData.get(), texture, textureView ); + + if ( FAILED(hr) && !maxsize && (mipCount > 1) ) + { + // Retry with a maxsize determined by feature level + switch( d3dDevice->GetFeatureLevel() ) + { + case D3D_FEATURE_LEVEL_9_1: + case D3D_FEATURE_LEVEL_9_2: + if (isCubeMap) + { + maxsize = 512 /*D3D_FL9_1_REQ_TEXTURECUBE_DIMENSION*/; + } + else + { + maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) + ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ + : 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + } + break; + + case D3D_FEATURE_LEVEL_9_3: + maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) + ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ + : 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; + + default: // D3D_FEATURE_LEVEL_10_0 & D3D_FEATURE_LEVEL_10_1 + maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D) + ? 2048 /*D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/ + : 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; + } + + hr = FillInitData( width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, + twidth, theight, tdepth, skipMip, initData.get() ); + if ( SUCCEEDED(hr) ) + { + hr = CreateD3DResources( d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize, format, isCubeMap, initData.get(), texture, textureView ); + } + } + } + + return hr; +} + +//-------------------------------------------------------------------------------------- +HRESULT CreateDDSTextureFromMemory( _In_ ID3D11Device* d3dDevice, + _In_bytecount_(ddsDataSize) const uint8_t* ddsData, + _In_ size_t ddsDataSize, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize ) +{ + if (!d3dDevice || !ddsData || (!texture && !textureView)) + { + return E_INVALIDARG; + } + + // Validate DDS file in memory + if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER))) + { + return E_FAIL; + } + + uint32_t dwMagicNumber = *( const uint32_t* )( ddsData ); + if (dwMagicNumber != DDS_MAGIC) + { + return E_FAIL; + } + + const DDS_HEADER* header = reinterpret_cast( ddsData + sizeof( uint32_t ) ); + + // Verify header to validate DDS file + if (header->size != sizeof(DDS_HEADER) || + header->ddspf.size != sizeof(DDS_PIXELFORMAT)) + { + return E_FAIL; + } + + // Check for DX10 extension + bool bDXT10Header = false; + if ((header->ddspf.flags & DDS_FOURCC) && + (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC) ) + { + // Must be long enough for both headers and magic value + if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10))) + { + return E_FAIL; + } + + bDXT10Header = true; + } + + ptrdiff_t offset = sizeof( uint32_t ) + + sizeof( DDS_HEADER ) + + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0); + + HRESULT hr = CreateTextureFromDDS( d3dDevice, + header, + ddsData + offset, + ddsDataSize - offset, + texture, + textureView, + maxsize + ); + +#if defined(_DEBUG) || defined(PROFILE) + if (texture != 0 && *texture != 0) + { + (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("DDSTextureLoader")-1, + "DDSTextureLoader" + ); + } + + if (textureView != 0 && *textureView != 0) + { + (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("DDSTextureLoader")-1, + "DDSTextureLoader" + ); + } +#endif + + return hr; +} + +//-------------------------------------------------------------------------------------- +HRESULT CreateDDSTextureFromFile( _In_ ID3D11Device* d3dDevice, + _In_z_ const wchar_t* fileName, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize ) +{ + if (!d3dDevice || !fileName || (!texture && !textureView)) + { + return E_INVALIDARG; + } + + DDS_HEADER* header = nullptr; + uint8_t* bitData = nullptr; + size_t bitSize = 0; + + std::unique_ptr ddsData; + HRESULT hr = LoadTextureDataFromFile( fileName, + ddsData, + &header, + &bitData, + &bitSize + ); + if (FAILED(hr)) + { + return hr; + } + + hr = CreateTextureFromDDS( d3dDevice, + header, + bitData, + bitSize, + texture, + textureView, + maxsize + ); + +#if defined(_DEBUG) || defined(PROFILE) + if (texture != 0 || textureView != 0) + { + CHAR strFileA[MAX_PATH]; + WideCharToMultiByte( CP_ACP, + WC_NO_BEST_FIT_CHARS, + fileName, + -1, + strFileA, + MAX_PATH, + nullptr, + FALSE + ); + const CHAR* pstrName = strrchr( strFileA, '\\' ); + if (!pstrName) + { + pstrName = strFileA; + } + else + { + pstrName++; + } + + if (texture != 0 && *texture != 0) + { + (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName, + static_cast( strnlen_s(pstrName, MAX_PATH) ), + pstrName + ); + } + + if (textureView != 0 && *textureView != 0 ) + { + (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName, + static_cast( strnlen_s(pstrName, MAX_PATH) ), + pstrName + ); + } + } +#endif + + return hr; +} + +} diff --git a/Shadows/SampleFramework11/DDSTextureLoader.h b/Shadows/SampleFramework11/DDSTextureLoader.h new file mode 100644 index 0000000..f7bc089 --- /dev/null +++ b/Shadows/SampleFramework11/DDSTextureLoader.h @@ -0,0 +1,43 @@ +//-------------------------------------------------------------------------------------- +// File: DDSTextureLoader.h +// +// Function for loading a DDS texture and creating a Direct3D 11 runtime resource for it +// +// Note these functions are useful as a light-weight runtime loader for DDS files. For +// a full-featured DDS file reader, writer, and texture processing pipeline see +// the 'Texconv' sample and the 'DirectXTex' library. +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// http://go.microsoft.com/fwlink/?LinkId=248926 +// http://go.microsoft.com/fwlink/?LinkId=248929 +//-------------------------------------------------------------------------------------- + +#pragma once + +#include "PCH.h" + +namespace SampleFramework11 +{ + +HRESULT CreateDDSTextureFromMemory( _In_ ID3D11Device* d3dDevice, + _In_bytecount_(ddsDataSize) const uint8_t* ddsData, + _In_ size_t ddsDataSize, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0 + ); + +HRESULT CreateDDSTextureFromFile( _In_ ID3D11Device* d3dDevice, + _In_z_ const wchar_t* szFileName, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0 + ); + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/DXErr.cpp b/Shadows/SampleFramework11/DXErr.cpp new file mode 100644 index 0000000..02b0c1c --- /dev/null +++ b/Shadows/SampleFramework11/DXErr.cpp @@ -0,0 +1,3275 @@ +//-------------------------------------------------------------------------------------- +// File: DXErr.cpp +// +// DirectX Error Library +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//-------------------------------------------------------------------------------------- + +// This version only supports UNICODE. +#include "PCH.h" +#include "dxerr.h" + +#define XAUDIO2_E_INVALID_CALL 0x88960001 +#define XAUDIO2_E_XMA_DECODER_ERROR 0x88960002 +#define XAUDIO2_E_XAPO_CREATION_FAILED 0x88960003 +#define XAUDIO2_E_DEVICE_INVALIDATED 0x88960004 + +#define XAPO_E_FORMAT_UNSUPPORTED MAKE_HRESULT(SEVERITY_ERROR, 0x897, 0x01) + +#define DXUTERR_NODIRECT3D MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0901) +#define DXUTERR_NOCOMPATIBLEDEVICES MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0902) +#define DXUTERR_MEDIANOTFOUND MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0903) +#define DXUTERR_NONZEROREFCOUNT MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0904) +#define DXUTERR_CREATINGDEVICE MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0905) +#define DXUTERR_RESETTINGDEVICE MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0906) +#define DXUTERR_CREATINGDEVICEOBJECTS MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0907) +#define DXUTERR_RESETTINGDEVICEOBJECTS MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0908) +#define DXUTERR_INCORRECTVERSION MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0909) +#define DXUTERR_DEVICEREMOVED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x090A) + +//----------------------------------------------------------------------------- +#define BUFFER_SIZE 3000 + +#pragma warning( disable : 6001 6221 ) + +//-------------------------------------------------------------------------------------- +#define CHK_ERR(hrchk, strOut) \ + case hrchk: \ + return L##strOut; + +#define CHK_ERRA(hrchk) \ + case hrchk: \ + return L#hrchk; + +#define HRESULT_FROM_WIN32b(x) ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000))) + +#define CHK_ERR_WIN32A(hrchk) \ + case HRESULT_FROM_WIN32b(hrchk): \ + case hrchk: \ + return L#hrchk; + +#define CHK_ERR_WIN32_ONLY(hrchk, strOut) \ + case HRESULT_FROM_WIN32b(hrchk): \ + return L##strOut; + +//----------------------------------------------------- +const wchar* WINAPI DXGetErrorStringW( _In_ HRESULT hr ) +{ + switch(hr) + { +// Commmented out codes are actually alises for other codes + +// ------------------------------------------------------------- +// Common Win32 error codes +// ------------------------------------------------------------- + CHK_ERRA(S_OK) + CHK_ERRA(S_FALSE) + + CHK_ERRA(E_UNEXPECTED) + CHK_ERRA(E_NOTIMPL) + CHK_ERRA(E_OUTOFMEMORY) + CHK_ERRA(E_INVALIDARG) + CHK_ERRA(E_NOINTERFACE) + CHK_ERRA(E_POINTER) + CHK_ERRA(E_HANDLE) + CHK_ERRA(E_ABORT) + CHK_ERRA(E_FAIL) + CHK_ERRA(E_ACCESSDENIED) + CHK_ERRA(E_PENDING) + CHK_ERRA(CO_E_INIT_TLS) + CHK_ERRA(CO_E_INIT_SHARED_ALLOCATOR) + CHK_ERRA(CO_E_INIT_MEMORY_ALLOCATOR) + CHK_ERRA(CO_E_INIT_CLASS_CACHE) + CHK_ERRA(CO_E_INIT_RPC_CHANNEL) + CHK_ERRA(CO_E_INIT_TLS_SET_CHANNEL_CONTROL) + CHK_ERRA(CO_E_INIT_TLS_CHANNEL_CONTROL) + CHK_ERRA(CO_E_INIT_UNACCEPTED_USER_ALLOCATOR) + CHK_ERRA(CO_E_INIT_SCM_MUTEX_EXISTS) + CHK_ERRA(CO_E_INIT_SCM_FILE_MAPPING_EXISTS) + CHK_ERRA(CO_E_INIT_SCM_MAP_VIEW_OF_FILE) + CHK_ERRA(CO_E_INIT_SCM_EXEC_FAILURE) + CHK_ERRA(CO_E_INIT_ONLY_SINGLE_THREADED) + CHK_ERRA(CO_E_CANT_REMOTE) + CHK_ERRA(CO_E_BAD_SERVER_NAME) + CHK_ERRA(CO_E_WRONG_SERVER_IDENTITY) + CHK_ERRA(CO_E_OLE1DDE_DISABLED) + CHK_ERRA(CO_E_RUNAS_SYNTAX) + CHK_ERRA(CO_E_CREATEPROCESS_FAILURE) + CHK_ERRA(CO_E_RUNAS_CREATEPROCESS_FAILURE) + CHK_ERRA(CO_E_RUNAS_LOGON_FAILURE) + CHK_ERRA(CO_E_LAUNCH_PERMSSION_DENIED) + CHK_ERRA(CO_E_START_SERVICE_FAILURE) + CHK_ERRA(CO_E_REMOTE_COMMUNICATION_FAILURE) + CHK_ERRA(CO_E_SERVER_START_TIMEOUT) + CHK_ERRA(CO_E_CLSREG_INCONSISTENT) + CHK_ERRA(CO_E_IIDREG_INCONSISTENT) + CHK_ERRA(CO_E_NOT_SUPPORTED) + CHK_ERRA(CO_E_RELOAD_DLL) + CHK_ERRA(CO_E_MSI_ERROR) + CHK_ERRA(OLE_E_FIRST) + CHK_ERRA(OLE_E_LAST) + CHK_ERRA(OLE_S_FIRST) + CHK_ERRA(OLE_S_LAST) +// CHK_ERRA(OLE_E_OLEVERB) + CHK_ERRA(OLE_E_ADVF) + CHK_ERRA(OLE_E_ENUM_NOMORE) + CHK_ERRA(OLE_E_ADVISENOTSUPPORTED) + CHK_ERRA(OLE_E_NOCONNECTION) + CHK_ERRA(OLE_E_NOTRUNNING) + CHK_ERRA(OLE_E_NOCACHE) + CHK_ERRA(OLE_E_BLANK) + CHK_ERRA(OLE_E_CLASSDIFF) + CHK_ERRA(OLE_E_CANT_GETMONIKER) + CHK_ERRA(OLE_E_CANT_BINDTOSOURCE) + CHK_ERRA(OLE_E_STATIC) + CHK_ERRA(OLE_E_PROMPTSAVECANCELLED) + CHK_ERRA(OLE_E_INVALIDRECT) + CHK_ERRA(OLE_E_WRONGCOMPOBJ) + CHK_ERRA(OLE_E_INVALIDHWND) + CHK_ERRA(OLE_E_NOT_INPLACEACTIVE) + CHK_ERRA(OLE_E_CANTCONVERT) + CHK_ERRA(OLE_E_NOSTORAGE) + CHK_ERRA(DV_E_FORMATETC) + CHK_ERRA(DV_E_DVTARGETDEVICE) + CHK_ERRA(DV_E_STGMEDIUM) + CHK_ERRA(DV_E_STATDATA) + CHK_ERRA(DV_E_LINDEX) + CHK_ERRA(DV_E_TYMED) + CHK_ERRA(DV_E_CLIPFORMAT) + CHK_ERRA(DV_E_DVASPECT) + CHK_ERRA(DV_E_DVTARGETDEVICE_SIZE) + CHK_ERRA(DV_E_NOIVIEWOBJECT) + CHK_ERRA(DRAGDROP_E_FIRST) + CHK_ERRA(DRAGDROP_E_LAST) + CHK_ERRA(DRAGDROP_S_FIRST) + CHK_ERRA(DRAGDROP_S_LAST) +// CHK_ERRA(DRAGDROP_E_NOTREGISTERED) + CHK_ERRA(DRAGDROP_E_ALREADYREGISTERED) + CHK_ERRA(DRAGDROP_E_INVALIDHWND) + CHK_ERRA(CLASSFACTORY_E_FIRST) + CHK_ERRA(CLASSFACTORY_E_LAST) + CHK_ERRA(CLASSFACTORY_S_FIRST) + CHK_ERRA(CLASSFACTORY_S_LAST) +// CHK_ERRA(CLASS_E_NOAGGREGATION) + CHK_ERRA(CLASS_E_CLASSNOTAVAILABLE) + CHK_ERRA(CLASS_E_NOTLICENSED) + CHK_ERRA(MARSHAL_E_FIRST) + CHK_ERRA(MARSHAL_E_LAST) + CHK_ERRA(MARSHAL_S_FIRST) + CHK_ERRA(MARSHAL_S_LAST) + CHK_ERRA(DATA_E_FIRST) + CHK_ERRA(DATA_E_LAST) + CHK_ERRA(DATA_S_FIRST) + CHK_ERRA(DATA_S_LAST) + CHK_ERRA(VIEW_E_FIRST) + CHK_ERRA(VIEW_E_LAST) + CHK_ERRA(VIEW_S_FIRST) + CHK_ERRA(VIEW_S_LAST) +// CHK_ERRA(VIEW_E_DRAW) + CHK_ERRA(REGDB_E_FIRST) + CHK_ERRA(REGDB_E_LAST) + CHK_ERRA(REGDB_S_FIRST) + CHK_ERRA(REGDB_S_LAST) +// CHK_ERRA(REGDB_E_READREGDB) + CHK_ERRA(REGDB_E_WRITEREGDB) + CHK_ERRA(REGDB_E_KEYMISSING) + CHK_ERRA(REGDB_E_INVALIDVALUE) + CHK_ERRA(REGDB_E_CLASSNOTREG) + CHK_ERRA(REGDB_E_IIDNOTREG) + CHK_ERRA(CAT_E_FIRST) + CHK_ERRA(CAT_E_LAST) +// CHK_ERRA(CAT_E_CATIDNOEXIST) +// CHK_ERRA(CAT_E_NODESCRIPTION) + CHK_ERRA(CS_E_FIRST) + CHK_ERRA(CS_E_LAST) +// CHK_ERRA(CS_E_PACKAGE_NOTFOUND) + CHK_ERRA(CS_E_NOT_DELETABLE) + CHK_ERRA(CS_E_CLASS_NOTFOUND) + CHK_ERRA(CS_E_INVALID_VERSION) + CHK_ERRA(CS_E_NO_CLASSSTORE) + CHK_ERRA(CS_E_OBJECT_NOTFOUND) + CHK_ERRA(CS_E_OBJECT_ALREADY_EXISTS) + CHK_ERRA(CS_E_INVALID_PATH) + CHK_ERRA(CS_E_NETWORK_ERROR) + CHK_ERRA(CS_E_ADMIN_LIMIT_EXCEEDED) + CHK_ERRA(CS_E_SCHEMA_MISMATCH) +// CHK_ERRA(CS_E_INTERNAL_ERROR) + CHK_ERRA(CACHE_E_FIRST) + CHK_ERRA(CACHE_E_LAST) + CHK_ERRA(CACHE_S_FIRST) + CHK_ERRA(CACHE_S_LAST) +// CHK_ERRA(CACHE_E_NOCACHE_UPDATED) + CHK_ERRA(OLEOBJ_E_FIRST) + CHK_ERRA(OLEOBJ_E_LAST) + CHK_ERRA(OLEOBJ_S_FIRST) + CHK_ERRA(OLEOBJ_S_LAST) +// CHK_ERRA(OLEOBJ_E_NOVERBS) + CHK_ERRA(OLEOBJ_E_INVALIDVERB) + CHK_ERRA(CLIENTSITE_E_FIRST) + CHK_ERRA(CLIENTSITE_E_LAST) + CHK_ERRA(CLIENTSITE_S_FIRST) + CHK_ERRA(CLIENTSITE_S_LAST) + CHK_ERRA(INPLACE_E_NOTUNDOABLE) + CHK_ERRA(INPLACE_E_NOTOOLSPACE) +// CHK_ERRA(INPLACE_E_FIRST) + CHK_ERRA(INPLACE_E_LAST) + CHK_ERRA(INPLACE_S_FIRST) + CHK_ERRA(INPLACE_S_LAST) + CHK_ERRA(ENUM_E_FIRST) + CHK_ERRA(ENUM_E_LAST) + CHK_ERRA(ENUM_S_FIRST) + CHK_ERRA(ENUM_S_LAST) + CHK_ERRA(CONVERT10_E_FIRST) + CHK_ERRA(CONVERT10_E_LAST) + CHK_ERRA(CONVERT10_S_FIRST) + CHK_ERRA(CONVERT10_S_LAST) +// CHK_ERRA(CONVERT10_E_OLESTREAM_GET) + CHK_ERRA(CONVERT10_E_OLESTREAM_PUT) + CHK_ERRA(CONVERT10_E_OLESTREAM_FMT) + CHK_ERRA(CONVERT10_E_OLESTREAM_BITMAP_TO_DIB) + CHK_ERRA(CONVERT10_E_STG_FMT) + CHK_ERRA(CONVERT10_E_STG_NO_STD_STREAM) + CHK_ERRA(CONVERT10_E_STG_DIB_TO_BITMAP) + CHK_ERRA(CLIPBRD_E_FIRST) + CHK_ERRA(CLIPBRD_E_LAST) + CHK_ERRA(CLIPBRD_S_FIRST) + CHK_ERRA(CLIPBRD_S_LAST) +// CHK_ERRA(CLIPBRD_E_CANT_OPEN) + CHK_ERRA(CLIPBRD_E_CANT_EMPTY) + CHK_ERRA(CLIPBRD_E_CANT_SET) + CHK_ERRA(CLIPBRD_E_BAD_DATA) + CHK_ERRA(CLIPBRD_E_CANT_CLOSE) + CHK_ERRA(MK_E_FIRST) + CHK_ERRA(MK_E_LAST) + CHK_ERRA(MK_S_FIRST) + CHK_ERRA(MK_S_LAST) +// CHK_ERRA(MK_E_CONNECTMANUALLY) + CHK_ERRA(MK_E_EXCEEDEDDEADLINE) + CHK_ERRA(MK_E_NEEDGENERIC) + CHK_ERRA(MK_E_UNAVAILABLE) + CHK_ERRA(MK_E_SYNTAX) + CHK_ERRA(MK_E_NOOBJECT) + CHK_ERRA(MK_E_INVALIDEXTENSION) + CHK_ERRA(MK_E_INTERMEDIATEINTERFACENOTSUPPORTED) + CHK_ERRA(MK_E_NOTBINDABLE) + CHK_ERRA(MK_E_NOTBOUND) + CHK_ERRA(MK_E_CANTOPENFILE) + CHK_ERRA(MK_E_MUSTBOTHERUSER) + CHK_ERRA(MK_E_NOINVERSE) + CHK_ERRA(MK_E_NOSTORAGE) + CHK_ERRA(MK_E_NOPREFIX) +// CHK_ERRA(MK_E_ENUMERATION_FAILED) + CHK_ERRA(CO_E_NOTINITIALIZED) + CHK_ERRA(CO_E_ALREADYINITIALIZED) + CHK_ERRA(CO_E_CANTDETERMINECLASS) + CHK_ERRA(CO_E_CLASSSTRING) + CHK_ERRA(CO_E_IIDSTRING) + CHK_ERRA(CO_E_APPNOTFOUND) + CHK_ERRA(CO_E_APPSINGLEUSE) + CHK_ERRA(CO_E_ERRORINAPP) + CHK_ERRA(CO_E_DLLNOTFOUND) + CHK_ERRA(CO_E_ERRORINDLL) + CHK_ERRA(CO_E_WRONGOSFORAPP) + CHK_ERRA(CO_E_OBJNOTREG) + CHK_ERRA(CO_E_OBJISREG) + CHK_ERRA(CO_E_OBJNOTCONNECTED) + CHK_ERRA(CO_E_APPDIDNTREG) + CHK_ERRA(CO_E_RELEASED) +// CHK_ERRA(OLE_S_USEREG) + CHK_ERRA(OLE_S_STATIC) + CHK_ERRA(OLE_S_MAC_CLIPFORMAT) +// CHK_ERRA(DRAGDROP_S_DROP) + CHK_ERRA(DRAGDROP_S_CANCEL) + CHK_ERRA(DRAGDROP_S_USEDEFAULTCURSORS) +// CHK_ERRA(DATA_S_SAMEFORMATETC) +// CHK_ERRA(VIEW_S_ALREADY_FROZEN) +// CHK_ERRA(CACHE_S_FORMATETC_NOTSUPPORTED) + CHK_ERRA(CACHE_S_SAMECACHE) + CHK_ERRA(CACHE_S_SOMECACHES_NOTUPDATED) +// CHK_ERRA(OLEOBJ_S_INVALIDVERB) + CHK_ERRA(OLEOBJ_S_CANNOT_DOVERB_NOW) + CHK_ERRA(OLEOBJ_S_INVALIDHWND) +// CHK_ERRA(INPLACE_S_TRUNCATED) +// CHK_ERRA(CONVERT10_S_NO_PRESENTATION) + CHK_ERRA(MK_S_REDUCED_TO_SELF) + CHK_ERRA(MK_S_ME) + CHK_ERRA(MK_S_HIM) + CHK_ERRA(MK_S_US) + CHK_ERRA(MK_S_MONIKERALREADYREGISTERED) + CHK_ERRA(CO_E_CLASS_CREATE_FAILED) + CHK_ERRA(CO_E_SCM_ERROR) + CHK_ERRA(CO_E_SCM_RPC_FAILURE) + CHK_ERRA(CO_E_BAD_PATH) + CHK_ERRA(CO_E_SERVER_EXEC_FAILURE) + CHK_ERRA(CO_E_OBJSRV_RPC_FAILURE) + CHK_ERRA(MK_E_NO_NORMALIZED) + CHK_ERRA(CO_E_SERVER_STOPPING) + CHK_ERRA(MEM_E_INVALID_ROOT) + CHK_ERRA(MEM_E_INVALID_LINK) + CHK_ERRA(MEM_E_INVALID_SIZE) + CHK_ERRA(CO_S_NOTALLINTERFACES) + CHK_ERRA(DISP_E_UNKNOWNINTERFACE) + CHK_ERRA(DISP_E_MEMBERNOTFOUND) + CHK_ERRA(DISP_E_PARAMNOTFOUND) + CHK_ERRA(DISP_E_TYPEMISMATCH) + CHK_ERRA(DISP_E_UNKNOWNNAME) + CHK_ERRA(DISP_E_NONAMEDARGS) + CHK_ERRA(DISP_E_BADVARTYPE) + CHK_ERRA(DISP_E_EXCEPTION) + CHK_ERRA(DISP_E_OVERFLOW) + CHK_ERRA(DISP_E_BADINDEX) + CHK_ERRA(DISP_E_UNKNOWNLCID) + CHK_ERRA(DISP_E_ARRAYISLOCKED) + CHK_ERRA(DISP_E_BADPARAMCOUNT) + CHK_ERRA(DISP_E_PARAMNOTOPTIONAL) + CHK_ERRA(DISP_E_BADCALLEE) + CHK_ERRA(DISP_E_NOTACOLLECTION) + CHK_ERRA(DISP_E_DIVBYZERO) + CHK_ERRA(DISP_E_BUFFERTOOSMALL) + CHK_ERRA(TYPE_E_BUFFERTOOSMALL) + CHK_ERRA(TYPE_E_FIELDNOTFOUND) + CHK_ERRA(TYPE_E_INVDATAREAD) + CHK_ERRA(TYPE_E_UNSUPFORMAT) + CHK_ERRA(TYPE_E_REGISTRYACCESS) + CHK_ERRA(TYPE_E_LIBNOTREGISTERED) + CHK_ERRA(TYPE_E_UNDEFINEDTYPE) + CHK_ERRA(TYPE_E_QUALIFIEDNAMEDISALLOWED) + CHK_ERRA(TYPE_E_INVALIDSTATE) + CHK_ERRA(TYPE_E_WRONGTYPEKIND) + CHK_ERRA(TYPE_E_ELEMENTNOTFOUND) + CHK_ERRA(TYPE_E_AMBIGUOUSNAME) + CHK_ERRA(TYPE_E_NAMECONFLICT) + CHK_ERRA(TYPE_E_UNKNOWNLCID) + CHK_ERRA(TYPE_E_DLLFUNCTIONNOTFOUND) + CHK_ERRA(TYPE_E_BADMODULEKIND) + CHK_ERRA(TYPE_E_SIZETOOBIG) + CHK_ERRA(TYPE_E_DUPLICATEID) + CHK_ERRA(TYPE_E_INVALIDID) + CHK_ERRA(TYPE_E_TYPEMISMATCH) + CHK_ERRA(TYPE_E_OUTOFBOUNDS) + CHK_ERRA(TYPE_E_IOERROR) + CHK_ERRA(TYPE_E_CANTCREATETMPFILE) + CHK_ERRA(TYPE_E_CANTLOADLIBRARY) + CHK_ERRA(TYPE_E_INCONSISTENTPROPFUNCS) + CHK_ERRA(TYPE_E_CIRCULARTYPE) + CHK_ERRA(STG_E_INVALIDFUNCTION) + CHK_ERRA(STG_E_FILENOTFOUND) + CHK_ERRA(STG_E_PATHNOTFOUND) + CHK_ERRA(STG_E_TOOMANYOPENFILES) + CHK_ERRA(STG_E_ACCESSDENIED) + CHK_ERRA(STG_E_INVALIDHANDLE) + CHK_ERRA(STG_E_INSUFFICIENTMEMORY) + CHK_ERRA(STG_E_INVALIDPOINTER) + CHK_ERRA(STG_E_NOMOREFILES) + CHK_ERRA(STG_E_DISKISWRITEPROTECTED) + CHK_ERRA(STG_E_SEEKERROR) + CHK_ERRA(STG_E_WRITEFAULT) + CHK_ERRA(STG_E_READFAULT) + CHK_ERRA(STG_E_SHAREVIOLATION) + CHK_ERRA(STG_E_LOCKVIOLATION) + CHK_ERRA(STG_E_FILEALREADYEXISTS) + CHK_ERRA(STG_E_INVALIDPARAMETER) + CHK_ERRA(STG_E_MEDIUMFULL) + CHK_ERRA(STG_E_PROPSETMISMATCHED) + CHK_ERRA(STG_E_ABNORMALAPIEXIT) + CHK_ERRA(STG_E_INVALIDHEADER) + CHK_ERRA(STG_E_INVALIDNAME) + CHK_ERRA(STG_E_UNKNOWN) + CHK_ERRA(STG_E_UNIMPLEMENTEDFUNCTION) + CHK_ERRA(STG_E_INVALIDFLAG) + CHK_ERRA(STG_E_INUSE) + CHK_ERRA(STG_E_NOTCURRENT) + CHK_ERRA(STG_E_REVERTED) + CHK_ERRA(STG_E_CANTSAVE) + CHK_ERRA(STG_E_OLDFORMAT) + CHK_ERRA(STG_E_OLDDLL) + CHK_ERRA(STG_E_SHAREREQUIRED) + CHK_ERRA(STG_E_NOTFILEBASEDSTORAGE) + CHK_ERRA(STG_E_EXTANTMARSHALLINGS) + CHK_ERRA(STG_E_DOCFILECORRUPT) + CHK_ERRA(STG_E_BADBASEADDRESS) + CHK_ERRA(STG_E_DOCFILETOOLARGE) + CHK_ERRA(STG_E_NOTSIMPLEFORMAT) + CHK_ERRA(STG_E_INCOMPLETE) + CHK_ERRA(STG_E_TERMINATED) + CHK_ERRA(STG_S_CONVERTED) + CHK_ERRA(STG_S_BLOCK) + CHK_ERRA(STG_S_RETRYNOW) + CHK_ERRA(STG_S_MONITORING) + CHK_ERRA(STG_S_MULTIPLEOPENS) + CHK_ERRA(STG_S_CONSOLIDATIONFAILED) + CHK_ERRA(STG_S_CANNOTCONSOLIDATE) + CHK_ERRA(RPC_E_CALL_REJECTED) + CHK_ERRA(RPC_E_CALL_CANCELED) + CHK_ERRA(RPC_E_CANTPOST_INSENDCALL) + CHK_ERRA(RPC_E_CANTCALLOUT_INASYNCCALL) + CHK_ERRA(RPC_E_CANTCALLOUT_INEXTERNALCALL) + CHK_ERRA(RPC_E_CONNECTION_TERMINATED) + CHK_ERRA(RPC_E_SERVER_DIED) + CHK_ERRA(RPC_E_CLIENT_DIED) + CHK_ERRA(RPC_E_INVALID_DATAPACKET) + CHK_ERRA(RPC_E_CANTTRANSMIT_CALL) + CHK_ERRA(RPC_E_CLIENT_CANTMARSHAL_DATA) + CHK_ERRA(RPC_E_CLIENT_CANTUNMARSHAL_DATA) + CHK_ERRA(RPC_E_SERVER_CANTMARSHAL_DATA) + CHK_ERRA(RPC_E_SERVER_CANTUNMARSHAL_DATA) + CHK_ERRA(RPC_E_INVALID_DATA) + CHK_ERRA(RPC_E_INVALID_PARAMETER) + CHK_ERRA(RPC_E_CANTCALLOUT_AGAIN) + CHK_ERRA(RPC_E_SERVER_DIED_DNE) + CHK_ERRA(RPC_E_SYS_CALL_FAILED) + CHK_ERRA(RPC_E_OUT_OF_RESOURCES) + CHK_ERRA(RPC_E_ATTEMPTED_MULTITHREAD) + CHK_ERRA(RPC_E_NOT_REGISTERED) + CHK_ERRA(RPC_E_FAULT) + CHK_ERRA(RPC_E_SERVERFAULT) + CHK_ERRA(RPC_E_CHANGED_MODE) + CHK_ERRA(RPC_E_INVALIDMETHOD) + CHK_ERRA(RPC_E_DISCONNECTED) + CHK_ERRA(RPC_E_RETRY) + CHK_ERRA(RPC_E_SERVERCALL_RETRYLATER) + CHK_ERRA(RPC_E_SERVERCALL_REJECTED) + CHK_ERRA(RPC_E_INVALID_CALLDATA) + CHK_ERRA(RPC_E_CANTCALLOUT_ININPUTSYNCCALL) + CHK_ERRA(RPC_E_WRONG_THREAD) + CHK_ERRA(RPC_E_THREAD_NOT_INIT) + CHK_ERRA(RPC_E_VERSION_MISMATCH) + CHK_ERRA(RPC_E_INVALID_HEADER) + CHK_ERRA(RPC_E_INVALID_EXTENSION) + CHK_ERRA(RPC_E_INVALID_IPID) + CHK_ERRA(RPC_E_INVALID_OBJECT) + CHK_ERRA(RPC_S_CALLPENDING) + CHK_ERRA(RPC_S_WAITONTIMER) + CHK_ERRA(RPC_E_CALL_COMPLETE) + CHK_ERRA(RPC_E_UNSECURE_CALL) + CHK_ERRA(RPC_E_TOO_LATE) + CHK_ERRA(RPC_E_NO_GOOD_SECURITY_PACKAGES) + CHK_ERRA(RPC_E_ACCESS_DENIED) + CHK_ERRA(RPC_E_REMOTE_DISABLED) + CHK_ERRA(RPC_E_INVALID_OBJREF) + CHK_ERRA(RPC_E_NO_CONTEXT) + CHK_ERRA(RPC_E_TIMEOUT) + CHK_ERRA(RPC_E_NO_SYNC) + CHK_ERRA(RPC_E_FULLSIC_REQUIRED) + CHK_ERRA(RPC_E_INVALID_STD_NAME) + CHK_ERRA(CO_E_FAILEDTOIMPERSONATE) + CHK_ERRA(CO_E_FAILEDTOGETSECCTX) + CHK_ERRA(CO_E_FAILEDTOOPENTHREADTOKEN) + CHK_ERRA(CO_E_FAILEDTOGETTOKENINFO) + CHK_ERRA(CO_E_TRUSTEEDOESNTMATCHCLIENT) + CHK_ERRA(CO_E_FAILEDTOQUERYCLIENTBLANKET) + CHK_ERRA(CO_E_FAILEDTOSETDACL) + CHK_ERRA(CO_E_ACCESSCHECKFAILED) + CHK_ERRA(CO_E_NETACCESSAPIFAILED) + CHK_ERRA(CO_E_WRONGTRUSTEENAMESYNTAX) + CHK_ERRA(CO_E_INVALIDSID) + CHK_ERRA(CO_E_CONVERSIONFAILED) + CHK_ERRA(CO_E_NOMATCHINGSIDFOUND) + CHK_ERRA(CO_E_LOOKUPACCSIDFAILED) + CHK_ERRA(CO_E_NOMATCHINGNAMEFOUND) + CHK_ERRA(CO_E_LOOKUPACCNAMEFAILED) + CHK_ERRA(CO_E_SETSERLHNDLFAILED) + CHK_ERRA(CO_E_FAILEDTOGETWINDIR) + CHK_ERRA(CO_E_PATHTOOLONG) + CHK_ERRA(CO_E_FAILEDTOGENUUID) + CHK_ERRA(CO_E_FAILEDTOCREATEFILE) + CHK_ERRA(CO_E_FAILEDTOCLOSEHANDLE) + CHK_ERRA(CO_E_EXCEEDSYSACLLIMIT) + CHK_ERRA(CO_E_ACESINWRONGORDER) + CHK_ERRA(CO_E_INCOMPATIBLESTREAMVERSION) + CHK_ERRA(CO_E_FAILEDTOOPENPROCESSTOKEN) + CHK_ERRA(CO_E_DECODEFAILED) + CHK_ERRA(CO_E_ACNOTINITIALIZED) + CHK_ERRA(RPC_E_UNEXPECTED) + CHK_ERRA(NTE_BAD_UID) + CHK_ERRA(NTE_BAD_HASH) + CHK_ERRA(NTE_BAD_KEY) + CHK_ERRA(NTE_BAD_LEN) + CHK_ERRA(NTE_BAD_DATA) + CHK_ERRA(NTE_BAD_SIGNATURE) + CHK_ERRA(NTE_BAD_VER) + CHK_ERRA(NTE_BAD_ALGID) + CHK_ERRA(NTE_BAD_FLAGS) + CHK_ERRA(NTE_BAD_TYPE) + CHK_ERRA(NTE_BAD_KEY_STATE) + CHK_ERRA(NTE_BAD_HASH_STATE) + CHK_ERRA(NTE_NO_KEY) + CHK_ERRA(NTE_NO_MEMORY) + CHK_ERRA(NTE_EXISTS) + CHK_ERRA(NTE_PERM) + CHK_ERRA(NTE_NOT_FOUND) + CHK_ERRA(NTE_DOUBLE_ENCRYPT) + CHK_ERRA(NTE_BAD_PROVIDER) + CHK_ERRA(NTE_BAD_PROV_TYPE) + CHK_ERRA(NTE_BAD_PUBLIC_KEY) + CHK_ERRA(NTE_BAD_KEYSET) + CHK_ERRA(NTE_PROV_TYPE_NOT_DEF) + CHK_ERRA(NTE_PROV_TYPE_ENTRY_BAD) + CHK_ERRA(NTE_KEYSET_NOT_DEF) + CHK_ERRA(NTE_KEYSET_ENTRY_BAD) + CHK_ERRA(NTE_PROV_TYPE_NO_MATCH) + CHK_ERRA(NTE_SIGNATURE_FILE_BAD) + CHK_ERRA(NTE_PROVIDER_DLL_FAIL) + CHK_ERRA(NTE_PROV_DLL_NOT_FOUND) + CHK_ERRA(NTE_BAD_KEYSET_PARAM) + CHK_ERRA(NTE_FAIL) + CHK_ERRA(NTE_SYS_ERR) + CHK_ERRA(NTE_SILENT_CONTEXT) + CHK_ERRA(NTE_TOKEN_KEYSET_STORAGE_FULL) + CHK_ERRA(CRYPT_E_MSG_ERROR) + CHK_ERRA(CRYPT_E_UNKNOWN_ALGO) + CHK_ERRA(CRYPT_E_OID_FORMAT) + CHK_ERRA(CRYPT_E_INVALID_MSG_TYPE) + CHK_ERRA(CRYPT_E_UNEXPECTED_ENCODING) + CHK_ERRA(CRYPT_E_AUTH_ATTR_MISSING) + CHK_ERRA(CRYPT_E_HASH_VALUE) + CHK_ERRA(CRYPT_E_INVALID_INDEX) + CHK_ERRA(CRYPT_E_ALREADY_DECRYPTED) + CHK_ERRA(CRYPT_E_NOT_DECRYPTED) + CHK_ERRA(CRYPT_E_RECIPIENT_NOT_FOUND) + CHK_ERRA(CRYPT_E_CONTROL_TYPE) + CHK_ERRA(CRYPT_E_ISSUER_SERIALNUMBER) + CHK_ERRA(CRYPT_E_SIGNER_NOT_FOUND) + CHK_ERRA(CRYPT_E_ATTRIBUTES_MISSING) + CHK_ERRA(CRYPT_E_STREAM_MSG_NOT_READY) + CHK_ERRA(CRYPT_E_STREAM_INSUFFICIENT_DATA) + CHK_ERRA(CRYPT_E_BAD_LEN) + CHK_ERRA(CRYPT_E_BAD_ENCODE) + CHK_ERRA(CRYPT_E_FILE_ERROR) + CHK_ERRA(CRYPT_E_NOT_FOUND) + CHK_ERRA(CRYPT_E_EXISTS) + CHK_ERRA(CRYPT_E_NO_PROVIDER) + CHK_ERRA(CRYPT_E_SELF_SIGNED) + CHK_ERRA(CRYPT_E_DELETED_PREV) + CHK_ERRA(CRYPT_E_NO_MATCH) + CHK_ERRA(CRYPT_E_UNEXPECTED_MSG_TYPE) + CHK_ERRA(CRYPT_E_NO_KEY_PROPERTY) + CHK_ERRA(CRYPT_E_NO_DECRYPT_CERT) + CHK_ERRA(CRYPT_E_BAD_MSG) + CHK_ERRA(CRYPT_E_NO_SIGNER) + CHK_ERRA(CRYPT_E_PENDING_CLOSE) + CHK_ERRA(CRYPT_E_REVOKED) + CHK_ERRA(CRYPT_E_NO_REVOCATION_DLL) + CHK_ERRA(CRYPT_E_NO_REVOCATION_CHECK) + CHK_ERRA(CRYPT_E_REVOCATION_OFFLINE) + CHK_ERRA(CRYPT_E_NOT_IN_REVOCATION_DATABASE) + CHK_ERRA(CRYPT_E_INVALID_NUMERIC_STRING) + CHK_ERRA(CRYPT_E_INVALID_PRINTABLE_STRING) + CHK_ERRA(CRYPT_E_INVALID_IA5_STRING) + CHK_ERRA(CRYPT_E_INVALID_X500_STRING) + CHK_ERRA(CRYPT_E_NOT_CHAR_STRING) + CHK_ERRA(CRYPT_E_FILERESIZED) + CHK_ERRA(CRYPT_E_SECURITY_SETTINGS) + CHK_ERRA(CRYPT_E_NO_VERIFY_USAGE_DLL) + CHK_ERRA(CRYPT_E_NO_VERIFY_USAGE_CHECK) + CHK_ERRA(CRYPT_E_VERIFY_USAGE_OFFLINE) + CHK_ERRA(CRYPT_E_NOT_IN_CTL) + CHK_ERRA(CRYPT_E_NO_TRUSTED_SIGNER) + CHK_ERRA(CRYPT_E_OSS_ERROR) + CHK_ERRA(OSS_MORE_BUF) + CHK_ERRA(OSS_NEGATIVE_UINTEGER) + CHK_ERRA(OSS_PDU_RANGE) + CHK_ERRA(OSS_MORE_INPUT) + CHK_ERRA(OSS_DATA_ERROR) + CHK_ERRA(OSS_BAD_ARG) + CHK_ERRA(OSS_BAD_VERSION) + CHK_ERRA(OSS_OUT_MEMORY) + CHK_ERRA(OSS_PDU_MISMATCH) + CHK_ERRA(OSS_LIMITED) + CHK_ERRA(OSS_BAD_PTR) + CHK_ERRA(OSS_BAD_TIME) + CHK_ERRA(OSS_INDEFINITE_NOT_SUPPORTED) + CHK_ERRA(OSS_MEM_ERROR) + CHK_ERRA(OSS_BAD_TABLE) + CHK_ERRA(OSS_TOO_LONG) + CHK_ERRA(OSS_CONSTRAINT_VIOLATED) + CHK_ERRA(OSS_FATAL_ERROR) + CHK_ERRA(OSS_ACCESS_SERIALIZATION_ERROR) + CHK_ERRA(OSS_NULL_TBL) + CHK_ERRA(OSS_NULL_FCN) + CHK_ERRA(OSS_BAD_ENCRULES) + CHK_ERRA(OSS_UNAVAIL_ENCRULES) + CHK_ERRA(OSS_CANT_OPEN_TRACE_WINDOW) + CHK_ERRA(OSS_UNIMPLEMENTED) + CHK_ERRA(OSS_OID_DLL_NOT_LINKED) + CHK_ERRA(OSS_CANT_OPEN_TRACE_FILE) + CHK_ERRA(OSS_TRACE_FILE_ALREADY_OPEN) + CHK_ERRA(OSS_TABLE_MISMATCH) + CHK_ERRA(OSS_TYPE_NOT_SUPPORTED) + CHK_ERRA(OSS_REAL_DLL_NOT_LINKED) + CHK_ERRA(OSS_REAL_CODE_NOT_LINKED) + CHK_ERRA(OSS_OUT_OF_RANGE) + CHK_ERRA(OSS_COPIER_DLL_NOT_LINKED) + CHK_ERRA(OSS_CONSTRAINT_DLL_NOT_LINKED) + CHK_ERRA(OSS_COMPARATOR_DLL_NOT_LINKED) + CHK_ERRA(OSS_COMPARATOR_CODE_NOT_LINKED) + CHK_ERRA(OSS_MEM_MGR_DLL_NOT_LINKED) + CHK_ERRA(OSS_PDV_DLL_NOT_LINKED) + CHK_ERRA(OSS_PDV_CODE_NOT_LINKED) + CHK_ERRA(OSS_API_DLL_NOT_LINKED) + CHK_ERRA(OSS_BERDER_DLL_NOT_LINKED) + CHK_ERRA(OSS_PER_DLL_NOT_LINKED) + CHK_ERRA(OSS_OPEN_TYPE_ERROR) + CHK_ERRA(OSS_MUTEX_NOT_CREATED) + CHK_ERRA(OSS_CANT_CLOSE_TRACE_FILE) + CHK_ERRA(CERTSRV_E_BAD_REQUESTSUBJECT) + CHK_ERRA(CERTSRV_E_NO_REQUEST) + CHK_ERRA(CERTSRV_E_BAD_REQUESTSTATUS) + CHK_ERRA(CERTSRV_E_PROPERTY_EMPTY) + CHK_ERRA(CERTSRV_E_INVALID_CA_CERTIFICATE) + CHK_ERRA(CERTSRV_E_UNSUPPORTED_CERT_TYPE) + CHK_ERRA(CERTSRV_E_NO_CERT_TYPE) + CHK_ERRA(TRUST_E_SYSTEM_ERROR) + CHK_ERRA(TRUST_E_NO_SIGNER_CERT) + CHK_ERRA(TRUST_E_COUNTER_SIGNER) + CHK_ERRA(TRUST_E_CERT_SIGNATURE) + CHK_ERRA(TRUST_E_TIME_STAMP) + CHK_ERRA(TRUST_E_BAD_DIGEST) + CHK_ERRA(TRUST_E_BASIC_CONSTRAINTS) + CHK_ERRA(TRUST_E_FINANCIAL_CRITERIA) +// CHK_ERRA(NTE_OP_OK) + CHK_ERRA(TRUST_E_PROVIDER_UNKNOWN) + CHK_ERRA(TRUST_E_ACTION_UNKNOWN) + CHK_ERRA(TRUST_E_SUBJECT_FORM_UNKNOWN) + CHK_ERRA(TRUST_E_SUBJECT_NOT_TRUSTED) + CHK_ERRA(DIGSIG_E_ENCODE) + CHK_ERRA(DIGSIG_E_DECODE) + CHK_ERRA(DIGSIG_E_EXTENSIBILITY) + CHK_ERRA(DIGSIG_E_CRYPTO) + CHK_ERRA(PERSIST_E_SIZEDEFINITE) + CHK_ERRA(PERSIST_E_SIZEINDEFINITE) + CHK_ERRA(PERSIST_E_NOTSELFSIZING) + CHK_ERRA(TRUST_E_NOSIGNATURE) + CHK_ERRA(CERT_E_EXPIRED) + CHK_ERRA(CERT_E_VALIDITYPERIODNESTING) + CHK_ERRA(CERT_E_ROLE) + CHK_ERRA(CERT_E_PATHLENCONST) + CHK_ERRA(CERT_E_CRITICAL) + CHK_ERRA(CERT_E_PURPOSE) + CHK_ERRA(CERT_E_ISSUERCHAINING) + CHK_ERRA(CERT_E_MALFORMED) + CHK_ERRA(CERT_E_UNTRUSTEDROOT) + CHK_ERRA(CERT_E_CHAINING) + CHK_ERRA(TRUST_E_FAIL) + CHK_ERRA(CERT_E_REVOKED) + CHK_ERRA(CERT_E_UNTRUSTEDTESTROOT) + CHK_ERRA(CERT_E_REVOCATION_FAILURE) + CHK_ERRA(CERT_E_CN_NO_MATCH) + CHK_ERRA(CERT_E_WRONG_USAGE) + CHK_ERRA(SPAPI_E_EXPECTED_SECTION_NAME) + CHK_ERRA(SPAPI_E_BAD_SECTION_NAME_LINE) + CHK_ERRA(SPAPI_E_SECTION_NAME_TOO_LONG) + CHK_ERRA(SPAPI_E_GENERAL_SYNTAX) + CHK_ERRA(SPAPI_E_WRONG_INF_STYLE) + CHK_ERRA(SPAPI_E_SECTION_NOT_FOUND) + CHK_ERRA(SPAPI_E_LINE_NOT_FOUND) + CHK_ERRA(SPAPI_E_NO_BACKUP) + CHK_ERRA(SPAPI_E_NO_ASSOCIATED_CLASS) + CHK_ERRA(SPAPI_E_CLASS_MISMATCH) + CHK_ERRA(SPAPI_E_DUPLICATE_FOUND) + CHK_ERRA(SPAPI_E_NO_DRIVER_SELECTED) + CHK_ERRA(SPAPI_E_KEY_DOES_NOT_EXIST) + CHK_ERRA(SPAPI_E_INVALID_DEVINST_NAME) + CHK_ERRA(SPAPI_E_INVALID_CLASS) + CHK_ERRA(SPAPI_E_DEVINST_ALREADY_EXISTS) + CHK_ERRA(SPAPI_E_DEVINFO_NOT_REGISTERED) + CHK_ERRA(SPAPI_E_INVALID_REG_PROPERTY) + CHK_ERRA(SPAPI_E_NO_INF) + CHK_ERRA(SPAPI_E_NO_SUCH_DEVINST) + CHK_ERRA(SPAPI_E_CANT_LOAD_CLASS_ICON) + CHK_ERRA(SPAPI_E_INVALID_CLASS_INSTALLER) + CHK_ERRA(SPAPI_E_DI_DO_DEFAULT) + CHK_ERRA(SPAPI_E_DI_NOFILECOPY) + CHK_ERRA(SPAPI_E_INVALID_HWPROFILE) + CHK_ERRA(SPAPI_E_NO_DEVICE_SELECTED) + CHK_ERRA(SPAPI_E_DEVINFO_LIST_LOCKED) + CHK_ERRA(SPAPI_E_DEVINFO_DATA_LOCKED) + CHK_ERRA(SPAPI_E_DI_BAD_PATH) + CHK_ERRA(SPAPI_E_NO_CLASSINSTALL_PARAMS) + CHK_ERRA(SPAPI_E_FILEQUEUE_LOCKED) + CHK_ERRA(SPAPI_E_BAD_SERVICE_INSTALLSECT) + CHK_ERRA(SPAPI_E_NO_CLASS_DRIVER_LIST) + CHK_ERRA(SPAPI_E_NO_ASSOCIATED_SERVICE) + CHK_ERRA(SPAPI_E_NO_DEFAULT_DEVICE_INTERFACE) + CHK_ERRA(SPAPI_E_DEVICE_INTERFACE_ACTIVE) + CHK_ERRA(SPAPI_E_DEVICE_INTERFACE_REMOVED) + CHK_ERRA(SPAPI_E_BAD_INTERFACE_INSTALLSECT) + CHK_ERRA(SPAPI_E_NO_SUCH_INTERFACE_CLASS) + CHK_ERRA(SPAPI_E_INVALID_REFERENCE_STRING) + CHK_ERRA(SPAPI_E_INVALID_MACHINENAME) + CHK_ERRA(SPAPI_E_REMOTE_COMM_FAILURE) + CHK_ERRA(SPAPI_E_MACHINE_UNAVAILABLE) + CHK_ERRA(SPAPI_E_NO_CONFIGMGR_SERVICES) + CHK_ERRA(SPAPI_E_INVALID_PROPPAGE_PROVIDER) + CHK_ERRA(SPAPI_E_NO_SUCH_DEVICE_INTERFACE) + CHK_ERRA(SPAPI_E_DI_POSTPROCESSING_REQUIRED) + CHK_ERRA(SPAPI_E_INVALID_COINSTALLER) + CHK_ERRA(SPAPI_E_NO_COMPAT_DRIVERS) + CHK_ERRA(SPAPI_E_NO_DEVICE_ICON) + CHK_ERRA(SPAPI_E_INVALID_INF_LOGCONFIG) + CHK_ERRA(SPAPI_E_DI_DONT_INSTALL) + CHK_ERRA(SPAPI_E_INVALID_FILTER_DRIVER) + CHK_ERRA(SPAPI_E_NON_WINDOWS_NT_DRIVER) + CHK_ERRA(SPAPI_E_NON_WINDOWS_DRIVER) + CHK_ERRA(SPAPI_E_NO_CATALOG_FOR_OEM_INF) + CHK_ERRA(SPAPI_E_DEVINSTALL_QUEUE_NONNATIVE) + CHK_ERRA(SPAPI_E_ERROR_NOT_INSTALLED) +// CHK_ERRA(SCARD_S_SUCCESS) + CHK_ERRA(SCARD_F_INTERNAL_ERROR) + CHK_ERRA(SCARD_E_CANCELLED) + CHK_ERRA(SCARD_E_INVALID_HANDLE) + CHK_ERRA(SCARD_E_INVALID_PARAMETER) + CHK_ERRA(SCARD_E_INVALID_TARGET) + CHK_ERRA(SCARD_E_NO_MEMORY) + CHK_ERRA(SCARD_F_WAITED_TOO_LONG) + CHK_ERRA(SCARD_E_INSUFFICIENT_BUFFER) + CHK_ERRA(SCARD_E_UNKNOWN_READER) + CHK_ERRA(SCARD_E_TIMEOUT) + CHK_ERRA(SCARD_E_SHARING_VIOLATION) + CHK_ERRA(SCARD_E_NO_SMARTCARD) + CHK_ERRA(SCARD_E_UNKNOWN_CARD) + CHK_ERRA(SCARD_E_CANT_DISPOSE) + CHK_ERRA(SCARD_E_PROTO_MISMATCH) + CHK_ERRA(SCARD_E_NOT_READY) + CHK_ERRA(SCARD_E_INVALID_VALUE) + CHK_ERRA(SCARD_E_SYSTEM_CANCELLED) + CHK_ERRA(SCARD_F_COMM_ERROR) + CHK_ERRA(SCARD_F_UNKNOWN_ERROR) + CHK_ERRA(SCARD_E_INVALID_ATR) + CHK_ERRA(SCARD_E_NOT_TRANSACTED) + CHK_ERRA(SCARD_E_READER_UNAVAILABLE) + CHK_ERRA(SCARD_P_SHUTDOWN) + CHK_ERRA(SCARD_E_PCI_TOO_SMALL) + CHK_ERRA(SCARD_E_READER_UNSUPPORTED) + CHK_ERRA(SCARD_E_DUPLICATE_READER) + CHK_ERRA(SCARD_E_CARD_UNSUPPORTED) + CHK_ERRA(SCARD_E_NO_SERVICE) + CHK_ERRA(SCARD_E_SERVICE_STOPPED) + CHK_ERRA(SCARD_E_UNEXPECTED) + CHK_ERRA(SCARD_E_ICC_INSTALLATION) + CHK_ERRA(SCARD_E_ICC_CREATEORDER) + CHK_ERRA(SCARD_E_UNSUPPORTED_FEATURE) + CHK_ERRA(SCARD_E_DIR_NOT_FOUND) + CHK_ERRA(SCARD_E_FILE_NOT_FOUND) + CHK_ERRA(SCARD_E_NO_DIR) + CHK_ERRA(SCARD_E_NO_FILE) + CHK_ERRA(SCARD_E_NO_ACCESS) + CHK_ERRA(SCARD_E_WRITE_TOO_MANY) + CHK_ERRA(SCARD_E_BAD_SEEK) + CHK_ERRA(SCARD_E_INVALID_CHV) + CHK_ERRA(SCARD_E_UNKNOWN_RES_MNG) + CHK_ERRA(SCARD_E_NO_SUCH_CERTIFICATE) + CHK_ERRA(SCARD_E_CERTIFICATE_UNAVAILABLE) + CHK_ERRA(SCARD_E_NO_READERS_AVAILABLE) + CHK_ERRA(SCARD_E_COMM_DATA_LOST) + CHK_ERRA(SCARD_W_UNSUPPORTED_CARD) + CHK_ERRA(SCARD_W_UNRESPONSIVE_CARD) + CHK_ERRA(SCARD_W_UNPOWERED_CARD) + CHK_ERRA(SCARD_W_RESET_CARD) + CHK_ERRA(SCARD_W_REMOVED_CARD) + CHK_ERRA(SCARD_W_SECURITY_VIOLATION) + CHK_ERRA(SCARD_W_WRONG_CHV) + CHK_ERRA(SCARD_W_CHV_BLOCKED) + CHK_ERRA(SCARD_W_EOF) + CHK_ERRA(SCARD_W_CANCELLED_BY_USER) + CHK_ERR_WIN32_ONLY(ERROR_INVALID_FUNCTION, "ERROR_INVALID_FUNCTION") + CHK_ERR_WIN32A(ERROR_FILE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_PATH_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_TOO_MANY_OPEN_FILES) + CHK_ERRA(ERROR_ACCESS_DENIED) + CHK_ERRA(ERROR_INVALID_HANDLE) + CHK_ERR_WIN32A(ERROR_ARENA_TRASHED) + CHK_ERR_WIN32A(ERROR_NOT_ENOUGH_MEMORY) + CHK_ERR_WIN32A(ERROR_INVALID_BLOCK) + CHK_ERR_WIN32A(ERROR_BAD_ENVIRONMENT) + CHK_ERR_WIN32A(ERROR_BAD_FORMAT) + CHK_ERR_WIN32A(ERROR_INVALID_ACCESS) + CHK_ERR_WIN32A(ERROR_INVALID_DATA) + CHK_ERRA(ERROR_OUTOFMEMORY) + CHK_ERR_WIN32A(ERROR_INVALID_DRIVE) + CHK_ERR_WIN32A(ERROR_CURRENT_DIRECTORY) + CHK_ERR_WIN32A(ERROR_NOT_SAME_DEVICE) + CHK_ERR_WIN32A(ERROR_NO_MORE_FILES) + CHK_ERR_WIN32A(ERROR_WRITE_PROTECT) + CHK_ERR_WIN32A(ERROR_BAD_UNIT) + CHK_ERR_WIN32A(ERROR_NOT_READY) + CHK_ERR_WIN32A(ERROR_BAD_COMMAND) + CHK_ERR_WIN32A(ERROR_CRC) + CHK_ERR_WIN32A(ERROR_BAD_LENGTH) + CHK_ERR_WIN32A(ERROR_SEEK) + CHK_ERR_WIN32A(ERROR_NOT_DOS_DISK) + CHK_ERR_WIN32A(ERROR_SECTOR_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_OUT_OF_PAPER) + CHK_ERR_WIN32A(ERROR_WRITE_FAULT) + CHK_ERR_WIN32A(ERROR_READ_FAULT) + CHK_ERR_WIN32A(ERROR_GEN_FAILURE) + CHK_ERR_WIN32A(ERROR_SHARING_VIOLATION) + CHK_ERR_WIN32A(ERROR_LOCK_VIOLATION) + CHK_ERR_WIN32A(ERROR_WRONG_DISK) + CHK_ERR_WIN32A(ERROR_SHARING_BUFFER_EXCEEDED) + CHK_ERR_WIN32A(ERROR_HANDLE_EOF) + CHK_ERR_WIN32A(ERROR_HANDLE_DISK_FULL) + CHK_ERR_WIN32A(ERROR_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_REM_NOT_LIST) + CHK_ERR_WIN32A(ERROR_DUP_NAME) + CHK_ERR_WIN32A(ERROR_BAD_NETPATH) + CHK_ERR_WIN32A(ERROR_NETWORK_BUSY) + CHK_ERR_WIN32A(ERROR_DEV_NOT_EXIST) + CHK_ERR_WIN32A(ERROR_TOO_MANY_CMDS) + CHK_ERR_WIN32A(ERROR_ADAP_HDW_ERR) + CHK_ERR_WIN32A(ERROR_BAD_NET_RESP) + CHK_ERR_WIN32A(ERROR_UNEXP_NET_ERR) + CHK_ERR_WIN32A(ERROR_BAD_REM_ADAP) + CHK_ERR_WIN32A(ERROR_PRINTQ_FULL) + CHK_ERR_WIN32A(ERROR_NO_SPOOL_SPACE) + CHK_ERR_WIN32A(ERROR_PRINT_CANCELLED) + CHK_ERR_WIN32A(ERROR_NETNAME_DELETED) + CHK_ERR_WIN32A(ERROR_NETWORK_ACCESS_DENIED) + CHK_ERR_WIN32A(ERROR_BAD_DEV_TYPE) + CHK_ERR_WIN32A(ERROR_BAD_NET_NAME) + CHK_ERR_WIN32A(ERROR_TOO_MANY_NAMES) + CHK_ERR_WIN32A(ERROR_TOO_MANY_SESS) + CHK_ERR_WIN32A(ERROR_SHARING_PAUSED) + CHK_ERR_WIN32A(ERROR_REQ_NOT_ACCEP) + CHK_ERR_WIN32A(ERROR_REDIR_PAUSED) + CHK_ERR_WIN32A(ERROR_FILE_EXISTS) + CHK_ERR_WIN32A(ERROR_CANNOT_MAKE) + CHK_ERR_WIN32A(ERROR_FAIL_I24) + CHK_ERR_WIN32A(ERROR_OUT_OF_STRUCTURES) + CHK_ERR_WIN32A(ERROR_ALREADY_ASSIGNED) + CHK_ERR_WIN32A(ERROR_INVALID_PASSWORD) + CHK_ERRA(ERROR_INVALID_PARAMETER) + CHK_ERR_WIN32A(ERROR_NET_WRITE_FAULT) + CHK_ERR_WIN32A(ERROR_NO_PROC_SLOTS) + CHK_ERR_WIN32A(ERROR_TOO_MANY_SEMAPHORES) + CHK_ERR_WIN32A(ERROR_EXCL_SEM_ALREADY_OWNED) + CHK_ERR_WIN32A(ERROR_SEM_IS_SET) + CHK_ERR_WIN32A(ERROR_TOO_MANY_SEM_REQUESTS) + CHK_ERR_WIN32A(ERROR_INVALID_AT_INTERRUPT_TIME) + CHK_ERR_WIN32A(ERROR_SEM_OWNER_DIED) + CHK_ERR_WIN32A(ERROR_SEM_USER_LIMIT) + CHK_ERR_WIN32A(ERROR_DISK_CHANGE) + CHK_ERR_WIN32A(ERROR_DRIVE_LOCKED) + CHK_ERR_WIN32A(ERROR_BROKEN_PIPE) + CHK_ERR_WIN32A(ERROR_OPEN_FAILED) + CHK_ERR_WIN32A(ERROR_BUFFER_OVERFLOW) + CHK_ERR_WIN32A(ERROR_DISK_FULL) + CHK_ERR_WIN32A(ERROR_NO_MORE_SEARCH_HANDLES) + CHK_ERR_WIN32A(ERROR_INVALID_TARGET_HANDLE) + CHK_ERR_WIN32A(ERROR_INVALID_CATEGORY) + CHK_ERR_WIN32A(ERROR_INVALID_VERIFY_SWITCH) + CHK_ERR_WIN32A(ERROR_BAD_DRIVER_LEVEL) + CHK_ERR_WIN32A(ERROR_CALL_NOT_IMPLEMENTED) + CHK_ERR_WIN32A(ERROR_SEM_TIMEOUT) + CHK_ERR_WIN32A(ERROR_INSUFFICIENT_BUFFER) + CHK_ERR_WIN32A(ERROR_INVALID_NAME) + CHK_ERR_WIN32A(ERROR_INVALID_LEVEL) + CHK_ERR_WIN32A(ERROR_NO_VOLUME_LABEL) + CHK_ERR_WIN32A(ERROR_MOD_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_PROC_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_WAIT_NO_CHILDREN) + CHK_ERR_WIN32A(ERROR_CHILD_NOT_COMPLETE) + CHK_ERR_WIN32A(ERROR_DIRECT_ACCESS_HANDLE) + CHK_ERR_WIN32A(ERROR_NEGATIVE_SEEK) + CHK_ERR_WIN32A(ERROR_SEEK_ON_DEVICE) + CHK_ERR_WIN32A(ERROR_IS_JOIN_TARGET) + CHK_ERR_WIN32A(ERROR_IS_JOINED) + CHK_ERR_WIN32A(ERROR_IS_SUBSTED) + CHK_ERR_WIN32A(ERROR_NOT_JOINED) + CHK_ERR_WIN32A(ERROR_NOT_SUBSTED) + CHK_ERR_WIN32A(ERROR_JOIN_TO_JOIN) + CHK_ERR_WIN32A(ERROR_SUBST_TO_SUBST) + CHK_ERR_WIN32A(ERROR_JOIN_TO_SUBST) + CHK_ERR_WIN32A(ERROR_SUBST_TO_JOIN) + CHK_ERR_WIN32A(ERROR_BUSY_DRIVE) + CHK_ERR_WIN32A(ERROR_SAME_DRIVE) + CHK_ERR_WIN32A(ERROR_DIR_NOT_ROOT) + CHK_ERR_WIN32A(ERROR_DIR_NOT_EMPTY) + CHK_ERR_WIN32A(ERROR_IS_SUBST_PATH) + CHK_ERR_WIN32A(ERROR_IS_JOIN_PATH) + CHK_ERR_WIN32A(ERROR_PATH_BUSY) + CHK_ERR_WIN32A(ERROR_IS_SUBST_TARGET) + CHK_ERR_WIN32A(ERROR_SYSTEM_TRACE) + CHK_ERR_WIN32A(ERROR_INVALID_EVENT_COUNT) + CHK_ERR_WIN32A(ERROR_TOO_MANY_MUXWAITERS) + CHK_ERR_WIN32A(ERROR_INVALID_LIST_FORMAT) + CHK_ERR_WIN32A(ERROR_LABEL_TOO_LONG) + CHK_ERR_WIN32A(ERROR_TOO_MANY_TCBS) + CHK_ERR_WIN32A(ERROR_SIGNAL_REFUSED) + CHK_ERR_WIN32A(ERROR_DISCARDED) + CHK_ERR_WIN32A(ERROR_NOT_LOCKED) + CHK_ERR_WIN32A(ERROR_BAD_THREADID_ADDR) + CHK_ERR_WIN32A(ERROR_BAD_ARGUMENTS) + CHK_ERR_WIN32A(ERROR_BAD_PATHNAME) + CHK_ERR_WIN32A(ERROR_SIGNAL_PENDING) + CHK_ERR_WIN32A(ERROR_MAX_THRDS_REACHED) + CHK_ERR_WIN32A(ERROR_LOCK_FAILED) + CHK_ERR_WIN32A(ERROR_BUSY) + CHK_ERR_WIN32A(ERROR_CANCEL_VIOLATION) + CHK_ERR_WIN32A(ERROR_ATOMIC_LOCKS_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_INVALID_SEGMENT_NUMBER) + CHK_ERR_WIN32A(ERROR_INVALID_ORDINAL) + CHK_ERR_WIN32A(ERROR_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_INVALID_FLAG_NUMBER) + CHK_ERR_WIN32A(ERROR_SEM_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_INVALID_STARTING_CODESEG) + CHK_ERR_WIN32A(ERROR_INVALID_STACKSEG) + CHK_ERR_WIN32A(ERROR_INVALID_MODULETYPE) + CHK_ERR_WIN32A(ERROR_INVALID_EXE_SIGNATURE) + CHK_ERR_WIN32A(ERROR_EXE_MARKED_INVALID) + CHK_ERR_WIN32A(ERROR_BAD_EXE_FORMAT) + CHK_ERR_WIN32A(ERROR_ITERATED_DATA_EXCEEDS_64k) + CHK_ERR_WIN32A(ERROR_INVALID_MINALLOCSIZE) + CHK_ERR_WIN32A(ERROR_DYNLINK_FROM_INVALID_RING) + CHK_ERR_WIN32A(ERROR_IOPL_NOT_ENABLED) + CHK_ERR_WIN32A(ERROR_INVALID_SEGDPL) + CHK_ERR_WIN32A(ERROR_AUTODATASEG_EXCEEDS_64k) + CHK_ERRA(ERROR_RING2SEG_MUST_BE_MOVABLE) + CHK_ERRA(ERROR_RELOC_CHAIN_XEEDS_SEGLIM) + CHK_ERR_WIN32A(ERROR_INFLOOP_IN_RELOC_CHAIN) + CHK_ERR_WIN32A(ERROR_ENVVAR_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NO_SIGNAL_SENT) + CHK_ERR_WIN32A(ERROR_FILENAME_EXCED_RANGE) + CHK_ERR_WIN32A(ERROR_RING2_STACK_IN_USE) + CHK_ERR_WIN32A(ERROR_META_EXPANSION_TOO_LONG) + CHK_ERR_WIN32A(ERROR_INVALID_SIGNAL_NUMBER) + CHK_ERR_WIN32A(ERROR_THREAD_1_INACTIVE) + CHK_ERR_WIN32A(ERROR_LOCKED) + CHK_ERR_WIN32A(ERROR_TOO_MANY_MODULES) + CHK_ERR_WIN32A(ERROR_NESTING_NOT_ALLOWED) + CHK_ERR_WIN32A(ERROR_EXE_MACHINE_TYPE_MISMATCH) + CHK_ERR_WIN32A(ERROR_BAD_PIPE) + CHK_ERR_WIN32A(ERROR_PIPE_BUSY) + CHK_ERR_WIN32A(ERROR_NO_DATA) + CHK_ERR_WIN32A(ERROR_PIPE_NOT_CONNECTED) + CHK_ERR_WIN32A(ERROR_MORE_DATA) + CHK_ERR_WIN32A(ERROR_VC_DISCONNECTED) + CHK_ERR_WIN32A(ERROR_INVALID_EA_NAME) + CHK_ERR_WIN32A(ERROR_EA_LIST_INCONSISTENT) + CHK_ERR_WIN32A(WAIT_TIMEOUT) + CHK_ERR_WIN32A(ERROR_NO_MORE_ITEMS) + CHK_ERR_WIN32A(ERROR_CANNOT_COPY) + CHK_ERR_WIN32A(ERROR_DIRECTORY) + CHK_ERR_WIN32A(ERROR_EAS_DIDNT_FIT) + CHK_ERR_WIN32A(ERROR_EA_FILE_CORRUPT) + CHK_ERR_WIN32A(ERROR_EA_TABLE_FULL) + CHK_ERR_WIN32A(ERROR_INVALID_EA_HANDLE) + CHK_ERR_WIN32A(ERROR_EAS_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_NOT_OWNER) + CHK_ERR_WIN32A(ERROR_TOO_MANY_POSTS) + CHK_ERR_WIN32A(ERROR_PARTIAL_COPY) + CHK_ERR_WIN32A(ERROR_OPLOCK_NOT_GRANTED) + CHK_ERR_WIN32A(ERROR_INVALID_OPLOCK_PROTOCOL) + CHK_ERR_WIN32A(ERROR_MR_MID_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_INVALID_ADDRESS) + CHK_ERR_WIN32A(ERROR_ARITHMETIC_OVERFLOW) + CHK_ERR_WIN32A(ERROR_PIPE_CONNECTED) + CHK_ERR_WIN32A(ERROR_PIPE_LISTENING) + CHK_ERR_WIN32A(ERROR_EA_ACCESS_DENIED) + CHK_ERR_WIN32A(ERROR_OPERATION_ABORTED) + CHK_ERR_WIN32A(ERROR_IO_INCOMPLETE) + CHK_ERR_WIN32A(ERROR_IO_PENDING) + CHK_ERR_WIN32A(ERROR_NOACCESS) + CHK_ERR_WIN32A(ERROR_SWAPERROR) + CHK_ERR_WIN32A(ERROR_STACK_OVERFLOW) + CHK_ERR_WIN32A(ERROR_INVALID_MESSAGE) + CHK_ERR_WIN32A(ERROR_CAN_NOT_COMPLETE) + CHK_ERR_WIN32A(ERROR_INVALID_FLAGS) + CHK_ERR_WIN32A(ERROR_UNRECOGNIZED_VOLUME) + CHK_ERR_WIN32A(ERROR_FILE_INVALID) + CHK_ERR_WIN32A(ERROR_FULLSCREEN_MODE) + CHK_ERR_WIN32A(ERROR_NO_TOKEN) + CHK_ERR_WIN32A(ERROR_BADDB) + CHK_ERR_WIN32A(ERROR_BADKEY) + CHK_ERR_WIN32A(ERROR_CANTOPEN) + CHK_ERR_WIN32A(ERROR_CANTREAD) + CHK_ERR_WIN32A(ERROR_CANTWRITE) + CHK_ERR_WIN32A(ERROR_REGISTRY_RECOVERED) + CHK_ERR_WIN32A(ERROR_REGISTRY_CORRUPT) + CHK_ERR_WIN32A(ERROR_REGISTRY_IO_FAILED) + CHK_ERR_WIN32A(ERROR_NOT_REGISTRY_FILE) + CHK_ERR_WIN32A(ERROR_KEY_DELETED) + CHK_ERR_WIN32A(ERROR_NO_LOG_SPACE) + CHK_ERR_WIN32A(ERROR_KEY_HAS_CHILDREN) + CHK_ERR_WIN32A(ERROR_CHILD_MUST_BE_VOLATILE) + CHK_ERR_WIN32A(ERROR_NOTIFY_ENUM_DIR) + CHK_ERR_WIN32A(ERROR_DEPENDENT_SERVICES_RUNNING) + CHK_ERR_WIN32A(ERROR_INVALID_SERVICE_CONTROL) + CHK_ERR_WIN32A(ERROR_SERVICE_REQUEST_TIMEOUT) + CHK_ERR_WIN32A(ERROR_SERVICE_NO_THREAD) + CHK_ERR_WIN32A(ERROR_SERVICE_DATABASE_LOCKED) + CHK_ERR_WIN32A(ERROR_SERVICE_ALREADY_RUNNING) + CHK_ERR_WIN32A(ERROR_INVALID_SERVICE_ACCOUNT) + CHK_ERR_WIN32A(ERROR_SERVICE_DISABLED) + CHK_ERR_WIN32A(ERROR_CIRCULAR_DEPENDENCY) + CHK_ERR_WIN32A(ERROR_SERVICE_DOES_NOT_EXIST) + CHK_ERR_WIN32A(ERROR_SERVICE_CANNOT_ACCEPT_CTRL) + CHK_ERR_WIN32A(ERROR_SERVICE_NOT_ACTIVE) + CHK_ERR_WIN32A(ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) + CHK_ERR_WIN32A(ERROR_EXCEPTION_IN_SERVICE) + CHK_ERR_WIN32A(ERROR_DATABASE_DOES_NOT_EXIST) + CHK_ERR_WIN32A(ERROR_SERVICE_SPECIFIC_ERROR) + CHK_ERR_WIN32A(ERROR_PROCESS_ABORTED) + CHK_ERR_WIN32A(ERROR_SERVICE_DEPENDENCY_FAIL) + CHK_ERR_WIN32A(ERROR_SERVICE_LOGON_FAILED) + CHK_ERR_WIN32A(ERROR_SERVICE_START_HANG) + CHK_ERR_WIN32A(ERROR_INVALID_SERVICE_LOCK) + CHK_ERR_WIN32A(ERROR_SERVICE_MARKED_FOR_DELETE) + CHK_ERR_WIN32A(ERROR_SERVICE_EXISTS) + CHK_ERR_WIN32A(ERROR_ALREADY_RUNNING_LKG) + CHK_ERR_WIN32A(ERROR_SERVICE_DEPENDENCY_DELETED) + CHK_ERR_WIN32A(ERROR_BOOT_ALREADY_ACCEPTED) + CHK_ERR_WIN32A(ERROR_SERVICE_NEVER_STARTED) + CHK_ERR_WIN32A(ERROR_DUPLICATE_SERVICE_NAME) + CHK_ERR_WIN32A(ERROR_DIFFERENT_SERVICE_ACCOUNT) + CHK_ERR_WIN32A(ERROR_CANNOT_DETECT_DRIVER_FAILURE) + CHK_ERR_WIN32A(ERROR_CANNOT_DETECT_PROCESS_ABORT) + CHK_ERR_WIN32A(ERROR_NO_RECOVERY_PROGRAM) + CHK_ERR_WIN32A(ERROR_SERVICE_NOT_IN_EXE) + CHK_ERR_WIN32A(ERROR_END_OF_MEDIA) + CHK_ERR_WIN32A(ERROR_FILEMARK_DETECTED) + CHK_ERR_WIN32A(ERROR_BEGINNING_OF_MEDIA) + CHK_ERR_WIN32A(ERROR_SETMARK_DETECTED) + CHK_ERR_WIN32A(ERROR_NO_DATA_DETECTED) + CHK_ERR_WIN32A(ERROR_PARTITION_FAILURE) + CHK_ERR_WIN32A(ERROR_INVALID_BLOCK_LENGTH) + CHK_ERR_WIN32A(ERROR_DEVICE_NOT_PARTITIONED) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_LOCK_MEDIA) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_UNLOAD_MEDIA) + CHK_ERR_WIN32A(ERROR_MEDIA_CHANGED) + CHK_ERR_WIN32A(ERROR_BUS_RESET) + CHK_ERR_WIN32A(ERROR_NO_MEDIA_IN_DRIVE) + CHK_ERR_WIN32A(ERROR_NO_UNICODE_TRANSLATION) + CHK_ERR_WIN32A(ERROR_DLL_INIT_FAILED) + CHK_ERR_WIN32A(ERROR_SHUTDOWN_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_NO_SHUTDOWN_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_IO_DEVICE) + CHK_ERR_WIN32A(ERROR_SERIAL_NO_DEVICE) + CHK_ERR_WIN32A(ERROR_IRQ_BUSY) + CHK_ERR_WIN32A(ERROR_MORE_WRITES) + CHK_ERR_WIN32A(ERROR_COUNTER_TIMEOUT) + CHK_ERR_WIN32A(ERROR_FLOPPY_ID_MARK_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_FLOPPY_WRONG_CYLINDER) + CHK_ERR_WIN32A(ERROR_FLOPPY_UNKNOWN_ERROR) + CHK_ERR_WIN32A(ERROR_FLOPPY_BAD_REGISTERS) + CHK_ERR_WIN32A(ERROR_DISK_RECALIBRATE_FAILED) + CHK_ERR_WIN32A(ERROR_DISK_OPERATION_FAILED) + CHK_ERR_WIN32A(ERROR_DISK_RESET_FAILED) + CHK_ERR_WIN32A(ERROR_EOM_OVERFLOW) + CHK_ERR_WIN32A(ERROR_NOT_ENOUGH_SERVER_MEMORY) + CHK_ERR_WIN32A(ERROR_POSSIBLE_DEADLOCK) + CHK_ERR_WIN32A(ERROR_MAPPED_ALIGNMENT) + CHK_ERR_WIN32A(ERROR_SET_POWER_STATE_VETOED) + CHK_ERR_WIN32A(ERROR_SET_POWER_STATE_FAILED) + CHK_ERR_WIN32A(ERROR_TOO_MANY_LINKS) + CHK_ERR_WIN32A(ERROR_OLD_WIN_VERSION) + CHK_ERR_WIN32A(ERROR_APP_WRONG_OS) + CHK_ERR_WIN32A(ERROR_SINGLE_INSTANCE_APP) + CHK_ERR_WIN32A(ERROR_RMODE_APP) + CHK_ERR_WIN32A(ERROR_INVALID_DLL) + CHK_ERR_WIN32A(ERROR_NO_ASSOCIATION) + CHK_ERR_WIN32A(ERROR_DDE_FAIL) + CHK_ERR_WIN32A(ERROR_DLL_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NO_MORE_USER_HANDLES) + CHK_ERR_WIN32A(ERROR_MESSAGE_SYNC_ONLY) + CHK_ERR_WIN32A(ERROR_SOURCE_ELEMENT_EMPTY) + CHK_ERR_WIN32A(ERROR_DESTINATION_ELEMENT_FULL) + CHK_ERR_WIN32A(ERROR_ILLEGAL_ELEMENT_ADDRESS) + CHK_ERR_WIN32A(ERROR_MAGAZINE_NOT_PRESENT) + CHK_ERR_WIN32A(ERROR_DEVICE_REINITIALIZATION_NEEDED) + CHK_ERR_WIN32A(ERROR_DEVICE_REQUIRES_CLEANING) + CHK_ERR_WIN32A(ERROR_DEVICE_DOOR_OPEN) + CHK_ERR_WIN32A(ERROR_DEVICE_NOT_CONNECTED) + CHK_ERR_WIN32A(ERROR_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NO_MATCH) + CHK_ERR_WIN32A(ERROR_SET_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_POINT_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NO_TRACKING_SERVICE) + CHK_ERR_WIN32A(ERROR_NO_VOLUME_ID) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_REMOVE_REPLACED) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_MOVE_REPLACEMENT) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_MOVE_REPLACEMENT_2) + CHK_ERR_WIN32A(ERROR_JOURNAL_DELETE_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_JOURNAL_NOT_ACTIVE) + CHK_ERR_WIN32A(ERROR_POTENTIAL_FILE_FOUND) + CHK_ERR_WIN32A(ERROR_BAD_DEVICE) + CHK_ERR_WIN32A(ERROR_CONNECTION_UNAVAIL) + CHK_ERR_WIN32A(ERROR_DEVICE_ALREADY_REMEMBERED) + CHK_ERR_WIN32A(ERROR_NO_NET_OR_BAD_PATH) + CHK_ERR_WIN32A(ERROR_BAD_PROVIDER) + CHK_ERR_WIN32A(ERROR_CANNOT_OPEN_PROFILE) + CHK_ERR_WIN32A(ERROR_BAD_PROFILE) + CHK_ERR_WIN32A(ERROR_NOT_CONTAINER) + CHK_ERR_WIN32A(ERROR_EXTENDED_ERROR) + CHK_ERR_WIN32A(ERROR_INVALID_GROUPNAME) + CHK_ERR_WIN32A(ERROR_INVALID_COMPUTERNAME) + CHK_ERR_WIN32A(ERROR_INVALID_EVENTNAME) + CHK_ERR_WIN32A(ERROR_INVALID_DOMAINNAME) + CHK_ERR_WIN32A(ERROR_INVALID_SERVICENAME) + CHK_ERR_WIN32A(ERROR_INVALID_NETNAME) + CHK_ERR_WIN32A(ERROR_INVALID_SHARENAME) + CHK_ERR_WIN32A(ERROR_INVALID_PASSWORDNAME) + CHK_ERR_WIN32A(ERROR_INVALID_MESSAGENAME) + CHK_ERR_WIN32A(ERROR_INVALID_MESSAGEDEST) + CHK_ERR_WIN32A(ERROR_SESSION_CREDENTIAL_CONFLICT) + CHK_ERR_WIN32A(ERROR_REMOTE_SESSION_LIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DUP_DOMAINNAME) + CHK_ERR_WIN32A(ERROR_NO_NETWORK) + CHK_ERR_WIN32A(ERROR_CANCELLED) + CHK_ERR_WIN32A(ERROR_USER_MAPPED_FILE) + CHK_ERR_WIN32A(ERROR_CONNECTION_REFUSED) + CHK_ERR_WIN32A(ERROR_GRACEFUL_DISCONNECT) + CHK_ERR_WIN32A(ERROR_ADDRESS_ALREADY_ASSOCIATED) + CHK_ERR_WIN32A(ERROR_ADDRESS_NOT_ASSOCIATED) + CHK_ERR_WIN32A(ERROR_CONNECTION_INVALID) + CHK_ERR_WIN32A(ERROR_CONNECTION_ACTIVE) + CHK_ERR_WIN32A(ERROR_NETWORK_UNREACHABLE) + CHK_ERR_WIN32A(ERROR_HOST_UNREACHABLE) + CHK_ERR_WIN32A(ERROR_PROTOCOL_UNREACHABLE) + CHK_ERR_WIN32A(ERROR_PORT_UNREACHABLE) + CHK_ERR_WIN32A(ERROR_REQUEST_ABORTED) + CHK_ERR_WIN32A(ERROR_CONNECTION_ABORTED) + CHK_ERR_WIN32A(ERROR_RETRY) + CHK_ERR_WIN32A(ERROR_CONNECTION_COUNT_LIMIT) + CHK_ERR_WIN32A(ERROR_LOGIN_TIME_RESTRICTION) + CHK_ERR_WIN32A(ERROR_LOGIN_WKSTA_RESTRICTION) + CHK_ERR_WIN32A(ERROR_INCORRECT_ADDRESS) + CHK_ERR_WIN32A(ERROR_ALREADY_REGISTERED) + CHK_ERR_WIN32A(ERROR_SERVICE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NOT_AUTHENTICATED) + CHK_ERR_WIN32A(ERROR_NOT_LOGGED_ON) + CHK_ERR_WIN32A(ERROR_CONTINUE) + CHK_ERR_WIN32A(ERROR_ALREADY_INITIALIZED) + CHK_ERR_WIN32A(ERROR_NO_MORE_DEVICES) + CHK_ERR_WIN32A(ERROR_NO_SUCH_SITE) + CHK_ERR_WIN32A(ERROR_DOMAIN_CONTROLLER_EXISTS) + CHK_ERR_WIN32A(ERROR_NOT_ALL_ASSIGNED) + CHK_ERR_WIN32A(ERROR_SOME_NOT_MAPPED) + CHK_ERR_WIN32A(ERROR_NO_QUOTAS_FOR_ACCOUNT) + CHK_ERR_WIN32A(ERROR_LOCAL_USER_SESSION_KEY) + CHK_ERR_WIN32A(ERROR_NULL_LM_PASSWORD) + CHK_ERR_WIN32A(ERROR_UNKNOWN_REVISION) + CHK_ERR_WIN32A(ERROR_REVISION_MISMATCH) + CHK_ERR_WIN32A(ERROR_INVALID_OWNER) + CHK_ERR_WIN32A(ERROR_INVALID_PRIMARY_GROUP) + CHK_ERR_WIN32A(ERROR_NO_IMPERSONATION_TOKEN) + CHK_ERR_WIN32A(ERROR_CANT_DISABLE_MANDATORY) + CHK_ERR_WIN32A(ERROR_NO_LOGON_SERVERS) + CHK_ERR_WIN32A(ERROR_NO_SUCH_LOGON_SESSION) + CHK_ERR_WIN32A(ERROR_NO_SUCH_PRIVILEGE) + CHK_ERR_WIN32A(ERROR_PRIVILEGE_NOT_HELD) + CHK_ERR_WIN32A(ERROR_INVALID_ACCOUNT_NAME) + CHK_ERR_WIN32A(ERROR_USER_EXISTS) + CHK_ERR_WIN32A(ERROR_NO_SUCH_USER) + CHK_ERR_WIN32A(ERROR_GROUP_EXISTS) + CHK_ERR_WIN32A(ERROR_NO_SUCH_GROUP) + CHK_ERR_WIN32A(ERROR_MEMBER_IN_GROUP) + CHK_ERR_WIN32A(ERROR_MEMBER_NOT_IN_GROUP) + CHK_ERR_WIN32A(ERROR_LAST_ADMIN) + CHK_ERR_WIN32A(ERROR_WRONG_PASSWORD) + CHK_ERR_WIN32A(ERROR_ILL_FORMED_PASSWORD) + CHK_ERR_WIN32A(ERROR_PASSWORD_RESTRICTION) + CHK_ERR_WIN32A(ERROR_LOGON_FAILURE) + CHK_ERR_WIN32A(ERROR_ACCOUNT_RESTRICTION) + CHK_ERR_WIN32A(ERROR_INVALID_LOGON_HOURS) + CHK_ERR_WIN32A(ERROR_INVALID_WORKSTATION) + CHK_ERR_WIN32A(ERROR_PASSWORD_EXPIRED) + CHK_ERR_WIN32A(ERROR_ACCOUNT_DISABLED) + CHK_ERR_WIN32A(ERROR_NONE_MAPPED) + CHK_ERR_WIN32A(ERROR_TOO_MANY_LUIDS_REQUESTED) + CHK_ERR_WIN32A(ERROR_LUIDS_EXHAUSTED) + CHK_ERR_WIN32A(ERROR_INVALID_SUB_AUTHORITY) + CHK_ERR_WIN32A(ERROR_INVALID_ACL) + CHK_ERR_WIN32A(ERROR_INVALID_SID) + CHK_ERR_WIN32A(ERROR_INVALID_SECURITY_DESCR) + CHK_ERR_WIN32A(ERROR_BAD_INHERITANCE_ACL) + CHK_ERR_WIN32A(ERROR_SERVER_DISABLED) + CHK_ERR_WIN32A(ERROR_SERVER_NOT_DISABLED) + CHK_ERR_WIN32A(ERROR_INVALID_ID_AUTHORITY) + CHK_ERR_WIN32A(ERROR_ALLOTTED_SPACE_EXCEEDED) + CHK_ERR_WIN32A(ERROR_INVALID_GROUP_ATTRIBUTES) + CHK_ERR_WIN32A(ERROR_BAD_IMPERSONATION_LEVEL) + CHK_ERR_WIN32A(ERROR_CANT_OPEN_ANONYMOUS) + CHK_ERR_WIN32A(ERROR_BAD_VALIDATION_CLASS) + CHK_ERR_WIN32A(ERROR_BAD_TOKEN_TYPE) + CHK_ERR_WIN32A(ERROR_NO_SECURITY_ON_OBJECT) + CHK_ERR_WIN32A(ERROR_CANT_ACCESS_DOMAIN_INFO) + CHK_ERR_WIN32A(ERROR_INVALID_SERVER_STATE) + CHK_ERR_WIN32A(ERROR_INVALID_DOMAIN_STATE) + CHK_ERR_WIN32A(ERROR_INVALID_DOMAIN_ROLE) + CHK_ERR_WIN32A(ERROR_NO_SUCH_DOMAIN) + CHK_ERR_WIN32A(ERROR_DOMAIN_EXISTS) + CHK_ERR_WIN32A(ERROR_DOMAIN_LIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_INTERNAL_DB_CORRUPTION) + CHK_ERR_WIN32A(ERROR_INTERNAL_ERROR) + CHK_ERR_WIN32A(ERROR_GENERIC_NOT_MAPPED) + CHK_ERR_WIN32A(ERROR_BAD_DESCRIPTOR_FORMAT) + CHK_ERR_WIN32A(ERROR_NOT_LOGON_PROCESS) + CHK_ERR_WIN32A(ERROR_LOGON_SESSION_EXISTS) + CHK_ERR_WIN32A(ERROR_NO_SUCH_PACKAGE) + CHK_ERR_WIN32A(ERROR_BAD_LOGON_SESSION_STATE) + CHK_ERR_WIN32A(ERROR_LOGON_SESSION_COLLISION) + CHK_ERR_WIN32A(ERROR_INVALID_LOGON_TYPE) + CHK_ERR_WIN32A(ERROR_CANNOT_IMPERSONATE) + CHK_ERR_WIN32A(ERROR_RXACT_INVALID_STATE) + CHK_ERR_WIN32A(ERROR_RXACT_COMMIT_FAILURE) + CHK_ERR_WIN32A(ERROR_SPECIAL_ACCOUNT) + CHK_ERR_WIN32A(ERROR_SPECIAL_GROUP) + CHK_ERR_WIN32A(ERROR_SPECIAL_USER) + CHK_ERR_WIN32A(ERROR_MEMBERS_PRIMARY_GROUP) + CHK_ERR_WIN32A(ERROR_TOKEN_ALREADY_IN_USE) + CHK_ERR_WIN32A(ERROR_NO_SUCH_ALIAS) + CHK_ERR_WIN32A(ERROR_MEMBER_NOT_IN_ALIAS) + CHK_ERR_WIN32A(ERROR_MEMBER_IN_ALIAS) + CHK_ERR_WIN32A(ERROR_ALIAS_EXISTS) + CHK_ERR_WIN32A(ERROR_LOGON_NOT_GRANTED) + CHK_ERR_WIN32A(ERROR_TOO_MANY_SECRETS) + CHK_ERR_WIN32A(ERROR_SECRET_TOO_LONG) + CHK_ERR_WIN32A(ERROR_INTERNAL_DB_ERROR) + CHK_ERR_WIN32A(ERROR_TOO_MANY_CONTEXT_IDS) + CHK_ERR_WIN32A(ERROR_LOGON_TYPE_NOT_GRANTED) + CHK_ERR_WIN32A(ERROR_NT_CROSS_ENCRYPTION_REQUIRED) + CHK_ERR_WIN32A(ERROR_NO_SUCH_MEMBER) + CHK_ERR_WIN32A(ERROR_INVALID_MEMBER) + CHK_ERR_WIN32A(ERROR_TOO_MANY_SIDS) + CHK_ERR_WIN32A(ERROR_LM_CROSS_ENCRYPTION_REQUIRED) + CHK_ERR_WIN32A(ERROR_NO_INHERITANCE) + CHK_ERR_WIN32A(ERROR_FILE_CORRUPT) + CHK_ERR_WIN32A(ERROR_DISK_CORRUPT) + CHK_ERR_WIN32A(ERROR_NO_USER_SESSION_KEY) + CHK_ERR_WIN32A(ERROR_LICENSE_QUOTA_EXCEEDED) + CHK_ERR_WIN32A(ERROR_WRONG_TARGET_NAME) + CHK_ERR_WIN32A(ERROR_MUTUAL_AUTH_FAILED) + CHK_ERR_WIN32A(ERROR_TIME_SKEW) + CHK_ERR_WIN32A(ERROR_INVALID_WINDOW_HANDLE) + CHK_ERR_WIN32A(ERROR_INVALID_MENU_HANDLE) + CHK_ERR_WIN32A(ERROR_INVALID_CURSOR_HANDLE) + CHK_ERR_WIN32A(ERROR_INVALID_ACCEL_HANDLE) + CHK_ERR_WIN32A(ERROR_INVALID_HOOK_HANDLE) + CHK_ERR_WIN32A(ERROR_INVALID_DWP_HANDLE) + CHK_ERR_WIN32A(ERROR_TLW_WITH_WSCHILD) + CHK_ERR_WIN32A(ERROR_CANNOT_FIND_WND_CLASS) + CHK_ERR_WIN32A(ERROR_WINDOW_OF_OTHER_THREAD) + CHK_ERR_WIN32A(ERROR_HOTKEY_ALREADY_REGISTERED) + CHK_ERR_WIN32A(ERROR_CLASS_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_CLASS_DOES_NOT_EXIST) + CHK_ERR_WIN32A(ERROR_CLASS_HAS_WINDOWS) + CHK_ERR_WIN32A(ERROR_INVALID_INDEX) + CHK_ERR_WIN32A(ERROR_INVALID_ICON_HANDLE) + CHK_ERR_WIN32A(ERROR_PRIVATE_DIALOG_INDEX) + CHK_ERR_WIN32A(ERROR_LISTBOX_ID_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NO_WILDCARD_CHARACTERS) + CHK_ERR_WIN32A(ERROR_CLIPBOARD_NOT_OPEN) + CHK_ERR_WIN32A(ERROR_HOTKEY_NOT_REGISTERED) + CHK_ERR_WIN32A(ERROR_WINDOW_NOT_DIALOG) + CHK_ERR_WIN32A(ERROR_CONTROL_ID_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_INVALID_COMBOBOX_MESSAGE) + CHK_ERR_WIN32A(ERROR_WINDOW_NOT_COMBOBOX) + CHK_ERR_WIN32A(ERROR_INVALID_EDIT_HEIGHT) + CHK_ERR_WIN32A(ERROR_DC_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_INVALID_HOOK_FILTER) + CHK_ERR_WIN32A(ERROR_INVALID_FILTER_PROC) + CHK_ERR_WIN32A(ERROR_HOOK_NEEDS_HMOD) + CHK_ERR_WIN32A(ERROR_GLOBAL_ONLY_HOOK) + CHK_ERR_WIN32A(ERROR_JOURNAL_HOOK_SET) + CHK_ERR_WIN32A(ERROR_HOOK_NOT_INSTALLED) + CHK_ERR_WIN32A(ERROR_INVALID_LB_MESSAGE) + CHK_ERR_WIN32A(ERROR_SETCOUNT_ON_BAD_LB) + CHK_ERR_WIN32A(ERROR_LB_WITHOUT_TABSTOPS) + CHK_ERR_WIN32A(ERROR_DESTROY_OBJECT_OF_OTHER_THREAD) + CHK_ERR_WIN32A(ERROR_CHILD_WINDOW_MENU) + CHK_ERR_WIN32A(ERROR_NO_SYSTEM_MENU) + CHK_ERR_WIN32A(ERROR_INVALID_MSGBOX_STYLE) + CHK_ERR_WIN32A(ERROR_INVALID_SPI_VALUE) + CHK_ERR_WIN32A(ERROR_SCREEN_ALREADY_LOCKED) + CHK_ERR_WIN32A(ERROR_HWNDS_HAVE_DIFF_PARENT) + CHK_ERR_WIN32A(ERROR_NOT_CHILD_WINDOW) + CHK_ERR_WIN32A(ERROR_INVALID_GW_COMMAND) + CHK_ERR_WIN32A(ERROR_INVALID_THREAD_ID) + CHK_ERR_WIN32A(ERROR_NON_MDICHILD_WINDOW) + CHK_ERR_WIN32A(ERROR_POPUP_ALREADY_ACTIVE) + CHK_ERR_WIN32A(ERROR_NO_SCROLLBARS) + CHK_ERR_WIN32A(ERROR_INVALID_SCROLLBAR_RANGE) + CHK_ERR_WIN32A(ERROR_INVALID_SHOWWIN_COMMAND) + CHK_ERR_WIN32A(ERROR_NO_SYSTEM_RESOURCES) + CHK_ERR_WIN32A(ERROR_NONPAGED_SYSTEM_RESOURCES) + CHK_ERR_WIN32A(ERROR_PAGED_SYSTEM_RESOURCES) + CHK_ERR_WIN32A(ERROR_WORKING_SET_QUOTA) + CHK_ERR_WIN32A(ERROR_PAGEFILE_QUOTA) + CHK_ERR_WIN32A(ERROR_COMMITMENT_LIMIT) + CHK_ERR_WIN32A(ERROR_MENU_ITEM_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_INVALID_KEYBOARD_HANDLE) + CHK_ERR_WIN32A(ERROR_HOOK_TYPE_NOT_ALLOWED) + CHK_ERR_WIN32A(ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION) + CHK_ERR_WIN32A(ERROR_TIMEOUT) + CHK_ERR_WIN32A(ERROR_INVALID_MONITOR_HANDLE) + CHK_ERR_WIN32A(ERROR_EVENTLOG_FILE_CORRUPT) + CHK_ERR_WIN32A(ERROR_EVENTLOG_CANT_START) + CHK_ERR_WIN32A(ERROR_LOG_FILE_FULL) + CHK_ERR_WIN32A(ERROR_EVENTLOG_FILE_CHANGED) + CHK_ERR_WIN32A(ERROR_INSTALL_USEREXIT) + CHK_ERR_WIN32A(ERROR_INSTALL_FAILURE) + CHK_ERR_WIN32A(ERROR_INSTALL_SUSPEND) + CHK_ERR_WIN32A(ERROR_UNKNOWN_PRODUCT) + CHK_ERR_WIN32A(ERROR_UNKNOWN_FEATURE) + CHK_ERR_WIN32A(ERROR_UNKNOWN_COMPONENT) + CHK_ERR_WIN32A(ERROR_UNKNOWN_PROPERTY) + CHK_ERR_WIN32A(ERROR_INVALID_HANDLE_STATE) + CHK_ERR_WIN32A(ERROR_BAD_CONFIGURATION) + CHK_ERR_WIN32A(ERROR_INDEX_ABSENT) + CHK_ERR_WIN32A(ERROR_INSTALL_SOURCE_ABSENT) + CHK_ERR_WIN32A(ERROR_PRODUCT_UNINSTALLED) + CHK_ERR_WIN32A(ERROR_BAD_QUERY_SYNTAX) + CHK_ERR_WIN32A(ERROR_INVALID_FIELD) + CHK_ERR_WIN32A(ERROR_DEVICE_REMOVED) + CHK_ERR_WIN32A(RPC_S_INVALID_STRING_BINDING) + CHK_ERR_WIN32A(RPC_S_WRONG_KIND_OF_BINDING) + CHK_ERR_WIN32A(RPC_S_INVALID_BINDING) + CHK_ERR_WIN32A(RPC_S_PROTSEQ_NOT_SUPPORTED) + CHK_ERR_WIN32A(RPC_S_INVALID_RPC_PROTSEQ) + CHK_ERR_WIN32A(RPC_S_INVALID_STRING_UUID) + CHK_ERR_WIN32A(RPC_S_INVALID_ENDPOINT_FORMAT) + CHK_ERR_WIN32A(RPC_S_INVALID_NET_ADDR) + CHK_ERR_WIN32A(RPC_S_NO_ENDPOINT_FOUND) + CHK_ERR_WIN32A(RPC_S_INVALID_TIMEOUT) + CHK_ERR_WIN32A(RPC_S_OBJECT_NOT_FOUND) + CHK_ERR_WIN32A(RPC_S_ALREADY_REGISTERED) + CHK_ERR_WIN32A(RPC_S_TYPE_ALREADY_REGISTERED) + CHK_ERR_WIN32A(RPC_S_ALREADY_LISTENING) + CHK_ERR_WIN32A(RPC_S_NO_PROTSEQS_REGISTERED) + CHK_ERR_WIN32A(RPC_S_NOT_LISTENING) + CHK_ERR_WIN32A(RPC_S_UNKNOWN_MGR_TYPE) + CHK_ERR_WIN32A(RPC_S_UNKNOWN_IF) + CHK_ERR_WIN32A(RPC_S_NO_BINDINGS) + CHK_ERR_WIN32A(RPC_S_NO_PROTSEQS) + CHK_ERR_WIN32A(RPC_S_CANT_CREATE_ENDPOINT) + CHK_ERR_WIN32A(RPC_S_OUT_OF_RESOURCES) + CHK_ERR_WIN32A(RPC_S_SERVER_UNAVAILABLE) + CHK_ERR_WIN32A(RPC_S_SERVER_TOO_BUSY) + CHK_ERR_WIN32A(RPC_S_INVALID_NETWORK_OPTIONS) + CHK_ERR_WIN32A(RPC_S_NO_CALL_ACTIVE) + CHK_ERR_WIN32A(RPC_S_CALL_FAILED) + CHK_ERR_WIN32A(RPC_S_CALL_FAILED_DNE) + CHK_ERR_WIN32A(RPC_S_PROTOCOL_ERROR) + CHK_ERR_WIN32A(RPC_S_UNSUPPORTED_TRANS_SYN) + CHK_ERR_WIN32A(RPC_S_UNSUPPORTED_TYPE) + CHK_ERR_WIN32A(RPC_S_INVALID_TAG) + CHK_ERR_WIN32A(RPC_S_INVALID_BOUND) + CHK_ERR_WIN32A(RPC_S_NO_ENTRY_NAME) + CHK_ERR_WIN32A(RPC_S_INVALID_NAME_SYNTAX) + CHK_ERR_WIN32A(RPC_S_UNSUPPORTED_NAME_SYNTAX) + CHK_ERR_WIN32A(RPC_S_UUID_NO_ADDRESS) + CHK_ERR_WIN32A(RPC_S_DUPLICATE_ENDPOINT) + CHK_ERR_WIN32A(RPC_S_UNKNOWN_AUTHN_TYPE) + CHK_ERR_WIN32A(RPC_S_MAX_CALLS_TOO_SMALL) + CHK_ERR_WIN32A(RPC_S_STRING_TOO_LONG) + CHK_ERR_WIN32A(RPC_S_PROTSEQ_NOT_FOUND) + CHK_ERR_WIN32A(RPC_S_PROCNUM_OUT_OF_RANGE) + CHK_ERR_WIN32A(RPC_S_BINDING_HAS_NO_AUTH) + CHK_ERR_WIN32A(RPC_S_UNKNOWN_AUTHN_SERVICE) + CHK_ERR_WIN32A(RPC_S_UNKNOWN_AUTHN_LEVEL) + CHK_ERR_WIN32A(RPC_S_INVALID_AUTH_IDENTITY) + CHK_ERR_WIN32A(RPC_S_UNKNOWN_AUTHZ_SERVICE) + CHK_ERR_WIN32A(EPT_S_INVALID_ENTRY) + CHK_ERR_WIN32A(EPT_S_CANT_PERFORM_OP) + CHK_ERR_WIN32A(EPT_S_NOT_REGISTERED) + CHK_ERR_WIN32A(RPC_S_NOTHING_TO_EXPORT) + CHK_ERR_WIN32A(RPC_S_INCOMPLETE_NAME) + CHK_ERR_WIN32A(RPC_S_INVALID_VERS_OPTION) + CHK_ERR_WIN32A(RPC_S_NO_MORE_MEMBERS) + CHK_ERR_WIN32A(RPC_S_NOT_ALL_OBJS_UNEXPORTED) + CHK_ERR_WIN32A(RPC_S_INTERFACE_NOT_FOUND) + CHK_ERR_WIN32A(RPC_S_ENTRY_ALREADY_EXISTS) + CHK_ERR_WIN32A(RPC_S_ENTRY_NOT_FOUND) + CHK_ERR_WIN32A(RPC_S_NAME_SERVICE_UNAVAILABLE) + CHK_ERR_WIN32A(RPC_S_INVALID_NAF_ID) + CHK_ERR_WIN32A(RPC_S_CANNOT_SUPPORT) + CHK_ERR_WIN32A(RPC_S_NO_CONTEXT_AVAILABLE) + CHK_ERR_WIN32A(RPC_S_INTERNAL_ERROR) + CHK_ERR_WIN32A(RPC_S_ZERO_DIVIDE) + CHK_ERR_WIN32A(RPC_S_ADDRESS_ERROR) + CHK_ERR_WIN32A(RPC_S_FP_DIV_ZERO) + CHK_ERR_WIN32A(RPC_S_FP_UNDERFLOW) + CHK_ERR_WIN32A(RPC_S_FP_OVERFLOW) + CHK_ERR_WIN32A(RPC_X_NO_MORE_ENTRIES) + CHK_ERR_WIN32A(RPC_X_SS_CHAR_TRANS_OPEN_FAIL) + CHK_ERR_WIN32A(RPC_X_SS_CHAR_TRANS_SHORT_FILE) + CHK_ERR_WIN32A(RPC_X_SS_IN_NULL_CONTEXT) + CHK_ERR_WIN32A(RPC_X_SS_CONTEXT_DAMAGED) + CHK_ERR_WIN32A(RPC_X_SS_HANDLES_MISMATCH) + CHK_ERR_WIN32A(RPC_X_SS_CANNOT_GET_CALL_HANDLE) + CHK_ERR_WIN32A(RPC_X_NULL_REF_POINTER) + CHK_ERR_WIN32A(RPC_X_ENUM_VALUE_OUT_OF_RANGE) + CHK_ERR_WIN32A(RPC_X_BYTE_COUNT_TOO_SMALL) + CHK_ERR_WIN32A(RPC_X_BAD_STUB_DATA) + CHK_ERR_WIN32A(ERROR_INVALID_USER_BUFFER) + CHK_ERR_WIN32A(ERROR_UNRECOGNIZED_MEDIA) + CHK_ERR_WIN32A(ERROR_NO_TRUST_LSA_SECRET) + CHK_ERR_WIN32A(ERROR_NO_TRUST_SAM_ACCOUNT) + CHK_ERR_WIN32A(ERROR_TRUSTED_DOMAIN_FAILURE) + CHK_ERR_WIN32A(ERROR_TRUSTED_RELATIONSHIP_FAILURE) + CHK_ERR_WIN32A(ERROR_TRUST_FAILURE) + CHK_ERR_WIN32A(RPC_S_CALL_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_NETLOGON_NOT_STARTED) + CHK_ERR_WIN32A(ERROR_ACCOUNT_EXPIRED) + CHK_ERR_WIN32A(ERROR_REDIRECTOR_HAS_OPEN_HANDLES) + CHK_ERR_WIN32A(ERROR_PRINTER_DRIVER_ALREADY_INSTALLED) + CHK_ERR_WIN32A(ERROR_UNKNOWN_PORT) + CHK_ERR_WIN32A(ERROR_UNKNOWN_PRINTER_DRIVER) + CHK_ERR_WIN32A(ERROR_UNKNOWN_PRINTPROCESSOR) + CHK_ERR_WIN32A(ERROR_INVALID_SEPARATOR_FILE) + CHK_ERR_WIN32A(ERROR_INVALID_PRIORITY) + CHK_ERR_WIN32A(ERROR_INVALID_PRINTER_NAME) + CHK_ERR_WIN32A(ERROR_PRINTER_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_INVALID_PRINTER_COMMAND) + CHK_ERR_WIN32A(ERROR_INVALID_DATATYPE) + CHK_ERR_WIN32A(ERROR_INVALID_ENVIRONMENT) + CHK_ERR_WIN32A(RPC_S_NO_MORE_BINDINGS) + CHK_ERR_WIN32A(ERROR_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT) + CHK_ERR_WIN32A(ERROR_NOLOGON_WORKSTATION_TRUST_ACCOUNT) + CHK_ERR_WIN32A(ERROR_NOLOGON_SERVER_TRUST_ACCOUNT) + CHK_ERR_WIN32A(ERROR_DOMAIN_TRUST_INCONSISTENT) + CHK_ERR_WIN32A(ERROR_SERVER_HAS_OPEN_HANDLES) + CHK_ERR_WIN32A(ERROR_RESOURCE_DATA_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_RESOURCE_TYPE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_RESOURCE_NAME_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_RESOURCE_LANG_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NOT_ENOUGH_QUOTA) + CHK_ERR_WIN32A(RPC_S_NO_INTERFACES) + CHK_ERR_WIN32A(RPC_S_CALL_CANCELLED) + CHK_ERR_WIN32A(RPC_S_BINDING_INCOMPLETE) + CHK_ERR_WIN32A(RPC_S_COMM_FAILURE) + CHK_ERR_WIN32A(RPC_S_UNSUPPORTED_AUTHN_LEVEL) + CHK_ERR_WIN32A(RPC_S_NO_PRINC_NAME) + CHK_ERR_WIN32A(RPC_S_NOT_RPC_ERROR) + CHK_ERR_WIN32A(RPC_S_UUID_LOCAL_ONLY) + CHK_ERR_WIN32A(RPC_S_SEC_PKG_ERROR) + CHK_ERR_WIN32A(RPC_S_NOT_CANCELLED) + CHK_ERR_WIN32A(RPC_X_INVALID_ES_ACTION) + CHK_ERR_WIN32A(RPC_X_WRONG_ES_VERSION) + CHK_ERR_WIN32A(RPC_X_WRONG_STUB_VERSION) + CHK_ERR_WIN32A(RPC_X_INVALID_PIPE_OBJECT) + CHK_ERR_WIN32A(RPC_X_WRONG_PIPE_ORDER) + CHK_ERR_WIN32A(RPC_X_WRONG_PIPE_VERSION) + CHK_ERR_WIN32A(RPC_S_GROUP_MEMBER_NOT_FOUND) + CHK_ERR_WIN32A(EPT_S_CANT_CREATE) + CHK_ERR_WIN32A(RPC_S_INVALID_OBJECT) + CHK_ERR_WIN32A(ERROR_INVALID_TIME) + CHK_ERR_WIN32A(ERROR_INVALID_FORM_NAME) + CHK_ERR_WIN32A(ERROR_INVALID_FORM_SIZE) + CHK_ERR_WIN32A(ERROR_ALREADY_WAITING) + CHK_ERR_WIN32A(ERROR_PRINTER_DELETED) + CHK_ERR_WIN32A(ERROR_INVALID_PRINTER_STATE) + CHK_ERR_WIN32A(ERROR_PASSWORD_MUST_CHANGE) + CHK_ERR_WIN32A(ERROR_DOMAIN_CONTROLLER_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_ACCOUNT_LOCKED_OUT) + CHK_ERR_WIN32A(OR_INVALID_OXID) + CHK_ERR_WIN32A(OR_INVALID_OID) + CHK_ERR_WIN32A(OR_INVALID_SET) + CHK_ERR_WIN32A(RPC_S_SEND_INCOMPLETE) + CHK_ERR_WIN32A(RPC_S_INVALID_ASYNC_HANDLE) + CHK_ERR_WIN32A(RPC_S_INVALID_ASYNC_CALL) + CHK_ERR_WIN32A(RPC_X_PIPE_CLOSED) + CHK_ERR_WIN32A(RPC_X_PIPE_DISCIPLINE_ERROR) + CHK_ERR_WIN32A(RPC_X_PIPE_EMPTY) + CHK_ERR_WIN32A(ERROR_NO_SITENAME) + CHK_ERR_WIN32A(ERROR_CANT_ACCESS_FILE) + CHK_ERR_WIN32A(ERROR_CANT_RESOLVE_FILENAME) + CHK_ERR_WIN32A(ERROR_INVALID_PIXEL_FORMAT) + CHK_ERR_WIN32A(ERROR_BAD_DRIVER) + CHK_ERR_WIN32A(ERROR_INVALID_WINDOW_STYLE) + CHK_ERR_WIN32A(ERROR_METAFILE_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_TRANSFORM_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_CLIPPING_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_INVALID_CMM) + CHK_ERR_WIN32A(ERROR_INVALID_PROFILE) + CHK_ERR_WIN32A(ERROR_TAG_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_TAG_NOT_PRESENT) + CHK_ERR_WIN32A(ERROR_DUPLICATE_TAG) + CHK_ERR_WIN32A(ERROR_PROFILE_NOT_ASSOCIATED_WITH_DEVICE) + CHK_ERR_WIN32A(ERROR_PROFILE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_INVALID_COLORSPACE) + CHK_ERR_WIN32A(ERROR_ICM_NOT_ENABLED) + CHK_ERR_WIN32A(ERROR_DELETING_ICM_XFORM) + CHK_ERR_WIN32A(ERROR_INVALID_TRANSFORM) + CHK_ERR_WIN32A(ERROR_COLORSPACE_MISMATCH) + CHK_ERR_WIN32A(ERROR_INVALID_COLORINDEX) + CHK_ERR_WIN32A(ERROR_CONNECTED_OTHER_PASSWORD) + CHK_ERR_WIN32A(ERROR_BAD_USERNAME) + CHK_ERR_WIN32A(ERROR_NOT_CONNECTED) + CHK_ERR_WIN32A(ERROR_OPEN_FILES) + CHK_ERR_WIN32A(ERROR_ACTIVE_CONNECTIONS) + CHK_ERR_WIN32A(ERROR_DEVICE_IN_USE) + CHK_ERR_WIN32A(ERROR_UNKNOWN_PRINT_MONITOR) + CHK_ERR_WIN32A(ERROR_PRINTER_DRIVER_IN_USE) + CHK_ERR_WIN32A(ERROR_SPOOL_FILE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_SPL_NO_STARTDOC) + CHK_ERR_WIN32A(ERROR_SPL_NO_ADDJOB) + CHK_ERR_WIN32A(ERROR_PRINT_PROCESSOR_ALREADY_INSTALLED) + CHK_ERR_WIN32A(ERROR_PRINT_MONITOR_ALREADY_INSTALLED) + CHK_ERR_WIN32A(ERROR_INVALID_PRINT_MONITOR) + CHK_ERR_WIN32A(ERROR_PRINT_MONITOR_IN_USE) + CHK_ERR_WIN32A(ERROR_PRINTER_HAS_JOBS_QUEUED) + CHK_ERR_WIN32A(ERROR_SUCCESS_REBOOT_REQUIRED) + CHK_ERR_WIN32A(ERROR_SUCCESS_RESTART_REQUIRED) + CHK_ERR_WIN32A(ERROR_PRINTER_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_WINS_INTERNAL) + CHK_ERR_WIN32A(ERROR_CAN_NOT_DEL_LOCAL_WINS) + CHK_ERR_WIN32A(ERROR_STATIC_INIT) + CHK_ERR_WIN32A(ERROR_INC_BACKUP) + CHK_ERR_WIN32A(ERROR_FULL_BACKUP) + CHK_ERR_WIN32A(ERROR_REC_NON_EXISTENT) + CHK_ERR_WIN32A(ERROR_RPL_NOT_ALLOWED) + CHK_ERR_WIN32A(ERROR_DHCP_ADDRESS_CONFLICT) + CHK_ERR_WIN32A(ERROR_WMI_GUID_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_WMI_INSTANCE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_WMI_ITEMID_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_WMI_TRY_AGAIN) + CHK_ERR_WIN32A(ERROR_WMI_DP_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_WMI_UNRESOLVED_INSTANCE_REF) + CHK_ERR_WIN32A(ERROR_WMI_ALREADY_ENABLED) + CHK_ERR_WIN32A(ERROR_WMI_GUID_DISCONNECTED) + CHK_ERR_WIN32A(ERROR_WMI_SERVER_UNAVAILABLE) + CHK_ERR_WIN32A(ERROR_WMI_DP_FAILED) + CHK_ERR_WIN32A(ERROR_WMI_INVALID_MOF) + CHK_ERR_WIN32A(ERROR_WMI_INVALID_REGINFO) + CHK_ERR_WIN32A(ERROR_WMI_ALREADY_DISABLED) + CHK_ERR_WIN32A(ERROR_WMI_READ_ONLY) + CHK_ERR_WIN32A(ERROR_WMI_SET_FAILURE) + CHK_ERR_WIN32A(ERROR_INVALID_MEDIA) + CHK_ERR_WIN32A(ERROR_INVALID_LIBRARY) + CHK_ERR_WIN32A(ERROR_INVALID_MEDIA_POOL) + CHK_ERR_WIN32A(ERROR_DRIVE_MEDIA_MISMATCH) + CHK_ERR_WIN32A(ERROR_MEDIA_OFFLINE) + CHK_ERR_WIN32A(ERROR_LIBRARY_OFFLINE) + CHK_ERR_WIN32A(ERROR_EMPTY) + CHK_ERR_WIN32A(ERROR_NOT_EMPTY) + CHK_ERR_WIN32A(ERROR_MEDIA_UNAVAILABLE) + CHK_ERR_WIN32A(ERROR_RESOURCE_DISABLED) + CHK_ERR_WIN32A(ERROR_INVALID_CLEANER) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_CLEAN) + CHK_ERR_WIN32A(ERROR_OBJECT_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DATABASE_FAILURE) + CHK_ERR_WIN32A(ERROR_DATABASE_FULL) + CHK_ERR_WIN32A(ERROR_MEDIA_INCOMPATIBLE) + CHK_ERR_WIN32A(ERROR_RESOURCE_NOT_PRESENT) + CHK_ERR_WIN32A(ERROR_INVALID_OPERATION) + CHK_ERR_WIN32A(ERROR_MEDIA_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_DEVICE_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_REQUEST_REFUSED) + CHK_ERR_WIN32A(ERROR_INVALID_DRIVE_OBJECT) + CHK_ERR_WIN32A(ERROR_LIBRARY_FULL) + CHK_ERR_WIN32A(ERROR_MEDIUM_NOT_ACCESSIBLE) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_LOAD_MEDIUM) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_INVENTORY_DRIVE) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_INVENTORY_SLOT) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_INVENTORY_TRANSPORT) + CHK_ERR_WIN32A(ERROR_TRANSPORT_FULL) + CHK_ERR_WIN32A(ERROR_CONTROLLING_IEPORT) + CHK_ERR_WIN32A(ERROR_UNABLE_TO_EJECT_MOUNTED_MEDIA) + CHK_ERR_WIN32A(ERROR_CLEANER_SLOT_SET) + CHK_ERR_WIN32A(ERROR_CLEANER_SLOT_NOT_SET) + CHK_ERR_WIN32A(ERROR_CLEANER_CARTRIDGE_SPENT) + CHK_ERR_WIN32A(ERROR_UNEXPECTED_OMID) + CHK_ERR_WIN32A(ERROR_CANT_DELETE_LAST_ITEM) + CHK_ERR_WIN32A(ERROR_MESSAGE_EXCEEDS_MAX_SIZE) + CHK_ERR_WIN32A(ERROR_FILE_OFFLINE) + CHK_ERR_WIN32A(ERROR_REMOTE_STORAGE_NOT_ACTIVE) + CHK_ERR_WIN32A(ERROR_REMOTE_STORAGE_MEDIA_ERROR) + CHK_ERR_WIN32A(ERROR_NOT_A_REPARSE_POINT) + CHK_ERR_WIN32A(ERROR_REPARSE_ATTRIBUTE_CONFLICT) + CHK_ERR_WIN32A(ERROR_INVALID_REPARSE_DATA) + CHK_ERR_WIN32A(ERROR_DEPENDENT_RESOURCE_EXISTS) + CHK_ERR_WIN32A(ERROR_DEPENDENCY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DEPENDENCY_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_RESOURCE_NOT_ONLINE) + CHK_ERR_WIN32A(ERROR_HOST_NODE_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_RESOURCE_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_RESOURCE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_SHUTDOWN_CLUSTER) + CHK_ERR_WIN32A(ERROR_CANT_EVICT_ACTIVE_NODE) + CHK_ERR_WIN32A(ERROR_OBJECT_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_OBJECT_IN_LIST) + CHK_ERR_WIN32A(ERROR_GROUP_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_GROUP_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_GROUP_NOT_ONLINE) + CHK_ERR_WIN32A(ERROR_HOST_NODE_NOT_RESOURCE_OWNER) + CHK_ERR_WIN32A(ERROR_HOST_NODE_NOT_GROUP_OWNER) + CHK_ERR_WIN32A(ERROR_RESMON_CREATE_FAILED) + CHK_ERR_WIN32A(ERROR_RESMON_ONLINE_FAILED) + CHK_ERR_WIN32A(ERROR_RESOURCE_ONLINE) + CHK_ERR_WIN32A(ERROR_QUORUM_RESOURCE) + CHK_ERR_WIN32A(ERROR_NOT_QUORUM_CAPABLE) + CHK_ERR_WIN32A(ERROR_CLUSTER_SHUTTING_DOWN) + CHK_ERR_WIN32A(ERROR_INVALID_STATE) + CHK_ERR_WIN32A(ERROR_RESOURCE_PROPERTIES_STORED) + CHK_ERR_WIN32A(ERROR_NOT_QUORUM_CLASS) + CHK_ERR_WIN32A(ERROR_CORE_RESOURCE) + CHK_ERR_WIN32A(ERROR_QUORUM_RESOURCE_ONLINE_FAILED) + CHK_ERR_WIN32A(ERROR_QUORUMLOG_OPEN_FAILED) + CHK_ERR_WIN32A(ERROR_CLUSTERLOG_CORRUPT) + CHK_ERR_WIN32A(ERROR_CLUSTERLOG_RECORD_EXCEEDS_MAXSIZE) + CHK_ERR_WIN32A(ERROR_CLUSTERLOG_EXCEEDS_MAXSIZE) + CHK_ERR_WIN32A(ERROR_CLUSTERLOG_CHKPOINT_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTERLOG_NOT_ENOUGH_SPACE) + CHK_ERR_WIN32A(ERROR_ENCRYPTION_FAILED) + CHK_ERR_WIN32A(ERROR_DECRYPTION_FAILED) + CHK_ERR_WIN32A(ERROR_FILE_ENCRYPTED) + CHK_ERR_WIN32A(ERROR_NO_RECOVERY_POLICY) + CHK_ERR_WIN32A(ERROR_NO_EFS) + CHK_ERR_WIN32A(ERROR_WRONG_EFS) + CHK_ERR_WIN32A(ERROR_NO_USER_KEYS) + CHK_ERR_WIN32A(ERROR_FILE_NOT_ENCRYPTED) + CHK_ERR_WIN32A(ERROR_NOT_EXPORT_FORMAT) + CHK_ERR_WIN32A(ERROR_NO_BROWSER_SERVERS_FOUND) + CHK_ERR_WIN32A(ERROR_CTX_WINSTATION_NAME_INVALID) + CHK_ERR_WIN32A(ERROR_CTX_INVALID_PD) + CHK_ERR_WIN32A(ERROR_CTX_PD_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CTX_WD_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CTX_CANNOT_MAKE_EVENTLOG_ENTRY) + CHK_ERR_WIN32A(ERROR_CTX_SERVICE_NAME_COLLISION) + CHK_ERR_WIN32A(ERROR_CTX_CLOSE_PENDING) + CHK_ERR_WIN32A(ERROR_CTX_NO_OUTBUF) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_INF_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CTX_INVALID_MODEMNAME) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_RESPONSE_ERROR) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_RESPONSE_TIMEOUT) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_RESPONSE_NO_CARRIER) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_RESPONSE_NO_DIALTONE) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_RESPONSE_BUSY) + CHK_ERR_WIN32A(ERROR_CTX_MODEM_RESPONSE_VOICE) + CHK_ERR_WIN32A(ERROR_CTX_TD_ERROR) + CHK_ERR_WIN32A(ERROR_CTX_WINSTATION_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CTX_WINSTATION_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_CTX_WINSTATION_BUSY) + CHK_ERR_WIN32A(ERROR_CTX_BAD_VIDEO_MODE) + CHK_ERR_WIN32A(ERROR_CTX_GRAPHICS_INVALID) + CHK_ERR_WIN32A(ERROR_CTX_LOGON_DISABLED) + CHK_ERR_WIN32A(ERROR_CTX_NOT_CONSOLE) + CHK_ERR_WIN32A(ERROR_CTX_CLIENT_QUERY_TIMEOUT) + CHK_ERR_WIN32A(ERROR_CTX_CONSOLE_DISCONNECT) + CHK_ERR_WIN32A(ERROR_CTX_CONSOLE_CONNECT) + CHK_ERR_WIN32A(ERROR_CTX_SHADOW_DENIED) + CHK_ERR_WIN32A(ERROR_CTX_WINSTATION_ACCESS_DENIED) + CHK_ERR_WIN32A(ERROR_CTX_INVALID_WD) + CHK_ERR_WIN32A(ERROR_CTX_SHADOW_INVALID) + CHK_ERR_WIN32A(ERROR_CTX_SHADOW_DISABLED) + CHK_ERR_WIN32A(FRS_ERR_INVALID_API_SEQUENCE) + CHK_ERR_WIN32A(FRS_ERR_STARTING_SERVICE) + CHK_ERR_WIN32A(FRS_ERR_STOPPING_SERVICE) + CHK_ERR_WIN32A(FRS_ERR_INTERNAL_API) + CHK_ERR_WIN32A(FRS_ERR_INTERNAL) + CHK_ERR_WIN32A(FRS_ERR_SERVICE_COMM) + CHK_ERR_WIN32A(FRS_ERR_INSUFFICIENT_PRIV) + CHK_ERR_WIN32A(FRS_ERR_AUTHENTICATION) + CHK_ERR_WIN32A(FRS_ERR_PARENT_INSUFFICIENT_PRIV) + CHK_ERR_WIN32A(FRS_ERR_PARENT_AUTHENTICATION) + CHK_ERR_WIN32A(FRS_ERR_CHILD_TO_PARENT_COMM) + CHK_ERR_WIN32A(FRS_ERR_PARENT_TO_CHILD_COMM) + CHK_ERR_WIN32A(FRS_ERR_SYSVOL_POPULATE) + CHK_ERR_WIN32A(FRS_ERR_SYSVOL_POPULATE_TIMEOUT) + CHK_ERR_WIN32A(FRS_ERR_SYSVOL_IS_BUSY) + CHK_ERR_WIN32A(FRS_ERR_SYSVOL_DEMOTE) + CHK_ERR_WIN32A(FRS_ERR_INVALID_SERVICE_PARAMETER) +// CHK_ERR_WIN32A(DS_S_SUCCESS) + CHK_ERR_WIN32A(ERROR_DS_NOT_INSTALLED) + CHK_ERR_WIN32A(ERROR_DS_MEMBERSHIP_EVALUATED_LOCALLY) + CHK_ERR_WIN32A(ERROR_DS_NO_ATTRIBUTE_OR_VALUE) + CHK_ERR_WIN32A(ERROR_DS_INVALID_ATTRIBUTE_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_ATTRIBUTE_TYPE_UNDEFINED) + CHK_ERR_WIN32A(ERROR_DS_ATTRIBUTE_OR_VALUE_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_BUSY) + CHK_ERR_WIN32A(ERROR_DS_UNAVAILABLE) + CHK_ERR_WIN32A(ERROR_DS_NO_RIDS_ALLOCATED) + CHK_ERR_WIN32A(ERROR_DS_NO_MORE_RIDS) + CHK_ERR_WIN32A(ERROR_DS_INCORRECT_ROLE_OWNER) + CHK_ERR_WIN32A(ERROR_DS_RIDMGR_INIT_ERROR) + CHK_ERR_WIN32A(ERROR_DS_OBJ_CLASS_VIOLATION) + CHK_ERR_WIN32A(ERROR_DS_CANT_ON_NON_LEAF) + CHK_ERR_WIN32A(ERROR_DS_CANT_ON_RDN) + CHK_ERR_WIN32A(ERROR_DS_CANT_MOD_OBJ_CLASS) + CHK_ERR_WIN32A(ERROR_DS_CROSS_DOM_MOVE_ERROR) + CHK_ERR_WIN32A(ERROR_DS_GC_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_SHARED_POLICY) + CHK_ERR_WIN32A(ERROR_POLICY_OBJECT_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_POLICY_ONLY_IN_DS) + CHK_ERR_WIN32A(ERROR_PROMOTION_ACTIVE) + CHK_ERR_WIN32A(ERROR_NO_PROMOTION_ACTIVE) + CHK_ERR_WIN32A(ERROR_DS_OPERATIONS_ERROR) + CHK_ERR_WIN32A(ERROR_DS_PROTOCOL_ERROR) + CHK_ERR_WIN32A(ERROR_DS_TIMELIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_SIZELIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_ADMIN_LIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_COMPARE_FALSE) + CHK_ERR_WIN32A(ERROR_DS_COMPARE_TRUE) + CHK_ERR_WIN32A(ERROR_DS_AUTH_METHOD_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_DS_STRONG_AUTH_REQUIRED) + CHK_ERR_WIN32A(ERROR_DS_INAPPROPRIATE_AUTH) + CHK_ERR_WIN32A(ERROR_DS_AUTH_UNKNOWN) + CHK_ERR_WIN32A(ERROR_DS_REFERRAL) + CHK_ERR_WIN32A(ERROR_DS_UNAVAILABLE_CRIT_EXTENSION) + CHK_ERR_WIN32A(ERROR_DS_CONFIDENTIALITY_REQUIRED) + CHK_ERR_WIN32A(ERROR_DS_INAPPROPRIATE_MATCHING) + CHK_ERR_WIN32A(ERROR_DS_CONSTRAINT_VIOLATION) + CHK_ERR_WIN32A(ERROR_DS_NO_SUCH_OBJECT) + CHK_ERR_WIN32A(ERROR_DS_ALIAS_PROBLEM) + CHK_ERR_WIN32A(ERROR_DS_INVALID_DN_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_IS_LEAF) + CHK_ERR_WIN32A(ERROR_DS_ALIAS_DEREF_PROBLEM) + CHK_ERR_WIN32A(ERROR_DS_UNWILLING_TO_PERFORM) + CHK_ERR_WIN32A(ERROR_DS_LOOP_DETECT) + CHK_ERR_WIN32A(ERROR_DS_NAMING_VIOLATION) + CHK_ERR_WIN32A(ERROR_DS_OBJECT_RESULTS_TOO_LARGE) + CHK_ERR_WIN32A(ERROR_DS_AFFECTS_MULTIPLE_DSAS) + CHK_ERR_WIN32A(ERROR_DS_SERVER_DOWN) + CHK_ERR_WIN32A(ERROR_DS_LOCAL_ERROR) + CHK_ERR_WIN32A(ERROR_DS_ENCODING_ERROR) + CHK_ERR_WIN32A(ERROR_DS_DECODING_ERROR) + CHK_ERR_WIN32A(ERROR_DS_FILTER_UNKNOWN) + CHK_ERR_WIN32A(ERROR_DS_PARAM_ERROR) + CHK_ERR_WIN32A(ERROR_DS_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_DS_NO_RESULTS_RETURNED) + CHK_ERR_WIN32A(ERROR_DS_CONTROL_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DS_CLIENT_LOOP) + CHK_ERR_WIN32A(ERROR_DS_REFERRAL_LIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_ROOT_MUST_BE_NC) + CHK_ERR_WIN32A(ERROR_DS_ADD_REPLICA_INHIBITED) + CHK_ERR_WIN32A(ERROR_DS_ATT_NOT_DEF_IN_SCHEMA) + CHK_ERR_WIN32A(ERROR_DS_MAX_OBJ_SIZE_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_OBJ_STRING_NAME_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_NO_RDN_DEFINED_IN_SCHEMA) + CHK_ERR_WIN32A(ERROR_DS_RDN_DOESNT_MATCH_SCHEMA) + CHK_ERR_WIN32A(ERROR_DS_NO_REQUESTED_ATTS_FOUND) + CHK_ERR_WIN32A(ERROR_DS_USER_BUFFER_TO_SMALL) + CHK_ERR_WIN32A(ERROR_DS_ATT_IS_NOT_ON_OBJ) + CHK_ERR_WIN32A(ERROR_DS_ILLEGAL_MOD_OPERATION) + CHK_ERR_WIN32A(ERROR_DS_OBJ_TOO_LARGE) + CHK_ERR_WIN32A(ERROR_DS_BAD_INSTANCE_TYPE) + CHK_ERR_WIN32A(ERROR_DS_MASTERDSA_REQUIRED) + CHK_ERR_WIN32A(ERROR_DS_OBJECT_CLASS_REQUIRED) + CHK_ERR_WIN32A(ERROR_DS_MISSING_REQUIRED_ATT) + CHK_ERR_WIN32A(ERROR_DS_ATT_NOT_DEF_FOR_CLASS) + CHK_ERR_WIN32A(ERROR_DS_ATT_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_CANT_ADD_ATT_VALUES) + CHK_ERR_WIN32A(ERROR_DS_SINGLE_VALUE_CONSTRAINT) + CHK_ERR_WIN32A(ERROR_DS_RANGE_CONSTRAINT) + CHK_ERR_WIN32A(ERROR_DS_ATT_VAL_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_CANT_REM_MISSING_ATT) + CHK_ERR_WIN32A(ERROR_DS_CANT_REM_MISSING_ATT_VAL) + CHK_ERR_WIN32A(ERROR_DS_ROOT_CANT_BE_SUBREF) + CHK_ERR_WIN32A(ERROR_DS_NO_CHAINING) + CHK_ERR_WIN32A(ERROR_DS_NO_CHAINED_EVAL) + CHK_ERR_WIN32A(ERROR_DS_NO_PARENT_OBJECT) + CHK_ERR_WIN32A(ERROR_DS_PARENT_IS_AN_ALIAS) + CHK_ERR_WIN32A(ERROR_DS_CANT_MIX_MASTER_AND_REPS) + CHK_ERR_WIN32A(ERROR_DS_CHILDREN_EXIST) + CHK_ERR_WIN32A(ERROR_DS_OBJ_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DS_ALIASED_OBJ_MISSING) + CHK_ERR_WIN32A(ERROR_DS_BAD_NAME_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_ALIAS_POINTS_TO_ALIAS) + CHK_ERR_WIN32A(ERROR_DS_CANT_DEREF_ALIAS) + CHK_ERR_WIN32A(ERROR_DS_OUT_OF_SCOPE) + CHK_ERR_WIN32A(ERROR_DS_CANT_DELETE_DSA_OBJ) + CHK_ERR_WIN32A(ERROR_DS_GENERIC_ERROR) + CHK_ERR_WIN32A(ERROR_DS_DSA_MUST_BE_INT_MASTER) + CHK_ERR_WIN32A(ERROR_DS_CLASS_NOT_DSA) + CHK_ERR_WIN32A(ERROR_DS_INSUFF_ACCESS_RIGHTS) + CHK_ERR_WIN32A(ERROR_DS_ILLEGAL_SUPERIOR) + CHK_ERR_WIN32A(ERROR_DS_ATTRIBUTE_OWNED_BY_SAM) + CHK_ERR_WIN32A(ERROR_DS_NAME_TOO_MANY_PARTS) + CHK_ERR_WIN32A(ERROR_DS_NAME_TOO_LONG) + CHK_ERR_WIN32A(ERROR_DS_NAME_VALUE_TOO_LONG) + CHK_ERR_WIN32A(ERROR_DS_NAME_UNPARSEABLE) + CHK_ERR_WIN32A(ERROR_DS_NAME_TYPE_UNKNOWN) + CHK_ERR_WIN32A(ERROR_DS_NOT_AN_OBJECT) + CHK_ERR_WIN32A(ERROR_DS_SEC_DESC_TOO_SHORT) + CHK_ERR_WIN32A(ERROR_DS_SEC_DESC_INVALID) + CHK_ERR_WIN32A(ERROR_DS_NO_DELETED_NAME) + CHK_ERR_WIN32A(ERROR_DS_SUBREF_MUST_HAVE_PARENT) + CHK_ERR_WIN32A(ERROR_DS_NCNAME_MUST_BE_NC) + CHK_ERR_WIN32A(ERROR_DS_CANT_ADD_SYSTEM_ONLY) + CHK_ERR_WIN32A(ERROR_DS_CLASS_MUST_BE_CONCRETE) + CHK_ERR_WIN32A(ERROR_DS_INVALID_DMD) + CHK_ERR_WIN32A(ERROR_DS_OBJ_GUID_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_NOT_ON_BACKLINK) + CHK_ERR_WIN32A(ERROR_DS_NO_CROSSREF_FOR_NC) + CHK_ERR_WIN32A(ERROR_DS_SHUTTING_DOWN) + CHK_ERR_WIN32A(ERROR_DS_UNKNOWN_OPERATION) + CHK_ERR_WIN32A(ERROR_DS_INVALID_ROLE_OWNER) + CHK_ERR_WIN32A(ERROR_DS_COULDNT_CONTACT_FSMO) + CHK_ERR_WIN32A(ERROR_DS_CROSS_NC_DN_RENAME) + CHK_ERR_WIN32A(ERROR_DS_CANT_MOD_SYSTEM_ONLY) + CHK_ERR_WIN32A(ERROR_DS_REPLICATOR_ONLY) + CHK_ERR_WIN32A(ERROR_DS_OBJ_CLASS_NOT_DEFINED) + CHK_ERR_WIN32A(ERROR_DS_OBJ_CLASS_NOT_SUBCLASS) + CHK_ERR_WIN32A(ERROR_DS_NAME_REFERENCE_INVALID) + CHK_ERR_WIN32A(ERROR_DS_CROSS_REF_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_CANT_DEL_MASTER_CROSSREF) + CHK_ERR_WIN32A(ERROR_DS_SUBTREE_NOTIFY_NOT_NC_HEAD) + CHK_ERR_WIN32A(ERROR_DS_NOTIFY_FILTER_TOO_COMPLEX) + CHK_ERR_WIN32A(ERROR_DS_DUP_RDN) + CHK_ERR_WIN32A(ERROR_DS_DUP_OID) + CHK_ERR_WIN32A(ERROR_DS_DUP_MAPI_ID) + CHK_ERR_WIN32A(ERROR_DS_DUP_SCHEMA_ID_GUID) + CHK_ERR_WIN32A(ERROR_DS_DUP_LDAP_DISPLAY_NAME) + CHK_ERR_WIN32A(ERROR_DS_SEMANTIC_ATT_TEST) + CHK_ERR_WIN32A(ERROR_DS_SYNTAX_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_EXISTS_IN_MUST_HAVE) + CHK_ERR_WIN32A(ERROR_DS_EXISTS_IN_MAY_HAVE) + CHK_ERR_WIN32A(ERROR_DS_NONEXISTENT_MAY_HAVE) + CHK_ERR_WIN32A(ERROR_DS_NONEXISTENT_MUST_HAVE) + CHK_ERR_WIN32A(ERROR_DS_AUX_CLS_TEST_FAIL) + CHK_ERR_WIN32A(ERROR_DS_NONEXISTENT_POSS_SUP) + CHK_ERR_WIN32A(ERROR_DS_SUB_CLS_TEST_FAIL) + CHK_ERR_WIN32A(ERROR_DS_BAD_RDN_ATT_ID_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_EXISTS_IN_AUX_CLS) + CHK_ERR_WIN32A(ERROR_DS_EXISTS_IN_SUB_CLS) + CHK_ERR_WIN32A(ERROR_DS_EXISTS_IN_POSS_SUP) + CHK_ERR_WIN32A(ERROR_DS_RECALCSCHEMA_FAILED) + CHK_ERR_WIN32A(ERROR_DS_TREE_DELETE_NOT_FINISHED) + CHK_ERR_WIN32A(ERROR_DS_CANT_DELETE) + CHK_ERR_WIN32A(ERROR_DS_ATT_SCHEMA_REQ_ID) + CHK_ERR_WIN32A(ERROR_DS_BAD_ATT_SCHEMA_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_CANT_CACHE_ATT) + CHK_ERR_WIN32A(ERROR_DS_CANT_CACHE_CLASS) + CHK_ERR_WIN32A(ERROR_DS_CANT_REMOVE_ATT_CACHE) + CHK_ERR_WIN32A(ERROR_DS_CANT_REMOVE_CLASS_CACHE) + CHK_ERR_WIN32A(ERROR_DS_CANT_RETRIEVE_DN) + CHK_ERR_WIN32A(ERROR_DS_MISSING_SUPREF) + CHK_ERR_WIN32A(ERROR_DS_CANT_RETRIEVE_INSTANCE) + CHK_ERR_WIN32A(ERROR_DS_CODE_INCONSISTENCY) + CHK_ERR_WIN32A(ERROR_DS_DATABASE_ERROR) + CHK_ERR_WIN32A(ERROR_DS_GOVERNSID_MISSING) + CHK_ERR_WIN32A(ERROR_DS_MISSING_EXPECTED_ATT) + CHK_ERR_WIN32A(ERROR_DS_NCNAME_MISSING_CR_REF) + CHK_ERR_WIN32A(ERROR_DS_SECURITY_CHECKING_ERROR) + CHK_ERR_WIN32A(ERROR_DS_SCHEMA_NOT_LOADED) + CHK_ERR_WIN32A(ERROR_DS_SCHEMA_ALLOC_FAILED) + CHK_ERR_WIN32A(ERROR_DS_ATT_SCHEMA_REQ_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_GCVERIFY_ERROR) + CHK_ERR_WIN32A(ERROR_DS_DRA_SCHEMA_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_CANT_FIND_DSA_OBJ) + CHK_ERR_WIN32A(ERROR_DS_CANT_FIND_EXPECTED_NC) + CHK_ERR_WIN32A(ERROR_DS_CANT_FIND_NC_IN_CACHE) + CHK_ERR_WIN32A(ERROR_DS_CANT_RETRIEVE_CHILD) + CHK_ERR_WIN32A(ERROR_DS_SECURITY_ILLEGAL_MODIFY) + CHK_ERR_WIN32A(ERROR_DS_CANT_REPLACE_HIDDEN_REC) + CHK_ERR_WIN32A(ERROR_DS_BAD_HIERARCHY_FILE) + CHK_ERR_WIN32A(ERROR_DS_BUILD_HIERARCHY_TABLE_FAILED) + CHK_ERR_WIN32A(ERROR_DS_CONFIG_PARAM_MISSING) + CHK_ERR_WIN32A(ERROR_DS_COUNTING_AB_INDICES_FAILED) + CHK_ERR_WIN32A(ERROR_DS_HIERARCHY_TABLE_MALLOC_FAILED) + CHK_ERR_WIN32A(ERROR_DS_INTERNAL_FAILURE) + CHK_ERR_WIN32A(ERROR_DS_UNKNOWN_ERROR) + CHK_ERR_WIN32A(ERROR_DS_ROOT_REQUIRES_CLASS_TOP) + CHK_ERR_WIN32A(ERROR_DS_REFUSING_FSMO_ROLES) + CHK_ERR_WIN32A(ERROR_DS_MISSING_FSMO_SETTINGS) + CHK_ERR_WIN32A(ERROR_DS_UNABLE_TO_SURRENDER_ROLES) + CHK_ERR_WIN32A(ERROR_DS_DRA_GENERIC) + CHK_ERR_WIN32A(ERROR_DS_DRA_INVALID_PARAMETER) + CHK_ERR_WIN32A(ERROR_DS_DRA_BUSY) + CHK_ERR_WIN32A(ERROR_DS_DRA_BAD_DN) + CHK_ERR_WIN32A(ERROR_DS_DRA_BAD_NC) + CHK_ERR_WIN32A(ERROR_DS_DRA_DN_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_DRA_INTERNAL_ERROR) + CHK_ERR_WIN32A(ERROR_DS_DRA_INCONSISTENT_DIT) + CHK_ERR_WIN32A(ERROR_DS_DRA_CONNECTION_FAILED) + CHK_ERR_WIN32A(ERROR_DS_DRA_BAD_INSTANCE_TYPE) + CHK_ERR_WIN32A(ERROR_DS_DRA_OUT_OF_MEM) + CHK_ERR_WIN32A(ERROR_DS_DRA_MAIL_PROBLEM) + CHK_ERR_WIN32A(ERROR_DS_DRA_REF_ALREADY_EXISTS) + CHK_ERR_WIN32A(ERROR_DS_DRA_REF_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DS_DRA_OBJ_IS_REP_SOURCE) + CHK_ERR_WIN32A(ERROR_DS_DRA_DB_ERROR) + CHK_ERR_WIN32A(ERROR_DS_DRA_NO_REPLICA) + CHK_ERR_WIN32A(ERROR_DS_DRA_ACCESS_DENIED) + CHK_ERR_WIN32A(ERROR_DS_DRA_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_DS_DRA_RPC_CANCELLED) + CHK_ERR_WIN32A(ERROR_DS_DRA_SOURCE_DISABLED) + CHK_ERR_WIN32A(ERROR_DS_DRA_SINK_DISABLED) + CHK_ERR_WIN32A(ERROR_DS_DRA_NAME_COLLISION) + CHK_ERR_WIN32A(ERROR_DS_DRA_SOURCE_REINSTALLED) + CHK_ERR_WIN32A(ERROR_DS_DRA_MISSING_PARENT) + CHK_ERR_WIN32A(ERROR_DS_DRA_PREEMPTED) + CHK_ERR_WIN32A(ERROR_DS_DRA_ABANDON_SYNC) + CHK_ERR_WIN32A(ERROR_DS_DRA_SHUTDOWN) + CHK_ERR_WIN32A(ERROR_DS_DRA_INCOMPATIBLE_PARTIAL_SET) + CHK_ERR_WIN32A(ERROR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA) + CHK_ERR_WIN32A(ERROR_DS_DRA_EXTN_CONNECTION_FAILED) + CHK_ERR_WIN32A(ERROR_DS_INSTALL_SCHEMA_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_DUP_LINK_ID) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_RESOLVING) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_NOT_UNIQUE) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_NO_MAPPING) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_DOMAIN_ONLY) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_NO_SYNTACTICAL_MAPPING) + CHK_ERR_WIN32A(ERROR_DS_CONSTRUCTED_ATT_MOD) + CHK_ERR_WIN32A(ERROR_DS_WRONG_OM_OBJ_CLASS) + CHK_ERR_WIN32A(ERROR_DS_DRA_REPL_PENDING) + CHK_ERR_WIN32A(DNS_ERROR_RESPONSE_CODES_BASE) +// CHK_ERR_WIN32A(DNS_ERROR_MASK) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_FORMAT_ERROR) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_SERVER_FAILURE) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_NAME_ERROR) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_NOT_IMPLEMENTED) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_REFUSED) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_YXDOMAIN) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_YXRRSET) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_NXRRSET) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_NOTAUTH) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_NOTZONE) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_BADSIG) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_BADKEY) + CHK_ERR_WIN32A(DNS_ERROR_RCODE_BADTIME) + CHK_ERR_WIN32A(DNS_ERROR_PACKET_FMT_BASE) + CHK_ERR_WIN32A(DNS_INFO_NO_RECORDS) + CHK_ERR_WIN32A(DNS_ERROR_BAD_PACKET) + CHK_ERR_WIN32A(DNS_ERROR_NO_PACKET) + CHK_ERR_WIN32A(DNS_ERROR_RCODE) + CHK_ERR_WIN32A(DNS_ERROR_UNSECURE_PACKET) + CHK_ERR_WIN32A(DNS_ERROR_GENERAL_API_BASE) + CHK_ERR_WIN32A(DNS_ERROR_INVALID_TYPE) + CHK_ERR_WIN32A(DNS_ERROR_INVALID_IP_ADDRESS) + CHK_ERR_WIN32A(DNS_ERROR_INVALID_PROPERTY) + CHK_ERR_WIN32A(DNS_ERROR_TRY_AGAIN_LATER) + CHK_ERR_WIN32A(DNS_ERROR_NOT_UNIQUE) + CHK_ERR_WIN32A(DNS_ERROR_NON_RFC_NAME) + CHK_ERR_WIN32A(DNS_STATUS_FQDN) + CHK_ERR_WIN32A(DNS_STATUS_DOTTED_NAME) + CHK_ERR_WIN32A(DNS_STATUS_SINGLE_PART_NAME) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_BASE) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_DOES_NOT_EXIST) + CHK_ERR_WIN32A(DNS_ERROR_NO_ZONE_INFO) + CHK_ERR_WIN32A(DNS_ERROR_INVALID_ZONE_OPERATION) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_CONFIGURATION_ERROR) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_HAS_NO_SOA_RECORD) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_HAS_NO_NS_RECORDS) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_LOCKED) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_CREATION_FAILED) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_ALREADY_EXISTS) + CHK_ERR_WIN32A(DNS_ERROR_AUTOZONE_ALREADY_EXISTS) + CHK_ERR_WIN32A(DNS_ERROR_INVALID_ZONE_TYPE) + CHK_ERR_WIN32A(DNS_ERROR_SECONDARY_REQUIRES_MASTER_IP) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_NOT_SECONDARY) + CHK_ERR_WIN32A(DNS_ERROR_NEED_SECONDARY_ADDRESSES) + CHK_ERR_WIN32A(DNS_ERROR_WINS_INIT_FAILED) + CHK_ERR_WIN32A(DNS_ERROR_NEED_WINS_SERVERS) + CHK_ERR_WIN32A(DNS_ERROR_NBSTAT_INIT_FAILED) + CHK_ERR_WIN32A(DNS_ERROR_SOA_DELETE_INVALID) + CHK_ERR_WIN32A(DNS_ERROR_DATAFILE_BASE) + CHK_ERR_WIN32A(DNS_ERROR_PRIMARY_REQUIRES_DATAFILE) + CHK_ERR_WIN32A(DNS_ERROR_INVALID_DATAFILE_NAME) + CHK_ERR_WIN32A(DNS_ERROR_DATAFILE_OPEN_FAILURE) + CHK_ERR_WIN32A(DNS_ERROR_FILE_WRITEBACK_FAILED) + CHK_ERR_WIN32A(DNS_ERROR_DATAFILE_PARSING) + CHK_ERR_WIN32A(DNS_ERROR_DATABASE_BASE) + CHK_ERR_WIN32A(DNS_ERROR_RECORD_DOES_NOT_EXIST) + CHK_ERR_WIN32A(DNS_ERROR_RECORD_FORMAT) + CHK_ERR_WIN32A(DNS_ERROR_NODE_CREATION_FAILED) + CHK_ERR_WIN32A(DNS_ERROR_UNKNOWN_RECORD_TYPE) + CHK_ERR_WIN32A(DNS_ERROR_RECORD_TIMED_OUT) + CHK_ERR_WIN32A(DNS_ERROR_NAME_NOT_IN_ZONE) + CHK_ERR_WIN32A(DNS_ERROR_CNAME_LOOP) + CHK_ERR_WIN32A(DNS_ERROR_NODE_IS_CNAME) + CHK_ERR_WIN32A(DNS_ERROR_CNAME_COLLISION) + CHK_ERR_WIN32A(DNS_ERROR_RECORD_ONLY_AT_ZONE_ROOT) + CHK_ERR_WIN32A(DNS_ERROR_RECORD_ALREADY_EXISTS) + CHK_ERR_WIN32A(DNS_ERROR_SECONDARY_DATA) + CHK_ERR_WIN32A(DNS_ERROR_NO_CREATE_CACHE_DATA) + CHK_ERR_WIN32A(DNS_ERROR_NAME_DOES_NOT_EXIST) + CHK_ERR_WIN32A(DNS_WARNING_PTR_CREATE_FAILED) + CHK_ERR_WIN32A(DNS_WARNING_DOMAIN_UNDELETED) + CHK_ERR_WIN32A(DNS_ERROR_DS_UNAVAILABLE) + CHK_ERR_WIN32A(DNS_ERROR_DS_ZONE_ALREADY_EXISTS) + CHK_ERR_WIN32A(DNS_ERROR_NO_BOOTFILE_IF_DS_ZONE) + CHK_ERR_WIN32A(DNS_ERROR_OPERATION_BASE) + CHK_ERR_WIN32A(DNS_INFO_AXFR_COMPLETE) + CHK_ERR_WIN32A(DNS_ERROR_AXFR) + CHK_ERR_WIN32A(DNS_INFO_ADDED_LOCAL_WINS) + CHK_ERR_WIN32A(DNS_ERROR_SECURE_BASE) + CHK_ERR_WIN32A(DNS_STATUS_CONTINUE_NEEDED) + CHK_ERR_WIN32A(DNS_ERROR_SETUP_BASE) + CHK_ERR_WIN32A(DNS_ERROR_NO_TCPIP) + CHK_ERR_WIN32A(DNS_ERROR_NO_DNS_SERVERS) + CHK_ERR_WIN32A(WSABASEERR) + CHK_ERR_WIN32A(WSAEINTR) + CHK_ERR_WIN32A(WSAEBADF) + CHK_ERR_WIN32A(WSAEACCES) + CHK_ERR_WIN32A(WSAEFAULT) + CHK_ERR_WIN32A(WSAEINVAL) + CHK_ERR_WIN32A(WSAEMFILE) + CHK_ERR_WIN32A(WSAEWOULDBLOCK) + CHK_ERR_WIN32A(WSAEINPROGRESS) + CHK_ERR_WIN32A(WSAEALREADY) + CHK_ERR_WIN32A(WSAENOTSOCK) + CHK_ERR_WIN32A(WSAEDESTADDRREQ) + CHK_ERR_WIN32A(WSAEMSGSIZE) + CHK_ERR_WIN32A(WSAEPROTOTYPE) + CHK_ERR_WIN32A(WSAENOPROTOOPT) + CHK_ERR_WIN32A(WSAEPROTONOSUPPORT) + CHK_ERR_WIN32A(WSAESOCKTNOSUPPORT) + CHK_ERR_WIN32A(WSAEOPNOTSUPP) + CHK_ERR_WIN32A(WSAEPFNOSUPPORT) + CHK_ERR_WIN32A(WSAEAFNOSUPPORT) + CHK_ERR_WIN32A(WSAEADDRINUSE) + CHK_ERR_WIN32A(WSAEADDRNOTAVAIL) + CHK_ERR_WIN32A(WSAENETDOWN) + CHK_ERR_WIN32A(WSAENETUNREACH) + CHK_ERR_WIN32A(WSAENETRESET) + CHK_ERR_WIN32A(WSAECONNABORTED) + CHK_ERR_WIN32A(WSAECONNRESET) + CHK_ERR_WIN32A(WSAENOBUFS) + CHK_ERR_WIN32A(WSAEISCONN) + CHK_ERR_WIN32A(WSAENOTCONN) + CHK_ERR_WIN32A(WSAESHUTDOWN) + CHK_ERR_WIN32A(WSAETOOMANYREFS) + CHK_ERR_WIN32A(WSAETIMEDOUT) + CHK_ERR_WIN32A(WSAECONNREFUSED) + CHK_ERR_WIN32A(WSAELOOP) + CHK_ERR_WIN32A(WSAENAMETOOLONG) + CHK_ERR_WIN32A(WSAEHOSTDOWN) + CHK_ERR_WIN32A(WSAEHOSTUNREACH) + CHK_ERR_WIN32A(WSAENOTEMPTY) + CHK_ERR_WIN32A(WSAEPROCLIM) + CHK_ERR_WIN32A(WSAEUSERS) + CHK_ERR_WIN32A(WSAEDQUOT) + CHK_ERR_WIN32A(WSAESTALE) + CHK_ERR_WIN32A(WSAEREMOTE) + CHK_ERR_WIN32A(WSASYSNOTREADY) + CHK_ERR_WIN32A(WSAVERNOTSUPPORTED) + CHK_ERR_WIN32A(WSANOTINITIALISED) + CHK_ERR_WIN32A(WSAEDISCON) + CHK_ERR_WIN32A(WSAENOMORE) + CHK_ERR_WIN32A(WSAECANCELLED) + CHK_ERR_WIN32A(WSAEINVALIDPROCTABLE) + CHK_ERR_WIN32A(WSAEINVALIDPROVIDER) + CHK_ERR_WIN32A(WSAEPROVIDERFAILEDINIT) + CHK_ERR_WIN32A(WSASYSCALLFAILURE) + CHK_ERR_WIN32A(WSASERVICE_NOT_FOUND) + CHK_ERR_WIN32A(WSATYPE_NOT_FOUND) + CHK_ERR_WIN32A(WSA_E_NO_MORE) + CHK_ERR_WIN32A(WSA_E_CANCELLED) + CHK_ERR_WIN32A(WSAEREFUSED) + CHK_ERR_WIN32A(WSAHOST_NOT_FOUND) + CHK_ERR_WIN32A(WSATRY_AGAIN) + CHK_ERR_WIN32A(WSANO_RECOVERY) + CHK_ERR_WIN32A(WSANO_DATA) + CHK_ERR_WIN32A(WSA_QOS_RECEIVERS) + CHK_ERR_WIN32A(WSA_QOS_SENDERS) + CHK_ERR_WIN32A(WSA_QOS_NO_SENDERS) + CHK_ERR_WIN32A(WSA_QOS_NO_RECEIVERS) + CHK_ERR_WIN32A(WSA_QOS_REQUEST_CONFIRMED) + CHK_ERR_WIN32A(WSA_QOS_ADMISSION_FAILURE) + CHK_ERR_WIN32A(WSA_QOS_POLICY_FAILURE) + CHK_ERR_WIN32A(WSA_QOS_BAD_STYLE) + CHK_ERR_WIN32A(WSA_QOS_BAD_OBJECT) + CHK_ERR_WIN32A(WSA_QOS_TRAFFIC_CTRL_ERROR) + CHK_ERR_WIN32A(WSA_QOS_GENERIC_ERROR) + + CHK_ERRA(CO_E_ATTEMPT_TO_CREATE_OUTSIDE_CLIENT_CONTEXT) + CHK_ERRA(CO_E_SERVER_PAUSED) + CHK_ERRA(CO_E_SERVER_NOT_PAUSED) + CHK_ERRA(CO_E_CLASS_DISABLED) + CHK_ERRA(CO_E_CLRNOTAVAILABLE) + CHK_ERRA(CO_E_ASYNC_WORK_REJECTED) + CHK_ERRA(CO_E_SERVER_INIT_TIMEOUT) + CHK_ERRA(CO_E_NO_SECCTX_IN_ACTIVATE) + CHK_ERRA(CO_E_TRACKER_CONFIG) + CHK_ERRA(CO_E_THREADPOOL_CONFIG) + CHK_ERRA(CO_E_SXS_CONFIG) + CHK_ERRA(CO_E_MALFORMED_SPN) + CHK_ERRA(REGDB_E_BADTHREADINGMODEL) +// CHK_ERRA(EVENT_E_FIRST) +// CHK_ERRA(EVENT_E_LAST) +// CHK_ERRA(EVENT_S_FIRST) +// CHK_ERRA(EVENT_S_LAST) +// CHK_ERRA(EVENT_S_SOME_SUBSCRIBERS_FAILED) +// CHK_ERRA(EVENT_E_ALL_SUBSCRIBERS_FAILED) + CHK_ERRA(EVENT_S_NOSUBSCRIBERS) +// CHK_ERRA(EVENT_E_QUERYSYNTAX) +// CHK_ERRA(EVENT_E_QUERYFIELD) +// CHK_ERRA(EVENT_E_INTERNALEXCEPTION) +// CHK_ERRA(EVENT_E_INTERNALERROR) +// CHK_ERRA(EVENT_E_INVALID_PER_USER_SID) +// CHK_ERRA(EVENT_E_USER_EXCEPTION) +// CHK_ERRA(EVENT_E_TOO_MANY_METHODS) +// CHK_ERRA(EVENT_E_MISSING_EVENTCLASS) +// CHK_ERRA(EVENT_E_NOT_ALL_REMOVED) +// CHK_ERRA(EVENT_E_COMPLUS_NOT_INSTALLED) +// CHK_ERRA(EVENT_E_CANT_MODIFY_OR_DELETE_UNCONFIGURED_OBJECT) +// CHK_ERRA(EVENT_E_CANT_MODIFY_OR_DELETE_CONFIGURED_OBJECT) +// CHK_ERRA(EVENT_E_INVALID_EVENT_CLASS_PARTITION) +// CHK_ERRA(EVENT_E_PER_USER_SID_NOT_LOGGED_ON) + CHK_ERRA(CONTEXT_E_FIRST) + CHK_ERRA(CONTEXT_E_LAST) + CHK_ERRA(CONTEXT_S_FIRST) + CHK_ERRA(CONTEXT_S_LAST) + CHK_ERRA(CONTEXT_E_ABORTED) + CHK_ERRA(CONTEXT_E_ABORTING) + CHK_ERRA(CONTEXT_E_NOCONTEXT) +// CHK_ERRA(CONTEXT_E_WOULD_DEADLOCK) + CHK_ERRA(CONTEXT_E_SYNCH_TIMEOUT) + CHK_ERRA(CONTEXT_E_OLDREF) + CHK_ERRA(CONTEXT_E_ROLENOTFOUND) + CHK_ERRA(CONTEXT_E_TMNOTAVAILABLE) + CHK_ERRA(CO_E_ACTIVATIONFAILED) + CHK_ERRA(CO_E_ACTIVATIONFAILED_EVENTLOGGED) + CHK_ERRA(CO_E_ACTIVATIONFAILED_CATALOGERROR) + CHK_ERRA(CO_E_ACTIVATIONFAILED_TIMEOUT) + CHK_ERRA(CO_E_INITIALIZATIONFAILED) + CHK_ERRA(CONTEXT_E_NOJIT) + CHK_ERRA(CONTEXT_E_NOTRANSACTION) + CHK_ERRA(CO_E_THREADINGMODEL_CHANGED) + CHK_ERRA(CO_E_NOIISINTRINSICS) + CHK_ERRA(CO_E_NOCOOKIES) + CHK_ERRA(CO_E_DBERROR) + CHK_ERRA(CO_E_NOTPOOLED) + CHK_ERRA(CO_E_NOTCONSTRUCTED) + CHK_ERRA(CO_E_NOSYNCHRONIZATION) +// CHK_ERRA(CO_E_ISOLEVELMISMATCH) + CHK_ERRA(SCHED_S_TASK_READY) + CHK_ERRA(SCHED_S_TASK_RUNNING) + CHK_ERRA(SCHED_S_TASK_DISABLED) + CHK_ERRA(SCHED_S_TASK_HAS_NOT_RUN) + CHK_ERRA(SCHED_S_TASK_NO_MORE_RUNS) + CHK_ERRA(SCHED_S_TASK_NOT_SCHEDULED) + CHK_ERRA(SCHED_S_TASK_TERMINATED) + CHK_ERRA(SCHED_S_TASK_NO_VALID_TRIGGERS) + CHK_ERRA(SCHED_S_EVENT_TRIGGER) + CHK_ERRA(SCHED_E_TRIGGER_NOT_FOUND) + CHK_ERRA(SCHED_E_TASK_NOT_READY) + CHK_ERRA(SCHED_E_TASK_NOT_RUNNING) + CHK_ERRA(SCHED_E_SERVICE_NOT_INSTALLED) + CHK_ERRA(SCHED_E_CANNOT_OPEN_TASK) + CHK_ERRA(SCHED_E_INVALID_TASK) + CHK_ERRA(SCHED_E_ACCOUNT_INFORMATION_NOT_SET) + CHK_ERRA(SCHED_E_ACCOUNT_NAME_NOT_FOUND) + CHK_ERRA(SCHED_E_ACCOUNT_DBASE_CORRUPT) + CHK_ERRA(SCHED_E_NO_SECURITY_SERVICES) + CHK_ERRA(SCHED_E_UNKNOWN_OBJECT_VERSION) + CHK_ERRA(SCHED_E_UNSUPPORTED_ACCOUNT_OPTION) + CHK_ERRA(SCHED_E_SERVICE_NOT_RUNNING) + CHK_ERRA(CO_S_MACHINENAMENOTFOUND) + CHK_ERRA(STG_E_STATUS_COPY_PROTECTION_FAILURE) + CHK_ERRA(STG_E_CSS_AUTHENTICATION_FAILURE) + CHK_ERRA(STG_E_CSS_KEY_NOT_PRESENT) + CHK_ERRA(STG_E_CSS_KEY_NOT_ESTABLISHED) + CHK_ERRA(STG_E_CSS_SCRAMBLED_SECTOR) + CHK_ERRA(STG_E_CSS_REGION_MISMATCH) + CHK_ERRA(STG_E_RESETS_EXHAUSTED) + CHK_ERRA(CO_E_CANCEL_DISABLED) + CHK_ERRA(ERROR_AUDITING_DISABLED) + CHK_ERRA(ERROR_ALL_SIDS_FILTERED) + CHK_ERRA(NTE_TEMPORARY_PROFILE) + CHK_ERRA(NTE_FIXEDPARAMETER) + CHK_ERRA(SEC_E_INSUFFICIENT_MEMORY) + CHK_ERRA(SEC_E_INVALID_HANDLE) + CHK_ERRA(SEC_E_UNSUPPORTED_FUNCTION) + CHK_ERRA(SEC_E_TARGET_UNKNOWN) + CHK_ERRA(SEC_E_INTERNAL_ERROR) + CHK_ERRA(SEC_E_SECPKG_NOT_FOUND) + CHK_ERRA(SEC_E_NOT_OWNER) + CHK_ERRA(SEC_E_CANNOT_INSTALL) + CHK_ERRA(SEC_E_INVALID_TOKEN) + CHK_ERRA(SEC_E_CANNOT_PACK) + CHK_ERRA(SEC_E_QOP_NOT_SUPPORTED) + CHK_ERRA(SEC_E_NO_IMPERSONATION) + CHK_ERRA(SEC_E_LOGON_DENIED) + CHK_ERRA(SEC_E_UNKNOWN_CREDENTIALS) + CHK_ERRA(SEC_E_NO_CREDENTIALS) + CHK_ERRA(SEC_E_MESSAGE_ALTERED) + CHK_ERRA(SEC_E_OUT_OF_SEQUENCE) + CHK_ERRA(SEC_E_NO_AUTHENTICATING_AUTHORITY) + CHK_ERRA(SEC_I_CONTINUE_NEEDED) + CHK_ERRA(SEC_I_COMPLETE_NEEDED) + CHK_ERRA(SEC_I_COMPLETE_AND_CONTINUE) + CHK_ERRA(SEC_I_LOCAL_LOGON) + CHK_ERRA(SEC_E_BAD_PKGID) + CHK_ERRA(SEC_E_CONTEXT_EXPIRED) + CHK_ERRA(SEC_I_CONTEXT_EXPIRED) + CHK_ERRA(SEC_E_INCOMPLETE_MESSAGE) + CHK_ERRA(SEC_E_INCOMPLETE_CREDENTIALS) + CHK_ERRA(SEC_E_BUFFER_TOO_SMALL) + CHK_ERRA(SEC_I_INCOMPLETE_CREDENTIALS) + CHK_ERRA(SEC_I_RENEGOTIATE) + CHK_ERRA(SEC_E_WRONG_PRINCIPAL) + CHK_ERRA(SEC_I_NO_LSA_CONTEXT) + CHK_ERRA(SEC_E_TIME_SKEW) + CHK_ERRA(SEC_E_UNTRUSTED_ROOT) + CHK_ERRA(SEC_E_ILLEGAL_MESSAGE) + CHK_ERRA(SEC_E_CERT_UNKNOWN) + CHK_ERRA(SEC_E_CERT_EXPIRED) + CHK_ERRA(SEC_E_ENCRYPT_FAILURE) + CHK_ERRA(SEC_E_DECRYPT_FAILURE) + CHK_ERRA(SEC_E_ALGORITHM_MISMATCH) + CHK_ERRA(SEC_E_SECURITY_QOS_FAILED) + CHK_ERRA(SEC_E_UNFINISHED_CONTEXT_DELETED) + CHK_ERRA(SEC_E_NO_TGT_REPLY) + CHK_ERRA(SEC_E_NO_IP_ADDRESSES) + CHK_ERRA(SEC_E_WRONG_CREDENTIAL_HANDLE) + CHK_ERRA(SEC_E_CRYPTO_SYSTEM_INVALID) + CHK_ERRA(SEC_E_MAX_REFERRALS_EXCEEDED) + CHK_ERRA(SEC_E_MUST_BE_KDC) + CHK_ERRA(SEC_E_STRONG_CRYPTO_NOT_SUPPORTED) + CHK_ERRA(SEC_E_TOO_MANY_PRINCIPALS) + CHK_ERRA(SEC_E_NO_PA_DATA) + CHK_ERRA(SEC_E_PKINIT_NAME_MISMATCH) + CHK_ERRA(SEC_E_SMARTCARD_LOGON_REQUIRED) + CHK_ERRA(SEC_E_SHUTDOWN_IN_PROGRESS) + CHK_ERRA(SEC_E_KDC_INVALID_REQUEST) + CHK_ERRA(SEC_E_KDC_UNABLE_TO_REFER) + CHK_ERRA(SEC_E_KDC_UNKNOWN_ETYPE) + CHK_ERRA(SEC_E_UNSUPPORTED_PREAUTH) + CHK_ERRA(SEC_E_DELEGATION_REQUIRED) + CHK_ERRA(SEC_E_BAD_BINDINGS) + CHK_ERRA(SEC_E_MULTIPLE_ACCOUNTS) + CHK_ERRA(SEC_E_NO_KERB_KEY) +// CHK_ERRA(SEC_E_CERT_WRONG_USAGE) +// CHK_ERRA(SEC_E_DOWNGRADE_DETECTED) + CHK_ERRA(SEC_E_SMARTCARD_CERT_REVOKED) + CHK_ERRA(SEC_E_ISSUING_CA_UNTRUSTED) + CHK_ERRA(SEC_E_REVOCATION_OFFLINE_C) + CHK_ERRA(SEC_E_PKINIT_CLIENT_FAILURE) + CHK_ERRA(SEC_E_SMARTCARD_CERT_EXPIRED) +// CHK_ERRA(SEC_E_NO_SPM) +// CHK_ERRA(SEC_E_NOT_SUPPORTED) + CHK_ERRA(CRYPT_I_NEW_PROTECTION_REQUIRED) + CHK_ERRA(CRYPT_E_MISSING_PUBKEY_PARA) + CHK_ERRA(CRYPT_E_ASN1_ERROR) + CHK_ERRA(CRYPT_E_ASN1_INTERNAL) + CHK_ERRA(CRYPT_E_ASN1_EOD) + CHK_ERRA(CRYPT_E_ASN1_CORRUPT) + CHK_ERRA(CRYPT_E_ASN1_LARGE) + CHK_ERRA(CRYPT_E_ASN1_CONSTRAINT) + CHK_ERRA(CRYPT_E_ASN1_MEMORY) + CHK_ERRA(CRYPT_E_ASN1_OVERFLOW) + CHK_ERRA(CRYPT_E_ASN1_BADPDU) + CHK_ERRA(CRYPT_E_ASN1_BADARGS) + CHK_ERRA(CRYPT_E_ASN1_BADREAL) + CHK_ERRA(CRYPT_E_ASN1_BADTAG) + CHK_ERRA(CRYPT_E_ASN1_CHOICE) + CHK_ERRA(CRYPT_E_ASN1_RULE) + CHK_ERRA(CRYPT_E_ASN1_UTF8) + CHK_ERRA(CRYPT_E_ASN1_PDU_TYPE) + CHK_ERRA(CRYPT_E_ASN1_NYI) + CHK_ERRA(CRYPT_E_ASN1_EXTENDED) + CHK_ERRA(CRYPT_E_ASN1_NOEOD) + CHK_ERRA(CERTSRV_E_SERVER_SUSPENDED) + CHK_ERRA(CERTSRV_E_ENCODING_LENGTH) + CHK_ERRA(CERTSRV_E_ROLECONFLICT) + CHK_ERRA(CERTSRV_E_RESTRICTEDOFFICER) + CHK_ERRA(CERTSRV_E_KEY_ARCHIVAL_NOT_CONFIGURED) + CHK_ERRA(CERTSRV_E_NO_VALID_KRA) + CHK_ERRA(CERTSRV_E_BAD_REQUEST_KEY_ARCHIVAL) + CHK_ERRA(CERTSRV_E_NO_CAADMIN_DEFINED) + CHK_ERRA(CERTSRV_E_BAD_RENEWAL_CERT_ATTRIBUTE) + CHK_ERRA(CERTSRV_E_NO_DB_SESSIONS) + CHK_ERRA(CERTSRV_E_ALIGNMENT_FAULT) + CHK_ERRA(CERTSRV_E_ENROLL_DENIED) + CHK_ERRA(CERTSRV_E_TEMPLATE_DENIED) +// CHK_ERRA(CERTSRV_E_DOWNLEVEL_DC_SSL_OR_UPGRADE) + CHK_ERRA(CERTSRV_E_TEMPLATE_CONFLICT) + CHK_ERRA(CERTSRV_E_SUBJECT_ALT_NAME_REQUIRED) + CHK_ERRA(CERTSRV_E_ARCHIVED_KEY_REQUIRED) + CHK_ERRA(CERTSRV_E_SMIME_REQUIRED) + CHK_ERRA(CERTSRV_E_BAD_RENEWAL_SUBJECT) + CHK_ERRA(CERTSRV_E_BAD_TEMPLATE_VERSION) + CHK_ERRA(CERTSRV_E_TEMPLATE_POLICY_REQUIRED) + CHK_ERRA(CERTSRV_E_SIGNATURE_POLICY_REQUIRED) + CHK_ERRA(CERTSRV_E_SIGNATURE_COUNT) + CHK_ERRA(CERTSRV_E_SIGNATURE_REJECTED) + CHK_ERRA(CERTSRV_E_ISSUANCE_POLICY_REQUIRED) + CHK_ERRA(CERTSRV_E_SUBJECT_UPN_REQUIRED) + CHK_ERRA(CERTSRV_E_SUBJECT_DIRECTORY_GUID_REQUIRED) + CHK_ERRA(CERTSRV_E_SUBJECT_DNS_REQUIRED) + CHK_ERRA(CERTSRV_E_ARCHIVED_KEY_UNEXPECTED) + CHK_ERRA(CERTSRV_E_KEY_LENGTH) +// CHK_ERRA(CERTSRV_E_SUBJECT_EMAIL_REQUIRED) +// CHK_ERRA(CERTSRV_E_UNKNOWN_CERT_TYPE) +// CHK_ERRA(CERTSRV_E_CERT_TYPE_OVERLAP) + CHK_ERRA(XENROLL_E_KEY_NOT_EXPORTABLE) + CHK_ERRA(XENROLL_E_CANNOT_ADD_ROOT_CERT) + CHK_ERRA(XENROLL_E_RESPONSE_KA_HASH_NOT_FOUND) + CHK_ERRA(XENROLL_E_RESPONSE_UNEXPECTED_KA_HASH) + CHK_ERRA(XENROLL_E_RESPONSE_KA_HASH_MISMATCH) + CHK_ERRA(XENROLL_E_KEYSPEC_SMIME_MISMATCH) + CHK_ERRA(MSSIPOTF_E_OUTOFMEMRANGE) + CHK_ERRA(MSSIPOTF_E_CANTGETOBJECT) + CHK_ERRA(MSSIPOTF_E_NOHEADTABLE) + CHK_ERRA(MSSIPOTF_E_BAD_MAGICNUMBER) + CHK_ERRA(MSSIPOTF_E_BAD_OFFSET_TABLE) + CHK_ERRA(MSSIPOTF_E_TABLE_TAGORDER) + CHK_ERRA(MSSIPOTF_E_TABLE_LONGWORD) + CHK_ERRA(MSSIPOTF_E_BAD_FIRST_TABLE_PLACEMENT) + CHK_ERRA(MSSIPOTF_E_TABLES_OVERLAP) + CHK_ERRA(MSSIPOTF_E_TABLE_PADBYTES) + CHK_ERRA(MSSIPOTF_E_FILETOOSMALL) + CHK_ERRA(MSSIPOTF_E_TABLE_CHECKSUM) + CHK_ERRA(MSSIPOTF_E_FILE_CHECKSUM) + CHK_ERRA(MSSIPOTF_E_FAILED_POLICY) + CHK_ERRA(MSSIPOTF_E_FAILED_HINTS_CHECK) + CHK_ERRA(MSSIPOTF_E_NOT_OPENTYPE) + CHK_ERRA(MSSIPOTF_E_FILE) + CHK_ERRA(MSSIPOTF_E_CRYPT) + CHK_ERRA(MSSIPOTF_E_BADVERSION) + CHK_ERRA(MSSIPOTF_E_DSIG_STRUCTURE) + CHK_ERRA(MSSIPOTF_E_PCONST_CHECK) + CHK_ERRA(MSSIPOTF_E_STRUCTURE) + CHK_ERRA(TRUST_E_EXPLICIT_DISTRUST) + CHK_ERRA(CERT_E_UNTRUSTEDCA) + CHK_ERRA(CERT_E_INVALID_POLICY) + CHK_ERRA(CERT_E_INVALID_NAME) + CHK_ERRA(SPAPI_E_NOT_DISABLEABLE) + CHK_ERRA(SPAPI_E_CANT_REMOVE_DEVINST) + CHK_ERRA(SPAPI_E_INVALID_TARGET) + CHK_ERRA(SPAPI_E_DRIVER_NONNATIVE) + CHK_ERRA(SPAPI_E_IN_WOW64) + CHK_ERRA(SPAPI_E_SET_SYSTEM_RESTORE_POINT) + CHK_ERRA(SPAPI_E_INCORRECTLY_COPIED_INF) + CHK_ERRA(SPAPI_E_SCE_DISABLED) + CHK_ERRA(SCARD_E_NO_KEY_CONTAINER) + CHK_ERRA(SCARD_W_CARD_NOT_AUTHENTICATED) + CHK_ERRA(COMADMIN_E_OBJECTERRORS) + CHK_ERRA(COMADMIN_E_OBJECTINVALID) + CHK_ERRA(COMADMIN_E_KEYMISSING) + CHK_ERRA(COMADMIN_E_ALREADYINSTALLED) + CHK_ERRA(COMADMIN_E_APP_FILE_WRITEFAIL) + CHK_ERRA(COMADMIN_E_APP_FILE_READFAIL) + CHK_ERRA(COMADMIN_E_APP_FILE_VERSION) + CHK_ERRA(COMADMIN_E_BADPATH) + CHK_ERRA(COMADMIN_E_APPLICATIONEXISTS) + CHK_ERRA(COMADMIN_E_ROLEEXISTS) + CHK_ERRA(COMADMIN_E_CANTCOPYFILE) + CHK_ERRA(COMADMIN_E_NOUSER) + CHK_ERRA(COMADMIN_E_INVALIDUSERIDS) + CHK_ERRA(COMADMIN_E_NOREGISTRYCLSID) + CHK_ERRA(COMADMIN_E_BADREGISTRYPROGID) + CHK_ERRA(COMADMIN_E_AUTHENTICATIONLEVEL) + CHK_ERRA(COMADMIN_E_USERPASSWDNOTVALID) + CHK_ERRA(COMADMIN_E_CLSIDORIIDMISMATCH) + CHK_ERRA(COMADMIN_E_REMOTEINTERFACE) + CHK_ERRA(COMADMIN_E_DLLREGISTERSERVER) + CHK_ERRA(COMADMIN_E_NOSERVERSHARE) + CHK_ERRA(COMADMIN_E_DLLLOADFAILED) + CHK_ERRA(COMADMIN_E_BADREGISTRYLIBID) + CHK_ERRA(COMADMIN_E_APPDIRNOTFOUND) + CHK_ERRA(COMADMIN_E_REGISTRARFAILED) + CHK_ERRA(COMADMIN_E_COMPFILE_DOESNOTEXIST) + CHK_ERRA(COMADMIN_E_COMPFILE_LOADDLLFAIL) + CHK_ERRA(COMADMIN_E_COMPFILE_GETCLASSOBJ) + CHK_ERRA(COMADMIN_E_COMPFILE_CLASSNOTAVAIL) + CHK_ERRA(COMADMIN_E_COMPFILE_BADTLB) + CHK_ERRA(COMADMIN_E_COMPFILE_NOTINSTALLABLE) + CHK_ERRA(COMADMIN_E_NOTCHANGEABLE) + CHK_ERRA(COMADMIN_E_NOTDELETEABLE) + CHK_ERRA(COMADMIN_E_SESSION) + CHK_ERRA(COMADMIN_E_COMP_MOVE_LOCKED) + CHK_ERRA(COMADMIN_E_COMP_MOVE_BAD_DEST) + CHK_ERRA(COMADMIN_E_REGISTERTLB) + CHK_ERRA(COMADMIN_E_SYSTEMAPP) + CHK_ERRA(COMADMIN_E_COMPFILE_NOREGISTRAR) + CHK_ERRA(COMADMIN_E_COREQCOMPINSTALLED) + CHK_ERRA(COMADMIN_E_SERVICENOTINSTALLED) + CHK_ERRA(COMADMIN_E_PROPERTYSAVEFAILED) + CHK_ERRA(COMADMIN_E_OBJECTEXISTS) + CHK_ERRA(COMADMIN_E_COMPONENTEXISTS) + CHK_ERRA(COMADMIN_E_REGFILE_CORRUPT) + CHK_ERRA(COMADMIN_E_PROPERTY_OVERFLOW) + CHK_ERRA(COMADMIN_E_NOTINREGISTRY) + CHK_ERRA(COMADMIN_E_OBJECTNOTPOOLABLE) + CHK_ERRA(COMADMIN_E_APPLID_MATCHES_CLSID) + CHK_ERRA(COMADMIN_E_ROLE_DOES_NOT_EXIST) + CHK_ERRA(COMADMIN_E_START_APP_NEEDS_COMPONENTS) + CHK_ERRA(COMADMIN_E_REQUIRES_DIFFERENT_PLATFORM) + CHK_ERRA(COMADMIN_E_CAN_NOT_EXPORT_APP_PROXY) + CHK_ERRA(COMADMIN_E_CAN_NOT_START_APP) + CHK_ERRA(COMADMIN_E_CAN_NOT_EXPORT_SYS_APP) + CHK_ERRA(COMADMIN_E_CANT_SUBSCRIBE_TO_COMPONENT) + CHK_ERRA(COMADMIN_E_EVENTCLASS_CANT_BE_SUBSCRIBER) + CHK_ERRA(COMADMIN_E_LIB_APP_PROXY_INCOMPATIBLE) + CHK_ERRA(COMADMIN_E_BASE_PARTITION_ONLY) + CHK_ERRA(COMADMIN_E_START_APP_DISABLED) + CHK_ERRA(COMADMIN_E_CAT_DUPLICATE_PARTITION_NAME) + CHK_ERRA(COMADMIN_E_CAT_INVALID_PARTITION_NAME) + CHK_ERRA(COMADMIN_E_CAT_PARTITION_IN_USE) + CHK_ERRA(COMADMIN_E_FILE_PARTITION_DUPLICATE_FILES) + CHK_ERRA(COMADMIN_E_CAT_IMPORTED_COMPONENTS_NOT_ALLOWED) + CHK_ERRA(COMADMIN_E_AMBIGUOUS_APPLICATION_NAME) + CHK_ERRA(COMADMIN_E_AMBIGUOUS_PARTITION_NAME) + CHK_ERRA(COMADMIN_E_REGDB_NOTINITIALIZED) + CHK_ERRA(COMADMIN_E_REGDB_NOTOPEN) + CHK_ERRA(COMADMIN_E_REGDB_SYSTEMERR) + CHK_ERRA(COMADMIN_E_REGDB_ALREADYRUNNING) + CHK_ERRA(COMADMIN_E_MIG_VERSIONNOTSUPPORTED) + CHK_ERRA(COMADMIN_E_MIG_SCHEMANOTFOUND) + CHK_ERRA(COMADMIN_E_CAT_BITNESSMISMATCH) + CHK_ERRA(COMADMIN_E_CAT_UNACCEPTABLEBITNESS) + CHK_ERRA(COMADMIN_E_CAT_WRONGAPPBITNESS) + CHK_ERRA(COMADMIN_E_CAT_PAUSE_RESUME_NOT_SUPPORTED) + CHK_ERRA(COMADMIN_E_CAT_SERVERFAULT) + CHK_ERRA(COMQC_E_APPLICATION_NOT_QUEUED) + CHK_ERRA(COMQC_E_NO_QUEUEABLE_INTERFACES) + CHK_ERRA(COMQC_E_QUEUING_SERVICE_NOT_AVAILABLE) + CHK_ERRA(COMQC_E_NO_IPERSISTSTREAM) + CHK_ERRA(COMQC_E_BAD_MESSAGE) + CHK_ERRA(COMQC_E_UNAUTHENTICATED) + CHK_ERRA(COMQC_E_UNTRUSTED_ENQUEUER) + CHK_ERRA(MSDTC_E_DUPLICATE_RESOURCE) + CHK_ERRA(COMADMIN_E_OBJECT_PARENT_MISSING) + CHK_ERRA(COMADMIN_E_OBJECT_DOES_NOT_EXIST) + CHK_ERRA(COMADMIN_E_APP_NOT_RUNNING) + CHK_ERRA(COMADMIN_E_INVALID_PARTITION) + CHK_ERRA(COMADMIN_E_SVCAPP_NOT_POOLABLE_OR_RECYCLABLE) + CHK_ERRA(COMADMIN_E_USER_IN_SET) + CHK_ERRA(COMADMIN_E_CANTRECYCLELIBRARYAPPS) + CHK_ERRA(COMADMIN_E_CANTRECYCLESERVICEAPPS) + CHK_ERRA(COMADMIN_E_PROCESSALREADYRECYCLED) + CHK_ERRA(COMADMIN_E_PAUSEDPROCESSMAYNOTBERECYCLED) + CHK_ERRA(COMADMIN_E_CANTMAKEINPROCSERVICE) + CHK_ERRA(COMADMIN_E_PROGIDINUSEBYCLSID) + CHK_ERRA(COMADMIN_E_DEFAULT_PARTITION_NOT_IN_SET) + CHK_ERRA(COMADMIN_E_RECYCLEDPROCESSMAYNOTBEPAUSED) + CHK_ERRA(COMADMIN_E_PARTITION_ACCESSDENIED) + CHK_ERRA(COMADMIN_E_PARTITION_MSI_ONLY) + CHK_ERRA(COMADMIN_E_LEGACYCOMPS_NOT_ALLOWED_IN_1_0_FORMAT) + CHK_ERRA(COMADMIN_E_LEGACYCOMPS_NOT_ALLOWED_IN_NONBASE_PARTITIONS) + CHK_ERRA(COMADMIN_E_COMP_MOVE_SOURCE) + CHK_ERRA(COMADMIN_E_COMP_MOVE_DEST) + CHK_ERRA(COMADMIN_E_COMP_MOVE_PRIVATE) + CHK_ERRA(COMADMIN_E_BASEPARTITION_REQUIRED_IN_SET) + CHK_ERRA(COMADMIN_E_CANNOT_ALIAS_EVENTCLASS) + CHK_ERRA(COMADMIN_E_PRIVATE_ACCESSDENIED) + CHK_ERRA(COMADMIN_E_SAFERINVALID) + CHK_ERRA(COMADMIN_E_REGISTRY_ACCESSDENIED) +// CHK_ERRA(COMADMIN_E_PARTITIONS_DISABLED) +// CHK_ERR_WIN32A(ERROR_EXE_CANNOT_MODIFY_SIGNED_BINARY) +// CHK_ERR_WIN32A(ERROR_EXE_CANNOT_MODIFY_STRONG_SIGNED_BINARY) + CHK_ERR_WIN32A(ERROR_DISK_TOO_FRAGMENTED) + CHK_ERR_WIN32A(ERROR_DELETE_PENDING) +// CHK_ERR_WIN32A(ERROR_SCOPE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_NOT_SAFEBOOT_SERVICE) + CHK_ERR_WIN32A(ERROR_JOURNAL_ENTRY_DELETED) + CHK_ERR_WIN32A(ERROR_ONLY_IF_CONNECTED) + CHK_ERR_WIN32A(ERROR_OVERRIDE_NOCHANGES) + CHK_ERR_WIN32A(ERROR_BAD_USER_PROFILE) + CHK_ERR_WIN32A(ERROR_NOT_SUPPORTED_ON_SBS) + CHK_ERR_WIN32A(ERROR_SERVER_SHUTDOWN_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_HOST_DOWN) + CHK_ERR_WIN32A(ERROR_NON_ACCOUNT_SID) + CHK_ERR_WIN32A(ERROR_NON_DOMAIN_SID) + CHK_ERR_WIN32A(ERROR_APPHELP_BLOCK) + CHK_ERR_WIN32A(ERROR_ACCESS_DISABLED_BY_POLICY) + CHK_ERR_WIN32A(ERROR_REG_NAT_CONSUMPTION) + CHK_ERR_WIN32A(ERROR_CSCSHARE_OFFLINE) + CHK_ERR_WIN32A(ERROR_PKINIT_FAILURE) + CHK_ERR_WIN32A(ERROR_SMARTCARD_SUBSYSTEM_FAILURE) + CHK_ERR_WIN32A(ERROR_DOWNGRADE_DETECTED) + CHK_ERR_WIN32A(ERROR_MACHINE_LOCKED) + CHK_ERR_WIN32A(ERROR_CALLBACK_SUPPLIED_INVALID_DATA) + CHK_ERR_WIN32A(ERROR_SYNC_FOREGROUND_REFRESH_REQUIRED) + CHK_ERR_WIN32A(ERROR_DRIVER_BLOCKED) + CHK_ERR_WIN32A(ERROR_INVALID_IMPORT_OF_NON_DLL) +// CHK_ERR_WIN32A(ERROR_ACCESS_DISABLED_WEBBLADE) +// CHK_ERR_WIN32A(ERROR_ACCESS_DISABLED_WEBBLADE_TAMPER) +// CHK_ERR_WIN32A(ERROR_RECOVERY_FAILURE) +// CHK_ERR_WIN32A(ERROR_ALREADY_FIBER) +// CHK_ERR_WIN32A(ERROR_ALREADY_THREAD) +// CHK_ERR_WIN32A(ERROR_STACK_BUFFER_OVERRUN) +// CHK_ERR_WIN32A(ERROR_PARAMETER_QUOTA_EXCEEDED) +// CHK_ERR_WIN32A(ERROR_DEBUGGER_INACTIVE) +// CHK_ERR_WIN32A(ERROR_DELAY_LOAD_FAILED) + CHK_ERR_WIN32A(ERROR_CURRENT_DOMAIN_NOT_ALLOWED) + CHK_ERR_WIN32A(ERROR_INSTALL_SERVICE_FAILURE) + CHK_ERR_WIN32A(ERROR_INSTALL_PACKAGE_VERSION) + CHK_ERR_WIN32A(ERROR_INSTALL_ALREADY_RUNNING) + CHK_ERR_WIN32A(ERROR_INSTALL_PACKAGE_OPEN_FAILED) + CHK_ERR_WIN32A(ERROR_INSTALL_PACKAGE_INVALID) + CHK_ERR_WIN32A(ERROR_INSTALL_UI_FAILURE) + CHK_ERR_WIN32A(ERROR_INSTALL_LOG_FAILURE) + CHK_ERR_WIN32A(ERROR_INSTALL_LANGUAGE_UNSUPPORTED) + CHK_ERR_WIN32A(ERROR_INSTALL_TRANSFORM_FAILURE) + CHK_ERR_WIN32A(ERROR_INSTALL_PACKAGE_REJECTED) + CHK_ERR_WIN32A(ERROR_FUNCTION_NOT_CALLED) + CHK_ERR_WIN32A(ERROR_FUNCTION_FAILED) + CHK_ERR_WIN32A(ERROR_INVALID_TABLE) + CHK_ERR_WIN32A(ERROR_DATATYPE_MISMATCH) + CHK_ERR_WIN32A(ERROR_UNSUPPORTED_TYPE) + CHK_ERR_WIN32A(ERROR_CREATE_FAILED) + CHK_ERR_WIN32A(ERROR_INSTALL_TEMP_UNWRITABLE) + CHK_ERR_WIN32A(ERROR_INSTALL_PLATFORM_UNSUPPORTED) + CHK_ERR_WIN32A(ERROR_INSTALL_NOTUSED) + CHK_ERR_WIN32A(ERROR_PATCH_PACKAGE_OPEN_FAILED) + CHK_ERR_WIN32A(ERROR_PATCH_PACKAGE_INVALID) + CHK_ERR_WIN32A(ERROR_PATCH_PACKAGE_UNSUPPORTED) + CHK_ERR_WIN32A(ERROR_PRODUCT_VERSION) + CHK_ERR_WIN32A(ERROR_INVALID_COMMAND_LINE) + CHK_ERR_WIN32A(ERROR_INSTALL_REMOTE_DISALLOWED) + CHK_ERR_WIN32A(ERROR_SUCCESS_REBOOT_INITIATED) + CHK_ERR_WIN32A(ERROR_PATCH_TARGET_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_PATCH_PACKAGE_REJECTED) + CHK_ERR_WIN32A(ERROR_INSTALL_TRANSFORM_REJECTED) +// CHK_ERR_WIN32A(ERROR_INSTALL_REMOTE_PROHIBITED) + CHK_ERR_WIN32A(RPC_S_ENTRY_TYPE_MISMATCH) + CHK_ERR_WIN32A(RPC_S_NOT_ALL_OBJS_EXPORTED) + CHK_ERR_WIN32A(RPC_S_INTERFACE_NOT_EXPORTED) + CHK_ERR_WIN32A(RPC_S_PROFILE_NOT_ADDED) + CHK_ERR_WIN32A(RPC_S_PRF_ELT_NOT_ADDED) + CHK_ERR_WIN32A(RPC_S_PRF_ELT_NOT_REMOVED) + CHK_ERR_WIN32A(RPC_S_GRP_ELT_NOT_ADDED) + CHK_ERR_WIN32A(RPC_S_GRP_ELT_NOT_REMOVED) + CHK_ERR_WIN32A(ERROR_KM_DRIVER_BLOCKED) + CHK_ERR_WIN32A(ERROR_CONTEXT_EXPIRED) +// CHK_ERR_WIN32A(ERROR_PER_USER_TRUST_QUOTA_EXCEEDED) +// CHK_ERR_WIN32A(ERROR_ALL_USER_TRUST_QUOTA_EXCEEDED) +// CHK_ERR_WIN32A(ERROR_USER_DELETE_TRUST_QUOTA_EXCEEDED) + CHK_ERR_WIN32A(ERROR_CONNECTED_OTHER_PASSWORD_DEFAULT) + CHK_ERR_WIN32A(ERROR_PRINTER_DRIVER_WARNED) + CHK_ERR_WIN32A(ERROR_PRINTER_DRIVER_BLOCKED) + CHK_ERR_WIN32A(ERROR_VOLUME_CONTAINS_SYS_FILES) + CHK_ERR_WIN32A(ERROR_INDIGENOUS_TYPE) + CHK_ERR_WIN32A(ERROR_NO_SUPPORTING_DRIVES) + CHK_ERR_WIN32A(ERROR_CLEANER_CARTRIDGE_INSTALLED) + CHK_ERR_WIN32A(ERROR_REPARSE_TAG_INVALID) + CHK_ERR_WIN32A(ERROR_REPARSE_TAG_MISMATCH) + CHK_ERR_WIN32A(ERROR_VOLUME_NOT_SIS_ENABLED) + CHK_ERR_WIN32A(ERROR_QUORUM_OWNER_ALIVE) + CHK_ERR_WIN32A(ERROR_NETWORK_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_NODE_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_ALL_NODES_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_RESOURCE_FAILED) + CHK_ERR_WIN32A(ERROR_CLUSTER_INVALID_NODE) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_EXISTS) + CHK_ERR_WIN32A(ERROR_CLUSTER_JOIN_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_LOCAL_NODE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_EXISTS) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETINTERFACE_EXISTS) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETINTERFACE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_INVALID_REQUEST) + CHK_ERR_WIN32A(ERROR_CLUSTER_INVALID_NETWORK_PROVIDER) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_DOWN) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_UNREACHABLE) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_NOT_MEMBER) + CHK_ERR_WIN32A(ERROR_CLUSTER_JOIN_NOT_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_CLUSTER_INVALID_NETWORK) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_UP) + CHK_ERR_WIN32A(ERROR_CLUSTER_IPADDR_IN_USE) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_NOT_PAUSED) + CHK_ERR_WIN32A(ERROR_CLUSTER_NO_SECURITY_CONTEXT) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_NOT_INTERNAL) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_ALREADY_UP) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_ALREADY_DOWN) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_ALREADY_ONLINE) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_ALREADY_OFFLINE) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_ALREADY_MEMBER) + CHK_ERR_WIN32A(ERROR_CLUSTER_LAST_INTERNAL_NETWORK) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_HAS_DEPENDENTS) + CHK_ERR_WIN32A(ERROR_INVALID_OPERATION_ON_QUORUM) + CHK_ERR_WIN32A(ERROR_DEPENDENCY_NOT_ALLOWED) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_PAUSED) + CHK_ERR_WIN32A(ERROR_NODE_CANT_HOST_RESOURCE) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_NOT_READY) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_SHUTTING_DOWN) + CHK_ERR_WIN32A(ERROR_CLUSTER_JOIN_ABORTED) + CHK_ERR_WIN32A(ERROR_CLUSTER_INCOMPATIBLE_VERSIONS) + CHK_ERR_WIN32A(ERROR_CLUSTER_MAXNUM_OF_RESOURCES_EXCEEDED) + CHK_ERR_WIN32A(ERROR_CLUSTER_SYSTEM_CONFIG_CHANGED) + CHK_ERR_WIN32A(ERROR_CLUSTER_RESOURCE_TYPE_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_RESTYPE_NOT_SUPPORTED) + CHK_ERR_WIN32A(ERROR_CLUSTER_RESNAME_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_NO_RPC_PACKAGES_REGISTERED) + CHK_ERR_WIN32A(ERROR_CLUSTER_OWNER_NOT_IN_PREFLIST) + CHK_ERR_WIN32A(ERROR_CLUSTER_DATABASE_SEQMISMATCH) + CHK_ERR_WIN32A(ERROR_RESMON_INVALID_STATE) + CHK_ERR_WIN32A(ERROR_CLUSTER_GUM_NOT_LOCKER) + CHK_ERR_WIN32A(ERROR_QUORUM_DISK_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_DATABASE_BACKUP_CORRUPT) + CHK_ERR_WIN32A(ERROR_CLUSTER_NODE_ALREADY_HAS_DFS_ROOT) + CHK_ERR_WIN32A(ERROR_RESOURCE_PROPERTY_UNCHANGEABLE) + CHK_ERR_WIN32A(ERROR_CLUSTER_MEMBERSHIP_INVALID_STATE) + CHK_ERR_WIN32A(ERROR_CLUSTER_QUORUMLOG_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_CLUSTER_MEMBERSHIP_HALT) + CHK_ERR_WIN32A(ERROR_CLUSTER_INSTANCE_ID_MISMATCH) + CHK_ERR_WIN32A(ERROR_CLUSTER_NETWORK_NOT_FOUND_FOR_IP) + CHK_ERR_WIN32A(ERROR_CLUSTER_PROPERTY_DATA_TYPE_MISMATCH) + CHK_ERR_WIN32A(ERROR_CLUSTER_EVICT_WITHOUT_CLEANUP) + CHK_ERR_WIN32A(ERROR_CLUSTER_PARAMETER_MISMATCH) + CHK_ERR_WIN32A(ERROR_NODE_CANNOT_BE_CLUSTERED) + CHK_ERR_WIN32A(ERROR_CLUSTER_WRONG_OS_VERSION) + CHK_ERR_WIN32A(ERROR_CLUSTER_CANT_CREATE_DUP_CLUSTER_NAME) +// CHK_ERR_WIN32A(ERROR_CLUSCFG_ALREADY_COMMITTED) +// CHK_ERR_WIN32A(ERROR_CLUSCFG_ROLLBACK_FAILED) +// CHK_ERR_WIN32A(ERROR_CLUSCFG_SYSTEM_DISK_DRIVE_LETTER_CONFLICT) +// CHK_ERR_WIN32A(ERROR_CLUSTER_OLD_VERSION) +// CHK_ERR_WIN32A(ERROR_CLUSTER_MISMATCHED_COMPUTER_ACCT_NAME) + CHK_ERR_WIN32A(ERROR_FILE_READ_ONLY) + CHK_ERR_WIN32A(ERROR_DIR_EFS_DISALLOWED) + CHK_ERR_WIN32A(ERROR_EFS_SERVER_NOT_TRUSTED) + CHK_ERR_WIN32A(ERROR_BAD_RECOVERY_POLICY) + CHK_ERR_WIN32A(ERROR_EFS_ALG_BLOB_TOO_BIG) + CHK_ERR_WIN32A(ERROR_VOLUME_NOT_SUPPORT_EFS) + CHK_ERR_WIN32A(ERROR_EFS_DISABLED) + CHK_ERR_WIN32A(ERROR_EFS_VERSION_NOT_SUPPORT) + CHK_ERR_WIN32A(SCHED_E_SERVICE_NOT_LOCALSYSTEM) + CHK_ERR_WIN32A(ERROR_CTX_CLIENT_LICENSE_IN_USE) + CHK_ERR_WIN32A(ERROR_CTX_CLIENT_LICENSE_NOT_SET) + CHK_ERR_WIN32A(ERROR_CTX_LICENSE_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_CTX_LICENSE_CLIENT_INVALID) + CHK_ERR_WIN32A(ERROR_CTX_LICENSE_EXPIRED) + CHK_ERR_WIN32A(ERROR_CTX_SHADOW_NOT_RUNNING) + CHK_ERR_WIN32A(ERROR_CTX_SHADOW_ENDED_BY_MODE_CHANGE) +// CHK_ERR_WIN32A(ERROR_ACTIVATION_COUNT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_SORT_CONTROL_MISSING) + CHK_ERR_WIN32A(ERROR_DS_OFFSET_RANGE_ERROR) + CHK_ERR_WIN32A(ERROR_DS_OBJECT_BEING_REMOVED) + CHK_ERR_WIN32A(ERROR_DS_DS_REQUIRED) + CHK_ERR_WIN32A(ERROR_DS_INVALID_LDAP_DISPLAY_NAME) + CHK_ERR_WIN32A(ERROR_DS_NON_BASE_SEARCH) + CHK_ERR_WIN32A(ERROR_DS_CANT_RETRIEVE_ATTS) + CHK_ERR_WIN32A(ERROR_DS_BACKLINK_WITHOUT_LINK) + CHK_ERR_WIN32A(ERROR_DS_EPOCH_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_SRC_NAME_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_SRC_AND_DST_NC_IDENTICAL) + CHK_ERR_WIN32A(ERROR_DS_DST_NC_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_NOT_AUTHORITIVE_FOR_DST_NC) + CHK_ERR_WIN32A(ERROR_DS_SRC_GUID_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_CANT_MOVE_DELETED_OBJECT) + CHK_ERR_WIN32A(ERROR_DS_PDC_OPERATION_IN_PROGRESS) + CHK_ERR_WIN32A(ERROR_DS_CROSS_DOMAIN_CLEANUP_REQD) + CHK_ERR_WIN32A(ERROR_DS_ILLEGAL_XDOM_MOVE_OPERATION) + CHK_ERR_WIN32A(ERROR_DS_CANT_WITH_ACCT_GROUP_MEMBERSHPS) + CHK_ERR_WIN32A(ERROR_DS_NC_MUST_HAVE_NC_PARENT) + CHK_ERR_WIN32A(ERROR_DS_CR_IMPOSSIBLE_TO_VALIDATE) + CHK_ERR_WIN32A(ERROR_DS_DST_DOMAIN_NOT_NATIVE) + CHK_ERR_WIN32A(ERROR_DS_MISSING_INFRASTRUCTURE_CONTAINER) + CHK_ERR_WIN32A(ERROR_DS_CANT_MOVE_ACCOUNT_GROUP) + CHK_ERR_WIN32A(ERROR_DS_CANT_MOVE_RESOURCE_GROUP) + CHK_ERR_WIN32A(ERROR_DS_INVALID_SEARCH_FLAG) + CHK_ERR_WIN32A(ERROR_DS_NO_TREE_DELETE_ABOVE_NC) + CHK_ERR_WIN32A(ERROR_DS_COULDNT_LOCK_TREE_FOR_DELETE) + CHK_ERR_WIN32A(ERROR_DS_COULDNT_IDENTIFY_OBJECTS_FOR_TREE_DELETE) + CHK_ERR_WIN32A(ERROR_DS_SAM_INIT_FAILURE) + CHK_ERR_WIN32A(ERROR_DS_SENSITIVE_GROUP_VIOLATION) + CHK_ERR_WIN32A(ERROR_DS_CANT_MOD_PRIMARYGROUPID) + CHK_ERR_WIN32A(ERROR_DS_ILLEGAL_BASE_SCHEMA_MOD) + CHK_ERR_WIN32A(ERROR_DS_NONSAFE_SCHEMA_CHANGE) + CHK_ERR_WIN32A(ERROR_DS_SCHEMA_UPDATE_DISALLOWED) + CHK_ERR_WIN32A(ERROR_DS_CANT_CREATE_UNDER_SCHEMA) + CHK_ERR_WIN32A(ERROR_DS_INSTALL_NO_SRC_SCH_VERSION) + CHK_ERR_WIN32A(ERROR_DS_INSTALL_NO_SCH_VERSION_IN_INIFILE) + CHK_ERR_WIN32A(ERROR_DS_INVALID_GROUP_TYPE) + CHK_ERR_WIN32A(ERROR_DS_NO_NEST_GLOBALGROUP_IN_MIXEDDOMAIN) + CHK_ERR_WIN32A(ERROR_DS_NO_NEST_LOCALGROUP_IN_MIXEDDOMAIN) + CHK_ERR_WIN32A(ERROR_DS_GLOBAL_CANT_HAVE_LOCAL_MEMBER) + CHK_ERR_WIN32A(ERROR_DS_GLOBAL_CANT_HAVE_UNIVERSAL_MEMBER) + CHK_ERR_WIN32A(ERROR_DS_UNIVERSAL_CANT_HAVE_LOCAL_MEMBER) + CHK_ERR_WIN32A(ERROR_DS_GLOBAL_CANT_HAVE_CROSSDOMAIN_MEMBER) + CHK_ERR_WIN32A(ERROR_DS_LOCAL_CANT_HAVE_CROSSDOMAIN_LOCAL_MEMBER) + CHK_ERR_WIN32A(ERROR_DS_HAVE_PRIMARY_MEMBERS) + CHK_ERR_WIN32A(ERROR_DS_STRING_SD_CONVERSION_FAILED) + CHK_ERR_WIN32A(ERROR_DS_NAMING_MASTER_GC) + CHK_ERR_WIN32A(ERROR_DS_DNS_LOOKUP_FAILURE) + CHK_ERR_WIN32A(ERROR_DS_COULDNT_UPDATE_SPNS) + CHK_ERR_WIN32A(ERROR_DS_CANT_RETRIEVE_SD) + CHK_ERR_WIN32A(ERROR_DS_KEY_NOT_UNIQUE) + CHK_ERR_WIN32A(ERROR_DS_WRONG_LINKED_ATT_SYNTAX) + CHK_ERR_WIN32A(ERROR_DS_SAM_NEED_BOOTKEY_PASSWORD) + CHK_ERR_WIN32A(ERROR_DS_SAM_NEED_BOOTKEY_FLOPPY) + CHK_ERR_WIN32A(ERROR_DS_CANT_START) + CHK_ERR_WIN32A(ERROR_DS_INIT_FAILURE) + CHK_ERR_WIN32A(ERROR_DS_NO_PKT_PRIVACY_ON_CONNECTION) + CHK_ERR_WIN32A(ERROR_DS_SOURCE_DOMAIN_IN_FOREST) + CHK_ERR_WIN32A(ERROR_DS_DESTINATION_DOMAIN_NOT_IN_FOREST) + CHK_ERR_WIN32A(ERROR_DS_DESTINATION_AUDITING_NOT_ENABLED) + CHK_ERR_WIN32A(ERROR_DS_CANT_FIND_DC_FOR_SRC_DOMAIN) + CHK_ERR_WIN32A(ERROR_DS_SRC_OBJ_NOT_GROUP_OR_USER) + CHK_ERR_WIN32A(ERROR_DS_SRC_SID_EXISTS_IN_FOREST) + CHK_ERR_WIN32A(ERROR_DS_SRC_AND_DST_OBJECT_CLASS_MISMATCH) + CHK_ERR_WIN32A(ERROR_SAM_INIT_FAILURE) + CHK_ERR_WIN32A(ERROR_DS_DRA_SCHEMA_INFO_SHIP) + CHK_ERR_WIN32A(ERROR_DS_DRA_SCHEMA_CONFLICT) + CHK_ERR_WIN32A(ERROR_DS_DRA_EARLIER_SCHEMA_CONFLICT) + CHK_ERR_WIN32A(ERROR_DS_DRA_OBJ_NC_MISMATCH) + CHK_ERR_WIN32A(ERROR_DS_NC_STILL_HAS_DSAS) + CHK_ERR_WIN32A(ERROR_DS_GC_REQUIRED) + CHK_ERR_WIN32A(ERROR_DS_LOCAL_MEMBER_OF_LOCAL_ONLY) + CHK_ERR_WIN32A(ERROR_DS_NO_FPO_IN_UNIVERSAL_GROUPS) + CHK_ERR_WIN32A(ERROR_DS_CANT_ADD_TO_GC) + CHK_ERR_WIN32A(ERROR_DS_NO_CHECKPOINT_WITH_PDC) + CHK_ERR_WIN32A(ERROR_DS_SOURCE_AUDITING_NOT_ENABLED) + CHK_ERR_WIN32A(ERROR_DS_CANT_CREATE_IN_NONDOMAIN_NC) + CHK_ERR_WIN32A(ERROR_DS_INVALID_NAME_FOR_SPN) + CHK_ERR_WIN32A(ERROR_DS_FILTER_USES_CONTRUCTED_ATTRS) + CHK_ERR_WIN32A(ERROR_DS_UNICODEPWD_NOT_IN_QUOTES) + CHK_ERR_WIN32A(ERROR_DS_MACHINE_ACCOUNT_QUOTA_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_MUST_BE_RUN_ON_DST_DC) + CHK_ERR_WIN32A(ERROR_DS_SRC_DC_MUST_BE_SP4_OR_GREATER) + CHK_ERR_WIN32A(ERROR_DS_CANT_TREE_DELETE_CRITICAL_OBJ) + CHK_ERR_WIN32A(ERROR_DS_INIT_FAILURE_CONSOLE) + CHK_ERR_WIN32A(ERROR_DS_SAM_INIT_FAILURE_CONSOLE) + CHK_ERR_WIN32A(ERROR_DS_FOREST_VERSION_TOO_HIGH) + CHK_ERR_WIN32A(ERROR_DS_DOMAIN_VERSION_TOO_HIGH) + CHK_ERR_WIN32A(ERROR_DS_FOREST_VERSION_TOO_LOW) + CHK_ERR_WIN32A(ERROR_DS_DOMAIN_VERSION_TOO_LOW) + CHK_ERR_WIN32A(ERROR_DS_INCOMPATIBLE_VERSION) + CHK_ERR_WIN32A(ERROR_DS_LOW_DSA_VERSION) + CHK_ERR_WIN32A(ERROR_DS_NO_BEHAVIOR_VERSION_IN_MIXEDDOMAIN) + CHK_ERR_WIN32A(ERROR_DS_NOT_SUPPORTED_SORT_ORDER) + CHK_ERR_WIN32A(ERROR_DS_NAME_NOT_UNIQUE) + CHK_ERR_WIN32A(ERROR_DS_MACHINE_ACCOUNT_CREATED_PRENT4) + CHK_ERR_WIN32A(ERROR_DS_OUT_OF_VERSION_STORE) + CHK_ERR_WIN32A(ERROR_DS_INCOMPATIBLE_CONTROLS_USED) + CHK_ERR_WIN32A(ERROR_DS_NO_REF_DOMAIN) + CHK_ERR_WIN32A(ERROR_DS_RESERVED_LINK_ID) + CHK_ERR_WIN32A(ERROR_DS_LINK_ID_NOT_AVAILABLE) + CHK_ERR_WIN32A(ERROR_DS_AG_CANT_HAVE_UNIVERSAL_MEMBER) + CHK_ERR_WIN32A(ERROR_DS_MODIFYDN_DISALLOWED_BY_INSTANCE_TYPE) + CHK_ERR_WIN32A(ERROR_DS_NO_OBJECT_MOVE_IN_SCHEMA_NC) + CHK_ERR_WIN32A(ERROR_DS_MODIFYDN_DISALLOWED_BY_FLAG) + CHK_ERR_WIN32A(ERROR_DS_MODIFYDN_WRONG_GRANDPARENT) + CHK_ERR_WIN32A(ERROR_DS_NAME_ERROR_TRUST_REFERRAL) + CHK_ERR_WIN32A(ERROR_NOT_SUPPORTED_ON_STANDARD_SERVER) + CHK_ERR_WIN32A(ERROR_DS_CANT_ACCESS_REMOTE_PART_OF_AD) + CHK_ERR_WIN32A(ERROR_DS_CR_IMPOSSIBLE_TO_VALIDATE_V2) + CHK_ERR_WIN32A(ERROR_DS_THREAD_LIMIT_EXCEEDED) + CHK_ERR_WIN32A(ERROR_DS_NOT_CLOSEST) + CHK_ERR_WIN32A(ERROR_DS_CANT_DERIVE_SPN_WITHOUT_SERVER_REF) + CHK_ERR_WIN32A(ERROR_DS_SINGLE_USER_MODE_FAILED) + CHK_ERR_WIN32A(ERROR_DS_NTDSCRIPT_SYNTAX_ERROR) + CHK_ERR_WIN32A(ERROR_DS_NTDSCRIPT_PROCESS_ERROR) + CHK_ERR_WIN32A(ERROR_DS_DIFFERENT_REPL_EPOCHS) + CHK_ERR_WIN32A(ERROR_DS_DRS_EXTENSIONS_CHANGED) + CHK_ERR_WIN32A(ERROR_DS_REPLICA_SET_CHANGE_NOT_ALLOWED_ON_DISABLED_CR) + CHK_ERR_WIN32A(ERROR_DS_NO_MSDS_INTID) + CHK_ERR_WIN32A(ERROR_DS_DUP_MSDS_INTID) + CHK_ERR_WIN32A(ERROR_DS_EXISTS_IN_RDNATTID) + CHK_ERR_WIN32A(ERROR_DS_AUTHORIZATION_FAILED) + CHK_ERR_WIN32A(ERROR_DS_INVALID_SCRIPT) + CHK_ERR_WIN32A(ERROR_DS_REMOTE_CROSSREF_OP_FAILED) +// CHK_ERR_WIN32A(ERROR_DS_CROSS_REF_BUSY) +// CHK_ERR_WIN32A(ERROR_DS_CANT_DERIVE_SPN_FOR_DELETED_DOMAIN) +// CHK_ERR_WIN32A(ERROR_DS_CANT_DEMOTE_WITH_WRITEABLE_NC) +// CHK_ERR_WIN32A(ERROR_DS_DUPLICATE_ID_FOUND) +// CHK_ERR_WIN32A(ERROR_DS_INSUFFICIENT_ATTR_TO_CREATE_OBJECT) +// CHK_ERR_WIN32A(ERROR_DS_GROUP_CONVERSION_ERROR) +// CHK_ERR_WIN32A(ERROR_DS_CANT_MOVE_APP_BASIC_GROUP) +// CHK_ERR_WIN32A(ERROR_DS_CANT_MOVE_APP_QUERY_GROUP) +// CHK_ERR_WIN32A(ERROR_DS_ROLE_NOT_VERIFIED) +// CHK_ERR_WIN32A(ERROR_DS_WKO_CONTAINER_CANNOT_BE_SPECIAL) +// CHK_ERR_WIN32A(ERROR_DS_DOMAIN_RENAME_IN_PROGRESS) +// CHK_ERR_WIN32A(ERROR_DS_EXISTING_AD_CHILD_NC) +// CHK_ERR_WIN32A(DNS_ERROR_INVALID_NAME_CHAR) +// CHK_ERR_WIN32A(DNS_ERROR_NUMERIC_NAME) +// CHK_ERR_WIN32A(DNS_ERROR_NOT_ALLOWED_ON_ROOT_SERVER) +// CHK_ERR_WIN32A(DNS_ERROR_NOT_ALLOWED_UNDER_DELEGATION) +// CHK_ERR_WIN32A(DNS_ERROR_CANNOT_FIND_ROOT_HINTS) +// CHK_ERR_WIN32A(DNS_ERROR_INCONSISTENT_ROOT_HINTS) + CHK_ERR_WIN32A(DNS_ERROR_FORWARDER_ALREADY_EXISTS) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_REQUIRES_MASTER_IP) + CHK_ERR_WIN32A(DNS_ERROR_ZONE_IS_SHUTDOWN) + CHK_ERR_WIN32A(DNS_ERROR_DP_BASE) + CHK_ERR_WIN32A(DNS_ERROR_DP_DOES_NOT_EXIST) + CHK_ERR_WIN32A(DNS_ERROR_DP_ALREADY_EXISTS) + CHK_ERR_WIN32A(DNS_ERROR_DP_NOT_ENLISTED) + CHK_ERR_WIN32A(DNS_ERROR_DP_ALREADY_ENLISTED) +// CHK_ERR_WIN32A(DNS_ERROR_DP_NOT_AVAILABLE) + CHK_ERR_WIN32A(WSA_QOS_ESERVICETYPE) + CHK_ERR_WIN32A(WSA_QOS_EFLOWSPEC) + CHK_ERR_WIN32A(WSA_QOS_EPROVSPECBUF) + CHK_ERR_WIN32A(WSA_QOS_EFILTERSTYLE) + CHK_ERR_WIN32A(WSA_QOS_EFILTERTYPE) + CHK_ERR_WIN32A(WSA_QOS_EFILTERCOUNT) + CHK_ERR_WIN32A(WSA_QOS_EOBJLENGTH) + CHK_ERR_WIN32A(WSA_QOS_EFLOWCOUNT) + CHK_ERR_WIN32A(WSA_QOS_EUNKOWNPSOBJ) + CHK_ERR_WIN32A(WSA_QOS_EPOLICYOBJ) + CHK_ERR_WIN32A(WSA_QOS_EFLOWDESC) + CHK_ERR_WIN32A(WSA_QOS_EPSFLOWSPEC) + CHK_ERR_WIN32A(WSA_QOS_EPSFILTERSPEC) + CHK_ERR_WIN32A(WSA_QOS_ESDMODEOBJ) + CHK_ERR_WIN32A(WSA_QOS_ESHAPERATEOBJ) + CHK_ERR_WIN32A(WSA_QOS_RESERVED_PETYPE) + CHK_ERR_WIN32A(ERROR_SXS_SECTION_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_SXS_CANT_GEN_ACTCTX) + CHK_ERR_WIN32A(ERROR_SXS_INVALID_ACTCTXDATA_FORMAT) + CHK_ERR_WIN32A(ERROR_SXS_ASSEMBLY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_SXS_MANIFEST_FORMAT_ERROR) + CHK_ERR_WIN32A(ERROR_SXS_MANIFEST_PARSE_ERROR) + CHK_ERR_WIN32A(ERROR_SXS_ACTIVATION_CONTEXT_DISABLED) + CHK_ERR_WIN32A(ERROR_SXS_KEY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_SXS_VERSION_CONFLICT) + CHK_ERR_WIN32A(ERROR_SXS_WRONG_SECTION_TYPE) + CHK_ERR_WIN32A(ERROR_SXS_THREAD_QUERIES_DISABLED) + CHK_ERR_WIN32A(ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET) + CHK_ERR_WIN32A(ERROR_SXS_UNKNOWN_ENCODING_GROUP) + CHK_ERR_WIN32A(ERROR_SXS_UNKNOWN_ENCODING) + CHK_ERR_WIN32A(ERROR_SXS_INVALID_XML_NAMESPACE_URI) + CHK_ERR_WIN32A(ERROR_SXS_ROOT_MANIFEST_DEPENDENCY_NOT_INSTALLED) + CHK_ERR_WIN32A(ERROR_SXS_LEAF_MANIFEST_DEPENDENCY_NOT_INSTALLED) + CHK_ERR_WIN32A(ERROR_SXS_INVALID_ASSEMBLY_IDENTITY_ATTRIBUTE) + CHK_ERR_WIN32A(ERROR_SXS_MANIFEST_MISSING_REQUIRED_DEFAULT_NAMESPACE) + CHK_ERR_WIN32A(ERROR_SXS_MANIFEST_INVALID_REQUIRED_DEFAULT_NAMESPACE) + CHK_ERR_WIN32A(ERROR_SXS_PRIVATE_MANIFEST_CROSS_PATH_WITH_REPARSE_POINT) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_DLL_NAME) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_WINDOWCLASS_NAME) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_CLSID) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_IID) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_TLBID) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_PROGID) + CHK_ERR_WIN32A(ERROR_SXS_DUPLICATE_ASSEMBLY_NAME) + CHK_ERR_WIN32A(ERROR_SXS_FILE_HASH_MISMATCH) + CHK_ERR_WIN32A(ERROR_SXS_POLICY_PARSE_ERROR) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MISSINGQUOTE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_COMMENTSYNTAX) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADSTARTNAMECHAR) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADNAMECHAR) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADCHARINSTRING) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_XMLDECLSYNTAX) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADCHARDATA) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MISSINGWHITESPACE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_EXPECTINGTAGEND) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MISSINGSEMICOLON) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNBALANCEDPAREN) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INTERNALERROR) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNEXPECTED_WHITESPACE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INCOMPLETE_ENCODING) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MISSING_PAREN) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_EXPECTINGCLOSEQUOTE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MULTIPLE_COLONS) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALID_DECIMAL) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALID_HEXIDECIMAL) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALID_UNICODE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_WHITESPACEORQUESTIONMARK) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNEXPECTEDENDTAG) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDTAG) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_DUPLICATEATTRIBUTE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MULTIPLEROOTS) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALIDATROOTLEVEL) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADXMLDECL) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MISSINGROOT) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNEXPECTEDEOF) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADPEREFINSUBSET) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDSTARTTAG) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDENDTAG) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDSTRING) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDCOMMENT) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDDECL) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNCLOSEDCDATA) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_RESERVEDNAMESPACE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALIDENCODING) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALIDSWITCH) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_BADXMLCASE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALID_STANDALONE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_UNEXPECTED_STANDALONE) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_INVALID_VERSION) + CHK_ERR_WIN32A(ERROR_SXS_XML_E_MISSINGEQUALS) + CHK_ERR_WIN32A(ERROR_SXS_PROTECTION_RECOVERY_FAILED) + CHK_ERR_WIN32A(ERROR_SXS_PROTECTION_PUBLIC_KEY_TOO_SHORT) + CHK_ERR_WIN32A(ERROR_SXS_PROTECTION_CATALOG_NOT_VALID) + CHK_ERR_WIN32A(ERROR_SXS_UNTRANSLATABLE_HRESULT) + CHK_ERR_WIN32A(ERROR_SXS_PROTECTION_CATALOG_FILE_MISSING) + CHK_ERR_WIN32A(ERROR_SXS_MISSING_ASSEMBLY_IDENTITY_ATTRIBUTE) + CHK_ERR_WIN32A(ERROR_SXS_INVALID_ASSEMBLY_IDENTITY_ATTRIBUTE_NAME) + CHK_ERR_WIN32A(ERROR_IPSEC_QM_POLICY_EXISTS) + CHK_ERR_WIN32A(ERROR_IPSEC_QM_POLICY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_QM_POLICY_IN_USE) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_POLICY_EXISTS) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_POLICY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_POLICY_IN_USE) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_FILTER_EXISTS) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_FILTER_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_TRANSPORT_FILTER_EXISTS) + CHK_ERR_WIN32A(ERROR_IPSEC_TRANSPORT_FILTER_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_AUTH_EXISTS) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_AUTH_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_AUTH_IN_USE) + CHK_ERR_WIN32A(ERROR_IPSEC_DEFAULT_MM_POLICY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_DEFAULT_MM_AUTH_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_DEFAULT_QM_POLICY_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_TUNNEL_FILTER_EXISTS) + CHK_ERR_WIN32A(ERROR_IPSEC_TUNNEL_FILTER_NOT_FOUND) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_FILTER_PENDING_DELETION) + CHK_ERR_WIN32A(ERROR_IPSEC_TRANSPORT_FILTER_PENDING_DELETION) + CHK_ERR_WIN32A(ERROR_IPSEC_TUNNEL_FILTER_PENDING_DELETION) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_POLICY_PENDING_DELETION) + CHK_ERR_WIN32A(ERROR_IPSEC_MM_AUTH_PENDING_DELETION) + CHK_ERR_WIN32A(ERROR_IPSEC_QM_POLICY_PENDING_DELETION) +// CHK_ERR_WIN32A(WARNING_IPSEC_MM_POLICY_PRUNED) +// CHK_ERR_WIN32A(WARNING_IPSEC_QM_POLICY_PRUNED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NEG_STATUS_BEGIN) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_AUTH_FAIL) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_ATTRIB_FAIL) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NEGOTIATION_PENDING) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_GENERAL_PROCESSING_ERROR) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_TIMED_OUT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NO_CERT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_SA_DELETED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_SA_REAPED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_MM_ACQUIRE_DROP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_QM_ACQUIRE_DROP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_QUEUE_DROP_MM) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_QUEUE_DROP_NO_MM) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_DROP_NO_RESPONSE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_MM_DELAY_DROP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_QM_DELAY_DROP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_ERROR) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_CRL_FAILED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_KEY_USAGE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_CERT_TYPE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NO_PRIVATE_KEY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_DH_FAIL) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_HEADER) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NO_POLICY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_SIGNATURE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_KERBEROS_ERROR) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NO_PUBLIC_KEY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_SA) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_PROP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_TRANS) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_KE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_ID) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_CERT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_CERT_REQ) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_HASH) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_SIG) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_NONCE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_NOTIFY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_DELETE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PROCESS_ERR_VENDOR) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_PAYLOAD) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_LOAD_SOFT_SA) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_SOFT_SA_TORN_DOWN) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_COOKIE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NO_PEER_CERT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_PEER_CRL_FAILED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_POLICY_CHANGE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NO_MM_POLICY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NOTCBPRIV) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_SECLOADFAIL) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_FAILSSPINIT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_FAILQUERYSSP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_SRVACQFAIL) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_SRVQUERYCRED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_GETSPIFAIL) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_FILTER) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_OUT_OF_MEMORY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_ADD_UPDATE_KEY_FAILED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_POLICY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_UNKNOWN_DOI) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_SITUATION) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_DH_FAILURE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_GROUP) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_ENCRYPT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_DECRYPT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_POLICY_MATCH) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_UNSUPPORTED_ID) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_HASH) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_HASH_ALG) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_HASH_SIZE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_ENCRYPT_ALG) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_AUTH_ALG) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_SIG) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_LOAD_FAILED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_RPC_DELETE) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_BENIGN_REINIT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_RESPONDER_LIFETIME_NOTIFY) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_INVALID_CERT_KEYLEN) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_MM_LIMIT) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NEGOTIATION_DISABLED) + CHK_ERR_WIN32A(ERROR_IPSEC_IKE_NEG_STATUS_END) + +// ------------------------------------------------------------- +// dxgi.h error codes +// ------------------------------------------------------------- + CHK_ERRA(DXGI_STATUS_OCCLUDED) + CHK_ERRA(DXGI_STATUS_CLIPPED) + CHK_ERRA(DXGI_STATUS_NO_REDIRECTION) + CHK_ERRA(DXGI_STATUS_NO_DESKTOP_ACCESS) + CHK_ERRA(DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE) + CHK_ERRA(DXGI_STATUS_MODE_CHANGED) + CHK_ERRA(DXGI_STATUS_MODE_CHANGE_IN_PROGRESS) + CHK_ERRA(DXGI_ERROR_INVALID_CALL) + CHK_ERRA(DXGI_ERROR_NOT_FOUND) + CHK_ERRA(DXGI_ERROR_MORE_DATA) + CHK_ERRA(DXGI_ERROR_UNSUPPORTED) + CHK_ERRA(DXGI_ERROR_DEVICE_REMOVED) + CHK_ERRA(DXGI_ERROR_DEVICE_HUNG) + CHK_ERRA(DXGI_ERROR_DEVICE_RESET) + CHK_ERRA(DXGI_ERROR_WAS_STILL_DRAWING) + CHK_ERRA(DXGI_ERROR_FRAME_STATISTICS_DISJOINT) + CHK_ERRA(DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE) + CHK_ERRA(DXGI_ERROR_DRIVER_INTERNAL_ERROR) + CHK_ERRA(DXGI_ERROR_NONEXCLUSIVE) + CHK_ERRA(DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) + CHK_ERRA(DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED) + CHK_ERRA(DXGI_ERROR_REMOTE_OUTOFMEMORY) + +// ------------------------------------------------------------- +// d3d11.h error codes +// ------------------------------------------------------------- + CHK_ERRA(D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS) + CHK_ERRA(D3D11_ERROR_FILE_NOT_FOUND) + CHK_ERRA(D3D11_ERROR_TOO_MANY_UNIQUE_VIEW_OBJECTS) + CHK_ERRA(D3D11_ERROR_DEFERRED_CONTEXT_MAP_WITHOUT_INITIAL_DISCARD) + +// ------------------------------------------------------------- +// WIC error codes +// ------------------------------------------------------------- + CHK_ERRA(WINCODEC_ERR_WRONGSTATE) + CHK_ERRA(WINCODEC_ERR_VALUEOUTOFRANGE) + CHK_ERRA(WINCODEC_ERR_UNKNOWNIMAGEFORMAT) + CHK_ERRA(WINCODEC_ERR_UNSUPPORTEDVERSION) + CHK_ERRA(WINCODEC_ERR_NOTINITIALIZED) + CHK_ERRA(WINCODEC_ERR_ALREADYLOCKED) + CHK_ERRA(WINCODEC_ERR_PROPERTYNOTFOUND) + CHK_ERRA(WINCODEC_ERR_PROPERTYNOTSUPPORTED) + CHK_ERRA(WINCODEC_ERR_PROPERTYSIZE) + CHK_ERRA(WINCODEC_ERR_CODECPRESENT) + CHK_ERRA(WINCODEC_ERR_CODECNOTHUMBNAIL) + CHK_ERRA(WINCODEC_ERR_PALETTEUNAVAILABLE) + CHK_ERRA(WINCODEC_ERR_CODECTOOMANYSCANLINES) + CHK_ERRA(WINCODEC_ERR_INTERNALERROR) + CHK_ERRA(WINCODEC_ERR_SOURCERECTDOESNOTMATCHDIMENSIONS) + CHK_ERRA(WINCODEC_ERR_COMPONENTNOTFOUND) + CHK_ERRA(WINCODEC_ERR_IMAGESIZEOUTOFRANGE) + CHK_ERRA(WINCODEC_ERR_TOOMUCHMETADATA) + CHK_ERRA(WINCODEC_ERR_BADIMAGE) + CHK_ERRA(WINCODEC_ERR_BADHEADER) + CHK_ERRA(WINCODEC_ERR_FRAMEMISSING) + CHK_ERRA(WINCODEC_ERR_BADMETADATAHEADER) + CHK_ERRA(WINCODEC_ERR_BADSTREAMDATA) + CHK_ERRA(WINCODEC_ERR_STREAMWRITE) + CHK_ERRA(WINCODEC_ERR_STREAMREAD) + CHK_ERRA(WINCODEC_ERR_STREAMNOTAVAILABLE) +// CHK_ERRA(WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT) + CHK_ERRA(WINCODEC_ERR_UNSUPPORTEDOPERATION) + CHK_ERRA(WINCODEC_ERR_INVALIDREGISTRATION) + CHK_ERRA(WINCODEC_ERR_COMPONENTINITIALIZEFAILURE) + CHK_ERRA(WINCODEC_ERR_INSUFFICIENTBUFFER) + CHK_ERRA(WINCODEC_ERR_DUPLICATEMETADATAPRESENT) + CHK_ERRA(WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE) + CHK_ERRA(WINCODEC_ERR_UNEXPECTEDSIZE) + CHK_ERRA(WINCODEC_ERR_INVALIDQUERYREQUEST) + CHK_ERRA(WINCODEC_ERR_UNEXPECTEDMETADATATYPE) + CHK_ERRA(WINCODEC_ERR_REQUESTONLYVALIDATMETADATAROOT) + CHK_ERRA(WINCODEC_ERR_INVALIDQUERYCHARACTER) + CHK_ERRA(WINCODEC_ERR_WIN32ERROR) + CHK_ERRA(WINCODEC_ERR_INVALIDPROGRESSIVELEVEL) + +// ------------------------------------------------------------- +// DXUT error codes +// ------------------------------------------------------------- + CHK_ERRA(DXUTERR_NODIRECT3D) + CHK_ERRA(DXUTERR_NOCOMPATIBLEDEVICES) + CHK_ERRA(DXUTERR_MEDIANOTFOUND) + CHK_ERRA(DXUTERR_NONZEROREFCOUNT) + CHK_ERRA(DXUTERR_CREATINGDEVICE) + CHK_ERRA(DXUTERR_RESETTINGDEVICE) + CHK_ERRA(DXUTERR_CREATINGDEVICEOBJECTS) + CHK_ERRA(DXUTERR_RESETTINGDEVICEOBJECTS) + CHK_ERRA(DXUTERR_INCORRECTVERSION) + CHK_ERRA(DXUTERR_DEVICEREMOVED) + +// ------------------------------------------------------------- +// xaudio2.h error codes +// ------------------------------------------------------------- + CHK_ERRA(XAUDIO2_E_INVALID_CALL) + CHK_ERRA(XAUDIO2_E_XMA_DECODER_ERROR) + CHK_ERRA(XAUDIO2_E_XAPO_CREATION_FAILED) + CHK_ERRA(XAUDIO2_E_DEVICE_INVALIDATED) + +// ------------------------------------------------------------- +// xapo.h error codes +// ------------------------------------------------------------- + CHK_ERRA(XAPO_E_FORMAT_UNSUPPORTED) + } + + return L"Unknown"; +} + +//-------------------------------------------------------------------------------------- +#undef CHK_ERR +#undef CHK_ERRA +#undef HRESULT_FROM_WIN32b +#undef CHK_ERR_WIN32A +#undef CHK_ERR_WIN32_ONLY + +#define CHK_ERRA(hrchk) \ + case hrchk: \ + wcscpy_s( desc, count, L#hrchk ); + +#define CHK_ERR(hrchk, strOut) \ + case hrchk: \ + wcscpy_s( desc, count, L##strOut ); + + +//-------------------------------------------------------------------------------------- +void WINAPI DXGetErrorDescriptionW( _In_ HRESULT hr, _Out_cap_(count) wchar* desc, _In_ size_t count ) +{ + if ( !count ) + return; + + *desc = 0; + + // First try to see if FormatMessage knows this hr + LPWSTR errorText = nullptr; + + DWORD result = FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, hr, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&errorText, 0, nullptr ); + + if (result > 0 && errorText) + { + wcscpy_s( desc, count, errorText ); + + if ( errorText ) + LocalFree( errorText ); + + return; + } + + if ( errorText ) + LocalFree( errorText ); + + switch (hr) + { +// Commmented out codes are actually alises for other codes + +// ------------------------------------------------------------- +// dxgi.h error codes +// ------------------------------------------------------------- + CHK_ERR(DXGI_STATUS_OCCLUDED, "The target window or output has been occluded. The application should suspend rendering operations if possible.") + CHK_ERR(DXGI_STATUS_CLIPPED, "Target window is clipped.") + CHK_ERR(DXGI_STATUS_NO_REDIRECTION, "") + CHK_ERR(DXGI_STATUS_NO_DESKTOP_ACCESS, "No access to desktop.") + CHK_ERR(DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE, "") + CHK_ERR(DXGI_STATUS_MODE_CHANGED, "Display mode has changed") + CHK_ERR(DXGI_STATUS_MODE_CHANGE_IN_PROGRESS, "Display mode is changing") + CHK_ERR(DXGI_ERROR_INVALID_CALL, "The application has made an erroneous API call that it had enough information to avoid. This error is intended to denote that the application should be altered to avoid the error. Use of the debug version of the DXGI.DLL will provide run-time debug output with further information.") + CHK_ERR(DXGI_ERROR_NOT_FOUND, "The item requested was not found. For GetPrivateData calls, this means that the specified GUID had not been previously associated with the object.") + CHK_ERR(DXGI_ERROR_MORE_DATA, "The specified size of the destination buffer is too small to hold the requested data.") + CHK_ERR(DXGI_ERROR_UNSUPPORTED, "Unsupported.") + CHK_ERR(DXGI_ERROR_DEVICE_REMOVED, "Hardware device removed.") + CHK_ERR(DXGI_ERROR_DEVICE_HUNG, "Device hung due to badly formed commands.") + CHK_ERR(DXGI_ERROR_DEVICE_RESET, "Device reset due to a badly formed commant.") + CHK_ERR(DXGI_ERROR_WAS_STILL_DRAWING, "Was still drawing.") + CHK_ERR(DXGI_ERROR_FRAME_STATISTICS_DISJOINT, "The requested functionality is not supported by the device or the driver.") + CHK_ERR(DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE, "The requested functionality is not supported by the device or the driver.") + CHK_ERR(DXGI_ERROR_DRIVER_INTERNAL_ERROR, "An internal driver error occurred.") + CHK_ERR(DXGI_ERROR_NONEXCLUSIVE, "The application attempted to perform an operation on an DXGI output that is only legal after the output has been claimed for exclusive owenership.") + CHK_ERR(DXGI_ERROR_NOT_CURRENTLY_AVAILABLE, "The requested functionality is not supported by the device or the driver.") + CHK_ERR(DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED, "Remote desktop client disconnected.") + CHK_ERR(DXGI_ERROR_REMOTE_OUTOFMEMORY, "Remote desktop client is out of memory.") + +// ------------------------------------------------------------- +// d3d11.h error codes +// ------------------------------------------------------------- + CHK_ERR(D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS, "There are too many unique state objects.") + CHK_ERR(D3D11_ERROR_FILE_NOT_FOUND, "File not found") + CHK_ERR(D3D11_ERROR_TOO_MANY_UNIQUE_VIEW_OBJECTS, "Therea are too many unique view objects.") + CHK_ERR(D3D11_ERROR_DEFERRED_CONTEXT_MAP_WITHOUT_INITIAL_DISCARD, "Deferred context requires Map-Discard usage pattern") + +// ------------------------------------------------------------- +// WIC error codes +// ------------------------------------------------------------- + CHK_ERR(WINCODEC_ERR_WRONGSTATE, "WIC object in incorrect state.") + CHK_ERR(WINCODEC_ERR_VALUEOUTOFRANGE, "WIC Value out of range.") + CHK_ERR(WINCODEC_ERR_UNKNOWNIMAGEFORMAT, "Encountered unexpected value or setting in WIC image format.") + CHK_ERR(WINCODEC_ERR_UNSUPPORTEDVERSION, "Unsupported WINCODEC_SD_VERSION passed to WIC factory.") + CHK_ERR(WINCODEC_ERR_NOTINITIALIZED, "WIC component not initialized.") + CHK_ERR(WINCODEC_ERR_ALREADYLOCKED, "WIC bitmap object already locked.") + CHK_ERR(WINCODEC_ERR_PROPERTYNOTFOUND, "WIC property not found.") + CHK_ERR(WINCODEC_ERR_PROPERTYNOTSUPPORTED, "WIC property not supported.") + CHK_ERR(WINCODEC_ERR_PROPERTYSIZE, "Invalid property size") + CHK_ERRA(WINCODEC_ERR_CODECPRESENT) // not currently used by WIC + CHK_ERRA(WINCODEC_ERR_CODECNOTHUMBNAIL) // not currently used by WIC + CHK_ERR(WINCODEC_ERR_PALETTEUNAVAILABLE, "Required palette data not available.") + CHK_ERR(WINCODEC_ERR_CODECTOOMANYSCANLINES, "More scanlines requested than are available in WIC bitmap.") + CHK_ERR(WINCODEC_ERR_INTERNALERROR, "Unexpected internal error in WIC.") + CHK_ERR(WINCODEC_ERR_SOURCERECTDOESNOTMATCHDIMENSIONS, "Source WIC rectangle does not match bitmap dimensions.") + CHK_ERR(WINCODEC_ERR_COMPONENTNOTFOUND, "WIC component not found.") + CHK_ERR(WINCODEC_ERR_IMAGESIZEOUTOFRANGE, "Image size beyond expected boundaries for WIC codec." ) + CHK_ERR(WINCODEC_ERR_TOOMUCHMETADATA, "Image metadata size beyond expected boundaries for WIC codec.") + CHK_ERR(WINCODEC_ERR_BADIMAGE, "WIC image is corrupted.") + CHK_ERR(WINCODEC_ERR_BADHEADER, "Invalid header found in WIC image.") + CHK_ERR(WINCODEC_ERR_FRAMEMISSING, "Expected bitmap frame data not found in WIC image." ) + CHK_ERR(WINCODEC_ERR_BADMETADATAHEADER, "Invalid metadata header found in WIC image.") + CHK_ERR(WINCODEC_ERR_BADSTREAMDATA, "Invalid stream data found in WIC image.") + CHK_ERR(WINCODEC_ERR_STREAMWRITE, "WIC operation on write stream failed.") + CHK_ERR(WINCODEC_ERR_STREAMREAD, "WIC operation on read stream failed.") + CHK_ERR(WINCODEC_ERR_STREAMNOTAVAILABLE, "Required stream is not available." ) +// CHK_ERRA(WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT) + CHK_ERR(WINCODEC_ERR_UNSUPPORTEDOPERATION, "This operation is not supported by WIC." ) + CHK_ERR(WINCODEC_ERR_INVALIDREGISTRATION, "Error occurred reading WIC codec registry keys.") + CHK_ERR(WINCODEC_ERR_COMPONENTINITIALIZEFAILURE, "Failed initializing WIC codec.") + CHK_ERR(WINCODEC_ERR_INSUFFICIENTBUFFER, "Not enough buffer space available for WIC operation.") + CHK_ERR(WINCODEC_ERR_DUPLICATEMETADATAPRESENT, "Duplicate metadata detected in WIC image.") + CHK_ERR(WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE, "Unexpected property type in WIC image.") + CHK_ERR(WINCODEC_ERR_UNEXPECTEDSIZE, "Unexpected value size in WIC metadata.") + CHK_ERR(WINCODEC_ERR_INVALIDQUERYREQUEST, "Invalid metadata query.") + CHK_ERR(WINCODEC_ERR_UNEXPECTEDMETADATATYPE, "Unexpected metadata type encountered in WIC image.") + CHK_ERR(WINCODEC_ERR_REQUESTONLYVALIDATMETADATAROOT, "Operation only valid on meatadata root.") + CHK_ERR(WINCODEC_ERR_INVALIDQUERYCHARACTER, "Invalid character in WIC metadata query.") + CHK_ERR(WINCODEC_ERR_WIN32ERROR, "General Win32 error encountered during WIC operation.") + CHK_ERR(WINCODEC_ERR_INVALIDPROGRESSIVELEVEL, "Invalid level for progressive WIC image decode.") + } +} + +//----------------------------------------------------------------------------- +HRESULT WINAPI DXTraceW( _In_z_ const wchar* strFile, _In_ DWORD dwLine, _In_ HRESULT hr, + _In_opt_ const wchar* strMsg, _In_ bool bPopMsgBox ) +{ + wchar strBufferFile[MAX_PATH]; + wchar strBufferLine[128]; + wchar strBufferError[256]; + wchar strBufferMsg[1024]; + wchar strBuffer[BUFFER_SIZE]; + + swprintf_s( strBufferLine, 128, L"%lu", dwLine ); + if( strFile ) + { + swprintf_s( strBuffer, BUFFER_SIZE, L"%s(%s): ", strFile, strBufferLine ); + OutputDebugStringW( strBuffer ); + } + + size_t nMsgLen = (strMsg) ? wcsnlen_s( strMsg, 1024 ) : 0; + if( nMsgLen > 0 ) + { + OutputDebugStringW( strMsg ); + OutputDebugStringW( L" " ); + } + + swprintf_s( strBufferError, 256, L"%s (0x%0.8x)", DXGetErrorStringW(hr), hr ); + swprintf_s( strBuffer, BUFFER_SIZE, L"hr=%s", strBufferError ); + OutputDebugStringW( strBuffer ); + + OutputDebugStringW( L"\n" ); + + if( bPopMsgBox ) + { + wcscpy_s( strBufferFile, MAX_PATH, L"" ); + if( strFile ) + wcscpy_s( strBufferFile, MAX_PATH, strFile ); + + wcscpy_s( strBufferMsg, 1024, L"" ); + if( nMsgLen > 0 ) + swprintf_s( strBufferMsg, 1024, L"Calling: %s\n", strMsg ); + + swprintf_s( strBuffer, BUFFER_SIZE, L"File: %s\nLine: %s\nError Code: %s\n%sDo you want to debug the application?", + strBufferFile, strBufferLine, strBufferError, strBufferMsg ); + + int nResult = MessageBoxW( GetForegroundWindow(), strBuffer, L"Unexpected error encountered", MB_YESNO | MB_ICONERROR ); + if( nResult == IDYES ) + DebugBreak(); + } + + return hr; +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/DXErr.h b/Shadows/SampleFramework11/DXErr.h new file mode 100644 index 0000000..4299c0b --- /dev/null +++ b/Shadows/SampleFramework11/DXErr.h @@ -0,0 +1,67 @@ +//-------------------------------------------------------------------------------------- +// File: DXErr.h +// +// DirectX Error Library +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//-------------------------------------------------------------------------------------- + +// This version only supports UNICODE. + +#pragma once + +#include "PCH.h" + +//-------------------------------------------------------------------------------------- +// DXGetErrorString +//-------------------------------------------------------------------------------------- +const wchar* WINAPI DXGetErrorStringW( _In_ HRESULT hr ); + +#define DXGetErrorString DXGetErrorStringW + +//-------------------------------------------------------------------------------------- +// DXGetErrorDescription has to be modified to return a copy in a buffer rather than +// the original static string. +//-------------------------------------------------------------------------------------- +void WINAPI DXGetErrorDescriptionW( _In_ HRESULT hr, _Out_cap_(count) wchar* desc, _In_ size_t count ); + +#define DXGetErrorDescription DXGetErrorDescriptionW + +//-------------------------------------------------------------------------------------- +// DXTrace +// +// Desc: Outputs a formatted error message to the debug stream +// +// Args: wchar* strFile The current file, typically passed in using the +// __FILEW__ macro. +// DWORD dwLine The current line number, typically passed in using the +// __LINE__ macro. +// HRESULT hr An HRESULT that will be traced to the debug stream. +// CHAR* strMsg A string that will be traced to the debug stream (may be NULL) +// BOOL bPopMsgBox If true, then a message box will popup also containing the passed info. +// +// Return: The hr that was passed in. +//-------------------------------------------------------------------------------------- +HRESULT WINAPI DXTraceW( _In_z_ const wchar* strFile, _In_ DWORD dwLine, _In_ HRESULT hr, _In_opt_ const wchar* strMsg, _In_ bool bPopMsgBox ); + +#define DXTrace DXTraceW + +//-------------------------------------------------------------------------------------- +// +// Helper macros +// +//-------------------------------------------------------------------------------------- +#if defined(DEBUG) || defined(_DEBUG) +#define DXTRACE_MSG(str) DXTrace( __FILEW__, (DWORD)__LINE__, 0, str, false ) +#define DXTRACE_ERR(str,hr) DXTrace( __FILEW__, (DWORD)__LINE__, hr, str, false ) +#define DXTRACE_ERR_MSGBOX(str,hr) DXTrace( __FILEW__, (DWORD)__LINE__, hr, str, true ) +#else +#define DXTRACE_MSG(str) (0L) +#define DXTRACE_ERR(str,hr) (hr) +#define DXTRACE_ERR_MSGBOX(str,hr) (hr) +#endif \ No newline at end of file diff --git a/Shadows/SampleFramework11/DeviceManager.cpp b/Shadows/SampleFramework11/DeviceManager.cpp new file mode 100644 index 0000000..76c6cb5 --- /dev/null +++ b/Shadows/SampleFramework11/DeviceManager.cpp @@ -0,0 +1,229 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "DeviceManager.h" +#include "Exceptions.h" +#include "Utility.h" + +using std::wstring; + +#if _DEBUG + #define UseDebugDevice_ 1 + #define BreakOnDXError_ (UseDebugDevice_ && 1) +#else + #define UseDebugDevice_ 0 + #define BreakOnDXError_ 0 +#endif + +namespace SampleFramework11 +{ + +DeviceManager::DeviceManager() : backBufferFormat(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB), + backBufferWidth(1280), + backBufferHeight(720), + msCount(1), + msQuality(0), + fullScreen(false), + featureLevel(D3D_FEATURE_LEVEL_11_0), + minFeatureLevel(D3D_FEATURE_LEVEL_10_0), + vsync(true), + numVSYNCIntervals(1) +{ + refreshRate.Numerator = 60; + refreshRate.Denominator = 1; +} + +DeviceManager::~DeviceManager() +{ + if(immediateContext) + { + immediateContext->ClearState(); + immediateContext->Flush(); + } +} + +void DeviceManager::Initialize(HWND outputWindow) +{ + CheckForSuitableOutput(); + + DXGI_SWAP_CHAIN_DESC desc; + ZeroMemory(&desc, sizeof(DXGI_SWAP_CHAIN_DESC)); + + if(fullScreen) + { + PrepareFullScreenSettings(); + } + else + { + refreshRate.Numerator = 60; + refreshRate.Denominator = 1; + } + + desc.BufferCount = 2; + desc.BufferDesc.Format = backBufferFormat; + desc.BufferDesc.Width = backBufferWidth; + desc.BufferDesc.Height = backBufferHeight; + desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; + desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; + desc.BufferDesc.RefreshRate = refreshRate; + desc.SampleDesc.Count = msCount; + desc.SampleDesc.Quality = msQuality; + desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; + desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; + desc.OutputWindow = outputWindow; + desc.Windowed = !fullScreen; + + uint32 flags = D3D11_CREATE_DEVICE_SINGLETHREADED; + #if UseDebugDevice_ + flags |= D3D11_CREATE_DEVICE_DEBUG; + #endif + + DXCall(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, flags, + NULL, 0, D3D11_SDK_VERSION, &desc, &swapChain, + &device, NULL, &immediateContext)); + + featureLevel = device->GetFeatureLevel(); + + if(featureLevel < minFeatureLevel) + { + wstring majorLevel = ToString(minFeatureLevel >> 12); + wstring minorLevel = ToString((minFeatureLevel >> 8) & 0xF); + throw Exception(L"The device doesn't support the minimum feature level required to run this sample (DX" + majorLevel + L"." + minorLevel + L")"); + } + + if(BreakOnDXError_ && D3DPERF_GetStatus() == 0) + { + ID3D11InfoQueuePtr infoQueue; + DXCall(device->QueryInterface(__uuidof(ID3D11InfoQueue), reinterpret_cast(&infoQueue))); + infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, TRUE); + infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, TRUE); + } + + AfterReset(); +} + +void DeviceManager::AfterReset() +{ + DXCall(swapChain->GetBuffer(0, __uuidof(bbTexture), reinterpret_cast(&bbTexture))); + DXCall(device->CreateRenderTargetView(bbTexture, NULL, &bbRTView)); + + // Set default render targets + immediateContext->OMSetRenderTargets(1, &(bbRTView.GetInterfacePtr()), NULL); + + // Setup the viewport + D3D11_VIEWPORT vp; + vp.Width = static_cast(backBufferWidth); + vp.Height = static_cast(backBufferHeight); + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.TopLeftX = 0; + vp.TopLeftY = 0; + immediateContext->RSSetViewports(1, &vp); +} + +void DeviceManager::CheckForSuitableOutput() +{ + HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast(&factory)); + if(FAILED(hr)) + throw Exception(L"Unable to create a DXGI 1.1 device.\n " + L"Make sure that your OS and driver support DirectX 11"); + + // Look for an adapter that supports D3D11 + IDXGIAdapter1Ptr curAdapter; + uint32 adapterIdx = 0; + LARGE_INTEGER umdVersion; + while(!adapter && SUCCEEDED(factory->EnumAdapters1(0, &adapter))) + if(SUCCEEDED(adapter->CheckInterfaceSupport(__uuidof(ID3D11Device), &umdVersion))) + adapter = curAdapter; + + if(!adapter) + throw Exception(L"Unable to locate a DXGI 1.1 adapter that supports a D3D11 device.\n" + L"Make sure that your OS and driver support DirectX 11"); + + // We'll just use the first output + DXCall(adapter->EnumOutputs(0, &output)); +} + +void DeviceManager::PrepareFullScreenSettings() +{ + Assert_(output); + + // Have the Output look for the closest matching mode + DXGI_MODE_DESC desiredMode; + desiredMode.Format = backBufferFormat; + desiredMode.Width = backBufferWidth; + desiredMode.Height = backBufferHeight; + desiredMode.RefreshRate.Numerator = 0; + desiredMode.RefreshRate.Denominator = 0; + desiredMode.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; + desiredMode.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; + + DXGI_MODE_DESC closestMatch; + DXCall(output->FindClosestMatchingMode(&desiredMode, &closestMatch, device.GetInterfacePtr())); + + backBufferFormat = closestMatch.Format; + backBufferWidth = closestMatch.Width; + backBufferHeight = closestMatch.Height; + refreshRate = closestMatch.RefreshRate; +} + +void DeviceManager::Reset() +{ + Assert_(swapChain); + + // Release all references + if(bbTexture) + bbTexture.Release(); + + if(bbRTView) + bbRTView.Release(); + + immediateContext->ClearState(); + + if(fullScreen) + PrepareFullScreenSettings(); + else + { + refreshRate.Numerator = 60; + refreshRate.Denominator = 1; + } + + DXCall(swapChain->SetFullscreenState(fullScreen, NULL)); + + DXCall(swapChain->ResizeBuffers(2, backBufferWidth, backBufferHeight, + backBufferFormat, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH)); + + if(fullScreen) + { + DXGI_MODE_DESC mode; + mode.Format = backBufferFormat; + mode.Width = backBufferWidth; + mode.Height = backBufferHeight; + mode.RefreshRate.Numerator = 0; + mode.RefreshRate.Denominator = 0; + mode.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; + mode.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; + DXCall(swapChain->ResizeTarget(&mode)); + } + + AfterReset(); +} + +void DeviceManager::Present() +{ + Assert_(device); + + uint32 interval = vsync ? numVSYNCIntervals : 0; + DXCall(swapChain->Present(interval, 0)); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/DeviceManager.h b/Shadows/SampleFramework11/DeviceManager.h new file mode 100644 index 0000000..316d8b3 --- /dev/null +++ b/Shadows/SampleFramework11/DeviceManager.h @@ -0,0 +1,92 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "GraphicsTypes.h" + +namespace SampleFramework11 +{ + +class DeviceManager +{ + +public: + + DeviceManager(); + ~DeviceManager(); + + void Initialize(HWND outputWindow); + void Reset(); + void Present(); + + // Getters + ID3D11Device* Device() const { return device.GetInterfacePtr(); }; + ID3D11DeviceContext* ImmediateContext() const { return immediateContext.GetInterfacePtr(); }; + IDXGISwapChain* SwapChain() const { return swapChain.GetInterfacePtr(); }; + ID3D11RenderTargetView* BackBuffer() const { return bbRTView.GetInterfacePtr(); }; + ID3D11Texture2D* BackBufferTexture() const { return bbTexture; }; + D3D_FEATURE_LEVEL FeatureLevel() const { return featureLevel; }; + D3D_FEATURE_LEVEL MinFeatureLevel() const { return minFeatureLevel; }; + + DXGI_FORMAT BackBufferFormat() const { return backBufferFormat; }; + uint32 BackBufferWidth() const { return backBufferWidth; }; + uint32 BackBufferHeight() const { return backBufferHeight; }; + uint32 BackBufferMSCount() const { return msCount; }; + uint32 BackBufferMSQuality() const { return msQuality; }; + bool FullScreen() const { return fullScreen; }; + bool VSYNCEnabled() const { return vsync; }; + uint32 NumVSYNCIntervals() const { return numVSYNCIntervals; }; + + + // Setters + void SetBackBufferFormat(DXGI_FORMAT format) { backBufferFormat = format; }; + void SetBackBufferWidth(uint32 width) { backBufferWidth = width; }; + void SetBackBufferHeight(uint32 height) { backBufferHeight = height; }; + void SetBackBufferMSCount(uint32 count) { msCount = count; }; + void SetBackBufferMSQuality(uint32 quality) { msQuality = quality; }; + void SetFullScreen(bool enabled) { fullScreen = enabled; }; + void SetVSYNCEnabled(bool enabled) { vsync = enabled; }; + void SetMinFeatureLevel(D3D_FEATURE_LEVEL level) { minFeatureLevel = level; }; + void SetNumVSYNCIntervals(uint32 intervals) { numVSYNCIntervals = intervals; }; + +protected: + + void CheckForSuitableOutput(); + void AfterReset(); + void PrepareFullScreenSettings(); + + IDXGIFactory1Ptr factory; + IDXGIAdapter1Ptr adapter; + IDXGIOutputPtr output; + + ID3D11DevicePtr device; + ID3D11DeviceContextPtr immediateContext; + IDXGISwapChainPtr swapChain; + ID3D11Texture2DPtr bbTexture; + ID3D11RenderTargetViewPtr bbRTView; + + DXGI_FORMAT backBufferFormat; + uint32 backBufferWidth; + uint32 backBufferHeight; + uint32 msCount; + uint32 msQuality; + bool fullScreen; + bool vsync; + DXGI_RATIONAL refreshRate; + uint32 numVSYNCIntervals; + + D3D_FEATURE_LEVEL featureLevel; + D3D_FEATURE_LEVEL minFeatureLevel; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/DeviceStates.cpp b/Shadows/SampleFramework11/DeviceStates.cpp new file mode 100644 index 0000000..08b0c85 --- /dev/null +++ b/Shadows/SampleFramework11/DeviceStates.cpp @@ -0,0 +1,601 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "DeviceStates.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +void BlendStates::Initialize(ID3D11Device* device) +{ + DXCall(device->CreateBlendState(&BlendDisabledDesc(), &blendDisabled)); + DXCall(device->CreateBlendState(&AdditiveBlendDesc(), &additiveBlend)); + DXCall(device->CreateBlendState(&AlphaBlendDesc(), &alphaBlend)); + DXCall(device->CreateBlendState(&PreMultipliedAlphaBlendDesc(), &pmAlphaBlend)); + DXCall(device->CreateBlendState(&ColorWriteDisabledDesc(), &noColor)); + DXCall(device->CreateBlendState(&AlphaToCoverageDesc(), &alphaToCoverage)); + DXCall(device->CreateBlendState(&OpacityBlendDesc(), &opacityBlend)); +} + +D3D11_BLEND_DESC BlendStates::BlendDisabledDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for(UINT i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = false; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + return blendDesc; +} + +D3D11_BLEND_DESC BlendStates::AdditiveBlendDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = true; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + return blendDesc; +} + +D3D11_BLEND_DESC BlendStates::AlphaBlendDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = true; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = true; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + blendDesc.RenderTarget[0].BlendEnable = true; + + return blendDesc; +} + +D3D11_BLEND_DESC BlendStates::PreMultipliedAlphaBlendDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = false; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + return blendDesc; +} + +D3D11_BLEND_DESC BlendStates::ColorWriteDisabledDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = false; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = 0; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + return blendDesc; +} + +D3D11_BLEND_DESC BlendStates::AlphaToCoverageDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = true; + blendDesc.IndependentBlendEnable = false; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = false; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + return blendDesc; +} + +D3D11_BLEND_DESC BlendStates::OpacityBlendDesc() +{ + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = true; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC1_COLOR; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + + return blendDesc; +} + +void RasterizerStates::Initialize(ID3D11Device* device) +{ + DXCall(device->CreateRasterizerState(&NoCullDesc(), &noCull)); + DXCall(device->CreateRasterizerState(&FrontFaceCullDesc(), &cullFrontFaces)); + DXCall(device->CreateRasterizerState(&FrontFaceCullScissorDesc(), &cullFrontFacesScissor)); + DXCall(device->CreateRasterizerState(&BackFaceCullDesc(), &cullBackFaces)); + DXCall(device->CreateRasterizerState(&BackFaceCullScissorDesc(), &cullBackFacesScissor)); + DXCall(device->CreateRasterizerState(&NoCullNoMSDesc(), &noCullNoMS)); + DXCall(device->CreateRasterizerState(&NoCullScissorDesc(), &noCullScissor)); + DXCall(device->CreateRasterizerState(&WireframeDesc(), &wireframe)); +} + +D3D11_RASTERIZER_DESC RasterizerStates::NoCullDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::FrontFaceCullDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_FRONT; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::FrontFaceCullScissorDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_FRONT; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = true; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = true; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::BackFaceCullDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_BACK; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::BackFaceCullScissorDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_BACK; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = true; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::NoCullNoMSDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = false; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::NoCullScissorDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = true; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +D3D11_RASTERIZER_DESC RasterizerStates::WireframeDesc() +{ + D3D11_RASTERIZER_DESC rastDesc; + + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_WIREFRAME; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + + return rastDesc; +} + +void DepthStencilStates::Initialize(ID3D11Device* device) +{ + DXCall(device->CreateDepthStencilState(&DepthDisabledDesc(), &depthDisabled)); + DXCall(device->CreateDepthStencilState(&DepthEnabledDesc(), &depthEnabled)); + DXCall(device->CreateDepthStencilState(&ReverseDepthEnabledDesc(), &revDepthEnabled)); + DXCall(device->CreateDepthStencilState(&DepthWriteEnabledDesc(), &depthWriteEnabled)); + DXCall(device->CreateDepthStencilState(&ReverseDepthWriteEnabledDesc(), &revDepthWriteEnabled)); + DXCall(device->CreateDepthStencilState(&DepthStencilWriteEnabledDesc(), &depthStencilWriteEnabled)); + DXCall(device->CreateDepthStencilState(&StencilEnabledDesc(), &stencilEnabled)); +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::DepthDisabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = false; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::DepthEnabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::ReverseDepthEnabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; + dsDesc.DepthFunc = D3D11_COMPARISON_GREATER_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::DepthWriteEnabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::ReverseDepthWriteEnabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + dsDesc.DepthFunc = D3D11_COMPARISON_GREATER_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::DepthStencilWriteEnabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = true; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +D3D11_DEPTH_STENCIL_DESC DepthStencilStates::StencilEnabledDesc() +{ + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = true; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = 0; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_EQUAL; + dsDesc.BackFace = dsDesc.FrontFace; + + return dsDesc; +} + +void SamplerStates::Initialize(ID3D11Device* device) +{ + DXCall(device->CreateSamplerState(&LinearDesc(), &linear)); + DXCall(device->CreateSamplerState(&LinearClampDesc(), &linearClamp)); + DXCall(device->CreateSamplerState(&LinearBorderDesc(), &linearBorder)); + DXCall(device->CreateSamplerState(&PointDesc(), &point)); + DXCall(device->CreateSamplerState(&AnisotropicDesc(), &anisotropic)); + DXCall(device->CreateSamplerState(&ShadowMapDesc(), &shadowMap)); + DXCall(device->CreateSamplerState(&ShadowMapPCFDesc(), &shadowMapPCF)); +} + +D3D11_SAMPLER_DESC SamplerStates::LinearDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 1; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +D3D11_SAMPLER_DESC SamplerStates::LinearClampDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 1; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +D3D11_SAMPLER_DESC SamplerStates::LinearBorderDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 1; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +D3D11_SAMPLER_DESC SamplerStates::PointDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 1; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +D3D11_SAMPLER_DESC SamplerStates::AnisotropicDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_ANISOTROPIC; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 16; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +D3D11_SAMPLER_DESC SamplerStates::ShadowMapDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 1; + sampDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +D3D11_SAMPLER_DESC SamplerStates::ShadowMapPCFDesc() +{ + D3D11_SAMPLER_DESC sampDesc; + + sampDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.MipLODBias = 0.0f; + sampDesc.MaxAnisotropy = 1; + sampDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL; + sampDesc.BorderColor[0] = sampDesc.BorderColor[1] = sampDesc.BorderColor[2] = sampDesc.BorderColor[3] = 0; + sampDesc.MinLOD = 0; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + + return sampDesc; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/DeviceStates.h b/Shadows/SampleFramework11/DeviceStates.h new file mode 100644 index 0000000..35e47f6 --- /dev/null +++ b/Shadows/SampleFramework11/DeviceStates.h @@ -0,0 +1,155 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" + +namespace SampleFramework11 +{ + +class BlendStates +{ + +protected: + + ID3D11BlendStatePtr blendDisabled; + ID3D11BlendStatePtr additiveBlend; + ID3D11BlendStatePtr alphaBlend; + ID3D11BlendStatePtr pmAlphaBlend; + ID3D11BlendStatePtr noColor; + ID3D11BlendStatePtr alphaToCoverage; + ID3D11BlendStatePtr opacityBlend; + +public: + + void Initialize(ID3D11Device* device); + + ID3D11BlendState* BlendDisabled () { return blendDisabled; }; + ID3D11BlendState* AdditiveBlend () { return additiveBlend; }; + ID3D11BlendState* AlphaBlend () { return alphaBlend; }; + ID3D11BlendState* PreMultipliedAlphaBlend () { return pmAlphaBlend; }; + ID3D11BlendState* ColorWriteDisabled () { return noColor; }; + ID3D11BlendState* AlphaToCoverage () { return alphaToCoverage; }; + ID3D11BlendState* OpacityBlend() { return opacityBlend; }; + + static D3D11_BLEND_DESC BlendDisabledDesc(); + static D3D11_BLEND_DESC AdditiveBlendDesc(); + static D3D11_BLEND_DESC AlphaBlendDesc(); + static D3D11_BLEND_DESC PreMultipliedAlphaBlendDesc(); + static D3D11_BLEND_DESC ColorWriteDisabledDesc(); + static D3D11_BLEND_DESC AlphaToCoverageDesc(); + static D3D11_BLEND_DESC OpacityBlendDesc(); +}; + + +class RasterizerStates +{ + +protected: + + ID3D11RasterizerStatePtr noCull; + ID3D11RasterizerStatePtr cullBackFaces; + ID3D11RasterizerStatePtr cullBackFacesScissor; + ID3D11RasterizerStatePtr cullFrontFaces; + ID3D11RasterizerStatePtr cullFrontFacesScissor; + ID3D11RasterizerStatePtr noCullNoMS; + ID3D11RasterizerStatePtr noCullScissor; + ID3D11RasterizerStatePtr wireframe; + +public: + + void Initialize(ID3D11Device* device); + + ID3D11RasterizerState* NoCull() { return noCull; }; + ID3D11RasterizerState* BackFaceCull() { return cullBackFaces; }; + ID3D11RasterizerState* BackFaceCullScissor() { return cullBackFacesScissor; }; + ID3D11RasterizerState* FrontFaceCull() { return cullFrontFaces; }; + ID3D11RasterizerState* FrontFaceCullScissor() { return cullFrontFacesScissor; }; + ID3D11RasterizerState* NoCullNoMS() { return noCullNoMS; }; + ID3D11RasterizerState* NoCullScissor() { return noCullScissor; }; + ID3D11RasterizerState* Wireframe() { return wireframe; }; + + static D3D11_RASTERIZER_DESC NoCullDesc(); + static D3D11_RASTERIZER_DESC FrontFaceCullDesc(); + static D3D11_RASTERIZER_DESC FrontFaceCullScissorDesc(); + static D3D11_RASTERIZER_DESC BackFaceCullDesc(); + static D3D11_RASTERIZER_DESC BackFaceCullScissorDesc(); + static D3D11_RASTERIZER_DESC NoCullNoMSDesc(); + static D3D11_RASTERIZER_DESC NoCullScissorDesc(); + static D3D11_RASTERIZER_DESC WireframeDesc(); +}; + + +class DepthStencilStates +{ + ID3D11DepthStencilStatePtr depthDisabled; + ID3D11DepthStencilStatePtr depthEnabled; + ID3D11DepthStencilStatePtr revDepthEnabled; + ID3D11DepthStencilStatePtr depthWriteEnabled; + ID3D11DepthStencilStatePtr revDepthWriteEnabled; + ID3D11DepthStencilStatePtr depthStencilWriteEnabled; + ID3D11DepthStencilStatePtr stencilEnabled; + +public: + + void Initialize(ID3D11Device* device); + + ID3D11DepthStencilState* DepthDisabled() { return depthDisabled; }; + ID3D11DepthStencilState* DepthEnabled() { return depthEnabled; }; + ID3D11DepthStencilState* ReverseDepthEnabled() { return revDepthEnabled; }; + ID3D11DepthStencilState* DepthWriteEnabled() { return depthWriteEnabled; }; + ID3D11DepthStencilState* ReverseDepthWriteEnabled() { return revDepthWriteEnabled; }; + ID3D11DepthStencilState* DepthStencilWriteEnabled() { return depthStencilWriteEnabled; }; + ID3D11DepthStencilState* StencilTestEnabled() { return depthStencilWriteEnabled; }; + + static D3D11_DEPTH_STENCIL_DESC DepthDisabledDesc(); + static D3D11_DEPTH_STENCIL_DESC DepthEnabledDesc(); + static D3D11_DEPTH_STENCIL_DESC ReverseDepthEnabledDesc(); + static D3D11_DEPTH_STENCIL_DESC DepthWriteEnabledDesc(); + static D3D11_DEPTH_STENCIL_DESC ReverseDepthWriteEnabledDesc(); + static D3D11_DEPTH_STENCIL_DESC DepthStencilWriteEnabledDesc(); + static D3D11_DEPTH_STENCIL_DESC StencilEnabledDesc(); +}; + + +class SamplerStates +{ + + ID3D11SamplerStatePtr linear; + ID3D11SamplerStatePtr linearClamp; + ID3D11SamplerStatePtr linearBorder; + ID3D11SamplerStatePtr point; + ID3D11SamplerStatePtr anisotropic; + ID3D11SamplerStatePtr shadowMap; + ID3D11SamplerStatePtr shadowMapPCF; +public: + + void Initialize(ID3D11Device* device); + + ID3D11SamplerState* Linear() { return linear; }; + ID3D11SamplerState* LinearClamp() { return linearClamp; }; + ID3D11SamplerState* LinearBorder() { return linearBorder; }; + ID3D11SamplerState* Point() { return point; }; + ID3D11SamplerState* Anisotropic() { return anisotropic; }; + ID3D11SamplerState* ShadowMap() { return shadowMap; }; + ID3D11SamplerState* ShadowMapPCF() { return shadowMapPCF; }; + + static D3D11_SAMPLER_DESC LinearDesc(); + static D3D11_SAMPLER_DESC LinearClampDesc(); + static D3D11_SAMPLER_DESC LinearBorderDesc(); + static D3D11_SAMPLER_DESC PointDesc(); + static D3D11_SAMPLER_DESC AnisotropicDesc(); + static D3D11_SAMPLER_DESC ShadowMapDesc(); + static D3D11_SAMPLER_DESC ShadowMapPCFDesc(); +}; + +} diff --git a/Shadows/SampleFramework11/Exceptions.h b/Shadows/SampleFramework11/Exceptions.h new file mode 100644 index 0000000..1990f7f --- /dev/null +++ b/Shadows/SampleFramework11/Exceptions.h @@ -0,0 +1,342 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "DXErr.h" +#include "Assert.h" + +namespace SampleFramework11 +{ + +// Error string functions +inline std::wstring GetWin32ErrorString(DWORD errorCode) +{ + wchar errorString[MAX_PATH]; + ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, + 0, + errorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + errorString, + MAX_PATH, + NULL ); + + std::wstring message = L"Win32 Error: "; + message += errorString; + return message; +} + +inline std::string GetWin32ErrorStringAnsi(DWORD errorCode) +{ + char errorString[MAX_PATH]; + ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, + 0, + errorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + errorString, + MAX_PATH, + NULL ); + + std::string message = "Win32 Error: "; + message += errorString; + return message; +} + +inline std::wstring GetDXErrorString(HRESULT hr) +{ + const uint32 errStringSize = 1024; + wchar errorString[errStringSize]; + DXGetErrorDescriptionW(hr, errorString, errStringSize); + + std::wstring message = L"DirectX Error: "; + message += errorString; + return message; +} + +inline std::string GetDXErrorStringAnsi(HRESULT hr) +{ + std::wstring errorString = GetDXErrorString(hr); + + std::string message; + for(uint64 i = 0; i < errorString.length(); ++i) + message.append(1, static_cast(errorString[i])); + return message; +} + +inline std::wstring GetGdiPlusErrorString(Gdiplus::Status status) +{ + std::wstring errorString; + + if (status == Gdiplus::GenericError) + errorString = L"Generic Error"; + else if (status == Gdiplus::InvalidParameter) + errorString = L"Invalid Parameter"; + else if (status == Gdiplus::OutOfMemory) + errorString = L"Out Of Memory"; + else if (status == Gdiplus::ObjectBusy) + errorString = L"Object Busy"; + else if (status == Gdiplus::InsufficientBuffer) + errorString = L"Insufficient Buffer"; + else if (status == Gdiplus::NotImplemented) + errorString = L"Not Implemented"; + else if (status == Gdiplus::Win32Error) + errorString = L"Win32 Error"; + else if (status == Gdiplus::WrongState) + errorString = L"Wrong State"; + else if (status == Gdiplus::Aborted) + errorString = L"Aborted"; + else if (status == Gdiplus::FileNotFound) + errorString = L"File Not Found"; + else if (status == Gdiplus::ValueOverflow) + errorString = L"Value Overflow"; + else if (status == Gdiplus::AccessDenied) + errorString = L"Access Denied"; + else if (status == Gdiplus::UnknownImageFormat) + errorString = L"Unknown Image Format"; + else if (status == Gdiplus::FontFamilyNotFound) + errorString = L"Font Family Not Found"; + else if (status == Gdiplus::FontStyleNotFound) + errorString = L"Font Style Not Found"; + else if (status == Gdiplus::NotTrueTypeFont) + errorString = L"Not TrueType Font"; + else if (status == Gdiplus::UnsupportedGdiplusVersion) + errorString = L"Unsupported GDI+ Version"; + else if (status == Gdiplus::GdiplusNotInitialized) + errorString = L"GDI+ Not Initialized"; + else if (status == Gdiplus::PropertyNotFound) + errorString = L"Property Not Found"; + else if (status == Gdiplus::PropertyNotSupported) + errorString = L"Property Not Supported"; + + return L"GDI+ Error: " + errorString; +} + +inline std::string GetGdiPlusErrorStringAnsi(Gdiplus::Status status) +{ + std::wstring errorString = GetDXErrorString(status); + std::string message; + for(uint64 i = 0; i < errorString.length(); ++i) + message.append(1, static_cast(errorString[i])); + return message; +} + +inline std::wstring GetTwErrorString() +{ + std::string antError = TwGetLastError(); + std::wstring errorString(antError.length(), ' '); + for(uint64 i = 0; i < antError.length(); ++i) + errorString[i] = antError[i]; + return errorString; +} + +inline std::string GetTwErrorStringAnsi() +{ + return TwGetLastError(); +} + +// Generic exception, used as base class for other types +class Exception +{ + +public: + + Exception() + { + } + + // Specify an actual error message + Exception (const std::wstring& exceptionMessage) : message(exceptionMessage) + { + } + + // Retrieve that error message + const std::wstring& GetMessage() const throw() + { + return message; + } + + void ShowErrorMessage() const throw () + { + MessageBox(NULL, message.c_str(), L"Error", MB_OK|MB_ICONERROR); + } + +protected: + + std::wstring message; // The error message +}; + +// Exception thrown when a Win32 function fails. +class Win32Exception : public Exception +{ + +public: + + // Obtains a string for the specified Win32 error code + Win32Exception(DWORD code) : errorCode(code) + { + wchar errorString[MAX_PATH]; + ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, + 0, + errorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + errorString, + MAX_PATH, + NULL ); + + message = L"Win32 Error: "; + message += errorString; + } + + // Retrieve the error code + DWORD GetErrorCode() const throw () + { + return errorCode; + } + +protected: + + DWORD errorCode; // The Win32 error code + +}; + +// Exception thrown when a DirectX Function fails +class DXException : public Exception +{ + +public: + + // Obtains a string for the specified HRESULT error code + DXException(HRESULT hresult) : errorCode(hresult) + { + message = GetDXErrorString(hresult); + } + + DXException(HRESULT hresult, LPCWSTR errorMsg) : errorCode(hresult) + { + message = L"DirectX Error: "; + message += errorMsg; + } + + // Retrieve the error code + HRESULT GetErrorCode() const throw () + { + return errorCode; + } + +protected: + + HRESULT errorCode; // The DX error code +}; + +// Exception thrown when a GDI+ function fails +class GdiPlusException : public Exception +{ + +public: + + // Obtains a string for the specified error code + GdiPlusException(Gdiplus::Status status) : errorCode(status) + { + message = GetGdiPlusErrorString(status); + } + + // Retrieve the error code + Gdiplus::Status GetErrorCode() const throw () + { + return errorCode; + } + +protected: + + Gdiplus::Status errorCode; // The GDI+ error code +}; + +// Exception thrown when an AntTweakBar function fails +class TwException : public Exception +{ + +public: + + TwException() + { + message = GetTwErrorString(); + } +}; + +// Error-handling functions + +#if UseAsserts_ + +#define DXCall(x) \ + do \ + { \ + HRESULT hr_ = x; \ + AssertMsg_(SUCCEEDED(hr_), GetDXErrorStringAnsi(hr_).c_str()); \ + } \ + while(0) + +#define Win32Call(x) \ + do \ + { \ + BOOL res_ = x; \ + AssertMsg_(res_ != 0, GetWin32ErrorStringAnsi(GetLastError()).c_str()); \ + } \ + while(0) + +#define GdiPlusCall(x) \ + do \ + { \ + Gdiplus::Status status_ = x; \ + AssertMsg_(status_ == Gdiplus::Ok, GetGdiPlusErrorStringAnsi(status_).c_str()); \ + } \ + while(0) + +#define TwCall(x) \ + do \ + { \ + int res_ = x; \ + AssertMsg_(res_ != 0, GetTwErrorStringAnsi().c_str()); \ + } \ + while(0) + + +#else + +// Throws a DXException on failing HRESULT +inline void DXCall(HRESULT hr) +{ + if(FAILED(hr)) + throw DXException(hr); +} + +// Throws a Win32Exception on failing return value +inline void Win32Call(BOOL retVal) +{ + if(retVal == 0) + throw Win32Exception(GetLastError()); +} + +// Throws a GdiPlusException on failing Status value +inline void GdiPlusCall(Gdiplus::Status status) +{ + if(status != Gdiplus::Ok) + throw GdiPlusException(status); +} + +// Throws an AntException on failing return value +inline void TwCall(int retVal) +{ + if(retVal == 0) + throw TwException(); +} + +#endif + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/FileIO.cpp b/Shadows/SampleFramework11/FileIO.cpp new file mode 100644 index 0000000..a4ea331 --- /dev/null +++ b/Shadows/SampleFramework11/FileIO.cpp @@ -0,0 +1,214 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "FileIO.h" + +namespace SampleFramework11 +{ + +// Returns true if a file exits +bool FileExists(const wchar* filePath) +{ + if(filePath == NULL) + return false; + + DWORD fileAttr = GetFileAttributes(filePath); + if (fileAttr == INVALID_FILE_ATTRIBUTES) + return false; + + return true; +} + +// Retursn true if a directory exists +bool DirectoryExists(const wchar* dirPath) +{ + if(dirPath == NULL) + return false; + + DWORD fileAttr = GetFileAttributes(dirPath); + return (fileAttr != INVALID_FILE_ATTRIBUTES && (fileAttr & FILE_ATTRIBUTE_DIRECTORY)); +} + + +// Returns the directory containing a file +std::wstring GetDirectoryFromFilePath(const wchar* filePath_) +{ + Assert_(filePath_); + + std::wstring filePath(filePath_); + size_t idx = filePath.rfind(L'\\'); + if(idx != std::wstring::npos) + return filePath.substr(0, idx + 1); + else + return std::wstring(L""); +} + +// Returns the name of the file given the path (extension included) +std::wstring GetFileName(const wchar* filePath_) +{ + Assert_(filePath_); + + std::wstring filePath(filePath_); + size_t idx = filePath.rfind(L'\\'); + if(idx != std::wstring::npos && idx < filePath.length() - 1) + return filePath.substr(idx + 1); + else + return filePath; +} + +// Returns the name of the file given the path, minus the extension +std::wstring GetFileNameWithoutExtension(const wchar* filePath) +{ + std::wstring fileName = GetFileName(filePath); + return GetFilePathWithoutExtension(fileName.c_str()); +} + +// Returns the given file path, minus the extension +std::wstring GetFilePathWithoutExtension(const wchar* filePath_) +{ + Assert_(filePath_); + + std::wstring filePath(filePath_); + size_t idx = filePath.rfind(L'.'); + if (idx != std::wstring::npos) + return filePath.substr(0, idx); + else + return std::wstring(L""); +} + +// Returns the extension of the file path +std::wstring GetFileExtension(const wchar* filePath_) +{ + Assert_(filePath_); + + std::wstring filePath(filePath_); + size_t idx = filePath.rfind(L'.'); + if (idx != std::wstring::npos) + return filePath.substr(idx + 1, filePath.length() - idx - 1); + else + return std::wstring(L""); +} + +// Gets the last written timestamp of the file +uint64 GetFileTimestamp(const wchar* filePath) +{ + Assert_(filePath); + + WIN32_FILE_ATTRIBUTE_DATA attributes; + Win32Call(GetFileAttributesEx(filePath, GetFileExInfoStandard, &attributes)); + return attributes.ftLastWriteTime.dwLowDateTime | (uint64(attributes.ftLastWriteTime.dwHighDateTime) << 32); +} + +// Returns the contents of a file as a string +std::string ReadFileAsString(const wchar* filePath) +{ + File file(filePath, File::OpenRead); + uint64 fileSize = file.Size(); + + std::string fileContents; + fileContents.resize(size_t(fileSize), 0); + file.Read(fileSize, &fileContents[0]); + + return fileContents; +} + +// Writes the contents of a string to a file +void WriteStringAsFile(const wchar* filePath, const std::string& data) +{ + File file(filePath, File::OpenWrite); + file.Write(data.length(), data.c_str()); +} + +// == File ======================================================================================== + +File::File() : fileHandle(INVALID_HANDLE_VALUE), openMode(OpenRead) +{ +} + +File::File(const wchar* filePath, OpenMode openMode) : fileHandle(INVALID_HANDLE_VALUE), + openMode(OpenRead) +{ + Open(filePath, openMode); +} + +File::~File() +{ + Close(); + Assert_(fileHandle == INVALID_HANDLE_VALUE); +} + +void File::Open(const wchar* filePath, OpenMode openMode_) +{ + Assert_(fileHandle == INVALID_HANDLE_VALUE); + openMode = openMode_; + + if(openMode == OpenRead) + { + Assert_(FileExists(filePath)); + + // Open the file + fileHandle = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if(fileHandle == INVALID_HANDLE_VALUE) + Win32Call(false); + } + else + { + // If the exists, delete it + if(FileExists(filePath)) + Win32Call(DeleteFile(filePath)); + + // Create the file + fileHandle = CreateFile(filePath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + if(fileHandle == INVALID_HANDLE_VALUE) + Win32Call(false); + } +} + +void File::Close() +{ + if(fileHandle == INVALID_HANDLE_VALUE) + return; + + // Close the file + Win32Call(CloseHandle(fileHandle)); + + fileHandle = INVALID_HANDLE_VALUE; +} + +void File::Read(uint64 size, void* data) const +{ + Assert_(fileHandle != INVALID_HANDLE_VALUE); + Assert_(openMode == OpenRead); + + DWORD bytesRead = 0; + Win32Call(ReadFile(fileHandle, data, static_cast(size), &bytesRead, NULL)); +} + +void File::Write(uint64 size, const void* data) const +{ + Assert_(fileHandle != INVALID_HANDLE_VALUE); + Assert_(openMode == OpenWrite); + + DWORD bytesWritten = 0; + Win32Call(WriteFile(fileHandle, data, static_cast(size), &bytesWritten, NULL)); +} + +uint64 File::Size() const +{ + Assert_(fileHandle != INVALID_HANDLE_VALUE); + + LARGE_INTEGER fileSize; + Win32Call(GetFileSizeEx(fileHandle, &fileSize)); + + return fileSize.QuadPart; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/FileIO.h b/Shadows/SampleFramework11/FileIO.h new file mode 100644 index 0000000..de74e45 --- /dev/null +++ b/Shadows/SampleFramework11/FileIO.h @@ -0,0 +1,99 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Exceptions.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +// Utility functions +bool FileExists(const wchar* filePath); +bool DirectoryExists(const wchar* dirPath); +std::wstring GetDirectoryFromFilePath(const wchar* filePath); +std::wstring GetFileName(const wchar* filePath); +std::wstring GetFileNameWithoutExtension(const wchar* filePath); +std::wstring GetFilePathWithoutExtension(const wchar* filePath); +std::wstring GetFileExtension(const wchar* filePath); +uint64 GetFileTimestamp(const wchar* filePath); + +std::string ReadFileAsString(const wchar* filePath); +void WriteStringAsFile(const wchar* filePath, const std::string& data); + +class File +{ + +public: + + enum OpenMode + { + OpenRead = 0, + OpenWrite = 1, + }; + +private: + + HANDLE fileHandle; + OpenMode openMode; + +public: + + // Lifetime + File(); + File(const wchar* filePath, OpenMode openMode); + ~File(); + + // Explicit Open and close + void Open(const wchar* filePath, OpenMode openMode); + void Close(); + + // I/O + void Read(uint64 size, void* data) const; + void Write(uint64 size, const void* data) const; + + template void Read(T& data) const; + template void Write(const T& data) const; + + // Accessors + uint64 Size() const; +}; + +// == File ======================================================================================== + +template void File::Read(T& data) const +{ + Read(sizeof(T), &data); +} + +template void File::Write(const T& data) const +{ + Write(sizeof(T), &data); +} + +// Templated helper functions + +// Reads a POD type from a file +template void ReadFromFile(const wchar* fileName, T& val) +{ + File file(fileName, File::OpenRead); + file.Read(val); +} + +// Writes a POD type to a file +template void WriteToFile(const wchar* fileName, const T& val) +{ + File file(fileName, File::OpenWrite); + file.Write(val); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/GUIObject.cpp b/Shadows/SampleFramework11/GUIObject.cpp new file mode 100644 index 0000000..e8f9e8f --- /dev/null +++ b/Shadows/SampleFramework11/GUIObject.cpp @@ -0,0 +1,34 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "GUIObject.h" + +#include "Utility.h" + +namespace SampleFramework11 +{ + +SpriteFont GUIObject::font; +ID3D11ShaderResourceViewPtr GUIObject::barTexture; +ID3D11ShaderResourceViewPtr GUIObject::knobTexture; + +void GUIObject::InitGlobalResources(ID3D11Device* device) +{ + // Load the texures + barTexture = LoadTexture(device, L"SampleFramework11\\Images\\SliderBar.png"); + knobTexture = LoadTexture(device, L"SampleFramework11\\Images\\SliderKnob.png"); + + font.Initialize(L"Microsoft Sans Serif", 8.5f, SpriteFont::Regular, true, device); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/GUIObject.h b/Shadows/SampleFramework11/GUIObject.h new file mode 100644 index 0000000..b24afca --- /dev/null +++ b/Shadows/SampleFramework11/GUIObject.h @@ -0,0 +1,50 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "SpriteFont.h" + +namespace SampleFramework11 +{ + +class KeyboardState; +class MouseState; +class SpriteRenderer; + +class GUIObject +{ + +public: + + virtual void Update(const KeyboardState& kbState, const MouseState& mouseState) = 0; + virtual void Render(SpriteRenderer& renderer, SpriteFont& defaultFont) = 0; + + XMFLOAT2& Position() { return position; }; + XMFLOAT2 Position() const { return position; }; + + bool& Enabled() { return enabled; }; + bool Enabled() const { return enabled; }; + + static void InitGlobalResources(ID3D11Device* device); + +protected: + + XMFLOAT2 position; + bool enabled; + + // Global shared resources + static SpriteFont font; + static ID3D11ShaderResourceViewPtr barTexture; + static ID3D11ShaderResourceViewPtr knobTexture; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/GraphicsTypes.cpp b/Shadows/SampleFramework11/GraphicsTypes.cpp new file mode 100644 index 0000000..18b9ca2 --- /dev/null +++ b/Shadows/SampleFramework11/GraphicsTypes.cpp @@ -0,0 +1,619 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "GraphicsTypes.h" +#include "Exceptions.h" +#include "Utility.h" +#include "Serialization.h" +#include "FileIO.h" + +namespace SampleFramework11 +{ + +// == RenderTarget2D ============================================================================== + +RenderTarget2D::RenderTarget2D() : Width(0), + Height(0), + Format(DXGI_FORMAT_UNKNOWN), + NumMipLevels(0), + MultiSamples(0), + MSQuality(0), + AutoGenMipMaps(false), + UAView(nullptr), + ArraySize(1) +{ + +} + +void RenderTarget2D::Initialize(ID3D11Device* device, + uint32 width, + uint32 height, + DXGI_FORMAT format, + uint32 numMipLevels, + uint32 multiSamples, + uint32 msQuality, + bool32 autoGenMipMaps, + bool32 createUAV, + uint32 arraySize, + bool32 cubeMap) +{ + D3D11_TEXTURE2D_DESC desc; + desc.Width = width; + desc.Height = height; + desc.ArraySize = arraySize; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET; + if(createUAV) + desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS; + + desc.CPUAccessFlags = 0; + desc.Format = format; + desc.MipLevels = numMipLevels; + desc.MiscFlags = (autoGenMipMaps && numMipLevels != 1) ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0; + desc.SampleDesc.Count = multiSamples; + desc.SampleDesc.Quality = msQuality; + desc.Usage = D3D11_USAGE_DEFAULT; + + if(cubeMap) + { + _ASSERT(arraySize == 6); + desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; + } + + DXCall(device->CreateTexture2D(&desc, nullptr, &Texture)); + + RTVArraySlices.clear(); + for(uint32 i = 0; i < arraySize; ++i) + { + ID3D11RenderTargetViewPtr rtView; + D3D11_RENDER_TARGET_VIEW_DESC rtDesc; + rtDesc.Format = format; + + if(arraySize == 1) + { + if(multiSamples > 1) + { + rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS; + } + else + { + rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; + rtDesc.Texture2D.MipSlice = 0; + } + } + else + { + if(multiSamples > 1) + { + rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY; + rtDesc.Texture2DMSArray.ArraySize = 1; + rtDesc.Texture2DMSArray.FirstArraySlice = i; + } + else + { + rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; + rtDesc.Texture2DArray.ArraySize = 1; + rtDesc.Texture2DArray.FirstArraySlice = i; + rtDesc.Texture2DArray.MipSlice = 0; + } + } + DXCall(device->CreateRenderTargetView(Texture, &rtDesc, &rtView)); + + RTVArraySlices.push_back(rtView); + } + + RTView = RTVArraySlices[0]; + + DXCall(device->CreateShaderResourceView(Texture, nullptr, &SRView)); + + SRVArraySlices.clear(); + for(uint32 i = 0; i < arraySize; ++i) + { + ID3D11ShaderResourceViewPtr srView; + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + srvDesc.Format = format; + + if(arraySize == 1) + { + if(multiSamples > 1) + { + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS; + } + else + { + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + srvDesc.Texture2D.MipLevels = -1; + srvDesc.Texture2D.MostDetailedMip = 0; + } + } + else + { + if(multiSamples > 1) + { + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY; + srvDesc.Texture2DMSArray.ArraySize = 1; + srvDesc.Texture2DMSArray.FirstArraySlice = i; + } + else + { + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + srvDesc.Texture2DArray.ArraySize = 1; + srvDesc.Texture2DArray.FirstArraySlice = i; + srvDesc.Texture2DArray.MipLevels = -1; + srvDesc.Texture2DArray.MostDetailedMip = 0; + } + } + + DXCall(device->CreateShaderResourceView(Texture, &srvDesc, &srView)); + SRVArraySlices.push_back(srView); + } + + Width = width; + Height = height; + NumMipLevels = numMipLevels; + MultiSamples = multiSamples; + Format = format; + AutoGenMipMaps = autoGenMipMaps; + ArraySize = arraySize; + CubeMap = cubeMap; + + if(createUAV) + DXCall(device->CreateUnorderedAccessView(Texture, nullptr, &UAView)); +}; + +// == DepthStencilBuffer ========================================================================== + +DepthStencilBuffer::DepthStencilBuffer() : Width(0), + Height(0), + MultiSamples(0), + MSQuality(0), + Format(DXGI_FORMAT_UNKNOWN), + ArraySize(1) +{ + +} + +void DepthStencilBuffer::Initialize(ID3D11Device* device, + uint32 width, + uint32 height, + DXGI_FORMAT format, + bool32 useAsShaderResource, + uint32 multiSamples, + uint32 msQuality, + uint32 arraySize) +{ + uint32 bindFlags = D3D11_BIND_DEPTH_STENCIL; + if (useAsShaderResource) + bindFlags |= D3D11_BIND_SHADER_RESOURCE; + + DXGI_FORMAT dsTexFormat; + if (!useAsShaderResource) + dsTexFormat = format; + else if (format == DXGI_FORMAT_D16_UNORM) + dsTexFormat = DXGI_FORMAT_R16_TYPELESS; + else if(format == DXGI_FORMAT_D24_UNORM_S8_UINT) + dsTexFormat = DXGI_FORMAT_R24G8_TYPELESS; + else + dsTexFormat = DXGI_FORMAT_R32_TYPELESS; + + D3D11_TEXTURE2D_DESC desc; + desc.Width = width; + desc.Height = height; + desc.ArraySize = arraySize; + desc.BindFlags = bindFlags; + desc.CPUAccessFlags = 0; + desc.Format = dsTexFormat; + desc.MipLevels = 1; + desc.MiscFlags = 0; + desc.SampleDesc.Count = multiSamples; + desc.SampleDesc.Quality = msQuality; + desc.Usage = D3D11_USAGE_DEFAULT; + DXCall(device->CreateTexture2D(&desc, nullptr, &Texture)); + + ArraySlices.clear(); + for (uint32 i = 0; i < arraySize; ++i) + { + D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; + ID3D11DepthStencilViewPtr dsView; + dsvDesc.Format = format; + + if (arraySize == 1) + { + dsvDesc.ViewDimension = multiSamples > 1 ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D; + dsvDesc.Texture2D.MipSlice = 0; + } + else + { + if(multiSamples > 1) + { + dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY; + dsvDesc.Texture2DMSArray.ArraySize = 1; + dsvDesc.Texture2DMSArray.FirstArraySlice = i; + } + else + { + dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; + dsvDesc.Texture2DArray.ArraySize = 1; + dsvDesc.Texture2DArray.FirstArraySlice = i; + dsvDesc.Texture2DArray.MipSlice = 0; + } + } + + dsvDesc.Flags = 0; + DXCall(device->CreateDepthStencilView(Texture, &dsvDesc, &dsView)); + ArraySlices.push_back(dsView); + + if (i == 0) + { + // Also create a read-only DSV + dsvDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH; + if (format == DXGI_FORMAT_D24_UNORM_S8_UINT || format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT) + dsvDesc.Flags |= D3D11_DSV_READ_ONLY_STENCIL; + DXCall(device->CreateDepthStencilView(Texture, &dsvDesc, &ReadOnlyDSView)); + dsvDesc.Flags = 0; + } + } + + DSView = ArraySlices[0]; + + if (useAsShaderResource) + { + DXGI_FORMAT dsSRVFormat; + if (format == DXGI_FORMAT_D16_UNORM) + dsSRVFormat = DXGI_FORMAT_R16_UNORM; + else if(format == DXGI_FORMAT_D24_UNORM_S8_UINT) + dsSRVFormat = DXGI_FORMAT_R24_UNORM_X8_TYPELESS ; + else + dsSRVFormat = DXGI_FORMAT_R32_FLOAT; + + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + srvDesc.Format = dsSRVFormat; + + if (arraySize == 1) + { + srvDesc.ViewDimension = multiSamples > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D; + srvDesc.Texture2D.MipLevels = 1; + srvDesc.Texture2D.MostDetailedMip = 0; + } + else + { + srvDesc.ViewDimension = multiSamples > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY : D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + srvDesc.Texture2DArray.ArraySize = arraySize; + srvDesc.Texture2DArray.FirstArraySlice = 0; + srvDesc.Texture2DArray.MipLevels = 1; + srvDesc.Texture2DArray.MostDetailedMip = 0; + } + + DXCall(device->CreateShaderResourceView(Texture, &srvDesc, &SRView)); + } + else + SRView = nullptr; + + Width = width; + Height = height; + MultiSamples = multiSamples; + Format = format; + ArraySize = arraySize; +} + +// == RWBuffer ==================================================================================== + +RWBuffer::RWBuffer() : Size(0), Stride(0), NumElements(0), Format(DXGI_FORMAT_UNKNOWN), RawBuffer(false) +{ + +} + +void RWBuffer::Initialize(ID3D11Device* device, DXGI_FORMAT format, uint32 stride, uint32 numElements, + bool32 rawBuffer, bool vertexBuffer, bool indexBuffer, bool indirectArgs, + const void* initData) +{ + if(rawBuffer) + { + stride = 4; + Format = DXGI_FORMAT_R32_TYPELESS; + } + + Format = format; + Size = stride * numElements; + Stride = stride; + NumElements = numElements; + RawBuffer = rawBuffer; + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.ByteWidth = stride * numElements; + bufferDesc.Usage = D3D11_USAGE_DEFAULT; + bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS; + bufferDesc.CPUAccessFlags = 0; + bufferDesc.MiscFlags = rawBuffer ? D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS : 0; + bufferDesc.StructureByteStride = 0; + + Assert_(vertexBuffer == false || indexBuffer == false); + + if(vertexBuffer) + bufferDesc.BindFlags |= D3D11_BIND_VERTEX_BUFFER; + + if(indexBuffer) + { + bufferDesc.BindFlags |= D3D11_BIND_INDEX_BUFFER; + Assert_(Format == DXGI_FORMAT_R32_TYPELESS || + Format == DXGI_FORMAT_R32_UINT || + Format == DXGI_FORMAT_R16_UINT); + } + + if(indirectArgs) + bufferDesc.MiscFlags |= D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS; + + D3D11_SUBRESOURCE_DATA srData; + srData.pSysMem = initData; + srData.SysMemPitch = 0; + srData.SysMemSlicePitch = 0; + + DXCall(device->CreateBuffer(&bufferDesc, initData ? &srData : nullptr, &Buffer)); + + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + srvDesc.Format = Format; + + if(rawBuffer) + { + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX; + srvDesc.BufferEx.FirstElement = 0; + srvDesc.BufferEx.NumElements = numElements; + srvDesc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW; + } + else + { + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; + srvDesc.Buffer.ElementOffset = 0; + srvDesc.Buffer.ElementWidth = numElements; + } + + DXCall(device->CreateShaderResourceView(Buffer, &srvDesc, &SRView)); + + D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc; + uavDesc.Format = format; + uavDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; + uavDesc.Buffer.FirstElement = 0; + uavDesc.Buffer.NumElements = numElements; + uavDesc.Buffer.Flags = rawBuffer ? D3D11_BUFFER_UAV_FLAG_RAW : 0; + + DXCall(device->CreateUnorderedAccessView(Buffer, &uavDesc, &UAView)); +} + +// == StructuredBuffer ============================================================================ + +StructuredBuffer::StructuredBuffer() : Size(0), Stride(0), NumElements(0) +{ +} + +void StructuredBuffer::Initialize(ID3D11Device* device, uint32 stride, uint32 numElements, bool32 useAsUAV, + bool32 appendConsume, bool32 hiddenCounter, const void* initData) +{ + Size = stride * numElements; + Stride = stride; + NumElements = numElements; + + Assert_(appendConsume == false || hiddenCounter == false); + + if(appendConsume || hiddenCounter) + useAsUAV = true; + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.ByteWidth = stride * numElements; + bufferDesc.Usage = useAsUAV ? D3D11_USAGE_DEFAULT : D3D11_USAGE_IMMUTABLE; + bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + bufferDesc.BindFlags |= useAsUAV ? D3D11_BIND_UNORDERED_ACCESS : 0; + bufferDesc.CPUAccessFlags = 0; + bufferDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + bufferDesc.StructureByteStride = stride; + + D3D11_SUBRESOURCE_DATA subresourceData; + subresourceData.pSysMem = initData; + subresourceData.SysMemPitch = 0; + subresourceData.SysMemSlicePitch = 0; + + DXCall(device->CreateBuffer(&bufferDesc, initData != nullptr ? &subresourceData : nullptr, &Buffer)); + + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + srvDesc.Format = DXGI_FORMAT_UNKNOWN; + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; + srvDesc.Buffer.FirstElement = 0; + srvDesc.Buffer.NumElements = numElements; + DXCall(device->CreateShaderResourceView(Buffer, &srvDesc, &SRView)); + + if(useAsUAV) + { + D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc; + uavDesc.Format = DXGI_FORMAT_UNKNOWN; + uavDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; + uavDesc.Buffer.FirstElement = 0; + uavDesc.Buffer.Flags = 0; + uavDesc.Buffer.Flags |= appendConsume ? D3D11_BUFFER_UAV_FLAG_APPEND : 0; + uavDesc.Buffer.Flags |= hiddenCounter ? D3D11_BUFFER_UAV_FLAG_COUNTER : 0; + uavDesc.Buffer.NumElements = numElements; + DXCall(device->CreateUnorderedAccessView(Buffer, &uavDesc, &UAView)); + } +} + +void StructuredBuffer::WriteToFile(const wchar* path, ID3D11Device* device, ID3D11DeviceContext* context) +{ + Assert_(Buffer != nullptr); + + // Get the buffer info + D3D11_BUFFER_DESC desc; + Buffer->GetDesc(&desc); + + uint32 useAsUAV = (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS) ? 1 : 0; + + uint32 appendConsume = 0; + uint32 hiddenCounter = 0; + if(useAsUAV) + { + D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc; + UAView->GetDesc(&uavDesc); + appendConsume = (uavDesc.Format & D3D11_BUFFER_UAV_FLAG_APPEND) ? 1 : 0; + hiddenCounter =(uavDesc.Format & D3D11_BUFFER_UAV_FLAG_COUNTER) ? 1 : 0; + } + + // If the exists, delete it + if(FileExists(path)) + Win32Call(DeleteFile(path)); + + // Create the file + HANDLE fileHandle = CreateFile(path, GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); + if(fileHandle == INVALID_HANDLE_VALUE) + Win32Call(false); + + // Write the buffer info + DWORD bytesWritten = 0; + Win32Call(WriteFile(fileHandle, &Size, 4, &bytesWritten, nullptr)); + Win32Call(WriteFile(fileHandle, &Stride, 4, &bytesWritten, nullptr)); + Win32Call(WriteFile(fileHandle, &NumElements, 4, &bytesWritten, nullptr)); + Win32Call(WriteFile(fileHandle, &useAsUAV, 4, &bytesWritten, nullptr)); + Win32Call(WriteFile(fileHandle, &hiddenCounter, 4, &bytesWritten, nullptr)); + Win32Call(WriteFile(fileHandle, &appendConsume, 4, &bytesWritten, nullptr)); + + // Get the buffer data + StagingBuffer stagingBuffer; + stagingBuffer.Initialize(device, Size); + context->CopyResource(stagingBuffer.Buffer, Buffer); + const void* bufferData= stagingBuffer.Map(context); + + // Write the data to the file + Win32Call(WriteFile(fileHandle, bufferData, Size, &bytesWritten, nullptr)); + + // Un-map the staging buffer + stagingBuffer.Unmap(context); + + // Close the file + Win32Call(CloseHandle(fileHandle)); +} + +void StructuredBuffer::ReadFromFile(const wchar* path, ID3D11Device* device) +{ + // Open the file + HANDLE fileHandle = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if(fileHandle == INVALID_HANDLE_VALUE) + Win32Call(false); + + // Read the buffer info + bool32 useAsUAV, hiddenCounter, appendConsume; + DWORD bytesRead = 0; + Win32Call(ReadFile(fileHandle, &Size, 4, &bytesRead, nullptr)); + Win32Call(ReadFile(fileHandle, &Stride, 4, &bytesRead, nullptr)); + Win32Call(ReadFile(fileHandle, &NumElements, 4, &bytesRead, nullptr)); + Win32Call(ReadFile(fileHandle, &useAsUAV, 4, &bytesRead, nullptr)); + Win32Call(ReadFile(fileHandle, &hiddenCounter, 4, &bytesRead, nullptr)); + Win32Call(ReadFile(fileHandle, &appendConsume, 4, &bytesRead, nullptr)); + + // Read the buffer data + UINT8* bufferData = new UINT8[Size]; + Win32Call(ReadFile(fileHandle, bufferData, Size, &bytesRead, nullptr)); + + // Close the file + Win32Call(CloseHandle(fileHandle)); + + // Init + Initialize(device, Stride, NumElements, useAsUAV, appendConsume, hiddenCounter, bufferData); + + // Clean up + delete [] bufferData; +} + +// == StagingBuffer =============================================================================== + +StagingBuffer::StagingBuffer() : Size(0) +{ +} + +void StagingBuffer::Initialize(ID3D11Device* device, uint32 size) +{ + Size = size; + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.ByteWidth = Size; + bufferDesc.Usage = D3D11_USAGE_STAGING; + bufferDesc.BindFlags = 0; + bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + DXCall(device->CreateBuffer(&bufferDesc, nullptr, &Buffer)); +} + +void* StagingBuffer::Map(ID3D11DeviceContext* context) +{ + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(Buffer, 0, D3D11_MAP_READ, 0, &mapped)); + + return mapped.pData; +} + +void StagingBuffer::Unmap(ID3D11DeviceContext* context) +{ + context->Unmap(Buffer, 0); +} + +// == StagingTexture2D ============================================================================ + +StagingTexture2D::StagingTexture2D() : Width(0), + Height(0), + Format(DXGI_FORMAT_UNKNOWN), + NumMipLevels(0), + MultiSamples(0), + MSQuality(0), + ArraySize(1) +{ +} + +void StagingTexture2D::Initialize(ID3D11Device* device, + uint32 width, + uint32 height, + DXGI_FORMAT format, + uint32 numMipLevels, + uint32 multiSamples, + uint32 msQuality, + uint32 arraySize) +{ + D3D11_TEXTURE2D_DESC desc; + desc.Width = width; + desc.Height = height; + desc.ArraySize = arraySize; + desc.BindFlags = 0; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + desc.Format = format; + desc.MipLevels = numMipLevels; + desc.MiscFlags = 0; + desc.SampleDesc.Count = multiSamples; + desc.SampleDesc.Quality = msQuality; + desc.Usage = D3D11_USAGE_STAGING; + DXCall(device->CreateTexture2D(&desc, nullptr, &Texture)); + + + Width = width; + Height = height; + NumMipLevels = numMipLevels; + MultiSamples = multiSamples; + Format = format; + ArraySize = arraySize; +}; + +void* StagingTexture2D::Map(ID3D11DeviceContext* context, uint32 subResourceIndex, uint32& pitch) +{ + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(Texture, subResourceIndex, D3D11_MAP_READ, 0, &mapped)); + pitch = mapped.RowPitch; + return mapped.pData; +} + +void StagingTexture2D::Unmap(ID3D11DeviceContext* context, uint32 subResourceIndex) +{ + context->Unmap(Texture, subResourceIndex); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/GraphicsTypes.h b/Shadows/SampleFramework11/GraphicsTypes.h new file mode 100644 index 0000000..9c5b85b --- /dev/null +++ b/Shadows/SampleFramework11/GraphicsTypes.h @@ -0,0 +1,300 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +struct RenderTarget2D +{ + ID3D11Texture2DPtr Texture; + ID3D11RenderTargetViewPtr RTView; + ID3D11ShaderResourceViewPtr SRView; + ID3D11UnorderedAccessViewPtr UAView; + uint32 Width; + uint32 Height; + uint32 NumMipLevels; + uint32 MultiSamples; + uint32 MSQuality; + DXGI_FORMAT Format; + bool32 AutoGenMipMaps; + uint32 ArraySize; + bool32 CubeMap; + std::vector RTVArraySlices; + std::vector SRVArraySlices; + + RenderTarget2D(); + + void Initialize( ID3D11Device* device, + uint32 width, + uint32 height, + DXGI_FORMAT format, + uint32 numMipLevels = 1, + uint32 multiSamples = 1, + uint32 msQuality = 0, + bool32 autoGenMipMaps = false, + bool32 createUAV = false, + uint32 arraySize = 1, + bool32 cubeMap = false); +}; + +struct DepthStencilBuffer +{ + ID3D11Texture2DPtr Texture; + ID3D11DepthStencilViewPtr DSView; + ID3D11DepthStencilViewPtr ReadOnlyDSView; + ID3D11ShaderResourceViewPtr SRView; + uint32 Width; + uint32 Height; + uint32 MultiSamples; + uint32 MSQuality; + DXGI_FORMAT Format; + uint32 ArraySize; + std::vector ArraySlices; + + DepthStencilBuffer(); + + void Initialize( ID3D11Device* device, + uint32 width, + uint32 height, + DXGI_FORMAT format = DXGI_FORMAT_D24_UNORM_S8_UINT, + bool32 useAsShaderResource = false, + uint32 multiSamples = 1, + uint32 msQuality = 0, + uint32 arraySize = 1); +}; + +struct RWBuffer +{ + ID3D11BufferPtr Buffer; + ID3D11ShaderResourceViewPtr SRView; + ID3D11UnorderedAccessViewPtr UAView; + uint32 Size; + uint32 Stride; + uint32 NumElements; + bool32 RawBuffer; + DXGI_FORMAT Format; + + RWBuffer(); + + void Initialize(ID3D11Device* device, DXGI_FORMAT format, uint32 stride, uint32 numElements, bool32 rawBuffer = false, + bool vertexBuffer = false, bool indexBuffer = false, bool indirectArgs = false, + const void* initData = nullptr); +}; + +struct StagingBuffer +{ + ID3D11BufferPtr Buffer; + uint32 Size; + + StagingBuffer(); + + void Initialize(ID3D11Device* device, uint32 size); + void* Map(ID3D11DeviceContext* context); + void Unmap(ID3D11DeviceContext* context); +}; + +struct StagingTexture2D +{ + ID3D11Texture2DPtr Texture; + uint32 Width; + uint32 Height; + uint32 NumMipLevels; + uint32 MultiSamples; + uint32 MSQuality; + DXGI_FORMAT Format; + uint32 ArraySize; + + StagingTexture2D(); + + void Initialize( ID3D11Device* device, + uint32 width, + uint32 height, + DXGI_FORMAT format, + uint32 numMipLevels = 1, + uint32 multiSamples = 1, + uint32 msQuality = 0, + uint32 arraySize = 1); + + void* Map(ID3D11DeviceContext* context, uint32 subResourceIndex, uint32& pitch); + void Unmap(ID3D11DeviceContext* context, uint32 subResourceIndex); +}; + +struct StructuredBuffer +{ + ID3D11BufferPtr Buffer; + ID3D11ShaderResourceViewPtr SRView; + ID3D11UnorderedAccessViewPtr UAView; + uint32 Size; + uint32 Stride; + uint32 NumElements; + StagingBuffer DebugBuffer; + + StructuredBuffer(); + + void Initialize(ID3D11Device* device, uint32 stride, uint32 numElements, bool32 useAsUAV = false, + bool32 appendConsume = false, bool32 hiddenCounter = false, const void* initData = nullptr); + + void WriteToFile(const wchar* path, ID3D11Device* device, ID3D11DeviceContext* context); + void ReadFromFile(const wchar* path, ID3D11Device* device); + + template void DebugPrint(ID3D11DeviceContext* context, const char* name) + { + Assert_(Buffer != nullptr); + if(DebugBuffer.Size != Size) + { + ID3D11DevicePtr device; + context->GetDevice(&device); + DebugBuffer.Initialize(device, Size); + } + + std::printf("%s\n", name); + + context->CopyResource(DebugBuffer.Buffer, Buffer); + const T* data = reinterpret_cast(DebugBuffer.Map(context)); + for(uint32 i = 0; i < NumElements; ++i) + { + std::string elemPrint = data[i].Print(); + std::printf(" [%u] %s", i, elemPrint.c_str()); + } + } +}; + +template class ConstantBuffer +{ +public: + + T Data; + + ID3D11BufferPtr Buffer; + bool GPUWritable; + +public: + + ConstantBuffer() : GPUWritable(false) + { + ZeroMemory(&Data, sizeof(T)); + } + + void Initialize(ID3D11Device* device, bool gpuWritable = false) + { + D3D11_BUFFER_DESC desc; + desc.Usage = D3D11_USAGE_DYNAMIC; + desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + desc.MiscFlags = 0; + desc.ByteWidth = static_cast(sizeof(T) + (16 - (sizeof(T) % 16))); + + if(gpuWritable) + { + desc.Usage = D3D11_USAGE_DEFAULT; + desc.CPUAccessFlags = 0; + } + GPUWritable = gpuWritable; + + DXCall(device->CreateBuffer(&desc, nullptr, &Buffer)); + } + + void ApplyChanges(ID3D11DeviceContext* deviceContext) + { + Assert_(Buffer != nullptr); + + if(GPUWritable) + { + deviceContext->UpdateSubresource(Buffer, 0, nullptr, &Data, 0, 0); + } + else + { + D3D11_MAPPED_SUBRESOURCE mappedResource; + DXCall(deviceContext->Map(Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)); + CopyMemory(mappedResource.pData, &Data, sizeof(T)); + deviceContext->Unmap(Buffer, 0); + } + } + + void SetVS(ID3D11DeviceContext* deviceContext, uint32 slot) const + { + Assert_(Buffer != nullptr); + + ID3D11Buffer* bufferArray[1]; + bufferArray[0] = Buffer; + deviceContext->VSSetConstantBuffers(slot, 1, bufferArray); + } + + void SetPS(ID3D11DeviceContext* deviceContext, uint32 slot) const + { + Assert_(Buffer != nullptr); + + ID3D11Buffer* bufferArray[1]; + bufferArray[0] = Buffer; + deviceContext->PSSetConstantBuffers(slot, 1, bufferArray); + } + + void SetGS(ID3D11DeviceContext* deviceContext, uint32 slot) const + { + Assert_(Buffer != nullptr); + + ID3D11Buffer* bufferArray[1]; + bufferArray[0] = Buffer; + deviceContext->GSSetConstantBuffers(slot, 1, bufferArray); + } + + void SetHS(ID3D11DeviceContext* deviceContext, uint32 slot) const + { + Assert_(Buffer != nullptr); + + ID3D11Buffer* bufferArray[1]; + bufferArray[0] = Buffer; + deviceContext->HSSetConstantBuffers(slot, 1, bufferArray); + } + + void SetDS(ID3D11DeviceContext* deviceContext, uint32 slot) const + { + Assert_(Buffer != nullptr); + + ID3D11Buffer* bufferArray[1]; + bufferArray[0] = Buffer; + deviceContext->DSSetConstantBuffers(slot, 1, bufferArray); + } + + void SetCS(ID3D11DeviceContext* deviceContext, uint32 slot) const + { + Assert_(Buffer != nullptr); + + ID3D11Buffer* bufferArray[1]; + bufferArray[0] = Buffer; + deviceContext->CSSetConstantBuffers(slot, 1, bufferArray); + } +}; + +// For aligning to float4 boundaries +#define Float4Align __declspec(align(16)) + +class PIXEvent +{ +public: + + PIXEvent(const wchar* markerName) + { + D3DPERF_BeginEvent(0xFFFFFFFF, markerName); + } + + ~PIXEvent() + { + D3DPERF_EndEvent(); + } +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Images/SliderBar.dds b/Shadows/SampleFramework11/Images/SliderBar.dds new file mode 100644 index 0000000..92bf15d Binary files /dev/null and b/Shadows/SampleFramework11/Images/SliderBar.dds differ diff --git a/Shadows/SampleFramework11/Images/SliderBar.png b/Shadows/SampleFramework11/Images/SliderBar.png new file mode 100644 index 0000000..268d039 Binary files /dev/null and b/Shadows/SampleFramework11/Images/SliderBar.png differ diff --git a/Shadows/SampleFramework11/Images/SliderKnob.dds b/Shadows/SampleFramework11/Images/SliderKnob.dds new file mode 100644 index 0000000..d030ce5 Binary files /dev/null and b/Shadows/SampleFramework11/Images/SliderKnob.dds differ diff --git a/Shadows/SampleFramework11/Images/SliderKnob.png b/Shadows/SampleFramework11/Images/SliderKnob.png new file mode 100644 index 0000000..67f675c Binary files /dev/null and b/Shadows/SampleFramework11/Images/SliderKnob.png differ diff --git a/Shadows/SampleFramework11/Input.cpp b/Shadows/SampleFramework11/Input.cpp new file mode 100644 index 0000000..cf3d682 --- /dev/null +++ b/Shadows/SampleFramework11/Input.cpp @@ -0,0 +1,128 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Input.h" +#include "Exceptions.h" + +namespace SampleFramework11 +{ + +MouseState MouseState::prevState; +KeyboardState KeyboardState::prevState; +BYTE KeyboardState::currState[256]; + +MouseState::MouseState() : X (0), + Y (0), + DX (0), + DY (0), + IsOverWindow (false) +{ + +} + +MouseState MouseState::GetMouseState(HWND hwnd) +{ + POINT pos; + if (!GetCursorPos(&pos)) + throw Win32Exception(GetLastError()); + + if (hwnd != NULL) + if(!ScreenToClient(hwnd, &pos)) + throw Win32Exception(GetLastError()); + + MouseState newState; + newState.X = pos.x; + newState.Y = pos.y; + newState.DX = pos.x - prevState.X; + newState.DY = pos.y - prevState.Y; + + newState.LButton.Pressed = (GetKeyState(VK_LBUTTON) & 0x8000) > 0; + newState.LButton.RisingEdge = newState.LButton.Pressed && !prevState.LButton.Pressed; + newState.LButton.RisingEdge = !newState.LButton.Pressed && prevState.LButton.Pressed; + + newState.MButton.Pressed = (GetKeyState(VK_MBUTTON) & 0x8000) > 0; + newState.MButton.RisingEdge = newState.MButton.Pressed && !prevState.MButton.Pressed; + newState.MButton.RisingEdge = !newState.MButton.Pressed && prevState.MButton.Pressed; + + newState.RButton.Pressed = (GetKeyState(VK_RBUTTON) & 0x8000) > 0; + newState.RButton.RisingEdge = newState.RButton.Pressed && !prevState.RButton.Pressed; + newState.RButton.RisingEdge = !newState.RButton.Pressed && prevState.RButton.Pressed; + + if (hwnd != NULL) + { + RECT clientRect; + if (!::GetClientRect(hwnd, &clientRect)) + throw Win32Exception(GetLastError()); + newState.IsOverWindow = (pos.x >= 0 && pos.x < clientRect.right + && pos.y >= 0 && pos.y < clientRect.bottom); + } + else + newState.IsOverWindow = false; + + prevState = newState; + return prevState; +} + +void MouseState::SetCursorPos(int x, int y, HWND hwnd) +{ + POINT pos; + pos.x = x; + pos.y = y; + + if (hwnd != NULL) + if (!ClientToScreen(hwnd, &pos)) + throw Win32Exception(GetLastError()); + + if (!::SetCursorPos(pos.x, pos.y)) + throw Win32Exception(GetLastError()); +} + +KeyState KeyboardState::GetKeyState(Keys key) const +{ + return keyStates[key]; +} + +bool KeyboardState::IsKeyDown(Keys key) const +{ + return keyStates[key].Pressed; +} + +bool KeyboardState::RisingEdge(Keys key) const +{ + return keyStates[key].RisingEdge; +} + +KeyboardState KeyboardState::GetKeyboardState(HWND hwnd) +{ + if(GetForegroundWindow() != hwnd) + return prevState; + + ::GetKeyboardState(currState); + + KeyState state; + for (UINT i = 0; i < 256; ++i) + { + state.Pressed = KeyPressed(currState[i]); + state.RisingEdge = state.Pressed && !prevState.keyStates[i].Pressed; + state.FallingEdge = !state.Pressed && prevState.keyStates[i].Pressed; + prevState.keyStates[i] = state; + } + + return prevState; +} + +KeyState::KeyState() : Pressed(false), + RisingEdge(false), + FallingEdge(false) +{ +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Input.h b/Shadows/SampleFramework11/Input.h new file mode 100644 index 0000000..bd123a1 --- /dev/null +++ b/Shadows/SampleFramework11/Input.h @@ -0,0 +1,241 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +namespace SampleFramework11 +{ + +struct KeyState +{ + bool Pressed; + bool RisingEdge; + bool FallingEdge; + + KeyState(); +}; + +class MouseState +{ + +public: + + MouseState(); + + static MouseState GetMouseState(HWND hwnd = NULL); + static void SetCursorPos(int x, int y, HWND hwnd = NULL); + + int X; + int Y; + int DX; + int DY; + + KeyState LButton; + KeyState MButton; + KeyState RButton; + + bool IsOverWindow; + +private: + + static MouseState prevState; +}; + +class KeyboardState +{ + +public: + + enum Keys + { + A = 0x41, + Add = 0x6b, + Apps = 0x5d, + Attn = 0xf6, + B = 0x42, + Back = 8, + BrowserBack = 0xa6, + BrowserFavorites = 0xab, + BrowserForward = 0xa7, + BrowserHome = 0xac, + BrowserRefresh = 0xa8, + BrowserSearch = 170, + BrowserStop = 0xa9, + C = 0x43, + CapsLock = 20, + ChatPadGreen = 0xca, + ChatPadOrange = 0xcb, + Crsel = 0xf7, + D = 0x44, + D0 = 0x30, + D1 = 0x31, + D2 = 50, + D3 = 0x33, + D4 = 0x34, + D5 = 0x35, + D6 = 0x36, + D7 = 0x37, + D8 = 0x38, + D9 = 0x39, + Decimal = 110, + Delete = 0x2e, + Divide = 0x6f, + Down = 40, + E = 0x45, + End = 0x23, + Enter = 13, + EraseEof = 0xf9, + Escape = 0x1b, + Execute = 0x2b, + Exsel = 0xf8, + F = 70, + F1 = 0x70, + F10 = 0x79, + F11 = 0x7a, + F12 = 0x7b, + F13 = 0x7c, + F14 = 0x7d, + F15 = 0x7e, + F16 = 0x7f, + F17 = 0x80, + F18 = 0x81, + F19 = 130, + F2 = 0x71, + F20 = 0x83, + F21 = 0x84, + F22 = 0x85, + F23 = 0x86, + F24 = 0x87, + F3 = 0x72, + F4 = 0x73, + F5 = 0x74, + F6 = 0x75, + F7 = 0x76, + F8 = 0x77, + F9 = 120, + G = 0x47, + H = 0x48, + Help = 0x2f, + Home = 0x24, + I = 0x49, + ImeConvert = 0x1c, + ImeNoConvert = 0x1d, + Insert = 0x2d, + J = 0x4a, + K = 0x4b, + Kana = 0x15, + Kanji = 0x19, + L = 0x4c, + LaunchApplication1 = 0xb6, + LaunchApplication2 = 0xb7, + LaunchMail = 180, + Left = 0x25, + LeftAlt = 0xa4, + LeftControl = 0xa2, + LeftShift = 160, + LeftWindows = 0x5b, + M = 0x4d, + MediaNextTrack = 0xb0, + MediaPlayPause = 0xb3, + MediaPreviousTrack = 0xb1, + MediaStop = 0xb2, + Multiply = 0x6a, + N = 0x4e, + None = 0, + NumLock = 0x90, + NumPad0 = 0x60, + NumPad1 = 0x61, + NumPad2 = 0x62, + NumPad3 = 0x63, + NumPad4 = 100, + NumPad5 = 0x65, + NumPad6 = 0x66, + NumPad7 = 0x67, + NumPad8 = 0x68, + NumPad9 = 0x69, + O = 0x4f, + Oem8 = 0xdf, + OemAuto = 0xf3, + OemBackslash = 0xe2, + OemClear = 0xfe, + OemCloseBrackets = 0xdd, + OemComma = 0xbc, + OemCopy = 0xf2, + OemEnlW = 0xf4, + OemMinus = 0xbd, + OemOpenBrackets = 0xdb, + OemPeriod = 190, + OemPipe = 220, + OemPlus = 0xbb, + OemQuestion = 0xbf, + OemQuotes = 0xde, + OemSemicolon = 0xba, + OemTilde = 0xc0, + P = 80, + Pa1 = 0xfd, + PageDown = 0x22, + PageUp = 0x21, + Pause = 0x13, + Play = 250, + Print = 0x2a, + PrintScreen = 0x2c, + ProcessKey = 0xe5, + Q = 0x51, + R = 0x52, + Right = 0x27, + RightAlt = 0xa5, + RightControl = 0xa3, + RightShift = 0xa1, + RightWindows = 0x5c, + S = 0x53, + Scroll = 0x91, + Select = 0x29, + SelectMedia = 0xb5, + Separator = 0x6c, + Sleep = 0x5f, + Space = 0x20, + Subtract = 0x6d, + T = 0x54, + Tab = 9, + U = 0x55, + Up = 0x26, + V = 0x56, + VolumeDown = 0xae, + VolumeMute = 0xad, + VolumeUp = 0xaf, + W = 0x57, + X = 0x58, + Y = 0x59, + Z = 90, + Zoom = 0xfb + }; + +public: + + KeyState GetKeyState(Keys key) const; + bool IsKeyDown(Keys key) const; + bool RisingEdge(Keys key) const; + + static KeyboardState GetKeyboardState(HWND hwnd); + +private: + + KeyState keyStates[256]; + + static KeyboardState prevState; + static BYTE currState[256]; + + static inline bool KeyPressed(BYTE value) { return (value & 0x80) > 0; }; +}; + + + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/InterfacePointers.h b/Shadows/SampleFramework11/InterfacePointers.h new file mode 100644 index 0000000..e273fc5 --- /dev/null +++ b/Shadows/SampleFramework11/InterfacePointers.h @@ -0,0 +1,81 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +namespace SampleFramework11 +{ + +// Device +_COM_SMARTPTR_TYPEDEF(ID3D11Device, __uuidof(ID3D11Device)); +_COM_SMARTPTR_TYPEDEF(ID3D11DeviceContext, __uuidof(ID3D11DeviceContext)); +_COM_SMARTPTR_TYPEDEF(ID3D11DeviceChild, __uuidof(ID3D11DeviceChild)); +_COM_SMARTPTR_TYPEDEF(ID3D11Query, __uuidof(ID3D11Query)); +_COM_SMARTPTR_TYPEDEF(ID3D11CommandList, __uuidof(ID3D11CommandList)); +_COM_SMARTPTR_TYPEDEF(ID3D11Counter, __uuidof(ID3D11Counter)); +_COM_SMARTPTR_TYPEDEF(ID3D11InputLayout, __uuidof(ID3D11InputLayout)); +_COM_SMARTPTR_TYPEDEF(ID3D11Predicate, __uuidof(ID3D11Predicate)); +_COM_SMARTPTR_TYPEDEF(ID3D11Asynchronous, __uuidof(ID3D11Asynchronous)); +_COM_SMARTPTR_TYPEDEF(ID3D11InfoQueue, __uuidof(ID3D11InfoQueue)); +_COM_SMARTPTR_TYPEDEF(ID3D11Debug, __uuidof(ID3D11Debug)); + +// States +_COM_SMARTPTR_TYPEDEF(ID3D11BlendState, __uuidof(ID3D11BlendState)); +_COM_SMARTPTR_TYPEDEF(ID3D11DepthStencilState, __uuidof(ID3D11DepthStencilState)); +_COM_SMARTPTR_TYPEDEF(ID3D11RasterizerState, __uuidof(ID3D11RasterizerState)); +_COM_SMARTPTR_TYPEDEF(ID3D11SamplerState, __uuidof(ID3D11SamplerState)); + +// Resources +_COM_SMARTPTR_TYPEDEF(ID3D11Resource, __uuidof(ID3D11Resource)); +_COM_SMARTPTR_TYPEDEF(ID3D11Texture1D, __uuidof(ID3D11Texture1D)); +_COM_SMARTPTR_TYPEDEF(ID3D11Texture2D, __uuidof(ID3D11Texture2D)); +_COM_SMARTPTR_TYPEDEF(ID3D11Texture3D, __uuidof(ID3D11Texture3D)); +_COM_SMARTPTR_TYPEDEF(ID3D11Buffer, __uuidof(ID3D11Buffer)); + +// Views +_COM_SMARTPTR_TYPEDEF(ID3D11View, __uuidof(ID3D11View)); +_COM_SMARTPTR_TYPEDEF(ID3D11RenderTargetView, __uuidof(ID3D11RenderTargetView)); +_COM_SMARTPTR_TYPEDEF(ID3D11ShaderResourceView, __uuidof(ID3D11ShaderResourceView)); +_COM_SMARTPTR_TYPEDEF(ID3D11DepthStencilView, __uuidof(ID3D11DepthStencilView)); +_COM_SMARTPTR_TYPEDEF(ID3D11UnorderedAccessView, __uuidof(ID3D11UnorderedAccessView)); + +// Shaders +_COM_SMARTPTR_TYPEDEF(ID3D11ComputeShader, __uuidof(ID3D11ComputeShader)); +_COM_SMARTPTR_TYPEDEF(ID3D11DomainShader, __uuidof(ID3D11DomainShader)); +_COM_SMARTPTR_TYPEDEF(ID3D11GeometryShader, __uuidof(ID3D11GeometryShader)); +_COM_SMARTPTR_TYPEDEF(ID3D11HullShader, __uuidof(ID3D11HullShader)); +_COM_SMARTPTR_TYPEDEF(ID3D11PixelShader, __uuidof(ID3D11PixelShader)); +_COM_SMARTPTR_TYPEDEF(ID3D11VertexShader, __uuidof(ID3D11VertexShader)); +_COM_SMARTPTR_TYPEDEF(ID3D11ClassInstance, __uuidof(ID3D11ClassInstance)); +_COM_SMARTPTR_TYPEDEF(ID3D11ClassLinkage, __uuidof(ID3D11ClassLinkage)); +_COM_SMARTPTR_TYPEDEF(ID3D11ShaderReflection, IID_ID3D11ShaderReflection); +_COM_SMARTPTR_TYPEDEF(ID3D11ShaderReflectionConstantBuffer, IID_ID3D11ShaderReflectionConstantBuffer); +_COM_SMARTPTR_TYPEDEF(ID3D11ShaderReflectionType, IID_ID3D11ShaderReflectionType); +_COM_SMARTPTR_TYPEDEF(ID3D11ShaderReflectionVariable, IID_ID3D11ShaderReflectionVariable); + +// D3D10 +_COM_SMARTPTR_TYPEDEF(ID3D10Blob, IID_ID3D10Blob); +typedef ID3D10BlobPtr ID3DBlobPtr; + +// DXGI +_COM_SMARTPTR_TYPEDEF(IDXGISwapChain, __uuidof(IDXGISwapChain)); +_COM_SMARTPTR_TYPEDEF(IDXGIAdapter, __uuidof(IDXGIAdapter)); +_COM_SMARTPTR_TYPEDEF(IDXGIAdapter1, __uuidof(IDXGIAdapter1)); +_COM_SMARTPTR_TYPEDEF(IDXGIDevice, __uuidof(IDXGIDevice)); +_COM_SMARTPTR_TYPEDEF(IDXGIDevice1, __uuidof(IDXGIDevice1)); +_COM_SMARTPTR_TYPEDEF(IDXGIDeviceSubObject, __uuidof(IDXGIDeviceSubObject)); +_COM_SMARTPTR_TYPEDEF(IDXGIFactory, __uuidof(IDXGIFactory)); +_COM_SMARTPTR_TYPEDEF(IDXGIFactory1, __uuidof(IDXGIFactory1)); +_COM_SMARTPTR_TYPEDEF(IDXGIKeyedMutex, __uuidof(IDXGIKeyedMutex)); +_COM_SMARTPTR_TYPEDEF(IDXGIObject, __uuidof(IDXGIObject)); +_COM_SMARTPTR_TYPEDEF(IDXGIOutput, __uuidof(IDXGIOutput)); +_COM_SMARTPTR_TYPEDEF(IDXGIResource, __uuidof(IDXGIResource)); +_COM_SMARTPTR_TYPEDEF(IDXGISurface1, __uuidof(IDXGISurface1)); + +} diff --git a/Shadows/SampleFramework11/LodePNG/lodepng.cpp b/Shadows/SampleFramework11/LodePNG/lodepng.cpp new file mode 100644 index 0000000..0ff9958 --- /dev/null +++ b/Shadows/SampleFramework11/LodePNG/lodepng.cpp @@ -0,0 +1,6200 @@ +/* +LodePNG version 20121014 + +Copyright (c) 2005-2012 Lode Vandevenne + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*/ + +/* +The manual and changelog are in the header file "lodepng.h" +Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for C. +*/ + +#include "PCH.h" + +#pragma warning(disable : 4996) +#pragma warning(disable : 4267) + +#include "lodepng.h" + +#include +#include + +#ifdef LODEPNG_COMPILE_CPP +#include +#endif /*LODEPNG_COMPILE_CPP*/ + +#define VERSION_STRING "20121014" + +/* +This source file is built up in the following large parts. The code sections +with the "LODEPNG_COMPILE_" #defines divide this up further in an intermixed way. +-Tools for C and common code for PNG and Zlib +-C Code for Zlib (huffman, deflate, ...) +-C Code for PNG (file format chunks, adam7, PNG filters, color conversions, ...) +-The C++ wrapper around all of the above +*/ + +/*The malloc, realloc and free functions defined here with "my" in front of the +name, so that you can easily change them to others related to your platform in +this one location if needed. Everything else in the code calls these.*/ + +static void* mymalloc(size_t size) +{ + return malloc(size); +} + +static void* myrealloc(void* ptr, size_t new_size) +{ + return realloc(ptr, new_size); +} + +static void myfree(void* ptr) +{ + free(ptr); +} + +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* // Tools for C, and common code for PNG and Zlib. // */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ + +/* +Often in case of an error a value is assigned to a variable and then it breaks +out of a loop (to go to the cleanup phase of a function). This macro does that. +It makes the error handling code shorter and more readable. + +Example: if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83); +*/ +#define CERROR_BREAK(errorvar, code)\ +{\ + errorvar = code;\ + break;\ +} + +/*version of CERROR_BREAK that assumes the common case where the error variable is named "error"*/ +#define ERROR_BREAK(code) CERROR_BREAK(error, code) + +/*Set error var to the error code, and return it.*/ +#define CERROR_RETURN_ERROR(errorvar, code)\ +{\ + errorvar = code;\ + return code;\ +} + +/*Try the code, if it returns error, also return the error.*/ +#define CERROR_TRY_RETURN(call)\ +{\ + unsigned error = call;\ + if(error) return error;\ +} + +/* +About uivector, ucvector and string: +-All of them wrap dynamic arrays or text strings in a similar way. +-LodePNG was originally written in C++. The vectors replace the std::vectors that were used in the C++ version. +-The string tools are made to avoid problems with compilers that declare things like strncat as deprecated. +-They're not used in the interface, only internally in this file as static functions. +-As with many other structs in this file, the init and cleanup functions serve as ctor and dtor. +*/ + +#ifdef LODEPNG_COMPILE_ZLIB +/*dynamic vector of unsigned ints*/ +typedef struct uivector +{ + unsigned* data; + size_t size; /*size in number of unsigned longs*/ + size_t allocsize; /*allocated size in bytes*/ +} uivector; + +static void uivector_cleanup(void* p) +{ + ((uivector*)p)->size = ((uivector*)p)->allocsize = 0; + myfree(((uivector*)p)->data); + ((uivector*)p)->data = NULL; +} + +/*returns 1 if success, 0 if failure ==> nothing done*/ +static unsigned uivector_resize(uivector* p, size_t size) +{ + if(size * sizeof(unsigned) > p->allocsize) + { + size_t newsize = size * sizeof(unsigned) * 2; + void* data = myrealloc(p->data, newsize); + if(data) + { + p->allocsize = newsize; + p->data = (unsigned*)data; + p->size = size; + } + else return 0; + } + else p->size = size; + return 1; +} + +/*resize and give all new elements the value*/ +static unsigned uivector_resizev(uivector* p, size_t size, unsigned value) +{ + size_t oldsize = p->size, i; + if(!uivector_resize(p, size)) return 0; + for(i = oldsize; i < size; i++) p->data[i] = value; + return 1; +} + +static void uivector_init(uivector* p) +{ + p->data = NULL; + p->size = p->allocsize = 0; +} + +#ifdef LODEPNG_COMPILE_ENCODER +/*returns 1 if success, 0 if failure ==> nothing done*/ +static unsigned uivector_push_back(uivector* p, unsigned c) +{ + if(!uivector_resize(p, p->size + 1)) return 0; + p->data[p->size - 1] = c; + return 1; +} + +/*copy q to p, returns 1 if success, 0 if failure ==> nothing done*/ +static unsigned uivector_copy(uivector* p, const uivector* q) +{ + size_t i; + if(!uivector_resize(p, q->size)) return 0; + for(i = 0; i < q->size; i++) p->data[i] = q->data[i]; + return 1; +} + +static void uivector_swap(uivector* p, uivector* q) +{ + size_t tmp; + unsigned* tmpp; + tmp = p->size; p->size = q->size; q->size = tmp; + tmp = p->allocsize; p->allocsize = q->allocsize; q->allocsize = tmp; + tmpp = p->data; p->data = q->data; q->data = tmpp; +} +#endif /*LODEPNG_COMPILE_ENCODER*/ +#endif /*LODEPNG_COMPILE_ZLIB*/ + +/* /////////////////////////////////////////////////////////////////////////// */ + +/*dynamic vector of unsigned chars*/ +typedef struct ucvector +{ + unsigned char* data; + size_t size; /*used size*/ + size_t allocsize; /*allocated size*/ +} ucvector; + +/*returns 1 if success, 0 if failure ==> nothing done*/ +static unsigned ucvector_resize(ucvector* p, size_t size) +{ + if(size * sizeof(unsigned char) > p->allocsize) + { + size_t newsize = size * sizeof(unsigned char) * 2; + void* data = myrealloc(p->data, newsize); + if(data) + { + p->allocsize = newsize; + p->data = (unsigned char*)data; + p->size = size; + } + else return 0; /*error: not enough memory*/ + } + else p->size = size; + return 1; +} + +#ifdef LODEPNG_COMPILE_PNG + +static void ucvector_cleanup(void* p) +{ + ((ucvector*)p)->size = ((ucvector*)p)->allocsize = 0; + myfree(((ucvector*)p)->data); + ((ucvector*)p)->data = NULL; +} + +static void ucvector_init(ucvector* p) +{ + p->data = NULL; + p->size = p->allocsize = 0; +} + +#ifdef LODEPNG_COMPILE_DECODER +/*resize and give all new elements the value*/ +static unsigned ucvector_resizev(ucvector* p, size_t size, unsigned char value) +{ + size_t oldsize = p->size, i; + if(!ucvector_resize(p, size)) return 0; + for(i = oldsize; i < size; i++) p->data[i] = value; + return 1; +} +#endif /*LODEPNG_COMPILE_DECODER*/ +#endif /*LODEPNG_COMPILE_PNG*/ + +#ifdef LODEPNG_COMPILE_ZLIB +/*you can both convert from vector to buffer&size and vica versa. If you use +init_buffer to take over a buffer and size, it is not needed to use cleanup*/ +static void ucvector_init_buffer(ucvector* p, unsigned char* buffer, size_t size) +{ + p->data = buffer; + p->allocsize = p->size = size; +} +#endif /*LODEPNG_COMPILE_ZLIB*/ + +#if (defined(LODEPNG_COMPILE_PNG) && defined(LODEPNG_COMPILE_ANCILLARY_CHUNKS)) || defined(LODEPNG_COMPILE_ENCODER) +/*returns 1 if success, 0 if failure ==> nothing done*/ +static unsigned ucvector_push_back(ucvector* p, unsigned char c) +{ + if(!ucvector_resize(p, p->size + 1)) return 0; + p->data[p->size - 1] = c; + return 1; +} +#endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/ + + +/* ////////////////////////////////////////////////////////////////////////// */ + +#ifdef LODEPNG_COMPILE_PNG +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS +/*returns 1 if success, 0 if failure ==> nothing done*/ +static unsigned string_resize(char** out, size_t size) +{ + char* data = (char*)myrealloc(*out, size + 1); + if(data) + { + data[size] = 0; /*null termination char*/ + *out = data; + } + return data != 0; +} + +/*init a {char*, size_t} pair for use as string*/ +static void string_init(char** out) +{ + *out = NULL; + string_resize(out, 0); +} + +/*free the above pair again*/ +static void string_cleanup(char** out) +{ + myfree(*out); + *out = NULL; +} + +static void string_set(char** out, const char* in) +{ + size_t insize = strlen(in), i = 0; + if(string_resize(out, insize)) + { + for(i = 0; i < insize; i++) + { + (*out)[i] = in[i]; + } + } +} +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +#endif /*LODEPNG_COMPILE_PNG*/ + +/* ////////////////////////////////////////////////////////////////////////// */ + +unsigned lodepng_read32bitInt(const unsigned char* buffer) +{ + return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; +} + +#if defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER) +/*buffer must have at least 4 allocated bytes available*/ +static void lodepng_set32bitInt(unsigned char* buffer, unsigned value) +{ + buffer[0] = (unsigned char)((value >> 24) & 0xff); + buffer[1] = (unsigned char)((value >> 16) & 0xff); + buffer[2] = (unsigned char)((value >> 8) & 0xff); + buffer[3] = (unsigned char)((value ) & 0xff); +} +#endif /*defined(LODEPNG_COMPILE_PNG) || defined(LODEPNG_COMPILE_ENCODER)*/ + +#ifdef LODEPNG_COMPILE_ENCODER +static void lodepng_add32bitInt(ucvector* buffer, unsigned value) +{ + ucvector_resize(buffer, buffer->size + 4); /*todo: give error if resize failed*/ + lodepng_set32bitInt(&buffer->data[buffer->size - 4], value); +} +#endif /*LODEPNG_COMPILE_ENCODER*/ + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / File IO / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +#ifdef LODEPNG_COMPILE_DISK + +unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) +{ + FILE* file; + long size; + + /*provide some proper output values if error will happen*/ + *out = 0; + *outsize = 0; + + file = fopen(filename, "rb"); + if(!file) return 78; + + /*get filesize:*/ + fseek(file , 0 , SEEK_END); + size = ftell(file); + rewind(file); + + /*read contents of the file into the vector*/ + *outsize = 0; + *out = (unsigned char*)mymalloc((size_t)size); + if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file); + + fclose(file); + if(!(*out) && size) return 83; /*the above malloc failed*/ + return 0; +} + +/*write given buffer to the file, overwriting the file, it doesn't append to it.*/ +unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) +{ + FILE* file; + file = fopen(filename, "wb" ); + if(!file) return 79; + fwrite((char*)buffer , 1 , buffersize, file); + fclose(file); + return 0; +} + +#endif /*LODEPNG_COMPILE_DISK*/ + +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* // End of common code and tools. Begin of Zlib related code. // */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ + +#ifdef LODEPNG_COMPILE_ZLIB +#ifdef LODEPNG_COMPILE_ENCODER +/*TODO: this ignores potential out of memory errors*/ +static void addBitToStream(size_t* bitpointer, ucvector* bitstream, unsigned char bit) +{ + /*add a new byte at the end*/ + if((*bitpointer) % 8 == 0) ucvector_push_back(bitstream, (unsigned char)0); + /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/ + (bitstream->data[bitstream->size - 1]) |= (bit << ((*bitpointer) & 0x7)); + (*bitpointer)++; +} + +static void addBitsToStream(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits) +{ + size_t i; + for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> i) & 1)); +} + +static void addBitsToStreamReversed(size_t* bitpointer, ucvector* bitstream, unsigned value, size_t nbits) +{ + size_t i; + for(i = 0; i < nbits; i++) addBitToStream(bitpointer, bitstream, (unsigned char)((value >> (nbits - 1 - i)) & 1)); +} +#endif /*LODEPNG_COMPILE_ENCODER*/ + +#ifdef LODEPNG_COMPILE_DECODER + +#define READBIT(bitpointer, bitstream) ((bitstream[bitpointer >> 3] >> (bitpointer & 0x7)) & (unsigned char)1) + +static unsigned char readBitFromStream(size_t* bitpointer, const unsigned char* bitstream) +{ + unsigned char result = (unsigned char)(READBIT(*bitpointer, bitstream)); + (*bitpointer)++; + return result; +} + +static unsigned readBitsFromStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) +{ + unsigned result = 0, i; + for(i = 0; i < nbits; i++) + { + result += ((unsigned)READBIT(*bitpointer, bitstream)) << i; + (*bitpointer)++; + } + return result; +} +#endif /*LODEPNG_COMPILE_DECODER*/ + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Deflate - Huffman / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +#define FIRST_LENGTH_CODE_INDEX 257 +#define LAST_LENGTH_CODE_INDEX 285 +/*256 literals, the end code, some length codes, and 2 unused codes*/ +#define NUM_DEFLATE_CODE_SYMBOLS 288 +/*the distance codes have their own symbols, 30 used, 2 unused*/ +#define NUM_DISTANCE_SYMBOLS 32 +/*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/ +#define NUM_CODE_LENGTH_CODES 19 + +/*the base lengths represented by codes 257-285*/ +static const unsigned LENGTHBASE[29] + = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, + 67, 83, 99, 115, 131, 163, 195, 227, 258}; + +/*the extra bits used by codes 257-285 (added to base length)*/ +static const unsigned LENGTHEXTRA[29] + = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, + 4, 4, 4, 4, 5, 5, 5, 5, 0}; + +/*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/ +static const unsigned DISTANCEBASE[30] + = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, + 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; + +/*the extra bits of backwards distances (added to base)*/ +static const unsigned DISTANCEEXTRA[30] + = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, + 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; + +/*the order in which "code length alphabet code lengths" are stored, out of this +the huffman tree of the dynamic huffman tree lengths is generated*/ +static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES] + = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + +/* ////////////////////////////////////////////////////////////////////////// */ + +/* +Huffman tree struct, containing multiple representations of the tree +*/ +typedef struct HuffmanTree +{ + unsigned* tree2d; + unsigned* tree1d; + unsigned* lengths; /*the lengths of the codes of the 1d-tree*/ + unsigned maxbitlen; /*maximum number of bits a single code can get*/ + unsigned numcodes; /*number of symbols in the alphabet = number of codes*/ +} HuffmanTree; + +/*function used for debug purposes to draw the tree in ascii art with C++*/ +/*#include +static void HuffmanTree_draw(HuffmanTree* tree) +{ + std::cout << "tree. length: " << tree->numcodes << " maxbitlen: " << tree->maxbitlen << std::endl; + for(size_t i = 0; i < tree->tree1d.size; i++) + { + if(tree->lengths.data[i]) + std::cout << i << " " << tree->tree1d.data[i] << " " << tree->lengths.data[i] << std::endl; + } + std::cout << std::endl; +}*/ + +static void HuffmanTree_init(HuffmanTree* tree) +{ + tree->tree2d = 0; + tree->tree1d = 0; + tree->lengths = 0; +} + +static void HuffmanTree_cleanup(HuffmanTree* tree) +{ + myfree(tree->tree2d); + myfree(tree->tree1d); + myfree(tree->lengths); +} + +/*the tree representation used by the decoder. return value is error*/ +static unsigned HuffmanTree_make2DTree(HuffmanTree* tree) +{ + unsigned nodefilled = 0; /*up to which node it is filled*/ + unsigned treepos = 0; /*position in the tree (1 of the numcodes columns)*/ + unsigned n, i; + + tree->tree2d = (unsigned*)mymalloc(tree->numcodes * 2 * sizeof(unsigned)); + if(!tree->tree2d) return 83; /*alloc fail*/ + + /* + convert tree1d[] to tree2d[][]. In the 2D array, a value of 32767 means + uninited, a value >= numcodes is an address to another bit, a value < numcodes + is a code. The 2 rows are the 2 possible bit values (0 or 1), there are as + many columns as codes - 1. + A good huffmann tree has N * 2 - 1 nodes, of which N - 1 are internal nodes. + Here, the internal nodes are stored (what their 0 and 1 option point to). + There is only memory for such good tree currently, if there are more nodes + (due to too long length codes), error 55 will happen + */ + for(n = 0; n < tree->numcodes * 2; n++) + { + tree->tree2d[n] = 32767; /*32767 here means the tree2d isn't filled there yet*/ + } + + for(n = 0; n < tree->numcodes; n++) /*the codes*/ + { + for(i = 0; i < tree->lengths[n]; i++) /*the bits for this code*/ + { + unsigned char bit = (unsigned char)((tree->tree1d[n] >> (tree->lengths[n] - i - 1)) & 1); + if(treepos > tree->numcodes - 2) return 55; /*oversubscribed, see comment in lodepng_error_text*/ + if(tree->tree2d[2 * treepos + bit] == 32767) /*not yet filled in*/ + { + if(i + 1 == tree->lengths[n]) /*last bit*/ + { + tree->tree2d[2 * treepos + bit] = n; /*put the current code in it*/ + treepos = 0; + } + else + { + /*put address of the next step in here, first that address has to be found of course + (it's just nodefilled + 1)...*/ + nodefilled++; + /*addresses encoded with numcodes added to it*/ + tree->tree2d[2 * treepos + bit] = nodefilled + tree->numcodes; + treepos = nodefilled; + } + } + else treepos = tree->tree2d[2 * treepos + bit] - tree->numcodes; + } + } + + for(n = 0; n < tree->numcodes * 2; n++) + { + if(tree->tree2d[n] == 32767) tree->tree2d[n] = 0; /*remove possible remaining 32767's*/ + } + + return 0; +} + +/* +Second step for the ...makeFromLengths and ...makeFromFrequencies functions. +numcodes, lengths and maxbitlen must already be filled in correctly. return +value is error. +*/ +static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) +{ + uivector blcount; + uivector nextcode; + unsigned bits, n, error = 0; + + uivector_init(&blcount); + uivector_init(&nextcode); + + tree->tree1d = (unsigned*)mymalloc(tree->numcodes * sizeof(unsigned)); + if(!tree->tree1d) error = 83; /*alloc fail*/ + + if(!uivector_resizev(&blcount, tree->maxbitlen + 1, 0) + || !uivector_resizev(&nextcode, tree->maxbitlen + 1, 0)) + error = 83; /*alloc fail*/ + + if(!error) + { + /*step 1: count number of instances of each code length*/ + for(bits = 0; bits < tree->numcodes; bits++) blcount.data[tree->lengths[bits]]++; + /*step 2: generate the nextcode values*/ + for(bits = 1; bits <= tree->maxbitlen; bits++) + { + nextcode.data[bits] = (nextcode.data[bits - 1] + blcount.data[bits - 1]) << 1; + } + /*step 3: generate all the codes*/ + for(n = 0; n < tree->numcodes; n++) + { + if(tree->lengths[n] != 0) tree->tree1d[n] = nextcode.data[tree->lengths[n]]++; + } + } + + uivector_cleanup(&blcount); + uivector_cleanup(&nextcode); + + if(!error) return HuffmanTree_make2DTree(tree); + else return error; +} + +/* +given the code lengths (as stored in the PNG file), generate the tree as defined +by Deflate. maxbitlen is the maximum bits that a code in the tree can have. +return value is error. +*/ +static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen, + size_t numcodes, unsigned maxbitlen) +{ + unsigned i; + tree->lengths = (unsigned*)mymalloc(numcodes * sizeof(unsigned)); + if(!tree->lengths) return 83; /*alloc fail*/ + for(i = 0; i < numcodes; i++) tree->lengths[i] = bitlen[i]; + tree->numcodes = (unsigned)numcodes; /*number of symbols*/ + tree->maxbitlen = maxbitlen; + return HuffmanTree_makeFromLengths2(tree); +} + +#ifdef LODEPNG_COMPILE_ENCODER + +/* +A coin, this is the terminology used for the package-merge algorithm and the +coin collector's problem. This is used to generate the huffman tree. +A coin can be multiple coins (when they're merged) +*/ +typedef struct Coin +{ + uivector symbols; + float weight; /*the sum of all weights in this coin*/ +} Coin; + +static void coin_init(Coin* c) +{ + uivector_init(&c->symbols); +} + +/*argument c is void* so that this dtor can be given as function pointer to the vector resize function*/ +static void coin_cleanup(void* c) +{ + uivector_cleanup(&((Coin*)c)->symbols); +} + +static void coin_copy(Coin* c1, const Coin* c2) +{ + c1->weight = c2->weight; + uivector_copy(&c1->symbols, &c2->symbols); +} + +static void add_coins(Coin* c1, const Coin* c2) +{ + size_t i; + for(i = 0; i < c2->symbols.size; i++) uivector_push_back(&c1->symbols, c2->symbols.data[i]); + c1->weight += c2->weight; +} + +static void init_coins(Coin* coins, size_t num) +{ + size_t i; + for(i = 0; i < num; i++) coin_init(&coins[i]); +} + +static void cleanup_coins(Coin* coins, size_t num) +{ + size_t i; + for(i = 0; i < num; i++) coin_cleanup(&coins[i]); +} + +/* +This uses a simple combsort to sort the data. This function is not critical for +overall encoding speed and the data amount isn't that large. +*/ +static void sort_coins(Coin* data, size_t amount) +{ + size_t gap = amount; + unsigned char swapped = 0; + while((gap > 1) || swapped) + { + size_t i; + gap = (gap * 10) / 13; /*shrink factor 1.3*/ + if(gap == 9 || gap == 10) gap = 11; /*combsort11*/ + if(gap < 1) gap = 1; + swapped = 0; + for(i = 0; i < amount - gap; i++) + { + size_t j = i + gap; + if(data[j].weight < data[i].weight) + { + float temp = data[j].weight; data[j].weight = data[i].weight; data[i].weight = temp; + uivector_swap(&data[i].symbols, &data[j].symbols); + swapped = 1; + } + } + } +} + +static unsigned append_symbol_coins(Coin* coins, const unsigned* frequencies, unsigned numcodes, size_t sum) +{ + unsigned i; + unsigned j = 0; /*index of present symbols*/ + for(i = 0; i < numcodes; i++) + { + if(frequencies[i] != 0) /*only include symbols that are present*/ + { + coins[j].weight = frequencies[i] / (float)sum; + uivector_push_back(&coins[j].symbols, i); + j++; + } + } + return 0; +} + +unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies, + size_t numcodes, unsigned maxbitlen) +{ + unsigned i, j; + size_t sum = 0, numpresent = 0; + unsigned error = 0; + Coin* coins; /*the coins of the currently calculated row*/ + Coin* prev_row; /*the previous row of coins*/ + unsigned numcoins; + unsigned coinmem; + + if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/ + + for(i = 0; i < numcodes; i++) + { + if(frequencies[i] > 0) + { + numpresent++; + sum += frequencies[i]; + } + } + + for(i = 0; i < numcodes; i++) lengths[i] = 0; + + /*ensure at least two present symbols. There should be at least one symbol + according to RFC 1951 section 3.2.7. To decoders incorrectly require two. To + make these work as well ensure there are at least two symbols. The + Package-Merge code below also doesn't work correctly if there's only one + symbol, it'd give it the theoritical 0 bits but in practice zlib wants 1 bit*/ + if(numpresent == 0) + { + lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/ + } + else if(numpresent == 1) + { + for(i = 0; i < numcodes; i++) + { + if(frequencies[i]) + { + lengths[i] = 1; + lengths[i == 0 ? 1 : 0] = 1; + break; + } + } + } + else + { + /*Package-Merge algorithm represented by coin collector's problem + For every symbol, maxbitlen coins will be created*/ + + coinmem = numpresent * 2; /*max amount of coins needed with the current algo*/ + coins = (Coin*)mymalloc(sizeof(Coin) * coinmem); + prev_row = (Coin*)mymalloc(sizeof(Coin) * coinmem); + if(!coins || !prev_row) return 83; /*alloc fail*/ + init_coins(coins, coinmem); + init_coins(prev_row, coinmem); + + /*first row, lowest denominator*/ + error = append_symbol_coins(coins, frequencies, numcodes, sum); + numcoins = numpresent; + sort_coins(coins, numcoins); + if(!error) + { + unsigned numprev = 0; + for(j = 1; j <= maxbitlen && !error; j++) /*each of the remaining rows*/ + { + unsigned tempnum; + Coin* tempcoins; + /*swap prev_row and coins, and their amounts*/ + tempcoins = prev_row; prev_row = coins; coins = tempcoins; + tempnum = numprev; numprev = numcoins; numcoins = tempnum; + + cleanup_coins(coins, numcoins); + init_coins(coins, numcoins); + + numcoins = 0; + + /*fill in the merged coins of the previous row*/ + for(i = 0; i + 1 < numprev; i += 2) + { + /*merge prev_row[i] and prev_row[i + 1] into new coin*/ + Coin* coin = &coins[numcoins++]; + coin_copy(coin, &prev_row[i]); + add_coins(coin, &prev_row[i + 1]); + } + /*fill in all the original symbols again*/ + if(j < maxbitlen) + { + error = append_symbol_coins(coins + numcoins, frequencies, numcodes, sum); + numcoins += numpresent; + } + sort_coins(coins, numcoins); + } + } + + if(!error) + { + /*calculate the lenghts of each symbol, as the amount of times a coin of each symbol is used*/ + for(i = 0; i < numpresent - 1; i++) + { + Coin* coin = &coins[i]; + for(j = 0; j < coin->symbols.size; j++) lengths[coin->symbols.data[j]]++; + } + } + + cleanup_coins(coins, coinmem); + myfree(coins); + cleanup_coins(prev_row, coinmem); + myfree(prev_row); + } + + return error; +} + +/*Create the Huffman tree given the symbol frequencies*/ +static unsigned HuffmanTree_makeFromFrequencies(HuffmanTree* tree, const unsigned* frequencies, + size_t mincodes, size_t numcodes, unsigned maxbitlen) +{ + unsigned error = 0; + while(!frequencies[numcodes - 1] && numcodes > mincodes) numcodes--; /*trim zeroes*/ + tree->maxbitlen = maxbitlen; + tree->numcodes = (unsigned)numcodes; /*number of symbols*/ + tree->lengths = (unsigned*)myrealloc(tree->lengths, numcodes * sizeof(unsigned)); + if(!tree->lengths) return 83; /*alloc fail*/ + /*initialize all lengths to 0*/ + memset(tree->lengths, 0, numcodes * sizeof(unsigned)); + + error = lodepng_huffman_code_lengths(tree->lengths, frequencies, numcodes, maxbitlen); + if(!error) error = HuffmanTree_makeFromLengths2(tree); + return error; +} + +static unsigned HuffmanTree_getCode(const HuffmanTree* tree, unsigned index) +{ + return tree->tree1d[index]; +} + +static unsigned HuffmanTree_getLength(const HuffmanTree* tree, unsigned index) +{ + return tree->lengths[index]; +} +#endif /*LODEPNG_COMPILE_ENCODER*/ + +/*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/ +static unsigned generateFixedLitLenTree(HuffmanTree* tree) +{ + unsigned i, error = 0; + unsigned* bitlen = (unsigned*)mymalloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); + if(!bitlen) return 83; /*alloc fail*/ + + /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/ + for(i = 0; i <= 143; i++) bitlen[i] = 8; + for(i = 144; i <= 255; i++) bitlen[i] = 9; + for(i = 256; i <= 279; i++) bitlen[i] = 7; + for(i = 280; i <= 287; i++) bitlen[i] = 8; + + error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15); + + myfree(bitlen); + return error; +} + +/*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/ +static unsigned generateFixedDistanceTree(HuffmanTree* tree) +{ + unsigned i, error = 0; + unsigned* bitlen = (unsigned*)mymalloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); + if(!bitlen) return 83; /*alloc fail*/ + + /*there are 32 distance codes, but 30-31 are unused*/ + for(i = 0; i < NUM_DISTANCE_SYMBOLS; i++) bitlen[i] = 5; + error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15); + + myfree(bitlen); + return error; +} + +#ifdef LODEPNG_COMPILE_DECODER + +/* +returns the code, or (unsigned)(-1) if error happened +inbitlength is the length of the complete buffer, in bits (so its byte length times 8) +*/ +static unsigned huffmanDecodeSymbol(const unsigned char* in, size_t* bp, + const HuffmanTree* codetree, size_t inbitlength) +{ + unsigned treepos = 0, ct; + for(;;) + { + if(*bp >= inbitlength) return (unsigned)(-1); /*error: end of input memory reached without endcode*/ + /* + decode the symbol from the tree. The "readBitFromStream" code is inlined in + the expression below because this is the biggest bottleneck while decoding + */ + ct = codetree->tree2d[(treepos << 1) + READBIT(*bp, in)]; + (*bp)++; + if(ct < codetree->numcodes) return ct; /*the symbol is decoded, return it*/ + else treepos = ct - codetree->numcodes; /*symbol not yet decoded, instead move tree position*/ + + if(treepos >= codetree->numcodes) return (unsigned)(-1); /*error: it appeared outside the codetree*/ + } +} +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_DECODER + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Inflator (Decompressor) / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +/*get the tree of a deflated block with fixed tree, as specified in the deflate specification*/ +static void getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d) +{ + /*TODO: check for out of memory errors*/ + generateFixedLitLenTree(tree_ll); + generateFixedDistanceTree(tree_d); +} + +/*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/ +static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, + const unsigned char* in, size_t* bp, size_t inlength) +{ + /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/ + unsigned error = 0; + unsigned n, HLIT, HDIST, HCLEN, i; + size_t inbitlength = inlength * 8; + + /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/ + unsigned* bitlen_ll = 0; /*lit,len code lengths*/ + unsigned* bitlen_d = 0; /*dist code lengths*/ + /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/ + unsigned* bitlen_cl = 0; + HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/ + + if((*bp) >> 3 >= inlength - 2) return 49; /*error: the bit pointer is or will go past the memory*/ + + /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/ + HLIT = readBitsFromStream(bp, in, 5) + 257; + /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/ + HDIST = readBitsFromStream(bp, in, 5) + 1; + /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/ + HCLEN = readBitsFromStream(bp, in, 4) + 4; + + HuffmanTree_init(&tree_cl); + + while(!error) + { + /*read the code length codes out of 3 * (amount of code length codes) bits*/ + + bitlen_cl = (unsigned*)mymalloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned)); + if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/); + + for(i = 0; i < NUM_CODE_LENGTH_CODES; i++) + { + if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3); + else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/ + } + + error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7); + if(error) break; + + /*now we can use this tree to read the lengths for the tree that this function will return*/ + bitlen_ll = (unsigned*)mymalloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned)); + bitlen_d = (unsigned*)mymalloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned)); + if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/); + for(i = 0; i < NUM_DEFLATE_CODE_SYMBOLS; i++) bitlen_ll[i] = 0; + for(i = 0; i < NUM_DISTANCE_SYMBOLS; i++) bitlen_d[i] = 0; + + /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/ + i = 0; + while(i < HLIT + HDIST) + { + unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength); + if(code <= 15) /*a length code*/ + { + if(i < HLIT) bitlen_ll[i] = code; + else bitlen_d[i - HLIT] = code; + i++; + } + else if(code == 16) /*repeat previous*/ + { + unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/ + unsigned value; /*set value to the previous code*/ + + if(*bp >= inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ + if (i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/ + + replength += readBitsFromStream(bp, in, 2); + + if(i < HLIT + 1) value = bitlen_ll[i - 1]; + else value = bitlen_d[i - HLIT - 1]; + /*repeat this value in the next lengths*/ + for(n = 0; n < replength; n++) + { + if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/ + if(i < HLIT) bitlen_ll[i] = value; + else bitlen_d[i - HLIT] = value; + i++; + } + } + else if(code == 17) /*repeat "0" 3-10 times*/ + { + unsigned replength = 3; /*read in the bits that indicate repeat length*/ + if(*bp >= inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ + + replength += readBitsFromStream(bp, in, 3); + + /*repeat this value in the next lengths*/ + for(n = 0; n < replength; n++) + { + if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/ + + if(i < HLIT) bitlen_ll[i] = 0; + else bitlen_d[i - HLIT] = 0; + i++; + } + } + else if(code == 18) /*repeat "0" 11-138 times*/ + { + unsigned replength = 11; /*read in the bits that indicate repeat length*/ + if(*bp >= inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/ + + replength += readBitsFromStream(bp, in, 7); + + /*repeat this value in the next lengths*/ + for(n = 0; n < replength; n++) + { + if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/ + + if(i < HLIT) bitlen_ll[i] = 0; + else bitlen_d[i - HLIT] = 0; + i++; + } + } + else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ + { + if(code == (unsigned)(-1)) + { + /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol + (10=no endcode, 11=wrong jump outside of tree)*/ + error = (*bp) > inbitlength ? 10 : 11; + } + else error = 16; /*unexisting code, this can never happen*/ + break; + } + } + if(error) break; + + if(bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/ + + /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/ + error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15); + if(error) break; + error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15); + + break; /*end of error-while*/ + } + + myfree(bitlen_cl); + myfree(bitlen_ll); + myfree(bitlen_d); + HuffmanTree_cleanup(&tree_cl); + + return error; +} + +/*inflate a block with dynamic of fixed Huffman tree*/ +static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size_t* bp, + size_t* pos, size_t inlength, unsigned btype) +{ + unsigned error = 0; + HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/ + HuffmanTree tree_d; /*the huffman tree for distance codes*/ + size_t inbitlength = inlength * 8; + + HuffmanTree_init(&tree_ll); + HuffmanTree_init(&tree_d); + + if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d); + else if(btype == 2) error = getTreeInflateDynamic(&tree_ll, &tree_d, in, bp, inlength); + + while(!error) /*decode all symbols until end reached, breaks at end code*/ + { + /*code_ll is literal, length or end code*/ + unsigned code_ll = huffmanDecodeSymbol(in, bp, &tree_ll, inbitlength); + if(code_ll <= 255) /*literal symbol*/ + { + if((*pos) >= out->size) + { + /*reserve more room at once*/ + if(!ucvector_resize(out, ((*pos) + 1) * 2)) ERROR_BREAK(83 /*alloc fail*/); + } + out->data[(*pos)] = (unsigned char)(code_ll); + (*pos)++; + } + else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ + { + unsigned code_d, distance; + unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/ + size_t start, forward, backward, length; + + /*part 1: get length base*/ + length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX]; + + /*part 2: get extra bits and add the value of that to length*/ + numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX]; + if(*bp >= inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/ + length += readBitsFromStream(bp, in, numextrabits_l); + + /*part 3: get distance code*/ + code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength); + if(code_d > 29) + { + if(code_ll == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ + { + /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol + (10=no endcode, 11=wrong jump outside of tree)*/ + error = (*bp) > inlength * 8 ? 10 : 11; + } + else error = 18; /*error: invalid distance code (30-31 are never used)*/ + break; + } + distance = DISTANCEBASE[code_d]; + + /*part 4: get extra bits from distance*/ + numextrabits_d = DISTANCEEXTRA[code_d]; + if(*bp >= inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/ + + distance += readBitsFromStream(bp, in, numextrabits_d); + + /*part 5: fill in all the out[n] values based on the length and dist*/ + start = (*pos); + if(distance > start) ERROR_BREAK(52); /*too long backward distance*/ + backward = start - distance; + if((*pos) + length >= out->size) + { + /*reserve more room at once*/ + if(!ucvector_resize(out, ((*pos) + length) * 2)) ERROR_BREAK(83 /*alloc fail*/); + } + + for(forward = 0; forward < length; forward++) + { + out->data[(*pos)] = out->data[backward]; + (*pos)++; + backward++; + if(backward >= start) backward = start - distance; + } + } + else if(code_ll == 256) + { + break; /*end code, break the loop*/ + } + else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ + { + /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol + (10=no endcode, 11=wrong jump outside of tree)*/ + error = (*bp) > inlength * 8 ? 10 : 11; + break; + } + } + + HuffmanTree_cleanup(&tree_ll); + HuffmanTree_cleanup(&tree_d); + + return error; +} + +static unsigned inflateNoCompression(ucvector* out, const unsigned char* in, size_t* bp, size_t* pos, size_t inlength) +{ + /*go to first boundary of byte*/ + size_t p; + unsigned LEN, NLEN, n, error = 0; + while(((*bp) & 0x7) != 0) (*bp)++; + p = (*bp) / 8; /*byte position*/ + + /*read LEN (2 bytes) and NLEN (2 bytes)*/ + if(p >= inlength - 4) return 52; /*error, bit pointer will jump past memory*/ + LEN = in[p] + 256 * in[p + 1]; p += 2; + NLEN = in[p] + 256 * in[p + 1]; p += 2; + + /*check if 16-bit NLEN is really the one's complement of LEN*/ + if(LEN + NLEN != 65535) return 21; /*error: NLEN is not one's complement of LEN*/ + + if((*pos) + LEN >= out->size) + { + if(!ucvector_resize(out, (*pos) + LEN)) return 83; /*alloc fail*/ + } + + /*read the literal data: LEN bytes are now stored in the out buffer*/ + if(p + LEN > inlength) return 23; /*error: reading outside of in buffer*/ + for(n = 0; n < LEN; n++) out->data[(*pos)++] = in[p++]; + + (*bp) = p * 8; + + return error; +} + +static unsigned lodepng_inflatev(ucvector* out, + const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings) +{ + /*bit pointer in the "in" data, current byte is bp >> 3, current bit is bp & 0x7 (from lsb to msb of the byte)*/ + size_t bp = 0; + unsigned BFINAL = 0; + size_t pos = 0; /*byte position in the out buffer*/ + + unsigned error = 0; + + (void)settings; + + while(!BFINAL) + { + unsigned BTYPE; + if(bp + 2 >= insize * 8) return 52; /*error, bit pointer will jump past memory*/ + BFINAL = readBitFromStream(&bp, in); + BTYPE = 1 * readBitFromStream(&bp, in); + BTYPE += 2 * readBitFromStream(&bp, in); + + if(BTYPE == 3) return 20; /*error: invalid BTYPE*/ + else if(BTYPE == 0) error = inflateNoCompression(out, in, &bp, &pos, insize); /*no compression*/ + else error = inflateHuffmanBlock(out, in, &bp, &pos, insize, BTYPE); /*compression, BTYPE 01 or 10*/ + + if(error) return error; + } + + /*Only now we know the true size of out, resize it to that*/ + if(!ucvector_resize(out, pos)) error = 83; /*alloc fail*/ + + return error; +} + +unsigned lodepng_inflate(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings) +{ + unsigned error; + ucvector v; + ucvector_init_buffer(&v, *out, *outsize); + error = lodepng_inflatev(&v, in, insize, settings); + *out = v.data; + *outsize = v.size; + return error; +} + +static unsigned inflate(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings) +{ + if(settings->custom_inflate) + { + return settings->custom_inflate(out, outsize, in, insize, settings); + } + else + { + return lodepng_inflate(out, outsize, in, insize, settings); + } +} + +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_ENCODER + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Deflator (Compressor) / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +static const size_t MAX_SUPPORTED_DEFLATE_LENGTH = 258; + +/*bitlen is the size in bits of the code*/ +static void addHuffmanSymbol(size_t* bp, ucvector* compressed, unsigned code, unsigned bitlen) +{ + addBitsToStreamReversed(bp, compressed, code, bitlen); +} + +/*search the index in the array, that has the largest value smaller than or equal to the given value, +given array must be sorted (if no value is smaller, it returns the size of the given array)*/ +static size_t searchCodeIndex(const unsigned* array, size_t array_size, size_t value) +{ + /*linear search implementation*/ + /*for(size_t i = 1; i < array_size; i++) if(array[i] > value) return i - 1; + return array_size - 1;*/ + + /*binary search implementation (not that much faster) (precondition: array_size > 0)*/ + size_t left = 1; + size_t right = array_size - 1; + while(left <= right) + { + size_t mid = (left + right) / 2; + if(array[mid] <= value) left = mid + 1; /*the value to find is more to the right*/ + else if(array[mid - 1] > value) right = mid - 1; /*the value to find is more to the left*/ + else return mid - 1; + } + return array_size - 1; +} + +static void addLengthDistance(uivector* values, size_t length, size_t distance) +{ + /*values in encoded vector are those used by deflate: + 0-255: literal bytes + 256: end + 257-285: length/distance pair (length code, followed by extra length bits, distance code, extra distance bits) + 286-287: invalid*/ + + unsigned length_code = (unsigned)searchCodeIndex(LENGTHBASE, 29, length); + unsigned extra_length = (unsigned)(length - LENGTHBASE[length_code]); + unsigned dist_code = (unsigned)searchCodeIndex(DISTANCEBASE, 30, distance); + unsigned extra_distance = (unsigned)(distance - DISTANCEBASE[dist_code]); + + uivector_push_back(values, length_code + FIRST_LENGTH_CODE_INDEX); + uivector_push_back(values, extra_length); + uivector_push_back(values, dist_code); + uivector_push_back(values, extra_distance); +} + +static const unsigned HASH_NUM_VALUES = 65536; +static const unsigned HASH_NUM_CHARACTERS = 3; +static const unsigned HASH_SHIFT = 2; +/* +The HASH_NUM_CHARACTERS value is used to make encoding faster by using longer +sequences to generate a hash value from the stream bytes. Setting it to 3 +gives exactly the same compression as the brute force method, since deflate's +run length encoding starts with lengths of 3. Setting it to higher values, +like 6, can make the encoding faster (not always though!), but will cause the +encoding to miss any length between 3 and this value, so that the compression +may be worse (but this can vary too depending on the image, sometimes it is +even a bit better instead). +The HASH_NUM_VALUES is the amount of unique possible hash values that +combinations of bytes can give, the higher it is the more memory is needed, but +if it's too low the advantage of hashing is gone. +*/ + +typedef struct Hash +{ + int* head; /*hash value to head circular pos*/ + int* val; /*circular pos to hash value*/ + /*circular pos to prev circular pos*/ + unsigned short* chain; + unsigned short* zeros; +} Hash; + +static unsigned hash_init(Hash* hash, unsigned windowsize) +{ + unsigned i; + hash->head = (int*)mymalloc(sizeof(int) * HASH_NUM_VALUES); + hash->val = (int*)mymalloc(sizeof(int) * windowsize); + hash->chain = (unsigned short*)mymalloc(sizeof(unsigned short) * windowsize); + hash->zeros = (unsigned short*)mymalloc(sizeof(unsigned short) * windowsize); + + if(!hash->head || !hash->val || !hash->chain || !hash->zeros) return 83; /*alloc fail*/ + + /*initialize hash table*/ + for(i = 0; i < HASH_NUM_VALUES; i++) hash->head[i] = -1; + for(i = 0; i < windowsize; i++) hash->val[i] = -1; + for(i = 0; i < windowsize; i++) hash->chain[i] = i; /*same value as index indicates uninitialized*/ + + return 0; +} + +static void hash_cleanup(Hash* hash) +{ + myfree(hash->head); + myfree(hash->val); + myfree(hash->chain); + myfree(hash->zeros); +} + +static unsigned getHash(const unsigned char* data, size_t size, size_t pos) +{ + unsigned result = 0; + size_t amount, i; + if(pos >= size) return 0; + amount = HASH_NUM_CHARACTERS; + if(pos + amount >= size) amount = size - pos; + for(i = 0; i < amount; i++) result ^= (data[pos + i] << (i * HASH_SHIFT)); + return result % HASH_NUM_VALUES; +} + +static unsigned countZeros(const unsigned char* data, size_t size, size_t pos) +{ + const unsigned char* start = data + pos; + const unsigned char* end = start + MAX_SUPPORTED_DEFLATE_LENGTH; + if(end > data + size) end = data + size; + data = start; + while (data != end && *data == 0) data++; + /*subtracting two addresses returned as 32-bit number (max value is MAX_SUPPORTED_DEFLATE_LENGTH)*/ + return (unsigned)(data - start); +} + +static void updateHashChain(Hash* hash, size_t pos, int hashval, unsigned windowsize) +{ + unsigned wpos = pos % windowsize; + hash->val[wpos] = hashval; + if(hash->head[hashval] != -1) hash->chain[wpos] = hash->head[hashval]; + hash->head[hashval] = wpos; +} + +/* +LZ77-encode the data. Return value is error code. The input are raw bytes, the output +is in the form of unsigned integers with codes representing for example literal bytes, or +length/distance pairs. +It uses a hash table technique to let it encode faster. When doing LZ77 encoding, a +sliding window (of windowsize) is used, and all past bytes in that window can be used as +the "dictionary". A brute force search through all possible distances would be slow, and +this hash technique is one out of several ways to speed this up. +*/ +static unsigned encodeLZ77(uivector* out, Hash* hash, + const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize, + unsigned minmatch, unsigned nicematch, unsigned lazymatching) +{ + unsigned short numzeros = 0; + int usezeros = windowsize >= 8192; /*for small window size, the 'max chain length' optimization does a better job*/ + unsigned pos, i, error = 0; + /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/ + unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8; + unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64; + + if(!error) + { + unsigned offset; /*the offset represents the distance in LZ77 terminology*/ + unsigned length; + unsigned lazy = 0; + unsigned lazylength = 0, lazyoffset = 0; + unsigned hashval; + unsigned current_offset, current_length; + const unsigned char *lastptr, *foreptr, *backptr; + unsigned short hashpos, prevpos; + + for(pos = inpos; pos < insize; pos++) + { + size_t wpos = pos % windowsize; /*position for in 'circular' hash buffers*/ + + hashval = getHash(in, insize, pos); + updateHashChain(hash, pos, hashval, windowsize); + + if(usezeros && hashval == 0) + { + numzeros = countZeros(in, insize, pos); + hash->zeros[wpos] = numzeros; + } + + /*the length and offset found for the current position*/ + length = 0; + offset = 0; + + prevpos = hash->head[hashval]; + hashpos = hash->chain[prevpos]; + + lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH]; + + /*search for the longest string*/ + if(hash->val[wpos] == (int)hashval) + { + unsigned chainlength = 0; + for(;;) + { + /*stop when went completely around the circular buffer*/ + if(prevpos < wpos && hashpos > prevpos && hashpos <= wpos) break; + if(prevpos > wpos && (hashpos <= wpos || hashpos > prevpos)) break; + if(chainlength++ >= maxchainlength) break; + + current_offset = hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize; + if(current_offset > 0) + { + /*test the next characters*/ + foreptr = &in[pos]; + backptr = &in[pos - current_offset]; + + /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/ + if(usezeros && hashval == 0 && hash->val[hashpos] == 0 /*hashval[hashpos] may be out of date*/) + { + unsigned short skip = hash->zeros[hashpos]; + if(skip > numzeros) skip = numzeros; + backptr += skip; + foreptr += skip; + } + + /* multiple checks at once per array bounds check */ + while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/ + { + ++backptr; + ++foreptr; + } + current_length = (unsigned)(foreptr - &in[pos]); + + if(current_length > length) + { + length = current_length; /*the longest length*/ + offset = current_offset; /*the offset that is related to this longest length*/ + /*jump out once a length of max length is found (speed gain)*/ + if(current_length >= nicematch || current_length == MAX_SUPPORTED_DEFLATE_LENGTH) break; + } + } + + if(hashpos == hash->chain[hashpos]) break; + + prevpos = hashpos; + hashpos = hash->chain[hashpos]; + } + } + + if(lazymatching) + { + if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH) + { + lazy = 1; + lazylength = length; + lazyoffset = offset; + continue; /*try the next byte*/ + } + if(lazy) + { + lazy = 0; + if(pos == 0) ERROR_BREAK(81); + if(length > lazylength + 1) + { + /*push the previous character as literal*/ + if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/); + } + else + { + length = lazylength; + offset = lazyoffset; + hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/ + pos--; + } + } + } + if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/); + + /**encode it as length/distance pair or literal value**/ + if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/ + { + if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/); + } + else if(length < minmatch || (length == 3 && offset > 4096)) + { + /*compensate for the fact that longer offsets have more extra bits, a + length of only 3 may be not worth it then*/ + if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/); + } + else + { + addLengthDistance(out, length, offset); + for(i = 1; i < length; i++) + { + pos++; + hashval = getHash(in, insize, pos); + updateHashChain(hash, pos, hashval, windowsize); + if(usezeros && hashval == 0) + { + hash->zeros[pos % windowsize] = countZeros(in, insize, pos); + } + } + } + + } /*end of the loop through each character of input*/ + } /*end of "if(!error)"*/ + + return error; +} + +/* /////////////////////////////////////////////////////////////////////////// */ + +static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) +{ + /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte, + 2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/ + + size_t i, j, numdeflateblocks = (datasize + 65534) / 65535; + unsigned datapos = 0; + for(i = 0; i < numdeflateblocks; i++) + { + unsigned BFINAL, BTYPE, LEN, NLEN; + unsigned char firstbyte; + + BFINAL = (i == numdeflateblocks - 1); + BTYPE = 0; + + firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1) << 1) + ((BTYPE & 2) << 1)); + ucvector_push_back(out, firstbyte); + + LEN = 65535; + if(datasize - datapos < 65535) LEN = (unsigned)datasize - datapos; + NLEN = 65535 - LEN; + + ucvector_push_back(out, (unsigned char)(LEN % 256)); + ucvector_push_back(out, (unsigned char)(LEN / 256)); + ucvector_push_back(out, (unsigned char)(NLEN % 256)); + ucvector_push_back(out, (unsigned char)(NLEN / 256)); + + /*Decompressed data*/ + for(j = 0; j < 65535 && datapos < datasize; j++) + { + ucvector_push_back(out, data[datapos++]); + } + } + + return 0; +} + +/* +write the lz77-encoded data, which has lit, len and dist codes, to compressed stream using huffman trees. +tree_ll: the tree for lit and len codes. +tree_d: the tree for distance codes. +*/ +static void writeLZ77data(size_t* bp, ucvector* out, const uivector* lz77_encoded, + const HuffmanTree* tree_ll, const HuffmanTree* tree_d) +{ + size_t i = 0; + for(i = 0; i < lz77_encoded->size; i++) + { + unsigned val = lz77_encoded->data[i]; + addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val)); + if(val > 256) /*for a length code, 3 more things have to be added*/ + { + unsigned length_index = val - FIRST_LENGTH_CODE_INDEX; + unsigned n_length_extra_bits = LENGTHEXTRA[length_index]; + unsigned length_extra_bits = lz77_encoded->data[++i]; + + unsigned distance_code = lz77_encoded->data[++i]; + + unsigned distance_index = distance_code; + unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index]; + unsigned distance_extra_bits = lz77_encoded->data[++i]; + + addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits); + addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_d, distance_code), + HuffmanTree_getLength(tree_d, distance_code)); + addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits); + } + } +} + +/*Deflate for a block of type "dynamic", that is, with freely, optimally, created huffman trees*/ +static unsigned deflateDynamic(ucvector* out, size_t* bp, Hash* hash, + const unsigned char* data, size_t datapos, size_t dataend, + const LodePNGCompressSettings* settings, int final) +{ + unsigned error = 0; + + /* + A block is compressed as follows: The PNG data is lz77 encoded, resulting in + literal bytes and length/distance pairs. This is then huffman compressed with + two huffman trees. One huffman tree is used for the lit and len values ("ll"), + another huffman tree is used for the dist values ("d"). These two trees are + stored using their code lengths, and to compress even more these code lengths + are also run-length encoded and huffman compressed. This gives a huffman tree + of code lengths "cl". The code lenghts used to describe this third tree are + the code length code lengths ("clcl"). + */ + + /*The lz77 encoded data, represented with integers since there will also be length and distance codes in it*/ + uivector lz77_encoded; + HuffmanTree tree_ll; /*tree for lit,len values*/ + HuffmanTree tree_d; /*tree for distance codes*/ + HuffmanTree tree_cl; /*tree for encoding the code lengths representing tree_ll and tree_d*/ + uivector frequencies_ll; /*frequency of lit,len codes*/ + uivector frequencies_d; /*frequency of dist codes*/ + uivector frequencies_cl; /*frequency of code length codes*/ + uivector bitlen_lld; /*lit,len,dist code lenghts (int bits), literally (without repeat codes).*/ + uivector bitlen_lld_e; /*bitlen_lld encoded with repeat codes (this is a rudemtary run length compression)*/ + /*bitlen_cl is the code length code lengths ("clcl"). The bit lengths of codes to represent tree_cl + (these are written as is in the file, it would be crazy to compress these using yet another huffman + tree that needs to be represented by yet another set of code lengths)*/ + uivector bitlen_cl; + size_t datasize = dataend - datapos; + + /* + Due to the huffman compression of huffman tree representations ("two levels"), there are some anologies: + bitlen_lld is to tree_cl what data is to tree_ll and tree_d. + bitlen_lld_e is to bitlen_lld what lz77_encoded is to data. + bitlen_cl is to bitlen_lld_e what bitlen_lld is to lz77_encoded. + */ + + unsigned BFINAL = final; + size_t numcodes_ll, numcodes_d, i; + unsigned HLIT, HDIST, HCLEN; + + uivector_init(&lz77_encoded); + HuffmanTree_init(&tree_ll); + HuffmanTree_init(&tree_d); + HuffmanTree_init(&tree_cl); + uivector_init(&frequencies_ll); + uivector_init(&frequencies_d); + uivector_init(&frequencies_cl); + uivector_init(&bitlen_lld); + uivector_init(&bitlen_lld_e); + uivector_init(&bitlen_cl); + + /*This while loop never loops due to a break at the end, it is here to + allow breaking out of it to the cleanup phase on error conditions.*/ + while(!error) + { + if(settings->use_lz77) + { + error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, + settings->minmatch, settings->nicematch, settings->lazymatching); + if(error) break; + } + else + { + if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83 /*alloc fail*/); + for(i = datapos; i < dataend; i++) lz77_encoded.data[i] = data[i]; /*no LZ77, but still will be Huffman compressed*/ + } + + if(!uivector_resizev(&frequencies_ll, 286, 0)) ERROR_BREAK(83 /*alloc fail*/); + if(!uivector_resizev(&frequencies_d, 30, 0)) ERROR_BREAK(83 /*alloc fail*/); + + /*Count the frequencies of lit, len and dist codes*/ + for(i = 0; i < lz77_encoded.size; i++) + { + unsigned symbol = lz77_encoded.data[i]; + frequencies_ll.data[symbol]++; + if(symbol > 256) + { + unsigned dist = lz77_encoded.data[i + 2]; + frequencies_d.data[dist]++; + i += 3; + } + } + frequencies_ll.data[256] = 1; /*there will be exactly 1 end code, at the end of the block*/ + + /*Make both huffman trees, one for the lit and len codes, one for the dist codes*/ + error = HuffmanTree_makeFromFrequencies(&tree_ll, frequencies_ll.data, 257, frequencies_ll.size, 15); + if(error) break; + /*2, not 1, is chosen for mincodes: some buggy PNG decoders require at least 2 symbols in the dist tree*/ + error = HuffmanTree_makeFromFrequencies(&tree_d, frequencies_d.data, 2, frequencies_d.size, 15); + if(error) break; + + numcodes_ll = tree_ll.numcodes; if(numcodes_ll > 286) numcodes_ll = 286; + numcodes_d = tree_d.numcodes; if(numcodes_d > 30) numcodes_d = 30; + /*store the code lengths of both generated trees in bitlen_lld*/ + for(i = 0; i < numcodes_ll; i++) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_ll, (unsigned)i)); + for(i = 0; i < numcodes_d; i++) uivector_push_back(&bitlen_lld, HuffmanTree_getLength(&tree_d, (unsigned)i)); + + /*run-length compress bitlen_ldd into bitlen_lld_e by using repeat codes 16 (copy length 3-6 times), + 17 (3-10 zeroes), 18 (11-138 zeroes)*/ + for(i = 0; i < (unsigned)bitlen_lld.size; i++) + { + unsigned j = 0; /*amount of repititions*/ + while(i + j + 1 < (unsigned)bitlen_lld.size && bitlen_lld.data[i + j + 1] == bitlen_lld.data[i]) j++; + + if(bitlen_lld.data[i] == 0 && j >= 2) /*repeat code for zeroes*/ + { + j++; /*include the first zero*/ + if(j <= 10) /*repeat code 17 supports max 10 zeroes*/ + { + uivector_push_back(&bitlen_lld_e, 17); + uivector_push_back(&bitlen_lld_e, j - 3); + } + else /*repeat code 18 supports max 138 zeroes*/ + { + if(j > 138) j = 138; + uivector_push_back(&bitlen_lld_e, 18); + uivector_push_back(&bitlen_lld_e, j - 11); + } + i += (j - 1); + } + else if(j >= 3) /*repeat code for value other than zero*/ + { + size_t k; + unsigned num = j / 6, rest = j % 6; + uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]); + for(k = 0; k < num; k++) + { + uivector_push_back(&bitlen_lld_e, 16); + uivector_push_back(&bitlen_lld_e, 6 - 3); + } + if(rest >= 3) + { + uivector_push_back(&bitlen_lld_e, 16); + uivector_push_back(&bitlen_lld_e, rest - 3); + } + else j -= rest; + i += j; + } + else /*too short to benefit from repeat code*/ + { + uivector_push_back(&bitlen_lld_e, bitlen_lld.data[i]); + } + } + + /*generate tree_cl, the huffmantree of huffmantrees*/ + + if(!uivector_resizev(&frequencies_cl, NUM_CODE_LENGTH_CODES, 0)) ERROR_BREAK(83 /*alloc fail*/); + for(i = 0; i < bitlen_lld_e.size; i++) + { + frequencies_cl.data[bitlen_lld_e.data[i]]++; + /*after a repeat code come the bits that specify the number of repetitions, + those don't need to be in the frequencies_cl calculation*/ + if(bitlen_lld_e.data[i] >= 16) i++; + } + + error = HuffmanTree_makeFromFrequencies(&tree_cl, frequencies_cl.data, + frequencies_cl.size, frequencies_cl.size, 7); + if(error) break; + + if(!uivector_resize(&bitlen_cl, tree_cl.numcodes)) ERROR_BREAK(83 /*alloc fail*/); + for(i = 0; i < tree_cl.numcodes; i++) + { + /*lenghts of code length tree is in the order as specified by deflate*/ + bitlen_cl.data[i] = HuffmanTree_getLength(&tree_cl, CLCL_ORDER[i]); + } + while(bitlen_cl.data[bitlen_cl.size - 1] == 0 && bitlen_cl.size > 4) + { + /*remove zeros at the end, but minimum size must be 4*/ + if(!uivector_resize(&bitlen_cl, bitlen_cl.size - 1)) ERROR_BREAK(83 /*alloc fail*/); + } + if(error) break; + + /* + Write everything into the output + + After the BFINAL and BTYPE, the dynamic block consists out of the following: + - 5 bits HLIT, 5 bits HDIST, 4 bits HCLEN + - (HCLEN+4)*3 bits code lengths of code length alphabet + - HLIT + 257 code lenghts of lit/length alphabet (encoded using the code length + alphabet, + possible repetition codes 16, 17, 18) + - HDIST + 1 code lengths of distance alphabet (encoded using the code length + alphabet, + possible repetition codes 16, 17, 18) + - compressed data + - 256 (end code) + */ + + /*Write block type*/ + addBitToStream(bp, out, BFINAL); + addBitToStream(bp, out, 0); /*first bit of BTYPE "dynamic"*/ + addBitToStream(bp, out, 1); /*second bit of BTYPE "dynamic"*/ + + /*write the HLIT, HDIST and HCLEN values*/ + HLIT = (unsigned)(numcodes_ll - 257); + HDIST = (unsigned)(numcodes_d - 1); + HCLEN = (unsigned)bitlen_cl.size - 4; + /*trim zeroes for HCLEN. HLIT and HDIST were already trimmed at tree creation*/ + while(!bitlen_cl.data[HCLEN + 4 - 1] && HCLEN > 0) HCLEN--; + addBitsToStream(bp, out, HLIT, 5); + addBitsToStream(bp, out, HDIST, 5); + addBitsToStream(bp, out, HCLEN, 4); + + /*write the code lenghts of the code length alphabet*/ + for(i = 0; i < HCLEN + 4; i++) addBitsToStream(bp, out, bitlen_cl.data[i], 3); + + /*write the lenghts of the lit/len AND the dist alphabet*/ + for(i = 0; i < bitlen_lld_e.size; i++) + { + addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_cl, bitlen_lld_e.data[i]), + HuffmanTree_getLength(&tree_cl, bitlen_lld_e.data[i])); + /*extra bits of repeat codes*/ + if(bitlen_lld_e.data[i] == 16) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 2); + else if(bitlen_lld_e.data[i] == 17) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 3); + else if(bitlen_lld_e.data[i] == 18) addBitsToStream(bp, out, bitlen_lld_e.data[++i], 7); + } + + /*write the compressed data symbols*/ + writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d); + /*error: the length of the end code 256 must be larger than 0*/ + if(HuffmanTree_getLength(&tree_ll, 256) == 0) ERROR_BREAK(64); + + /*write the end code*/ + addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256)); + + break; /*end of error-while*/ + } + + /*cleanup*/ + uivector_cleanup(&lz77_encoded); + HuffmanTree_cleanup(&tree_ll); + HuffmanTree_cleanup(&tree_d); + HuffmanTree_cleanup(&tree_cl); + uivector_cleanup(&frequencies_ll); + uivector_cleanup(&frequencies_d); + uivector_cleanup(&frequencies_cl); + uivector_cleanup(&bitlen_lld_e); + uivector_cleanup(&bitlen_lld); + uivector_cleanup(&bitlen_cl); + + return error; +} + +static unsigned deflateFixed(ucvector* out, size_t* bp, Hash* hash, + const unsigned char* data, + size_t datapos, size_t dataend, + const LodePNGCompressSettings* settings, int final) +{ + HuffmanTree tree_ll; /*tree for literal values and length codes*/ + HuffmanTree tree_d; /*tree for distance codes*/ + + unsigned BFINAL = final; + unsigned error = 0; + size_t i; + + HuffmanTree_init(&tree_ll); + HuffmanTree_init(&tree_d); + + generateFixedLitLenTree(&tree_ll); + generateFixedDistanceTree(&tree_d); + + addBitToStream(bp, out, BFINAL); + addBitToStream(bp, out, 1); /*first bit of BTYPE*/ + addBitToStream(bp, out, 0); /*second bit of BTYPE*/ + + if(settings->use_lz77) /*LZ77 encoded*/ + { + uivector lz77_encoded; + uivector_init(&lz77_encoded); + error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize, + settings->minmatch, settings->nicematch, settings->lazymatching); + if(!error) writeLZ77data(bp, out, &lz77_encoded, &tree_ll, &tree_d); + uivector_cleanup(&lz77_encoded); + } + else /*no LZ77, but still will be Huffman compressed*/ + { + for(i = datapos; i < dataend; i++) + { + addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i])); + } + } + /*add END code*/ + if(!error) addHuffmanSymbol(bp, out, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256)); + + /*cleanup*/ + HuffmanTree_cleanup(&tree_ll); + HuffmanTree_cleanup(&tree_d); + + return error; +} + +static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize, + const LodePNGCompressSettings* settings) +{ + unsigned error = 0; + size_t i, blocksize, numdeflateblocks; + size_t bp = 0; /*the bit pointer*/ + Hash hash; + + if(settings->btype > 2) return 61; + else if(settings->btype == 0) return deflateNoCompression(out, in, insize); + else if(settings->btype == 1) blocksize = insize; + else /*if(settings->btype == 2)*/ + { + blocksize = insize / 8 + 8; + if(blocksize < 65535) blocksize = 65535; + } + + numdeflateblocks = (insize + blocksize - 1) / blocksize; + if(numdeflateblocks == 0) numdeflateblocks = 1; + + error = hash_init(&hash, settings->windowsize); + if(error) return error; + + for(i = 0; i < numdeflateblocks && !error; i++) + { + int final = i == numdeflateblocks - 1; + size_t start = i * blocksize; + size_t end = start + blocksize; + if(end > insize) end = insize; + + if(settings->btype == 1) error = deflateFixed(out, &bp, &hash, in, start, end, settings, final); + else if(settings->btype == 2) error = deflateDynamic(out, &bp, &hash, in, start, end, settings, final); + } + + hash_cleanup(&hash); + + return error; +} + +unsigned lodepng_deflate(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGCompressSettings* settings) +{ + unsigned error; + ucvector v; + ucvector_init_buffer(&v, *out, *outsize); + error = lodepng_deflatev(&v, in, insize, settings); + *out = v.data; + *outsize = v.size; + return error; +} + +static unsigned deflate(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGCompressSettings* settings) +{ + if(settings->custom_deflate) + { + return settings->custom_deflate(out, outsize, in, insize, settings); + } + else + { + return lodepng_deflate(out, outsize, in, insize, settings); + } +} + +#endif /*LODEPNG_COMPILE_DECODER*/ + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Adler32 */ +/* ////////////////////////////////////////////////////////////////////////// */ + +static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len) +{ + unsigned s1 = adler & 0xffff; + unsigned s2 = (adler >> 16) & 0xffff; + + while(len > 0) + { + /*at least 5550 sums can be done before the sums overflow, saving a lot of module divisions*/ + unsigned amount = len > 5550 ? 5550 : len; + len -= amount; + while(amount > 0) + { + s1 += (*data++); + s2 += s1; + amount--; + } + s1 %= 65521; + s2 %= 65521; + } + + return (s2 << 16) | s1; +} + +/*Return the adler32 of the bytes data[0..len-1]*/ +static unsigned adler32(const unsigned char* data, unsigned len) +{ + return update_adler32(1L, data, len); +} + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Zlib / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +#ifdef LODEPNG_COMPILE_DECODER + +unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGDecompressSettings* settings) +{ + unsigned error = 0; + unsigned CM, CINFO, FDICT; + + if(insize < 2) return 53; /*error, size of zlib data too small*/ + /*read information from zlib header*/ + if((in[0] * 256 + in[1]) % 31 != 0) + { + /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/ + return 24; + } + + CM = in[0] & 15; + CINFO = (in[0] >> 4) & 15; + /*FCHECK = in[1] & 31;*/ /*FCHECK is already tested above*/ + FDICT = (in[1] >> 5) & 1; + /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/ + + if(CM != 8 || CINFO > 7) + { + /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/ + return 25; + } + if(FDICT != 0) + { + /*error: the specification of PNG says about the zlib stream: + "The additional flags shall not specify a preset dictionary."*/ + return 26; + } + + error = inflate(out, outsize, in + 2, insize - 2, settings); + if(error) return error; + + if(!settings->ignore_adler32) + { + unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]); + unsigned checksum = adler32(*out, (unsigned)(*outsize)); + if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/ + } + + return 0; /*no error*/ +} + +static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGDecompressSettings* settings) +{ + if(settings->custom_zlib) + { + return settings->custom_zlib(out, outsize, in, insize, settings); + } + else + { + return lodepng_zlib_decompress(out, outsize, in, insize, settings); + } +} + +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_ENCODER + +unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGCompressSettings* settings) +{ + /*initially, *out must be NULL and outsize 0, if you just give some random *out + that's pointing to a non allocated buffer, this'll crash*/ + ucvector outv; + size_t i; + unsigned error; + unsigned char* deflatedata = 0; + size_t deflatesize = 0; + + unsigned ADLER32; + /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/ + unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/ + unsigned FLEVEL = 0; + unsigned FDICT = 0; + unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64; + unsigned FCHECK = 31 - CMFFLG % 31; + CMFFLG += FCHECK; + + /*ucvector-controlled version of the output buffer, for dynamic array*/ + ucvector_init_buffer(&outv, *out, *outsize); + + ucvector_push_back(&outv, (unsigned char)(CMFFLG / 256)); + ucvector_push_back(&outv, (unsigned char)(CMFFLG % 256)); + + error = deflate(&deflatedata, &deflatesize, in, insize, settings); + + if(!error) + { + ADLER32 = adler32(in, (unsigned)insize); + for(i = 0; i < deflatesize; i++) ucvector_push_back(&outv, deflatedata[i]); + free(deflatedata); + lodepng_add32bitInt(&outv, ADLER32); + } + + *out = outv.data; + *outsize = outv.size; + + return error; +} + +/* compress using the default or custom zlib function */ +static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGCompressSettings* settings) +{ + if(settings->custom_zlib) + { + return settings->custom_zlib(out, outsize, in, insize, settings); + } + else + { + return lodepng_zlib_compress(out, outsize, in, insize, settings); + } +} + +#endif /*LODEPNG_COMPILE_ENCODER*/ + +#else /*no LODEPNG_COMPILE_ZLIB*/ + +#ifdef LODEPNG_COMPILE_DECODER +static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGDecompressSettings* settings) +{ + if (!settings->custom_zlib) return 87; /*no custom zlib function provided */ + return settings->custom_zlib(out, outsize, in, insize, settings); +} +#endif /*LODEPNG_COMPILE_DECODER*/ +#ifdef LODEPNG_COMPILE_ENCODER +static unsigned zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, + size_t insize, const LodePNGCompressSettings* settings) +{ + if (!settings->custom_zlib) return 87; /*no custom zlib function provided */ + return settings->custom_zlib(out, outsize, in, insize, settings); +} +#endif /*LODEPNG_COMPILE_ENCODER*/ + +#endif /*LODEPNG_COMPILE_ZLIB*/ + +/* ////////////////////////////////////////////////////////////////////////// */ + +#ifdef LODEPNG_COMPILE_ENCODER + +/*this is a good tradeoff between speed and compression ratio*/ +#define DEFAULT_WINDOWSIZE 2048 + +void lodepng_compress_settings_init(LodePNGCompressSettings* settings) +{ + /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/ + settings->btype = 2; + settings->use_lz77 = 1; + settings->windowsize = DEFAULT_WINDOWSIZE; + settings->minmatch = 3; + settings->nicematch = 128; + settings->lazymatching = 1; + + settings->custom_zlib = 0; + settings->custom_deflate = 0; + settings->custom_context = 0; +} + +const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT_WINDOWSIZE, 3, 128, 1, 0, 0, 0}; + + +#endif /*LODEPNG_COMPILE_ENCODER*/ + +#ifdef LODEPNG_COMPILE_DECODER + +void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings) +{ + settings->ignore_adler32 = 0; + + settings->custom_zlib = 0; + settings->custom_inflate = 0; + settings->custom_context = 0; +} + +const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0, 0}; + +#endif /*LODEPNG_COMPILE_DECODER*/ + +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* // End of Zlib related code. Begin of PNG related code. // */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ + +#ifdef LODEPNG_COMPILE_PNG + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / CRC32 / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +static unsigned Crc32_crc_table_computed = 0; +static unsigned Crc32_crc_table[256]; + +/*Make the table for a fast CRC.*/ +static void Crc32_make_crc_table(void) +{ + unsigned c, k, n; + for(n = 0; n < 256; n++) + { + c = n; + for(k = 0; k < 8; k++) + { + if(c & 1) c = 0xedb88320L ^ (c >> 1); + else c = c >> 1; + } + Crc32_crc_table[n] = c; + } + Crc32_crc_table_computed = 1; +} + +/*Update a running CRC with the bytes buf[0..len-1]--the CRC should be +initialized to all 1's, and the transmitted value is the 1's complement of the +final running CRC (see the crc() routine below).*/ +static unsigned Crc32_update_crc(const unsigned char* buf, unsigned crc, size_t len) +{ + unsigned c = crc; + size_t n; + + if(!Crc32_crc_table_computed) Crc32_make_crc_table(); + for(n = 0; n < len; n++) + { + c = Crc32_crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); + } + return c; +} + +/*Return the CRC of the bytes buf[0..len-1].*/ +unsigned lodepng_crc32(const unsigned char* buf, size_t len) +{ + return Crc32_update_crc(buf, 0xffffffffL, len) ^ 0xffffffffL; +} + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Reading and writing single bits and bytes from/to stream for LodePNG / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) +{ + unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1); + (*bitpointer)++; + return result; +} + +static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) +{ + unsigned result = 0; + size_t i; + for(i = nbits - 1; i < nbits; i--) + { + result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i; + } + return result; +} + +#ifdef LODEPNG_COMPILE_DECODER +static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) +{ + /*the current bit in bitstream must be 0 for this to work*/ + if(bit) + { + /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/ + bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7))); + } + (*bitpointer)++; +} +#endif /*LODEPNG_COMPILE_DECODER*/ + +static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) +{ + /*the current bit in bitstream may be 0 or 1 for this to work*/ + if(bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7)))); + else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7))); + (*bitpointer)++; +} + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / PNG chunks / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +unsigned lodepng_chunk_length(const unsigned char* chunk) +{ + return lodepng_read32bitInt(&chunk[0]); +} + +void lodepng_chunk_type(char type[5], const unsigned char* chunk) +{ + unsigned i; + for(i = 0; i < 4; i++) type[i] = chunk[4 + i]; + type[4] = 0; /*null termination char*/ +} + +unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type) +{ + if(strlen(type) != 4) return 0; + return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]); +} + +unsigned char lodepng_chunk_ancillary(const unsigned char* chunk) +{ + return((chunk[4] & 32) != 0); +} + +unsigned char lodepng_chunk_private(const unsigned char* chunk) +{ + return((chunk[6] & 32) != 0); +} + +unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk) +{ + return((chunk[7] & 32) != 0); +} + +unsigned char* lodepng_chunk_data(unsigned char* chunk) +{ + return &chunk[8]; +} + +const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk) +{ + return &chunk[8]; +} + +unsigned lodepng_chunk_check_crc(const unsigned char* chunk) +{ + unsigned length = lodepng_chunk_length(chunk); + unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]); + /*the CRC is taken of the data and the 4 chunk type letters, not the length*/ + unsigned checksum = lodepng_crc32(&chunk[4], length + 4); + if(CRC != checksum) return 1; + else return 0; +} + +void lodepng_chunk_generate_crc(unsigned char* chunk) +{ + unsigned length = lodepng_chunk_length(chunk); + unsigned CRC = lodepng_crc32(&chunk[4], length + 4); + lodepng_set32bitInt(chunk + 8 + length, CRC); +} + +unsigned char* lodepng_chunk_next(unsigned char* chunk) +{ + unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; + return &chunk[total_chunk_length]; +} + +const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk) +{ + unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; + return &chunk[total_chunk_length]; +} + +unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk) +{ + unsigned i; + unsigned total_chunk_length = lodepng_chunk_length(chunk) + 12; + unsigned char *chunk_start, *new_buffer; + size_t new_length = (*outlength) + total_chunk_length; + if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/ + + new_buffer = (unsigned char*)myrealloc(*out, new_length); + if(!new_buffer) return 83; /*alloc fail*/ + (*out) = new_buffer; + (*outlength) = new_length; + chunk_start = &(*out)[new_length - total_chunk_length]; + + for(i = 0; i < total_chunk_length; i++) chunk_start[i] = chunk[i]; + + return 0; +} + +unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, + const char* type, const unsigned char* data) +{ + unsigned i; + unsigned char *chunk, *new_buffer; + size_t new_length = (*outlength) + length + 12; + if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/ + new_buffer = (unsigned char*)myrealloc(*out, new_length); + if(!new_buffer) return 83; /*alloc fail*/ + (*out) = new_buffer; + (*outlength) = new_length; + chunk = &(*out)[(*outlength) - length - 12]; + + /*1: length*/ + lodepng_set32bitInt(chunk, (unsigned)length); + + /*2: chunk name (4 letters)*/ + chunk[4] = type[0]; + chunk[5] = type[1]; + chunk[6] = type[2]; + chunk[7] = type[3]; + + /*3: the data*/ + for(i = 0; i < length; i++) chunk[8 + i] = data[i]; + + /*4: CRC (of the chunkname characters and the data)*/ + lodepng_chunk_generate_crc(chunk); + + return 0; +} + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / Color types and such / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +/*return type is a LodePNG error code*/ +static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd) /*bd = bitdepth*/ +{ + switch(colortype) + { + case 0: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/ + case 2: if(!( bd == 8 || bd == 16)) return 37; break; /*RGB*/ + case 3: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/ + case 4: if(!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/ + case 6: if(!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/ + default: return 31; + } + return 0; /*allowed color type / bits combination*/ +} + +static unsigned getNumColorChannels(LodePNGColorType colortype) +{ + switch(colortype) + { + case 0: return 1; /*grey*/ + case 2: return 3; /*RGB*/ + case 3: return 1; /*palette*/ + case 4: return 2; /*grey + alpha*/ + case 6: return 4; /*RGBA*/ + } + return 0; /*unexisting color type*/ +} + +static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth) +{ + /*bits per pixel is amount of channels * bits per channel*/ + return getNumColorChannels(colortype) * bitdepth; +} + +/* ////////////////////////////////////////////////////////////////////////// */ + +void lodepng_color_mode_init(LodePNGColorMode* info) +{ + info->key_defined = 0; + info->key_r = info->key_g = info->key_b = 0; + info->colortype = LCT_RGBA; + info->bitdepth = 8; + info->palette = 0; + info->palettesize = 0; +} + +void lodepng_color_mode_cleanup(LodePNGColorMode* info) +{ + lodepng_palette_clear(info); +} + +unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source) +{ + size_t i; + lodepng_color_mode_cleanup(dest); + *dest = *source; + if(source->palette) + { + dest->palette = (unsigned char*)mymalloc(source->palettesize * 4); + if(!dest->palette && source->palettesize) return 83; /*alloc fail*/ + for(i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i]; + } + return 0; +} + +static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b) +{ + size_t i; + if(a->colortype != b->colortype) return 0; + if(a->bitdepth != b->bitdepth) return 0; + if(a->key_defined != b->key_defined) return 0; + if(a->key_defined) + { + if(a->key_r != b->key_r) return 0; + if(a->key_g != b->key_g) return 0; + if(a->key_b != b->key_b) return 0; + } + if(a->palettesize != b->palettesize) return 0; + for(i = 0; i < a->palettesize * 4; i++) + { + if(a->palette[i] != b->palette[i]) return 0; + } + return 1; +} + +void lodepng_palette_clear(LodePNGColorMode* info) +{ + if(info->palette) myfree(info->palette); + info->palettesize = 0; +} + +unsigned lodepng_palette_add(LodePNGColorMode* info, + unsigned char r, unsigned char g, unsigned char b, unsigned char a) +{ + unsigned char* data; + /*the same resize technique as C++ std::vectors is used, and here it's made so that for a palette with + the max of 256 colors, it'll have the exact alloc size*/ + if(!(info->palettesize & (info->palettesize - 1))) /*if palettesize is 0 or a power of two*/ + { + /*allocated data must be at least 4* palettesize (for 4 color bytes)*/ + size_t alloc_size = info->palettesize == 0 ? 4 : info->palettesize * 4 * 2; + data = (unsigned char*)myrealloc(info->palette, alloc_size); + if(!data) return 83; /*alloc fail*/ + else info->palette = data; + } + info->palette[4 * info->palettesize + 0] = r; + info->palette[4 * info->palettesize + 1] = g; + info->palette[4 * info->palettesize + 2] = b; + info->palette[4 * info->palettesize + 3] = a; + info->palettesize++; + return 0; +} + +unsigned lodepng_get_bpp(const LodePNGColorMode* info) +{ + /*calculate bits per pixel out of colortype and bitdepth*/ + return lodepng_get_bpp_lct(info->colortype, info->bitdepth); +} + +unsigned lodepng_get_channels(const LodePNGColorMode* info) +{ + return getNumColorChannels(info->colortype); +} + +unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info) +{ + return info->colortype == LCT_GREY || info->colortype == LCT_GREY_ALPHA; +} + +unsigned lodepng_is_alpha_type(const LodePNGColorMode* info) +{ + return (info->colortype & 4) != 0; /*4 or 6*/ +} + +unsigned lodepng_is_palette_type(const LodePNGColorMode* info) +{ + return info->colortype == LCT_PALETTE; +} + +unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info) +{ + size_t i; + for(i = 0; i < info->palettesize; i++) + { + if(info->palette[i * 4 + 3] < 255) return 1; + } + return 0; +} + +unsigned lodepng_can_have_alpha(const LodePNGColorMode* info) +{ + return info->key_defined + || lodepng_is_alpha_type(info) + || lodepng_has_palette_alpha(info); +} + +size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color) +{ + return (w * h * lodepng_get_bpp(color) + 7) / 8; +} + +size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) +{ + return (w * h * lodepng_get_bpp_lct(colortype, bitdepth) + 7) / 8; +} + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + +static void LodePNGUnknownChunks_init(LodePNGInfo* info) +{ + unsigned i; + for(i = 0; i < 3; i++) info->unknown_chunks_data[i] = 0; + for(i = 0; i < 3; i++) info->unknown_chunks_size[i] = 0; +} + +static void LodePNGUnknownChunks_cleanup(LodePNGInfo* info) +{ + unsigned i; + for(i = 0; i < 3; i++) myfree(info->unknown_chunks_data[i]); +} + +static unsigned LodePNGUnknownChunks_copy(LodePNGInfo* dest, const LodePNGInfo* src) +{ + unsigned i; + + LodePNGUnknownChunks_cleanup(dest); + + for(i = 0; i < 3; i++) + { + size_t j; + dest->unknown_chunks_size[i] = src->unknown_chunks_size[i]; + dest->unknown_chunks_data[i] = (unsigned char*)mymalloc(src->unknown_chunks_size[i]); + if(!dest->unknown_chunks_data[i] && dest->unknown_chunks_size[i]) return 83; /*alloc fail*/ + for(j = 0; j < src->unknown_chunks_size[i]; j++) + { + dest->unknown_chunks_data[i][j] = src->unknown_chunks_data[i][j]; + } + } + + return 0; +} + +/******************************************************************************/ + +static void LodePNGText_init(LodePNGInfo* info) +{ + info->text_num = 0; + info->text_keys = NULL; + info->text_strings = NULL; +} + +static void LodePNGText_cleanup(LodePNGInfo* info) +{ + size_t i; + for(i = 0; i < info->text_num; i++) + { + string_cleanup(&info->text_keys[i]); + string_cleanup(&info->text_strings[i]); + } + myfree(info->text_keys); + myfree(info->text_strings); +} + +static unsigned LodePNGText_copy(LodePNGInfo* dest, const LodePNGInfo* source) +{ + size_t i = 0; + dest->text_keys = 0; + dest->text_strings = 0; + dest->text_num = 0; + for(i = 0; i < source->text_num; i++) + { + CERROR_TRY_RETURN(lodepng_add_text(dest, source->text_keys[i], source->text_strings[i])); + } + return 0; +} + +void lodepng_clear_text(LodePNGInfo* info) +{ + LodePNGText_cleanup(info); +} + +unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str) +{ + char** new_keys = (char**)(myrealloc(info->text_keys, sizeof(char*) * (info->text_num + 1))); + char** new_strings = (char**)(myrealloc(info->text_strings, sizeof(char*) * (info->text_num + 1))); + if(!new_keys || !new_strings) + { + myfree(new_keys); + myfree(new_strings); + return 83; /*alloc fail*/ + } + + info->text_num++; + info->text_keys = new_keys; + info->text_strings = new_strings; + + string_init(&info->text_keys[info->text_num - 1]); + string_set(&info->text_keys[info->text_num - 1], key); + + string_init(&info->text_strings[info->text_num - 1]); + string_set(&info->text_strings[info->text_num - 1], str); + + return 0; +} + +/******************************************************************************/ + +static void LodePNGIText_init(LodePNGInfo* info) +{ + info->itext_num = 0; + info->itext_keys = NULL; + info->itext_langtags = NULL; + info->itext_transkeys = NULL; + info->itext_strings = NULL; +} + +static void LodePNGIText_cleanup(LodePNGInfo* info) +{ + size_t i; + for(i = 0; i < info->itext_num; i++) + { + string_cleanup(&info->itext_keys[i]); + string_cleanup(&info->itext_langtags[i]); + string_cleanup(&info->itext_transkeys[i]); + string_cleanup(&info->itext_strings[i]); + } + myfree(info->itext_keys); + myfree(info->itext_langtags); + myfree(info->itext_transkeys); + myfree(info->itext_strings); +} + +static unsigned LodePNGIText_copy(LodePNGInfo* dest, const LodePNGInfo* source) +{ + size_t i = 0; + dest->itext_keys = 0; + dest->itext_langtags = 0; + dest->itext_transkeys = 0; + dest->itext_strings = 0; + dest->itext_num = 0; + for(i = 0; i < source->itext_num; i++) + { + CERROR_TRY_RETURN(lodepng_add_itext(dest, source->itext_keys[i], source->itext_langtags[i], + source->itext_transkeys[i], source->itext_strings[i])); + } + return 0; +} + +void lodepng_clear_itext(LodePNGInfo* info) +{ + LodePNGIText_cleanup(info); +} + +unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, + const char* transkey, const char* str) +{ + char** new_keys = (char**)(myrealloc(info->itext_keys, sizeof(char*) * (info->itext_num + 1))); + char** new_langtags = (char**)(myrealloc(info->itext_langtags, sizeof(char*) * (info->itext_num + 1))); + char** new_transkeys = (char**)(myrealloc(info->itext_transkeys, sizeof(char*) * (info->itext_num + 1))); + char** new_strings = (char**)(myrealloc(info->itext_strings, sizeof(char*) * (info->itext_num + 1))); + if(!new_keys || !new_langtags || !new_transkeys || !new_strings) + { + myfree(new_keys); + myfree(new_langtags); + myfree(new_transkeys); + myfree(new_strings); + return 83; /*alloc fail*/ + } + + info->itext_num++; + info->itext_keys = new_keys; + info->itext_langtags = new_langtags; + info->itext_transkeys = new_transkeys; + info->itext_strings = new_strings; + + string_init(&info->itext_keys[info->itext_num - 1]); + string_set(&info->itext_keys[info->itext_num - 1], key); + + string_init(&info->itext_langtags[info->itext_num - 1]); + string_set(&info->itext_langtags[info->itext_num - 1], langtag); + + string_init(&info->itext_transkeys[info->itext_num - 1]); + string_set(&info->itext_transkeys[info->itext_num - 1], transkey); + + string_init(&info->itext_strings[info->itext_num - 1]); + string_set(&info->itext_strings[info->itext_num - 1], str); + + return 0; +} +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +void lodepng_info_init(LodePNGInfo* info) +{ + lodepng_color_mode_init(&info->color); + info->interlace_method = 0; + info->compression_method = 0; + info->filter_method = 0; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + info->background_defined = 0; + info->background_r = info->background_g = info->background_b = 0; + + LodePNGText_init(info); + LodePNGIText_init(info); + + info->time_defined = 0; + info->phys_defined = 0; + + LodePNGUnknownChunks_init(info); +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +} + +void lodepng_info_cleanup(LodePNGInfo* info) +{ + lodepng_color_mode_cleanup(&info->color); +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + LodePNGText_cleanup(info); + LodePNGIText_cleanup(info); + + LodePNGUnknownChunks_cleanup(info); +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +} + +unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) +{ + lodepng_info_cleanup(dest); + *dest = *source; + lodepng_color_mode_init(&dest->color); + CERROR_TRY_RETURN(lodepng_color_mode_copy(&dest->color, &source->color)); + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + CERROR_TRY_RETURN(LodePNGText_copy(dest, source)); + CERROR_TRY_RETURN(LodePNGIText_copy(dest, source)); + + LodePNGUnknownChunks_init(dest); + CERROR_TRY_RETURN(LodePNGUnknownChunks_copy(dest, source)); +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + return 0; +} + +void lodepng_info_swap(LodePNGInfo* a, LodePNGInfo* b) +{ + LodePNGInfo temp = *a; + *a = *b; + *b = temp; +} + +/* ////////////////////////////////////////////////////////////////////////// */ + +/*index: bitgroup index, bits: bitgroup size(1, 2 or 4, in: bitgroup value, out: octet array to add bits to*/ +static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in) +{ + /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/ + unsigned p = index % (8 / bits); + in &= (1 << bits) - 1; /*filter out any other bits of the input value*/ + in = in << (bits * (8 / bits - p - 1)); + if(p == 0) out[index * bits / 8] = in; + else out[index * bits / 8] |= in; +} + +typedef struct ColorTree ColorTree; + +/* +One node of a color tree +This is the data structure used to count the number of unique colors and to get a palette +index for a color. It's like an octree, but because the alpha channel is used too, each +node has 16 instead of 8 children. +*/ +struct ColorTree +{ + ColorTree* children[16]; /*up to 16 pointers to ColorTree of next level*/ + int index; /*the payload. Only has a meaningful value if this is in the last level*/ +}; + +static void color_tree_init(ColorTree* tree) +{ + int i; + for(i = 0; i < 16; i++) tree->children[i] = 0; + tree->index = -1; +} + +static void color_tree_cleanup(ColorTree* tree) +{ + int i; + for(i = 0; i < 16; i++) + { + if(tree->children[i]) + { + color_tree_cleanup(tree->children[i]); + myfree(tree->children[i]); + } + } +} + +/*returns -1 if color not present, its index otherwise*/ +static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) +{ + int bit = 0; + for(bit = 0; bit < 8; bit++) + { + int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); + if(!tree->children[i]) return -1; + else tree = tree->children[i]; + } + return tree ? tree->index : -1; +} + +#ifdef LODEPNG_COMPILE_ENCODER +static int color_tree_has(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a) +{ + return color_tree_get(tree, r, g, b, a) >= 0; +} +#endif /*LODEPNG_COMPILE_ENCODER*/ + +/*color is not allowed to already exist. +Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")*/ +static void color_tree_add(ColorTree* tree, + unsigned char r, unsigned char g, unsigned char b, unsigned char a, int index) +{ + int bit; + for(bit = 0; bit < 8; bit++) + { + int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1); + if(!tree->children[i]) + { + tree->children[i] = (ColorTree*)mymalloc(sizeof(ColorTree)); + color_tree_init(tree->children[i]); + } + tree = tree->children[i]; + } + tree->index = index; +} + +/*put a pixel, given its RGBA color, into image of any color type*/ +static unsigned rgba8ToPixel(unsigned char* out, size_t i, + const LodePNGColorMode* mode, ColorTree* tree /*for palette*/, + unsigned char r, unsigned char g, unsigned char b, unsigned char a) +{ + if(mode->colortype == LCT_GREY) + { + unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; + if(mode->bitdepth == 8) out[i] = grey; + else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey; + else + { + /*take the most significant bits of grey*/ + grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1); + addColorBits(out, i, mode->bitdepth, grey); + } + } + else if(mode->colortype == LCT_RGB) + { + if(mode->bitdepth == 8) + { + out[i * 3 + 0] = r; + out[i * 3 + 1] = g; + out[i * 3 + 2] = b; + } + else + { + out[i * 6 + 0] = out[i * 6 + 1] = r; + out[i * 6 + 2] = out[i * 6 + 3] = g; + out[i * 6 + 4] = out[i * 6 + 5] = b; + } + } + else if(mode->colortype == LCT_PALETTE) + { + int index = color_tree_get(tree, r, g, b, a); + if(index < 0) return 82; /*color not in palette*/ + if(mode->bitdepth == 8) out[i] = index; + else addColorBits(out, i, mode->bitdepth, index); + } + else if(mode->colortype == LCT_GREY_ALPHA) + { + unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; + if(mode->bitdepth == 8) + { + out[i * 2 + 0] = grey; + out[i * 2 + 1] = a; + } + else if(mode->bitdepth == 16) + { + out[i * 4 + 0] = out[i * 4 + 1] = grey; + out[i * 4 + 2] = out[i * 4 + 3] = a; + } + } + else if(mode->colortype == LCT_RGBA) + { + if(mode->bitdepth == 8) + { + out[i * 4 + 0] = r; + out[i * 4 + 1] = g; + out[i * 4 + 2] = b; + out[i * 4 + 3] = a; + } + else + { + out[i * 8 + 0] = out[i * 8 + 1] = r; + out[i * 8 + 2] = out[i * 8 + 3] = g; + out[i * 8 + 4] = out[i * 8 + 5] = b; + out[i * 8 + 6] = out[i * 8 + 7] = a; + } + } + + return 0; /*no error*/ +} + +/*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/ +static unsigned rgba16ToPixel(unsigned char* out, size_t i, + const LodePNGColorMode* mode, + unsigned short r, unsigned short g, unsigned short b, unsigned short a) +{ + if(mode->bitdepth != 16) return 85; /*must be 16 for this function*/ + if(mode->colortype == LCT_GREY) + { + unsigned short grey = r; /*((unsigned)r + g + b) / 3*/; + out[i * 2 + 0] = (grey >> 8) & 255; + out[i * 2 + 1] = grey & 255; + } + else if(mode->colortype == LCT_RGB) + { + out[i * 6 + 0] = (r >> 8) & 255; + out[i * 6 + 1] = r & 255; + out[i * 6 + 2] = (g >> 8) & 255; + out[i * 6 + 3] = g & 255; + out[i * 6 + 4] = (b >> 8) & 255; + out[i * 6 + 5] = b & 255; + } + else if(mode->colortype == LCT_GREY_ALPHA) + { + unsigned short grey = r; /*((unsigned)r + g + b) / 3*/; + out[i * 4 + 0] = (grey >> 8) & 255; + out[i * 4 + 1] = grey & 255; + out[i * 4 + 2] = (a >> 8) & 255; + out[i * 4 + 3] = a & 255; + } + else if(mode->colortype == LCT_RGBA) + { + out[i * 8 + 0] = (r >> 8) & 255; + out[i * 8 + 1] = r & 255; + out[i * 8 + 2] = (g >> 8) & 255; + out[i * 8 + 3] = g & 255; + out[i * 8 + 4] = (b >> 8) & 255; + out[i * 8 + 5] = b & 255; + out[i * 8 + 6] = (a >> 8) & 255; + out[i * 8 + 7] = a & 255; + } + + return 0; /*no error*/ +} + +/*Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type.*/ +static unsigned getPixelColorRGBA8(unsigned char* r, unsigned char* g, + unsigned char* b, unsigned char* a, + const unsigned char* in, size_t i, + const LodePNGColorMode* mode) +{ + if(mode->colortype == LCT_GREY) + { + if(mode->bitdepth == 8) + { + *r = *g = *b = in[i]; + if(mode->key_defined && *r == mode->key_r) *a = 0; + else *a = 255; + } + else if(mode->bitdepth == 16) + { + *r = *g = *b = in[i * 2 + 0]; + if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; + else *a = 255; + } + else + { + unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ + size_t j = i * mode->bitdepth; + unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); + *r = *g = *b = (value * 255) / highest; + if(mode->key_defined && value == mode->key_r) *a = 0; + else *a = 255; + } + } + else if(mode->colortype == LCT_RGB) + { + if(mode->bitdepth == 8) + { + *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2]; + if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0; + else *a = 255; + } + else + { + *r = in[i * 6 + 0]; + *g = in[i * 6 + 2]; + *b = in[i * 6 + 4]; + if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r + && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g + && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; + else *a = 255; + } + } + else if(mode->colortype == LCT_PALETTE) + { + unsigned index; + if(mode->bitdepth == 8) index = in[i]; + else + { + size_t j = i * mode->bitdepth; + index = readBitsFromReversedStream(&j, in, mode->bitdepth); + } + if(index >= mode->palettesize) return 47; /*index out of palette*/ + *r = mode->palette[index * 4 + 0]; + *g = mode->palette[index * 4 + 1]; + *b = mode->palette[index * 4 + 2]; + *a = mode->palette[index * 4 + 3]; + } + else if(mode->colortype == LCT_GREY_ALPHA) + { + if(mode->bitdepth == 8) + { + *r = *g = *b = in[i * 2 + 0]; + *a = in[i * 2 + 1]; + } + else + { + *r = *g = *b = in[i * 4 + 0]; + *a = in[i * 4 + 2]; + } + } + else if(mode->colortype == LCT_RGBA) + { + if(mode->bitdepth == 8) + { + *r = in[i * 4 + 0]; + *g = in[i * 4 + 1]; + *b = in[i * 4 + 2]; + *a = in[i * 4 + 3]; + } + else + { + *r = in[i * 8 + 0]; + *g = in[i * 8 + 2]; + *b = in[i * 8 + 4]; + *a = in[i * 8 + 6]; + } + } + + return 0; /*no error*/ +} + +/*Similar to getPixelColorRGBA8, but with all the for loops inside of the color +mode test cases, optimized to convert the colors much faster, when converting +to RGBA or RGB with 8 bit per cannel. buffer must be RGBA or RGB output with +enough memory, if has_alpha is true the output is RGBA. mode has the color mode +of the input buffer.*/ +static unsigned getPixelColorsRGBA8(unsigned char* buffer, size_t numpixels, + unsigned has_alpha, const unsigned char* in, + const LodePNGColorMode* mode) +{ + unsigned num_channels = has_alpha ? 4 : 3; + size_t i; + if(mode->colortype == LCT_GREY) + { + if(mode->bitdepth == 8) + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = buffer[1] = buffer[2] = in[i]; + if(has_alpha) buffer[3] = mode->key_defined && in[i] == mode->key_r ? 0 : 255; + } + } + else if(mode->bitdepth == 16) + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = buffer[1] = buffer[2] = in[i * 2]; + if(has_alpha) buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255; + } + } + else + { + unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/ + size_t j = 0; + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth); + buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest; + if(has_alpha) buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255; + } + } + } + else if(mode->colortype == LCT_RGB) + { + if(mode->bitdepth == 8) + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = in[i * 3 + 0]; + buffer[1] = in[i * 3 + 1]; + buffer[2] = in[i * 3 + 2]; + if(has_alpha) buffer[3] = mode->key_defined && buffer[0] == mode->key_r + && buffer[1]== mode->key_g && buffer[2] == mode->key_b ? 0 : 255; + } + } + else + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = in[i * 6 + 0]; + buffer[1] = in[i * 6 + 2]; + buffer[2] = in[i * 6 + 4]; + if(has_alpha) buffer[3] = mode->key_defined + && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r + && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g + && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255; + } + } + } + else if(mode->colortype == LCT_PALETTE) + { + unsigned index; + size_t j = 0; + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + if(mode->bitdepth == 8) index = in[i]; + else index = readBitsFromReversedStream(&j, in, mode->bitdepth); + if(index >= mode->palettesize) return 47; /*index out of palette*/ + buffer[0] = mode->palette[index * 4 + 0]; + buffer[1] = mode->palette[index * 4 + 1]; + buffer[2] = mode->palette[index * 4 + 2]; + if(has_alpha) buffer[3] = mode->palette[index * 4 + 3]; + } + } + else if(mode->colortype == LCT_GREY_ALPHA) + { + if(mode->bitdepth == 8) + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0]; + if(has_alpha) buffer[3] = in[i * 2 + 1]; + } + } + else + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0]; + if(has_alpha) buffer[3] = in[i * 4 + 2]; + } + } + } + else if(mode->colortype == LCT_RGBA) + { + if(mode->bitdepth == 8) + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = in[i * 4 + 0]; + buffer[1] = in[i * 4 + 1]; + buffer[2] = in[i * 4 + 2]; + if(has_alpha) buffer[3] = in[i * 4 + 3]; + } + } + else + { + for(i = 0; i < numpixels; i++, buffer += num_channels) + { + buffer[0] = in[i * 8 + 0]; + buffer[1] = in[i * 8 + 2]; + buffer[2] = in[i * 8 + 4]; + if(has_alpha) buffer[3] = in[i * 8 + 6]; + } + } + } + + return 0; /*no error*/ +} + +/*Get RGBA16 color of pixel with index i (y * width + x) from the raw image with +given color type, but the given color type must be 16-bit itself.*/ +static unsigned getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned short* b, unsigned short* a, + const unsigned char* in, size_t i, const LodePNGColorMode* mode) +{ + if(mode->bitdepth != 16) return 85; /*error: this function only supports 16-bit input*/ + + if(mode->colortype == LCT_GREY) + { + *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1]; + if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0; + else *a = 65535; + } + else if(mode->colortype == LCT_RGB) + { + *r = 256 * in[i * 6 + 0] + in[i * 6 + 1]; + *g = 256 * in[i * 6 + 2] + in[i * 6 + 3]; + *b = 256 * in[i * 6 + 4] + in[i * 6 + 5]; + if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r + && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g + && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0; + else *a = 65535; + } + else if(mode->colortype == LCT_GREY_ALPHA) + { + *r = *g = *b = 256 * in[i * 4 + 0] + in[i * 4 + 1]; + *a = 256 * in[i * 4 + 2] + in[i * 4 + 3]; + } + else if(mode->colortype == LCT_RGBA) + { + *r = 256 * in[i * 8 + 0] + in[i * 8 + 1]; + *g = 256 * in[i * 8 + 2] + in[i * 8 + 3]; + *b = 256 * in[i * 8 + 4] + in[i * 8 + 5]; + *a = 256 * in[i * 8 + 6] + in[i * 8 + 7]; + } + else return 85; /*error: this function only supports 16-bit input, not palettes*/ + + return 0; /*no error*/ +} + +/* +converts from any color type to 24-bit or 32-bit (later maybe more supported). return value = LodePNG error code +the out buffer must have (w * h * bpp + 7) / 8 bytes, where bpp is the bits per pixel of the output color type +(lodepng_get_bpp) for < 8 bpp images, there may _not_ be padding bits at the end of scanlines. +*/ +unsigned lodepng_convert(unsigned char* out, const unsigned char* in, + LodePNGColorMode* mode_out, LodePNGColorMode* mode_in, + unsigned w, unsigned h) +{ + unsigned error = 0; + size_t i; + ColorTree tree; + size_t numpixels = w * h; + + if(lodepng_color_mode_equal(mode_out, mode_in)) + { + size_t numbytes = lodepng_get_raw_size(w, h, mode_in); + for(i = 0; i < numbytes; i++) out[i] = in[i]; + return error; + } + + if(mode_out->colortype == LCT_PALETTE) + { + size_t palsize = 1 << mode_out->bitdepth; + if(mode_out->palettesize < palsize) palsize = mode_out->palettesize; + color_tree_init(&tree); + for(i = 0; i < palsize; i++) + { + unsigned char* p = &mode_out->palette[i * 4]; + color_tree_add(&tree, p[0], p[1], p[2], p[3], i); + } + } + + if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) + { + for(i = 0; i < numpixels; i++) + { + unsigned short r = 0, g = 0, b = 0, a = 0; + error = getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); + if(error) break; + error = rgba16ToPixel(out, i, mode_out, r, g, b, a); + if(error) break; + } + } + else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) + { + error = getPixelColorsRGBA8(out, numpixels, 1, in, mode_in); + } + else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) + { + error = getPixelColorsRGBA8(out, numpixels, 0, in, mode_in); + } + else + { + unsigned char r = 0, g = 0, b = 0, a = 0; + for(i = 0; i < numpixels; i++) + { + error = getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); + if(error) break; + error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a); + if(error) break; + } + } + + if(mode_out->colortype == LCT_PALETTE) + { + color_tree_cleanup(&tree); + } + + return error; +} + +#ifdef LODEPNG_COMPILE_ENCODER + +typedef struct ColorProfile +{ + unsigned char sixteenbit; /*needs more than 8 bits per channel*/ + unsigned char sixteenbit_done; + + + unsigned char colored; /*not greyscale*/ + unsigned char colored_done; + + unsigned char key; /*a color key is required, or more*/ + unsigned short key_r; /*these values are always in 16-bit bitdepth in the profile*/ + unsigned short key_g; + unsigned short key_b; + unsigned char alpha; /*alpha channel, or alpha palette, required*/ + unsigned char alpha_done; + + unsigned numcolors; + ColorTree tree; /*for listing the counted colors, up to 256*/ + unsigned char* palette; /*size 1024. Remember up to the first 256 RGBA colors*/ + unsigned maxnumcolors; /*if more than that amount counted*/ + unsigned char numcolors_done; + + unsigned greybits; /*amount of bits required for greyscale (1, 2, 4, 8). Does not take 16 bit into account.*/ + unsigned char greybits_done; + +} ColorProfile; + +static void color_profile_init(ColorProfile* profile, LodePNGColorMode* mode) +{ + profile->sixteenbit = 0; + profile->sixteenbit_done = mode->bitdepth == 16 ? 0 : 1; + + profile->colored = 0; + profile->colored_done = lodepng_is_greyscale_type(mode) ? 1 : 0; + + profile->key = 0; + profile->alpha = 0; + profile->alpha_done = lodepng_can_have_alpha(mode) ? 0 : 1; + + profile->numcolors = 0; + color_tree_init(&profile->tree); + profile->palette = (unsigned char*)mymalloc(1024); + profile->maxnumcolors = 257; + if(lodepng_get_bpp(mode) <= 8) + { + int bpp = lodepng_get_bpp(mode); + profile->maxnumcolors = bpp == 1 ? 2 : (bpp == 2 ? 4 : (bpp == 4 ? 16 : 256)); + } + profile->numcolors_done = 0; + + profile->greybits = 1; + profile->greybits_done = lodepng_get_bpp(mode) == 1 ? 1 : 0; +} + +static void color_profile_cleanup(ColorProfile* profile) +{ + color_tree_cleanup(&profile->tree); + myfree(profile->palette); +} + +/*function used for debug purposes with C++*/ +/*void printColorProfile(ColorProfile* p) +{ + std::cout << "sixteenbit: " << (int)p->sixteenbit << std::endl; + std::cout << "sixteenbit_done: " << (int)p->sixteenbit_done << std::endl; + std::cout << "colored: " << (int)p->colored << std::endl; + std::cout << "colored_done: " << (int)p->colored_done << std::endl; + std::cout << "key: " << (int)p->key << std::endl; + std::cout << "key_r: " << (int)p->key_r << std::endl; + std::cout << "key_g: " << (int)p->key_g << std::endl; + std::cout << "key_b: " << (int)p->key_b << std::endl; + std::cout << "alpha: " << (int)p->alpha << std::endl; + std::cout << "alpha_done: " << (int)p->alpha_done << std::endl; + std::cout << "numcolors: " << (int)p->numcolors << std::endl; + std::cout << "maxnumcolors: " << (int)p->maxnumcolors << std::endl; + std::cout << "numcolors_done: " << (int)p->numcolors_done << std::endl; + std::cout << "greybits: " << (int)p->greybits << std::endl; + std::cout << "greybits_done: " << (int)p->greybits_done << std::endl; +}*/ + +/*Returns how many bits needed to represent given value (max 8 bit)*/ +unsigned getValueRequiredBits(unsigned short value) +{ + if(value == 0 || value == 255) return 1; + /*The scaling of 2-bit and 4-bit values uses multiples of 85 and 17*/ + if(value % 17 == 0) return value % 85 == 0 ? 2 : 4; + return 8; +} + +/*profile must already have been inited with mode. +It's ok to set some parameters of profile to done already.*/ +static unsigned get_color_profile(ColorProfile* profile, + const unsigned char* in, size_t numpixels, + LodePNGColorMode* mode) +{ + unsigned error = 0; + size_t i; + + if(mode->bitdepth == 16) + { + for(i = 0; i < numpixels; i++) + { + unsigned short r, g, b, a; + error = getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode); + if(error) break; + + /*a color is considered good for 8-bit if the first byte and the second byte are equal, + (so if it's divisible through 257), NOT necessarily if the second byte is 0*/ + if(!profile->sixteenbit_done + && (((r & 255) != ((r >> 8) & 255)) + || ((g & 255) != ((g >> 8) & 255)) + || ((b & 255) != ((b >> 8) & 255)))) + { + profile->sixteenbit = 1; + profile->sixteenbit_done = 1; + profile->greybits_done = 1; /*greybits is not applicable anymore at 16-bit*/ + profile->numcolors_done = 1; /*counting colors no longer useful, palette doesn't support 16-bit*/ + } + + if(!profile->colored_done && (r != g || r != b)) + { + profile->colored = 1; + profile->colored_done = 1; + profile->greybits_done = 1; /*greybits is not applicable anymore*/ + } + + if(!profile->alpha_done && a != 255) + { + if(a == 0 && !(profile->key && (r != profile->key_r || g != profile->key_g || b != profile->key_b))) + { + if(!profile->key) + { + profile->key = 1; + profile->key_r = r; + profile->key_g = g; + profile->key_b = b; + } + } + else + { + profile->alpha = 1; + profile->alpha_done = 1; + profile->greybits_done = 1; /*greybits is not applicable anymore*/ + } + } + + if(!profile->greybits_done) + { + /*assuming 8-bit r, this test does not care about 16-bit*/ + unsigned bits = getValueRequiredBits(r); + if(bits > profile->greybits) profile->greybits = bits; + if(profile->greybits >= 8) profile->greybits_done = 1; + } + + if(!profile->numcolors_done) + { + /*assuming 8-bit rgba, this test does not care about 16-bit*/ + if(!color_tree_has(&profile->tree, (unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a)) + { + color_tree_add(&profile->tree, (unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a, + profile->numcolors); + if(profile->numcolors < 256) + { + unsigned char* p = profile->palette; + unsigned i = profile->numcolors; + p[i * 4 + 0] = (unsigned char)r; + p[i * 4 + 1] = (unsigned char)g; + p[i * 4 + 2] = (unsigned char)b; + p[i * 4 + 3] = (unsigned char)a; + } + profile->numcolors++; + if(profile->numcolors >= profile->maxnumcolors) profile->numcolors_done = 1; + } + } + + if(profile->alpha_done && profile->numcolors_done + && profile->colored_done && profile->sixteenbit_done && profile->greybits_done) + { + break; + } + }; + } + else + { + for(i = 0; i < numpixels; i++) + { + unsigned char r = 0, g = 0, b = 0, a = 0; + error = getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode); + if(error) break; + + if(!profile->colored_done && (r != g || r != b)) + { + profile->colored = 1; + profile->colored_done = 1; + profile->greybits_done = 1; /*greybits is not applicable anymore*/ + } + + if(!profile->alpha_done && a != 255) + { + if(a == 0 && !(profile->key && (r != profile->key_r || g != profile->key_g || b != profile->key_b))) + { + if(!profile->key) + { + profile->key = 1; + profile->key_r = r; + profile->key_g = g; + profile->key_b = b; + } + } + else + { + profile->alpha = 1; + profile->alpha_done = 1; + profile->greybits_done = 1; /*greybits is not applicable anymore*/ + } + } + + if(!profile->greybits_done) + { + unsigned bits = getValueRequiredBits(r); + if(bits > profile->greybits) profile->greybits = bits; + if(profile->greybits >= 8) profile->greybits_done = 1; + } + + if(!profile->numcolors_done) + { + if(!color_tree_has(&profile->tree, r, g, b, a)) + { + + color_tree_add(&profile->tree, r, g, b, a, profile->numcolors); + if(profile->numcolors < 256) + { + unsigned char* p = profile->palette; + unsigned i = profile->numcolors; + p[i * 4 + 0] = r; + p[i * 4 + 1] = g; + p[i * 4 + 2] = b; + p[i * 4 + 3] = a; + } + profile->numcolors++; + if(profile->numcolors >= profile->maxnumcolors) profile->numcolors_done = 1; + } + } + + if(profile->alpha_done && profile->numcolors_done && profile->colored_done && profile->greybits_done) + { + break; + } + }; + } + + /*make the profile's key always 16-bit for consistency*/ + if(mode->bitdepth < 16) + { + /*repeat each byte twice*/ + profile->key_r *= 257; + profile->key_g *= 257; + profile->key_b *= 257; + } + + return error; +} + +/*updates values of mode with a potentially smaller color model. mode_out should +contain the user chosen color model, but will be overwritten with the new chosen one.*/ +static unsigned doAutoChooseColor(LodePNGColorMode* mode_out, + const unsigned char* image, unsigned w, unsigned h, LodePNGColorMode* mode_in, + LodePNGAutoConvert auto_convert) +{ + ColorProfile profile; + unsigned error = 0; + int no_nibbles = auto_convert == LAC_AUTO_NO_NIBBLES || auto_convert == LAC_AUTO_NO_NIBBLES_NO_PALETTE; + int no_palette = auto_convert == LAC_AUTO_NO_PALETTE || auto_convert == LAC_AUTO_NO_NIBBLES_NO_PALETTE; + + if(auto_convert == LAC_ALPHA) + { + if(mode_out->colortype != LCT_RGBA && mode_out->colortype != LCT_GREY_ALPHA) return 0; + } + + color_profile_init(&profile, mode_in); + if(auto_convert == LAC_ALPHA) + { + profile.colored_done = 1; + profile.greybits_done = 1; + profile.numcolors_done = 1; + profile.sixteenbit_done = 1; + } + error = get_color_profile(&profile, image, w * h, mode_in); + + if(!error && auto_convert == LAC_ALPHA) + { + if(!profile.alpha) + { + mode_out->colortype = (mode_out->colortype == LCT_RGBA ? LCT_RGB : LCT_GREY); + } + } + else if(!error && auto_convert != LAC_ALPHA) + { + mode_out->key_defined = 0; + + if(profile.sixteenbit) + { + mode_out->bitdepth = 16; + if(profile.alpha) + { + mode_out->colortype = profile.colored ? LCT_RGBA : LCT_GREY_ALPHA; + } + else + { + mode_out->colortype = profile.colored ? LCT_RGB : LCT_GREY; + if(profile.key) + { + mode_out->key_defined = 1; + mode_out->key_r = profile.key_r; + mode_out->key_g = profile.key_g; + mode_out->key_b = profile.key_b; + } + } + } + else /*less than 16 bits per channel*/ + { + /*don't add palette overhead if image hasn't got a lot of pixels*/ + unsigned n = profile.numcolors; + int palette_ok = !no_palette && n <= 256 && (n * 2 < w * h); + unsigned palettebits = n <= 2 ? 1 : (n <= 4 ? 2 : (n <= 16 ? 4 : 8)); + int grey_ok = !profile.colored && !profile.alpha; /*grey without alpha, with potentially low bits*/ + if(palette_ok || grey_ok) + { + if(!palette_ok || (grey_ok && profile.greybits <= palettebits)) + { + mode_out->colortype = LCT_GREY; + mode_out->bitdepth = profile.greybits; + if(profile.key) + { + unsigned keyval = profile.key_r; + keyval &= (profile.greybits - 1); /*same subgroup of bits repeated, so taking right bits is fine*/ + mode_out->key_defined = 1; + mode_out->key_r = keyval; + mode_out->key_g = keyval; + mode_out->key_b = keyval; + } + } + else + { + /*fill in the palette*/ + unsigned i; + unsigned char* p = profile.palette; + for(i = 0; i < profile.numcolors; i++) + { + error = lodepng_palette_add(mode_out, p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3]); + if(error) break; + } + + mode_out->colortype = LCT_PALETTE; + mode_out->bitdepth = palettebits; + } + } + else /*8-bit per channel*/ + { + mode_out->bitdepth = 8; + if(profile.alpha) + { + mode_out->colortype = profile.colored ? LCT_RGBA : LCT_GREY_ALPHA; + } + else + { + mode_out->colortype = profile.colored ? LCT_RGB : LCT_GREY /*LCT_GREY normally won't occur, already done earlier*/; + if(profile.key) + { + mode_out->key_defined = 1; + mode_out->key_r = profile.key_r % 256; + mode_out->key_g = profile.key_g % 256; + mode_out->key_b = profile.key_b % 256; + } + } + } + } + } + + color_profile_cleanup(&profile); + + if(mode_out->colortype == LCT_PALETTE && mode_in->palettesize == mode_out->palettesize) + { + /*In this case keep the palette order of the input, so that the user can choose an optimal one*/ + size_t i; + for(i = 0; i < mode_in->palettesize * 4; i++) + { + mode_out->palette[i] = mode_in->palette[i]; + } + } + + if(no_nibbles && mode_out->bitdepth < 8) + { + /*palette can keep its small amount of colors, as long as no indices use it*/ + mode_out->bitdepth = 8; + } + + return error; +} + +#endif /* #ifdef LODEPNG_COMPILE_ENCODER */ + +/* +Paeth predicter, used by PNG filter type 4 +The parameters are of type short, but should come from unsigned chars, the shorts +are only needed to make the paeth calculation correct. +*/ +static unsigned char paethPredictor(short a, short b, short c) +{ + short pa = abs(b - c); + short pb = abs(a - c); + short pc = abs(a + b - c - c); + + if(pc < pa && pc < pb) return (unsigned char)c; + else if(pb < pa) return (unsigned char)b; + else return (unsigned char)a; +} + +/*shared values used by multiple Adam7 related functions*/ + +static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/ +static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/ +static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/ +static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/ + +static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], + size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp) +{ + /*the passstart values have 8 values: the 8th one indicates the byte after the end of the 7th (= last) pass*/ + unsigned i; + + /*calculate width and height in pixels of each pass*/ + for(i = 0; i < 7; i++) + { + passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; + passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; + if(passw[i] == 0) passh[i] = 0; + if(passh[i] == 0) passw[i] = 0; + } + + filter_passstart[0] = padded_passstart[0] = passstart[0] = 0; + for(i = 0; i < 7; i++) + { + /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/ + filter_passstart[i + 1] = filter_passstart[i] + + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0); + /*bits padded if needed to fill full byte at end of each scanline*/ + padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8); + /*only padded at end of reduced image*/ + passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; + } +} + +#ifdef LODEPNG_COMPILE_DECODER + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / PNG Decoder / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +/*read the information from the header and store it in the LodePNGInfo. return value is error*/ +unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, + const unsigned char* in, size_t insize) +{ + LodePNGInfo* info = &state->info_png; + if(insize == 0 || in == 0) + { + CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/ + } + if(insize < 29) + { + CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/ + } + + /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/ + lodepng_info_cleanup(info); + lodepng_info_init(info); + + if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 + || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) + { + CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/ + } + if(in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') + { + CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/ + } + + /*read the values given in the header*/ + *w = lodepng_read32bitInt(&in[16]); + *h = lodepng_read32bitInt(&in[20]); + info->color.bitdepth = in[24]; + info->color.colortype = (LodePNGColorType)in[25]; + info->compression_method = in[26]; + info->filter_method = in[27]; + info->interlace_method = in[28]; + + if(!state->decoder.ignore_crc) + { + unsigned CRC = lodepng_read32bitInt(&in[29]); + unsigned checksum = lodepng_crc32(&in[12], 17); + if(CRC != checksum) + { + CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ + } + } + + /*error: only compression method 0 is allowed in the specification*/ + if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32); + /*error: only filter method 0 is allowed in the specification*/ + if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33); + /*error: only interlace methods 0 and 1 exist in the specification*/ + if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34); + + state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); + return state->error; +} + +static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, + size_t bytewidth, unsigned char filterType, size_t length) +{ + /* + For PNG filter method 0 + unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte, + the filter works byte per byte (bytewidth = 1) + precon is the previous unfiltered scanline, recon the result, scanline the current one + the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead + recon and scanline MAY be the same memory address! precon must be disjoint. + */ + + size_t i; + switch(filterType) + { + case 0: + for(i = 0; i < length; i++) recon[i] = scanline[i]; + break; + case 1: + for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; + for(i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth]; + break; + case 2: + if(precon) + { + for(i = 0; i < length; i++) recon[i] = scanline[i] + precon[i]; + } + else + { + for(i = 0; i < length; i++) recon[i] = scanline[i]; + } + break; + case 3: + if(precon) + { + for(i = 0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2; + for(i = bytewidth; i < length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2); + } + else + { + for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; + for(i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2; + } + break; + case 4: + if(precon) + { + for(i = 0; i < bytewidth; i++) + { + recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/ + } + for(i = bytewidth; i < length; i++) + { + recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth])); + } + } + else + { + for(i = 0; i < bytewidth; i++) + { + recon[i] = scanline[i]; + } + for(i = bytewidth; i < length; i++) + { + /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/ + recon[i] = (scanline[i] + recon[i - bytewidth]); + } + } + break; + default: return 36; /*error: unexisting filter type given*/ + } + return 0; +} + +static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) +{ + /* + For PNG filter method 0 + this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times) + out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline + w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel + in and out are allowed to be the same memory address (but aren't the same size since in has the extra filter bytes) + */ + + unsigned y; + unsigned char* prevline = 0; + + /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ + size_t bytewidth = (bpp + 7) / 8; + size_t linebytes = (w * bpp + 7) / 8; + + for(y = 0; y < h; y++) + { + size_t outindex = linebytes * y; + size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ + unsigned char filterType = in[inindex]; + + CERROR_TRY_RETURN(unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes)); + + prevline = &out[outindex]; + } + + return 0; +} + +/* +in: Adam7 interlaced image, with no padding bits between scanlines, but between + reduced images so that each reduced image starts at a byte. +out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h +bpp: bits per pixel +out has the following size in bits: w * h * bpp. +in is possibly bigger due to padding bits between reduced images. +out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation +(because that's likely a little bit faster) +NOTE: comments about padding bits are only relevant if bpp < 8 +*/ +static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) +{ + unsigned passw[7], passh[7]; + size_t filter_passstart[8], padded_passstart[8], passstart[8]; + unsigned i; + + Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); + + if(bpp >= 8) + { + for(i = 0; i < 7; i++) + { + unsigned x, y, b; + size_t bytewidth = bpp / 8; + for(y = 0; y < passh[i]; y++) + for(x = 0; x < passw[i]; x++) + { + size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth; + size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; + for(b = 0; b < bytewidth; b++) + { + out[pixeloutstart + b] = in[pixelinstart + b]; + } + } + } + } + else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ + { + for(i = 0; i < 7; i++) + { + unsigned x, y, b; + unsigned ilinebits = bpp * passw[i]; + unsigned olinebits = bpp * w; + size_t obp, ibp; /*bit pointers (for out and in buffer)*/ + for(y = 0; y < passh[i]; y++) + for(x = 0; x < passw[i]; x++) + { + ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp); + obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; + for(b = 0; b < bpp; b++) + { + unsigned char bit = readBitFromReversedStream(&ibp, in); + /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/ + setBitOfReversedStream0(&obp, out, bit); + } + } + } + } +} + +static void removePaddingBits(unsigned char* out, const unsigned char* in, + size_t olinebits, size_t ilinebits, unsigned h) +{ + /* + After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need + to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers + for the Adam7 code, the color convert code and the output to the user. + in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must + have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits + also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7 + only useful if (ilinebits - olinebits) is a value in the range 1..7 + */ + unsigned y; + size_t diff = ilinebits - olinebits; + size_t ibp = 0, obp = 0; /*input and output bit pointers*/ + for(y = 0; y < h; y++) + { + size_t x; + for(x = 0; x < olinebits; x++) + { + unsigned char bit = readBitFromReversedStream(&ibp, in); + setBitOfReversedStream(&obp, out, bit); + } + ibp += diff; + } +} + +/*out must be buffer big enough to contain full image, and in must contain the full decompressed data from +the IDAT chunks (with filter index bytes and possible padding bits) +return value is error*/ +static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, + unsigned w, unsigned h, const LodePNGInfo* info_png) +{ + /* + This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype. + Steps: + *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8) + *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace + NOTE: the in buffer will be overwritten with intermediate data! + */ + unsigned bpp = lodepng_get_bpp(&info_png->color); + if(bpp == 0) return 31; /*error: invalid colortype*/ + + if(info_png->interlace_method == 0) + { + if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) + { + CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp)); + removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h); + } + /*we can immediatly filter into the out buffer, no other steps needed*/ + else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp)); + } + else /*interlace_method is 1 (Adam7)*/ + { + unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; + unsigned i; + + Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); + + for(i = 0; i < 7; i++) + { + CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp)); + /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline, + move bytes instead of bits or move not at all*/ + if(bpp < 8) + { + /*remove padding bits in scanlines; after this there still may be padding + bits between the different reduced images: each reduced image still starts nicely at a byte*/ + removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, + ((passw[i] * bpp + 7) / 8) * 8, passh[i]); + } + } + + Adam7_deinterlace(out, in, w, h, bpp); + } + + return 0; +} + +static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) +{ + unsigned pos = 0, i; + if(color->palette) myfree(color->palette); + color->palettesize = chunkLength / 3; + color->palette = (unsigned char*)mymalloc(4 * color->palettesize); + if(!color->palette && color->palettesize) + { + color->palettesize = 0; + return 83; /*alloc fail*/ + } + if(color->palettesize > 256) return 38; /*error: palette too big*/ + + for(i = 0; i < color->palettesize; i++) + { + color->palette[4 * i + 0] = data[pos++]; /*R*/ + color->palette[4 * i + 1] = data[pos++]; /*G*/ + color->palette[4 * i + 2] = data[pos++]; /*B*/ + color->palette[4 * i + 3] = 255; /*alpha*/ + } + + return 0; /* OK */ +} + +static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) +{ + unsigned i; + if(color->colortype == LCT_PALETTE) + { + /*error: more alpha values given than there are palette entries*/ + if(chunkLength > color->palettesize) return 38; + + for(i = 0; i < chunkLength; i++) color->palette[4 * i + 3] = data[i]; + } + else if(color->colortype == LCT_GREY) + { + /*error: this chunk must be 2 bytes for greyscale image*/ + if(chunkLength != 2) return 30; + + color->key_defined = 1; + color->key_r = color->key_g = color->key_b = 256 * data[0] + data[1]; + } + else if(color->colortype == LCT_RGB) + { + /*error: this chunk must be 6 bytes for RGB image*/ + if(chunkLength != 6) return 41; + + color->key_defined = 1; + color->key_r = 256 * data[0] + data[1]; + color->key_g = 256 * data[2] + data[3]; + color->key_b = 256 * data[4] + data[5]; + } + else return 42; /*error: tRNS chunk not allowed for other color models*/ + + return 0; /* OK */ +} + + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS +/*background color chunk (bKGD)*/ +static unsigned readChunk_bKGD(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) +{ + if(info->color.colortype == LCT_PALETTE) + { + /*error: this chunk must be 1 byte for indexed color image*/ + if(chunkLength != 1) return 43; + + info->background_defined = 1; + info->background_r = info->background_g = info->background_b = data[0]; + } + else if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) + { + /*error: this chunk must be 2 bytes for greyscale image*/ + if(chunkLength != 2) return 44; + + info->background_defined = 1; + info->background_r = info->background_g = info->background_b + = 256 * data[0] + data[1]; + } + else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) + { + /*error: this chunk must be 6 bytes for greyscale image*/ + if(chunkLength != 6) return 45; + + info->background_defined = 1; + info->background_r = 256 * data[0] + data[1]; + info->background_g = 256 * data[2] + data[3]; + info->background_b = 256 * data[4] + data[5]; + } + + return 0; /* OK */ +} + +/*text chunk (tEXt)*/ +static unsigned readChunk_tEXt(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) +{ + unsigned error = 0; + char *key = 0, *str = 0; + unsigned i; + + while(!error) /*not really a while loop, only used to break on error*/ + { + unsigned length, string2_begin; + + length = 0; + while(length < chunkLength && data[length] != 0) length++; + /*error, end reached, no null terminator?*/ + if(length + 1 >= chunkLength) CERROR_BREAK(error, 75); + + key = (char*)mymalloc(length + 1); + if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ + + key[length] = 0; + for(i = 0; i < length; i++) key[i] = data[i]; + + string2_begin = length + 1; + /*error, end reached, no null terminator?*/ + if(string2_begin > chunkLength) CERROR_BREAK(error, 75); + + length = chunkLength - string2_begin; + str = (char*)mymalloc(length + 1); + if(!str) CERROR_BREAK(error, 83); /*alloc fail*/ + + str[length] = 0; + for(i = 0; i < length; i++) str[i] = data[string2_begin + i]; + + error = lodepng_add_text(info, key, str); + + break; + } + + myfree(key); + myfree(str); + + return error; +} + +/*compressed text chunk (zTXt)*/ +static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, + const unsigned char* data, size_t chunkLength) +{ + unsigned error = 0; + unsigned i; + + unsigned length, string2_begin; + char *key = 0; + ucvector decoded; + + ucvector_init(&decoded); + + while(!error) /*not really a while loop, only used to break on error*/ + { + for(length = 0; length < chunkLength && data[length] != 0; length++) ; + if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ + + key = (char*)mymalloc(length + 1); + if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ + + key[length] = 0; + for(i = 0; i < length; i++) key[i] = data[i]; + + if(data[length + 1] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/ + + string2_begin = length + 2; + if(string2_begin > chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ + + length = chunkLength - string2_begin; + error = zlib_decompress(&decoded.data, &decoded.size, + (unsigned char*)(&data[string2_begin]), + length, zlibsettings); + if(error) break; + ucvector_push_back(&decoded, 0); + + error = lodepng_add_text(info, key, (char*)decoded.data); + + break; + } + + myfree(key); + ucvector_cleanup(&decoded); + + return error; +} + +/*international text chunk (iTXt)*/ +static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, + const unsigned char* data, size_t chunkLength) +{ + unsigned error = 0; + unsigned i; + + unsigned length, begin, compressed; + char *key = 0, *langtag = 0, *transkey = 0; + ucvector decoded; + ucvector_init(&decoded); + + while(!error) /*not really a while loop, only used to break on error*/ + { + /*Quick check if the chunk length isn't too small. Even without check + it'd still fail with other error checks below if it's too short. This just gives a different error code.*/ + if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/ + + /*read the key*/ + for(length = 0; length < chunkLength && data[length] != 0; length++) ; + if(length + 2 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char found*/ + + key = (char*)mymalloc(length + 1); + if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ + + key[length] = 0; + for(i = 0; i < length; i++) key[i] = data[i]; + + /*read the compression method*/ + compressed = data[length + 1]; + if(data[length + 2] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/ + + /*read the langtag*/ + begin = length + 3; + length = 0; + for(i = begin; i < chunkLength && data[i] != 0; i++) length++; + if(begin + length + 1 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char found*/ + + langtag = (char*)mymalloc(length + 1); + if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/ + + langtag[length] = 0; + for(i = 0; i < length; i++) langtag[i] = data[begin + i]; + + /*read the transkey*/ + begin += length + 1; + length = 0; + for(i = begin; i < chunkLength && data[i] != 0; i++) length++; + if(begin + length + 1 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ + + transkey = (char*)mymalloc(length + 1); + if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/ + + transkey[length] = 0; + for(i = 0; i < length; i++) transkey[i] = data[begin + i]; + + /*read the actual text*/ + begin += length + 1; + if(begin > chunkLength) CERROR_BREAK(error, 75); /*no null termination, corrupt?*/ + + length = chunkLength - begin; + + if(compressed) + { + error = zlib_decompress(&decoded.data, &decoded.size, + (unsigned char*)(&data[begin]), + length, zlibsettings); + if(error) break; + if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size; + ucvector_push_back(&decoded, 0); + } + else + { + if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 /*alloc fail*/); + + decoded.data[length] = 0; + for(i = 0; i < length; i++) decoded.data[i] = data[begin + i]; + } + + error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data); + + break; + } + + myfree(key); + myfree(langtag); + myfree(transkey); + ucvector_cleanup(&decoded); + + return error; +} + +static unsigned readChunk_tIME(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) +{ + if(chunkLength != 7) return 73; /*invalid tIME chunk size*/ + + info->time_defined = 1; + info->time.year = 256 * data[0] + data[+ 1]; + info->time.month = data[2]; + info->time.day = data[3]; + info->time.hour = data[4]; + info->time.minute = data[5]; + info->time.second = data[6]; + + return 0; /* OK */ +} + +static unsigned readChunk_pHYs(LodePNGInfo* info, const unsigned char* data, size_t chunkLength) +{ + if(chunkLength != 9) return 74; /*invalid pHYs chunk size*/ + + info->phys_defined = 1; + info->phys_x = 16777216 * data[0] + 65536 * data[1] + 256 * data[2] + data[3]; + info->phys_y = 16777216 * data[4] + 65536 * data[5] + 256 * data[6] + data[7]; + info->phys_unit = data[8]; + + return 0; /* OK */ +} +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +/*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/ +static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, + LodePNGState* state, + const unsigned char* in, size_t insize) +{ + unsigned char IEND = 0; + const unsigned char* chunk; + size_t i; + ucvector idat; /*the data from idat chunks*/ + + /*for unknown chunk order*/ + unsigned unknown = 0; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/ +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + + /*provide some proper output values if error will happen*/ + *out = 0; + + state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/ + if(state->error) return; + + ucvector_init(&idat); + chunk = &in[33]; /*first byte of the first chunk after the header*/ + + /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. + IDAT data is put at the start of the in buffer*/ + while(!IEND && !state->error) + { + unsigned chunkLength; + const unsigned char* data; /*the data in the chunk*/ + + /*error: size of the in buffer too small to contain next chunk*/ + if((size_t)((chunk - in) + 12) > insize || chunk < in) CERROR_BREAK(state->error, 30); + + /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/ + chunkLength = lodepng_chunk_length(chunk); + /*error: chunk length larger than the max PNG chunk size*/ + if(chunkLength > 2147483647) CERROR_BREAK(state->error, 63); + + if((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in) + { + CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk*/ + } + + data = lodepng_chunk_data_const(chunk); + + /*IDAT chunk, containing compressed image data*/ + if(lodepng_chunk_type_equals(chunk, "IDAT")) + { + size_t oldsize = idat.size; + if(!ucvector_resize(&idat, oldsize + chunkLength)) CERROR_BREAK(state->error, 83 /*alloc fail*/); + for(i = 0; i < chunkLength; i++) idat.data[oldsize + i] = data[i]; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + critical_pos = 3; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + } + /*IEND chunk*/ + else if(lodepng_chunk_type_equals(chunk, "IEND")) + { + IEND = 1; + } + /*palette chunk (PLTE)*/ + else if(lodepng_chunk_type_equals(chunk, "PLTE")) + { + state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength); + if(state->error) break; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + critical_pos = 2; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + } + /*palette transparency chunk (tRNS)*/ + else if(lodepng_chunk_type_equals(chunk, "tRNS")) + { + state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength); + if(state->error) break; + } +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*background color chunk (bKGD)*/ + else if(lodepng_chunk_type_equals(chunk, "bKGD")) + { + state->error = readChunk_bKGD(&state->info_png, data, chunkLength); + if(state->error) break; + } + /*text chunk (tEXt)*/ + else if(lodepng_chunk_type_equals(chunk, "tEXt")) + { + if(state->decoder.read_text_chunks) + { + state->error = readChunk_tEXt(&state->info_png, data, chunkLength); + if(state->error) break; + } + } + /*compressed text chunk (zTXt)*/ + else if(lodepng_chunk_type_equals(chunk, "zTXt")) + { + if(state->decoder.read_text_chunks) + { + state->error = readChunk_zTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); + if(state->error) break; + } + } + /*international text chunk (iTXt)*/ + else if(lodepng_chunk_type_equals(chunk, "iTXt")) + { + if(state->decoder.read_text_chunks) + { + state->error = readChunk_iTXt(&state->info_png, &state->decoder.zlibsettings, data, chunkLength); + if(state->error) break; + } + } + else if(lodepng_chunk_type_equals(chunk, "tIME")) + { + state->error = readChunk_tIME(&state->info_png, data, chunkLength); + if(state->error) break; + } + else if(lodepng_chunk_type_equals(chunk, "pHYs")) + { + state->error = readChunk_pHYs(&state->info_png, data, chunkLength); + if(state->error) break; + } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + else /*it's not an implemented chunk type, so ignore it: skip over the data*/ + { + /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/ + if(!lodepng_chunk_ancillary(chunk)) CERROR_BREAK(state->error, 69); + + unknown = 1; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + if(state->decoder.remember_unknown_chunks) + { + state->error = lodepng_chunk_append(&state->info_png.unknown_chunks_data[critical_pos - 1], + &state->info_png.unknown_chunks_size[critical_pos - 1], chunk); + if(state->error) break; + } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + } + + if(!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ + { + if(lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/ + } + + if(!IEND) chunk = lodepng_chunk_next_const(chunk); + } + + if(!state->error) + { + ucvector scanlines; + ucvector_init(&scanlines); + + /*maximum final image length is already reserved in the vector's length - this is not really necessary*/ + if(!ucvector_resize(&scanlines, lodepng_get_raw_size(*w, *h, &state->info_png.color) + *h)) + { + state->error = 83; /*alloc fail*/ + } + if(!state->error) + { + /*decompress with the Zlib decompressor*/ + state->error = zlib_decompress(&scanlines.data, &scanlines.size, idat.data, + idat.size, &state->decoder.zlibsettings); + } + + if(!state->error) + { + ucvector outv; + ucvector_init(&outv); + if(!ucvector_resizev(&outv, + lodepng_get_raw_size(*w, *h, &state->info_png.color), 0)) state->error = 83; /*alloc fail*/ + if(!state->error) state->error = postProcessScanlines(outv.data, scanlines.data, *w, *h, &state->info_png); + *out = outv.data; + } + ucvector_cleanup(&scanlines); + } + + ucvector_cleanup(&idat); +} + +unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, + LodePNGState* state, + const unsigned char* in, size_t insize) +{ + *out = 0; + decodeGeneric(out, w, h, state, in, insize); + if(state->error) return state->error; + if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color)) + { + /*same color type, no copying or converting of data needed*/ + /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype + the raw image has to the end user*/ + if(!state->decoder.color_convert) + { + state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color); + if(state->error) return state->error; + } + } + else + { + /*color conversion needed; sort of copy of the data*/ + unsigned char* data = *out; + size_t outsize; + + /*TODO: check if this works according to the statement in the documentation: "The converter can convert + from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/ + if(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA) + && !(state->info_raw.bitdepth == 8)) + { + return 56; /*unsupported color mode conversion*/ + } + + outsize = lodepng_get_raw_size(*w, *h, &state->info_raw); + *out = (unsigned char*)mymalloc(outsize); + if(!(*out)) + { + state->error = 83; /*alloc fail*/ + } + else state->error = lodepng_convert(*out, data, &state->info_raw, &state->info_png.color, *w, *h); + myfree(data); + } + return state->error; +} + +unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, + size_t insize, LodePNGColorType colortype, unsigned bitdepth) +{ + unsigned error; + LodePNGState state; + lodepng_state_init(&state); + state.info_raw.colortype = colortype; + state.info_raw.bitdepth = bitdepth; + error = lodepng_decode(out, w, h, &state, in, insize); + lodepng_state_cleanup(&state); + return error; +} + +unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) +{ + return lodepng_decode_memory(out, w, h, in, insize, LCT_RGBA, 8); +} + +unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, const unsigned char* in, size_t insize) +{ + return lodepng_decode_memory(out, w, h, in, insize, LCT_RGB, 8); +} + +#ifdef LODEPNG_COMPILE_DISK +unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename, + LodePNGColorType colortype, unsigned bitdepth) +{ + unsigned char* buffer; + size_t buffersize; + unsigned error; + error = lodepng_load_file(&buffer, &buffersize, filename); + if(!error) error = lodepng_decode_memory(out, w, h, buffer, buffersize, colortype, bitdepth); + myfree(buffer); + return error; +} + +unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) +{ + return lodepng_decode_file(out, w, h, filename, LCT_RGBA, 8); +} + +unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, const char* filename) +{ + return lodepng_decode_file(out, w, h, filename, LCT_RGB, 8); +} +#endif /*LODEPNG_COMPILE_DISK*/ + +void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) +{ + settings->color_convert = 1; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + settings->read_text_chunks = 1; + settings->remember_unknown_chunks = 0; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + settings->ignore_crc = 0; + lodepng_decompress_settings_init(&settings->zlibsettings); +} + +#endif /*LODEPNG_COMPILE_DECODER*/ + +#if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) + +void lodepng_state_init(LodePNGState* state) +{ +#ifdef LODEPNG_COMPILE_DECODER + lodepng_decoder_settings_init(&state->decoder); +#endif /*LODEPNG_COMPILE_DECODER*/ +#ifdef LODEPNG_COMPILE_ENCODER + lodepng_encoder_settings_init(&state->encoder); +#endif /*LODEPNG_COMPILE_ENCODER*/ + lodepng_color_mode_init(&state->info_raw); + lodepng_info_init(&state->info_png); + state->error = 1; +} + +void lodepng_state_cleanup(LodePNGState* state) +{ + lodepng_color_mode_cleanup(&state->info_raw); + lodepng_info_cleanup(&state->info_png); +} + +void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source) +{ + lodepng_state_cleanup(dest); + *dest = *source; + lodepng_color_mode_init(&dest->info_raw); + lodepng_info_init(&dest->info_png); + dest->error = lodepng_color_mode_copy(&dest->info_raw, &source->info_raw); if(dest->error) return; + dest->error = lodepng_info_copy(&dest->info_png, &source->info_png); if(dest->error) return; +} + +#endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */ + +#ifdef LODEPNG_COMPILE_ENCODER + +/* ////////////////////////////////////////////////////////////////////////// */ +/* / PNG Encoder / */ +/* ////////////////////////////////////////////////////////////////////////// */ + +/*chunkName must be string of 4 characters*/ +static unsigned addChunk(ucvector* out, const char* chunkName, const unsigned char* data, size_t length) +{ + CERROR_TRY_RETURN(lodepng_chunk_create(&out->data, &out->size, (unsigned)length, chunkName, data)); + out->allocsize = out->size; /*fix the allocsize again*/ + return 0; +} + +static void writeSignature(ucvector* out) +{ + /*8 bytes PNG signature, aka the magic bytes*/ + ucvector_push_back(out, 137); + ucvector_push_back(out, 80); + ucvector_push_back(out, 78); + ucvector_push_back(out, 71); + ucvector_push_back(out, 13); + ucvector_push_back(out, 10); + ucvector_push_back(out, 26); + ucvector_push_back(out, 10); +} + +static unsigned addChunk_IHDR(ucvector* out, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth, unsigned interlace_method) +{ + unsigned error = 0; + ucvector header; + ucvector_init(&header); + + lodepng_add32bitInt(&header, w); /*width*/ + lodepng_add32bitInt(&header, h); /*height*/ + ucvector_push_back(&header, (unsigned char)bitdepth); /*bit depth*/ + ucvector_push_back(&header, (unsigned char)colortype); /*color type*/ + ucvector_push_back(&header, 0); /*compression method*/ + ucvector_push_back(&header, 0); /*filter method*/ + ucvector_push_back(&header, interlace_method); /*interlace method*/ + + error = addChunk(out, "IHDR", header.data, header.size); + ucvector_cleanup(&header); + + return error; +} + +static unsigned addChunk_PLTE(ucvector* out, const LodePNGColorMode* info) +{ + unsigned error = 0; + size_t i; + ucvector PLTE; + ucvector_init(&PLTE); + for(i = 0; i < info->palettesize * 4; i++) + { + /*add all channels except alpha channel*/ + if(i % 4 != 3) ucvector_push_back(&PLTE, info->palette[i]); + } + error = addChunk(out, "PLTE", PLTE.data, PLTE.size); + ucvector_cleanup(&PLTE); + + return error; +} + +static unsigned addChunk_tRNS(ucvector* out, const LodePNGColorMode* info) +{ + unsigned error = 0; + size_t i; + ucvector tRNS; + ucvector_init(&tRNS); + if(info->colortype == LCT_PALETTE) + { + size_t amount = info->palettesize; + /*the tail of palette values that all have 255 as alpha, does not have to be encoded*/ + for(i = info->palettesize; i > 0; i--) + { + if(info->palette[4 * (i - 1) + 3] == 255) amount--; + else break; + } + /*add only alpha channel*/ + for(i = 0; i < amount; i++) ucvector_push_back(&tRNS, info->palette[4 * i + 3]); + } + else if(info->colortype == LCT_GREY) + { + if(info->key_defined) + { + ucvector_push_back(&tRNS, (unsigned char)(info->key_r / 256)); + ucvector_push_back(&tRNS, (unsigned char)(info->key_r % 256)); + } + } + else if(info->colortype == LCT_RGB) + { + if(info->key_defined) + { + ucvector_push_back(&tRNS, (unsigned char)(info->key_r / 256)); + ucvector_push_back(&tRNS, (unsigned char)(info->key_r % 256)); + ucvector_push_back(&tRNS, (unsigned char)(info->key_g / 256)); + ucvector_push_back(&tRNS, (unsigned char)(info->key_g % 256)); + ucvector_push_back(&tRNS, (unsigned char)(info->key_b / 256)); + ucvector_push_back(&tRNS, (unsigned char)(info->key_b % 256)); + } + } + + error = addChunk(out, "tRNS", tRNS.data, tRNS.size); + ucvector_cleanup(&tRNS); + + return error; +} + +static unsigned addChunk_IDAT(ucvector* out, const unsigned char* data, size_t datasize, + LodePNGCompressSettings* zlibsettings) +{ + ucvector zlibdata; + unsigned error = 0; + + /*compress with the Zlib compressor*/ + ucvector_init(&zlibdata); + error = zlib_compress(&zlibdata.data, &zlibdata.size, data, datasize, zlibsettings); + if(!error) error = addChunk(out, "IDAT", zlibdata.data, zlibdata.size); + ucvector_cleanup(&zlibdata); + + return error; +} + +static unsigned addChunk_IEND(ucvector* out) +{ + unsigned error = 0; + error = addChunk(out, "IEND", 0, 0); + return error; +} + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + +static unsigned addChunk_tEXt(ucvector* out, const char* keyword, const char* textstring) +{ + unsigned error = 0; + size_t i; + ucvector text; + ucvector_init(&text); + for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&text, (unsigned char)keyword[i]); + ucvector_push_back(&text, 0); + for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&text, (unsigned char)textstring[i]); + error = addChunk(out, "tEXt", text.data, text.size); + ucvector_cleanup(&text); + + return error; +} + +static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* textstring, + LodePNGCompressSettings* zlibsettings) +{ + unsigned error = 0; + ucvector data, compressed; + size_t i, textsize = strlen(textstring); + + ucvector_init(&data); + ucvector_init(&compressed); + for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&data, (unsigned char)keyword[i]); + ucvector_push_back(&data, 0); /*0 termination char*/ + ucvector_push_back(&data, 0); /*compression method: 0*/ + + error = zlib_compress(&compressed.data, &compressed.size, + (unsigned char*)textstring, textsize, zlibsettings); + if(!error) + { + for(i = 0; i < compressed.size; i++) ucvector_push_back(&data, compressed.data[i]); + error = addChunk(out, "zTXt", data.data, data.size); + } + + ucvector_cleanup(&compressed); + ucvector_cleanup(&data); + return error; +} + +static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* keyword, const char* langtag, + const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings) +{ + unsigned error = 0; + ucvector data; + size_t i, textsize = strlen(textstring); + + ucvector_init(&data); + + for(i = 0; keyword[i] != 0; i++) ucvector_push_back(&data, (unsigned char)keyword[i]); + ucvector_push_back(&data, 0); /*null termination char*/ + ucvector_push_back(&data, compressed ? 1 : 0); /*compression flag*/ + ucvector_push_back(&data, 0); /*compression method*/ + for(i = 0; langtag[i] != 0; i++) ucvector_push_back(&data, (unsigned char)langtag[i]); + ucvector_push_back(&data, 0); /*null termination char*/ + for(i = 0; transkey[i] != 0; i++) ucvector_push_back(&data, (unsigned char)transkey[i]); + ucvector_push_back(&data, 0); /*null termination char*/ + + if(compressed) + { + ucvector compressed_data; + ucvector_init(&compressed_data); + error = zlib_compress(&compressed_data.data, &compressed_data.size, + (unsigned char*)textstring, textsize, zlibsettings); + if(!error) + { + for(i = 0; i < compressed_data.size; i++) ucvector_push_back(&data, compressed_data.data[i]); + } + ucvector_cleanup(&compressed_data); + } + else /*not compressed*/ + { + for(i = 0; textstring[i] != 0; i++) ucvector_push_back(&data, (unsigned char)textstring[i]); + } + + if(!error) error = addChunk(out, "iTXt", data.data, data.size); + ucvector_cleanup(&data); + return error; +} + +static unsigned addChunk_bKGD(ucvector* out, const LodePNGInfo* info) +{ + unsigned error = 0; + ucvector bKGD; + ucvector_init(&bKGD); + if(info->color.colortype == LCT_GREY || info->color.colortype == LCT_GREY_ALPHA) + { + ucvector_push_back(&bKGD, (unsigned char)(info->background_r / 256)); + ucvector_push_back(&bKGD, (unsigned char)(info->background_r % 256)); + } + else if(info->color.colortype == LCT_RGB || info->color.colortype == LCT_RGBA) + { + ucvector_push_back(&bKGD, (unsigned char)(info->background_r / 256)); + ucvector_push_back(&bKGD, (unsigned char)(info->background_r % 256)); + ucvector_push_back(&bKGD, (unsigned char)(info->background_g / 256)); + ucvector_push_back(&bKGD, (unsigned char)(info->background_g % 256)); + ucvector_push_back(&bKGD, (unsigned char)(info->background_b / 256)); + ucvector_push_back(&bKGD, (unsigned char)(info->background_b % 256)); + } + else if(info->color.colortype == LCT_PALETTE) + { + ucvector_push_back(&bKGD, (unsigned char)(info->background_r % 256)); /*palette index*/ + } + + error = addChunk(out, "bKGD", bKGD.data, bKGD.size); + ucvector_cleanup(&bKGD); + + return error; +} + +static unsigned addChunk_tIME(ucvector* out, const LodePNGTime* time) +{ + unsigned error = 0; + unsigned char* data = (unsigned char*)mymalloc(7); + if(!data) return 83; /*alloc fail*/ + data[0] = (unsigned char)(time->year / 256); + data[1] = (unsigned char)(time->year % 256); + data[2] = time->month; + data[3] = time->day; + data[4] = time->hour; + data[5] = time->minute; + data[6] = time->second; + error = addChunk(out, "tIME", data, 7); + myfree(data); + return error; +} + +static unsigned addChunk_pHYs(ucvector* out, const LodePNGInfo* info) +{ + unsigned error = 0; + ucvector data; + ucvector_init(&data); + + lodepng_add32bitInt(&data, info->phys_x); + lodepng_add32bitInt(&data, info->phys_y); + ucvector_push_back(&data, info->phys_unit); + + error = addChunk(out, "pHYs", data.data, data.size); + ucvector_cleanup(&data); + + return error; +} + +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +static void filterScanline(unsigned char* out, const unsigned char* scanline, const unsigned char* prevline, + size_t length, size_t bytewidth, unsigned char filterType) +{ + size_t i; + switch(filterType) + { + case 0: /*None*/ + for(i = 0; i < length; i++) out[i] = scanline[i]; + break; + case 1: /*Sub*/ + if(prevline) + { + for(i = 0; i < bytewidth; i++) out[i] = scanline[i]; + for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth]; + } + else + { + for(i = 0; i < bytewidth; i++) out[i] = scanline[i]; + for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth]; + } + break; + case 2: /*Up*/ + if(prevline) + { + for(i = 0; i < length; i++) out[i] = scanline[i] - prevline[i]; + } + else + { + for(i = 0; i < length; i++) out[i] = scanline[i]; + } + break; + case 3: /*Average*/ + if(prevline) + { + for(i = 0; i < bytewidth; i++) out[i] = scanline[i] - prevline[i] / 2; + for(i = bytewidth; i < length; i++) out[i] = scanline[i] - ((scanline[i - bytewidth] + prevline[i]) / 2); + } + else + { + for(i = 0; i < bytewidth; i++) out[i] = scanline[i]; + for(i = bytewidth; i < length; i++) out[i] = scanline[i] - scanline[i - bytewidth] / 2; + } + break; + case 4: /*Paeth*/ + if(prevline) + { + /*paethPredictor(0, prevline[i], 0) is always prevline[i]*/ + for(i = 0; i < bytewidth; i++) out[i] = (scanline[i] - prevline[i]); + for(i = bytewidth; i < length; i++) + { + out[i] = (scanline[i] - paethPredictor(scanline[i - bytewidth], prevline[i], prevline[i - bytewidth])); + } + } + else + { + for(i = 0; i < bytewidth; i++) out[i] = scanline[i]; + /*paethPredictor(scanline[i - bytewidth], 0, 0) is always scanline[i - bytewidth]*/ + for(i = bytewidth; i < length; i++) out[i] = (scanline[i] - scanline[i - bytewidth]); + } + break; + default: return; /*unexisting filter type given*/ + } +} + +/* log2 approximation. A slight bit faster than std::log. */ +static float flog2(float f) +{ + float result = 0; + while(f > 32) { result += 4; f /= 16; } + while(f > 2) { result++; f /= 2; } + return result + 1.442695f * (f * f * f / 3 - 3 * f * f / 2 + 3 * f - 1.83333f); +} + +static unsigned filter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, + const LodePNGColorMode* info, const LodePNGEncoderSettings* settings) +{ + /* + For PNG filter method 0 + out must be a buffer with as size: h + (w * h * bpp + 7) / 8, because there are + the scanlines with 1 extra byte per scanline + */ + + unsigned bpp = lodepng_get_bpp(info); + /*the width of a scanline in bytes, not including the filter type*/ + size_t linebytes = (w * bpp + 7) / 8; + /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ + size_t bytewidth = (bpp + 7) / 8; + const unsigned char* prevline = 0; + unsigned x, y; + unsigned error = 0; + LodePNGFilterStrategy strategy = settings->filter_strategy; + + /* + There is a heuristic called the minimum sum of absolute differences heuristic, suggested by the PNG standard: + * If the image type is Palette, or the bit depth is smaller than 8, then do not filter the image (i.e. + use fixed filtering, with the filter None). + * (The other case) If the image type is Grayscale or RGB (with or without Alpha), and the bit depth is + not smaller than 8, then use adaptive filtering heuristic as follows: independently for each row, apply + all five filters and select the filter that produces the smallest sum of absolute values per row. + This heuristic is used if filter strategy is LFS_MINSUM and filter_palette_zero is true. + + If filter_palette_zero is true and filter_strategy is not LFS_MINSUM, the above heuristic is followed, + but for "the other case", whatever strategy filter_strategy is set to instead of the minimum sum + heuristic is used. + */ + if(settings->filter_palette_zero && + (info->colortype == LCT_PALETTE || info->bitdepth < 8)) strategy = LFS_ZERO; + + if(bpp == 0) return 31; /*error: invalid color type*/ + + if(strategy == LFS_ZERO) + { + for(y = 0; y < h; y++) + { + size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ + size_t inindex = linebytes * y; + out[outindex] = 0; /*filter type byte*/ + filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, 0); + prevline = &in[inindex]; + } + } + else if(strategy == LFS_MINSUM) + { + /*adaptive filtering*/ + size_t sum[5]; + ucvector attempt[5]; /*five filtering attempts, one for each filter type*/ + size_t smallest = 0; + unsigned type, bestType = 0; + + for(type = 0; type < 5; type++) + { + ucvector_init(&attempt[type]); + if(!ucvector_resize(&attempt[type], linebytes)) return 83; /*alloc fail*/ + } + + if(!error) + { + for(y = 0; y < h; y++) + { + /*try the 5 filter types*/ + for(type = 0; type < 5; type++) + { + filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type); + + /*calculate the sum of the result*/ + sum[type] = 0; + if(type == 0) + { + for(x = 0; x < linebytes; x++) sum[type] += (unsigned char)(attempt[type].data[x]); + } + else + { + for(x = 0; x < linebytes; x++) + { + /*For differences, each byte should be treated as signed, values above 127 are negative + (converted to signed char). Filtertype 0 isn't a difference though, so use unsigned there. + This means filtertype 0 is almost never chosen, but that is justified.*/ + signed char s = (signed char)(attempt[type].data[x]); + sum[type] += s < 0 ? -s : s; + } + } + + /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ + if(type == 0 || sum[type] < smallest) + { + bestType = type; + smallest = sum[type]; + } + } + + prevline = &in[y * linebytes]; + + /*now fill the out values*/ + out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ + for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x]; + } + } + + for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]); + } + else if(strategy == LFS_ENTROPY) + { + float sum[5]; + ucvector attempt[5]; /*five filtering attempts, one for each filter type*/ + float smallest = 0; + unsigned type, bestType = 0; + unsigned count[256]; + + for(type = 0; type < 5; type++) + { + ucvector_init(&attempt[type]); + if(!ucvector_resize(&attempt[type], linebytes)) return 83; /*alloc fail*/ + } + + for(y = 0; y < h; y++) + { + /*try the 5 filter types*/ + for(type = 0; type < 5; type++) + { + filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type); + for(x = 0; x < 256; x++) count[x] = 0; + for(x = 0; x < linebytes; x++) count[attempt[type].data[x]]++; + count[type]++; /*the filter type itself is part of the scanline*/ + sum[type] = 0; + for(x = 0; x < 256; x++) + { + float p = count[x] / (float)(linebytes + 1); + sum[type] += count[x] == 0 ? 0 : flog2(1 / p) * p; + } + /*check if this is smallest sum (or if type == 0 it's the first case so always store the values)*/ + if(type == 0 || sum[type] < smallest) + { + bestType = type; + smallest = sum[type]; + } + } + + prevline = &in[y * linebytes]; + + /*now fill the out values*/ + out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ + for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x]; + } + + for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]); + } + else if(strategy == LFS_PREDEFINED) + { + for(y = 0; y < h; y++) + { + size_t outindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ + size_t inindex = linebytes * y; + unsigned type = settings->predefined_filters[y]; + out[outindex] = type; /*filter type byte*/ + filterScanline(&out[outindex + 1], &in[inindex], prevline, linebytes, bytewidth, type); + prevline = &in[inindex]; + } + } + else if(strategy == LFS_BRUTE_FORCE) + { + /*brute force filter chooser. + deflate the scanline after every filter attempt to see which one deflates best. + This is very slow and gives only slightly smaller, sometimes even larger, result*/ + size_t size[5]; + ucvector attempt[5]; /*five filtering attempts, one for each filter type*/ + size_t smallest = 0; + unsigned type = 0, bestType = 0; + unsigned char* dummy; + LodePNGCompressSettings zlibsettings = settings->zlibsettings; + /*use fixed tree on the attempts so that the tree is not adapted to the filtertype on purpose, + to simulate the true case where the tree is the same for the whole image. Sometimes it gives + better result with dynamic tree anyway. Using the fixed tree sometimes gives worse, but in rare + cases better compression. It does make this a bit less slow, so it's worth doing this.*/ + zlibsettings.btype = 1; + /*a custom encoder likely doesn't read the btype setting and is optimized for complete PNG + images only, so disable it*/ + zlibsettings.custom_zlib = 0; + zlibsettings.custom_deflate = 0; + for(type = 0; type < 5; type++) + { + ucvector_init(&attempt[type]); + ucvector_resize(&attempt[type], linebytes); /*todo: give error if resize failed*/ + } + for(y = 0; y < h; y++) /*try the 5 filter types*/ + { + for(type = 0; type < 5; type++) + { + unsigned testsize = attempt[type].size; + /*if(testsize > 8) testsize /= 8;*/ /*it already works good enough by testing a part of the row*/ + + filterScanline(attempt[type].data, &in[y * linebytes], prevline, linebytes, bytewidth, type); + size[type] = 0; + dummy = 0; + zlib_compress(&dummy, &size[type], attempt[type].data, testsize, &zlibsettings); + myfree(dummy); + /*check if this is smallest size (or if type == 0 it's the first case so always store the values)*/ + if(type == 0 || size[type] < smallest) + { + bestType = type; + smallest = size[type]; + } + } + prevline = &in[y * linebytes]; + out[y * (linebytes + 1)] = bestType; /*the first byte of a scanline will be the filter type*/ + for(x = 0; x < linebytes; x++) out[y * (linebytes + 1) + 1 + x] = attempt[bestType].data[x]; + } + for(type = 0; type < 5; type++) ucvector_cleanup(&attempt[type]); + } + else return 88; /* unknown filter strategy */ + + return error; +} + +static void addPaddingBits(unsigned char* out, const unsigned char* in, + size_t olinebits, size_t ilinebits, unsigned h) +{ + /*The opposite of the removePaddingBits function + olinebits must be >= ilinebits*/ + unsigned y; + size_t diff = olinebits - ilinebits; + size_t obp = 0, ibp = 0; /*bit pointers*/ + for(y = 0; y < h; y++) + { + size_t x; + for(x = 0; x < ilinebits; x++) + { + unsigned char bit = readBitFromReversedStream(&ibp, in); + setBitOfReversedStream(&obp, out, bit); + } + /*obp += diff; --> no, fill in some value in the padding bits too, to avoid + "Use of uninitialised value of size ###" warning from valgrind*/ + for(x = 0; x < diff; x++) setBitOfReversedStream(&obp, out, 0); + } +} + +/* +in: non-interlaced image with size w*h +out: the same pixels, but re-ordered according to PNG's Adam7 interlacing, with + no padding bits between scanlines, but between reduced images so that each + reduced image starts at a byte. +bpp: bits per pixel +there are no padding bits, not between scanlines, not between reduced images +in has the following size in bits: w * h * bpp. +out is possibly bigger due to padding bits between reduced images +NOTE: comments about padding bits are only relevant if bpp < 8 +*/ +static void Adam7_interlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) +{ + unsigned passw[7], passh[7]; + size_t filter_passstart[8], padded_passstart[8], passstart[8]; + unsigned i; + + Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); + + if(bpp >= 8) + { + for(i = 0; i < 7; i++) + { + unsigned x, y, b; + size_t bytewidth = bpp / 8; + for(y = 0; y < passh[i]; y++) + for(x = 0; x < passw[i]; x++) + { + size_t pixelinstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; + size_t pixeloutstart = passstart[i] + (y * passw[i] + x) * bytewidth; + for(b = 0; b < bytewidth; b++) + { + out[pixeloutstart + b] = in[pixelinstart + b]; + } + } + } + } + else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ + { + for(i = 0; i < 7; i++) + { + unsigned x, y, b; + unsigned ilinebits = bpp * passw[i]; + unsigned olinebits = bpp * w; + size_t obp, ibp; /*bit pointers (for out and in buffer)*/ + for(y = 0; y < passh[i]; y++) + for(x = 0; x < passw[i]; x++) + { + ibp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; + obp = (8 * passstart[i]) + (y * ilinebits + x * bpp); + for(b = 0; b < bpp; b++) + { + unsigned char bit = readBitFromReversedStream(&ibp, in); + setBitOfReversedStream(&obp, out, bit); + } + } + } + } +} + +/*out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image. +return value is error**/ +static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in, + unsigned w, unsigned h, + const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings) +{ + /* + This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps: + *) if no Adam7: 1) add padding bits (= posible extra bits per scanline if bpp < 8) 2) filter + *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter + */ + unsigned bpp = lodepng_get_bpp(&info_png->color); + unsigned error = 0; + + if(info_png->interlace_method == 0) + { + *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/ + *out = (unsigned char*)mymalloc(*outsize); + if(!(*out) && (*outsize)) error = 83; /*alloc fail*/ + + if(!error) + { + /*non multiple of 8 bits per scanline, padding bits needed per scanline*/ + if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) + { + ucvector padded; + ucvector_init(&padded); + if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 83; /*alloc fail*/ + if(!error) + { + addPaddingBits(padded.data, in, ((w * bpp + 7) / 8) * 8, w * bpp, h); + error = filter(*out, padded.data, w, h, &info_png->color, settings); + } + ucvector_cleanup(&padded); + } + else + { + /*we can immediatly filter into the out buffer, no other steps needed*/ + error = filter(*out, in, w, h, &info_png->color, settings); + } + } + } + else /*interlace_method is 1 (Adam7)*/ + { + /*the alloc size is an estimate: 6 bytes are added for worse-case padding bits between the 7 passes.*/ + unsigned char* adam7 = (unsigned char*)mymalloc((h * w * bpp + 7) / 8 + 6); + if(!adam7 && ((h * w * bpp + 7) / 8)) error = 83; /*alloc fail*/ + + while(!error) /*not a real while loop, used to break out to cleanup to avoid a goto*/ + { + unsigned passw[7], passh[7]; + size_t filter_passstart[8], padded_passstart[8], passstart[8]; + unsigned i; + + Adam7_interlace(adam7, in, w, h, bpp); + Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); + + *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/ + *out = (unsigned char*)mymalloc(*outsize); + if(!(*out) && (*outsize)) ERROR_BREAK(83 /*alloc fail*/); + + for(i = 0; i < 7; i++) + { + if(bpp < 8) + { + ucvector padded; + ucvector_init(&padded); + if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 83; /*alloc fail*/ + if(!error) + { + addPaddingBits(&padded.data[padded_passstart[i]], &adam7[passstart[i]], + ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]); + error = filter(&(*out)[filter_passstart[i]], &padded.data[padded_passstart[i]], + passw[i], passh[i], &info_png->color, settings); + } + + ucvector_cleanup(&padded); + } + else + { + error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]], + passw[i], passh[i], &info_png->color, settings); + } + } + + break; + } + + myfree(adam7); + } + + return error; +} + +/* +palette must have 4 * palettesize bytes allocated, and given in format RGBARGBARGBARGBA... +returns 0 if the palette is opaque, +returns 1 if the palette has a single color with alpha 0 ==> color key +returns 2 if the palette is semi-translucent. +*/ +static unsigned getPaletteTranslucency(const unsigned char* palette, size_t palettesize) +{ + size_t i, key = 0; + unsigned r = 0, g = 0, b = 0; /*the value of the color with alpha 0, so long as color keying is possible*/ + for(i = 0; i < palettesize; i++) + { + if(!key && palette[4 * i + 3] == 0) + { + r = palette[4 * i + 0]; g = palette[4 * i + 1]; b = palette[4 * i + 2]; + key = 1; + i = (size_t)(-1); /*restart from beginning, to detect earlier opaque colors with key's value*/ + } + else if(palette[4 * i + 3] != 255) return 2; + /*when key, no opaque RGB may have key's RGB*/ + else if(key && r == palette[i * 4 + 0] && g == palette[i * 4 + 1] && b == palette[i * 4 + 2]) return 2; + } + return key; +} + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS +static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize) +{ + unsigned char* inchunk = data; + while((size_t)(inchunk - data) < datasize) + { + CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk)); + out->allocsize = out->size; /*fix the allocsize again*/ + inchunk = lodepng_chunk_next(inchunk); + } + return 0; +} +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +unsigned lodepng_encode(unsigned char** out, size_t* outsize, + const unsigned char* image, unsigned w, unsigned h, + LodePNGState* state) +{ + LodePNGInfo info; + ucvector outv; + unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ + size_t datasize = 0; + + /*provide some proper output values if error will happen*/ + *out = 0; + *outsize = 0; + state->error = 0; + + lodepng_info_init(&info); + lodepng_info_copy(&info, &state->info_png); + + if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette) + && (info.color.palettesize == 0 || info.color.palettesize > 256)) + { + state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ + return state->error; + } + + if(state->encoder.auto_convert != LAC_NO) + { + state->error = doAutoChooseColor(&info.color, image, w, h, &state->info_raw, + state->encoder.auto_convert); + } + if(state->error) return state->error; + + if(state->encoder.zlibsettings.windowsize > 32768) + { + CERROR_RETURN_ERROR(state->error, 60); /*error: windowsize larger than allowed*/ + } + if(state->encoder.zlibsettings.btype > 2) + { + CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/ + } + if(state->info_png.interlace_method > 1) + { + CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/ + } + + state->error = checkColorValidity(info.color.colortype, info.color.bitdepth); + if(state->error) return state->error; /*error: unexisting color type given*/ + state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); + if(state->error) return state->error; /*error: unexisting color type given*/ + + if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) + { + unsigned char* converted; + size_t size = (w * h * lodepng_get_bpp(&info.color) + 7) / 8; + + converted = (unsigned char*)mymalloc(size); + if(!converted && size) state->error = 83; /*alloc fail*/ + if(!state->error) + { + state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); + } + if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); + myfree(converted); + } + else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); + + ucvector_init(&outv); + while(!state->error) /*while only executed once, to break on error*/ + { +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + size_t i; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + /*write signature and chunks*/ + writeSignature(&outv); + /*IHDR*/ + addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method); +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*unknown chunks between IHDR and PLTE*/ + if(info.unknown_chunks_data[0]) + { + state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]); + if(state->error) break; + } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + /*PLTE*/ + if(info.color.colortype == LCT_PALETTE) + { + addChunk_PLTE(&outv, &info.color); + } + if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA)) + { + addChunk_PLTE(&outv, &info.color); + } + /*tRNS*/ + if(info.color.colortype == LCT_PALETTE && getPaletteTranslucency(info.color.palette, info.color.palettesize) != 0) + { + addChunk_tRNS(&outv, &info.color); + } + if((info.color.colortype == LCT_GREY || info.color.colortype == LCT_RGB) && info.color.key_defined) + { + addChunk_tRNS(&outv, &info.color); + } +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*bKGD (must come between PLTE and the IDAt chunks*/ + if(info.background_defined) addChunk_bKGD(&outv, &info); + /*pHYs (must come before the IDAT chunks)*/ + if(info.phys_defined) addChunk_pHYs(&outv, &info); + + /*unknown chunks between PLTE and IDAT*/ + if(info.unknown_chunks_data[1]) + { + state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]); + if(state->error) break; + } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + /*IDAT (multiple IDAT chunks must be consecutive)*/ + state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings); + if(state->error) break; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*tIME*/ + if(info.time_defined) addChunk_tIME(&outv, &info.time); + /*tEXt and/or zTXt*/ + for(i = 0; i < info.text_num; i++) + { + if(strlen(info.text_keys[i]) > 79) + { + state->error = 66; /*text chunk too large*/ + break; + } + if(strlen(info.text_keys[i]) < 1) + { + state->error = 67; /*text chunk too small*/ + break; + } + if(state->encoder.text_compression) + addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings); + else + addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]); + } + /*LodePNG version id in text chunk*/ + if(state->encoder.add_id) + { + unsigned alread_added_id_text = 0; + for(i = 0; i < info.text_num; i++) + { + if(!strcmp(info.text_keys[i], "LodePNG")) + { + alread_added_id_text = 1; + break; + } + } + if(alread_added_id_text == 0) + addChunk_tEXt(&outv, "LodePNG", VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/ + } + /*iTXt*/ + for(i = 0; i < info.itext_num; i++) + { + if(strlen(info.itext_keys[i]) > 79) + { + state->error = 66; /*text chunk too large*/ + break; + } + if(strlen(info.itext_keys[i]) < 1) + { + state->error = 67; /*text chunk too small*/ + break; + } + addChunk_iTXt(&outv, state->encoder.text_compression, + info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i], + &state->encoder.zlibsettings); + } + + /*unknown chunks between IDAT and IEND*/ + if(info.unknown_chunks_data[2]) + { + state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]); + if(state->error) break; + } +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + /*IEND*/ + addChunk_IEND(&outv); + + break; /*this isn't really a while loop; no error happened so break out now!*/ + } + + lodepng_info_cleanup(&info); + myfree(data); + /*instead of cleaning the vector up, give it to the output*/ + *out = outv.data; + *outsize = outv.size; + + return state->error; +} + +unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, const unsigned char* image, + unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) +{ + unsigned error; + LodePNGState state; + lodepng_state_init(&state); + state.info_raw.colortype = colortype; + state.info_raw.bitdepth = bitdepth; + state.info_png.color.colortype = colortype; + state.info_png.color.bitdepth = bitdepth; + lodepng_encode(out, outsize, image, w, h, &state); + error = state.error; + lodepng_state_cleanup(&state); + return error; +} + +unsigned lodepng_encode32(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) +{ + return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGBA, 8); +} + +unsigned lodepng_encode24(unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h) +{ + return lodepng_encode_memory(out, outsize, image, w, h, LCT_RGB, 8); +} + +#ifdef LODEPNG_COMPILE_DISK +unsigned lodepng_encode_file(const char* filename, const unsigned char* image, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth) +{ + unsigned char* buffer; + size_t buffersize; + unsigned error = lodepng_encode_memory(&buffer, &buffersize, image, w, h, colortype, bitdepth); + if(!error) error = lodepng_save_file(buffer, buffersize, filename); + myfree(buffer); + return error; +} + +unsigned lodepng_encode32_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) +{ + return lodepng_encode_file(filename, image, w, h, LCT_RGBA, 8); +} + +unsigned lodepng_encode24_file(const char* filename, const unsigned char* image, unsigned w, unsigned h) +{ + return lodepng_encode_file(filename, image, w, h, LCT_RGB, 8); +} +#endif /*LODEPNG_COMPILE_DISK*/ + +void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) +{ + lodepng_compress_settings_init(&settings->zlibsettings); + settings->filter_palette_zero = 1; + settings->filter_strategy = LFS_MINSUM; + settings->auto_convert = LAC_AUTO; + settings->force_palette = 0; + settings->predefined_filters = 0; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + settings->add_id = 0; + settings->text_compression = 1; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +} + +#endif /*LODEPNG_COMPILE_ENCODER*/ +#endif /*LODEPNG_COMPILE_PNG*/ + +#ifdef LODEPNG_COMPILE_ERROR_TEXT +/* +This returns the description of a numerical error code in English. This is also +the documentation of all the error codes. +*/ +const char* lodepng_error_text(unsigned code) +{ + switch(code) + { + case 0: return "no error, everything went ok"; + case 1: return "nothing done yet"; /*the Encoder/Decoder has done nothing yet, error checking makes no sense yet*/ + case 10: return "end of input memory reached without huffman end code"; /*while huffman decoding*/ + case 11: return "error in code tree made it jump outside of huffman tree"; /*while huffman decoding*/ + case 13: return "problem while processing dynamic deflate block"; + case 14: return "problem while processing dynamic deflate block"; + case 15: return "problem while processing dynamic deflate block"; + case 16: return "unexisting code while processing dynamic deflate block"; + case 17: return "end of out buffer memory reached while inflating"; + case 18: return "invalid distance code while inflating"; + case 19: return "end of out buffer memory reached while inflating"; + case 20: return "invalid deflate block BTYPE encountered while decoding"; + case 21: return "NLEN is not ones complement of LEN in a deflate block"; + /*end of out buffer memory reached while inflating: + This can happen if the inflated deflate data is longer than the amount of bytes required to fill up + all the pixels of the image, given the color depth and image dimensions. Something that doesn't + happen in a normal, well encoded, PNG image.*/ + case 22: return "end of out buffer memory reached while inflating"; + case 23: return "end of in buffer memory reached while inflating"; + case 24: return "invalid FCHECK in zlib header"; + case 25: return "invalid compression method in zlib header"; + case 26: return "FDICT encountered in zlib header while it's not used for PNG"; + case 27: return "PNG file is smaller than a PNG header"; + /*Checks the magic file header, the first 8 bytes of the PNG file*/ + case 28: return "incorrect PNG signature, it's no PNG or corrupted"; + case 29: return "first chunk is not the header chunk"; + case 30: return "chunk length too large, chunk broken off at end of file"; + case 31: return "illegal PNG color type or bpp"; + case 32: return "illegal PNG compression method"; + case 33: return "illegal PNG filter method"; + case 34: return "illegal PNG interlace method"; + case 35: return "chunk length of a chunk is too large or the chunk too small"; + case 36: return "illegal PNG filter type encountered"; + case 37: return "illegal bit depth for this color type given"; + case 38: return "the palette is too big"; /*more than 256 colors*/ + case 39: return "more palette alpha values given in tRNS chunk than there are colors in the palette"; + case 40: return "tRNS chunk has wrong size for greyscale image"; + case 41: return "tRNS chunk has wrong size for RGB image"; + case 42: return "tRNS chunk appeared while it was not allowed for this color type"; + case 43: return "bKGD chunk has wrong size for palette image"; + case 44: return "bKGD chunk has wrong size for greyscale image"; + case 45: return "bKGD chunk has wrong size for RGB image"; + /*Is the palette too small?*/ + case 46: return "a value in indexed image is larger than the palette size (bitdepth = 8)"; + /*Is the palette too small?*/ + case 47: return "a value in indexed image is larger than the palette size (bitdepth < 8)"; + /*the input data is empty, maybe a PNG file doesn't exist or is in the wrong path*/ + case 48: return "empty input or file doesn't exist"; + case 49: return "jumped past memory while generating dynamic huffman tree"; + case 50: return "jumped past memory while generating dynamic huffman tree"; + case 51: return "jumped past memory while inflating huffman block"; + case 52: return "jumped past memory while inflating"; + case 53: return "size of zlib data too small"; + case 54: return "repeat symbol in tree while there was no value symbol yet"; + /*jumped past tree while generating huffman tree, this could be when the + tree will have more leaves than symbols after generating it out of the + given lenghts. They call this an oversubscribed dynamic bit lengths tree in zlib.*/ + case 55: return "jumped past tree while generating huffman tree"; + case 56: return "given output image colortype or bitdepth not supported for color conversion"; + case 57: return "invalid CRC encountered (checking CRC can be disabled)"; + case 58: return "invalid ADLER32 encountered (checking ADLER32 can be disabled)"; + case 59: return "requested color conversion not supported"; + case 60: return "invalid window size given in the settings of the encoder (must be 0-32768)"; + case 61: return "invalid BTYPE given in the settings of the encoder (only 0, 1 and 2 are allowed)"; + /*LodePNG leaves the choice of RGB to greyscale conversion formula to the user.*/ + case 62: return "conversion from color to greyscale not supported"; + case 63: return "length of a chunk too long, max allowed for PNG is 2147483647 bytes per chunk"; /*(2^31-1)*/ + /*this would result in the inability of a deflated block to ever contain an end code. It must be at least 1.*/ + case 64: return "the length of the END symbol 256 in the Huffman tree is 0"; + case 66: return "the length of a text chunk keyword given to the encoder is longer than the maximum of 79 bytes"; + case 67: return "the length of a text chunk keyword given to the encoder is smaller than the minimum of 1 byte"; + case 68: return "tried to encode a PLTE chunk with a palette that has less than 1 or more than 256 colors"; + case 69: return "unknown chunk type with 'critical' flag encountered by the decoder"; + case 71: return "unexisting interlace mode given to encoder (must be 0 or 1)"; + case 72: return "while decoding, unexisting compression method encountering in zTXt or iTXt chunk (it must be 0)"; + case 73: return "invalid tIME chunk size"; + case 74: return "invalid pHYs chunk size"; + /*length could be wrong, or data chopped off*/ + case 75: return "no null termination char found while decoding text chunk"; + case 76: return "iTXt chunk too short to contain required bytes"; + case 77: return "integer overflow in buffer size"; + case 78: return "failed to open file for reading"; /*file doesn't exist or couldn't be opened for reading*/ + case 79: return "failed to open file for writing"; + case 80: return "tried creating a tree of 0 symbols"; + case 81: return "lazy matching at pos 0 is impossible"; + case 82: return "color conversion to palette requested while a color isn't in palette"; + case 83: return "memory allocation failed"; + case 84: return "given image too small to contain all pixels to be encoded"; + case 85: return "internal color conversion bug"; + case 86: return "impossible offset in lz77 encoding (internal bug)"; + case 87: return "must provide custom zlib function pointer if LODEPNG_COMPILE_ZLIB is not defined"; + case 88: return "invalid filter strategy given for LodePNGEncoderSettings.filter_strategy"; + } + return "unknown error code"; +} +#endif /*LODEPNG_COMPILE_ERROR_TEXT*/ + +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* // C++ Wrapper // */ +/* ////////////////////////////////////////////////////////////////////////// */ +/* ////////////////////////////////////////////////////////////////////////// */ + + +#ifdef LODEPNG_COMPILE_CPP +namespace lodepng +{ + +#ifdef LODEPNG_COMPILE_DISK +void load_file(std::vector& buffer, const std::string& filename) +{ + std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate); + + /*get filesize*/ + std::streamsize size = 0; + if(file.seekg(0, std::ios::end).good()) size = file.tellg(); + if(file.seekg(0, std::ios::beg).good()) size -= file.tellg(); + + /*read contents of the file into the vector*/ + buffer.resize(size_t(size)); + if(size > 0) file.read((char*)(&buffer[0]), size); +} + +/*write given buffer to the file, overwriting the file, it doesn't append to it.*/ +void save_file(const std::vector& buffer, const std::string& filename) +{ + std::ofstream file(filename.c_str(), std::ios::out|std::ios::binary); + file.write(buffer.empty() ? 0 : (char*)&buffer[0], std::streamsize(buffer.size())); +} +#endif //LODEPNG_COMPILE_DISK + +#ifdef LODEPNG_COMPILE_ZLIB +#ifdef LODEPNG_COMPILE_DECODER +unsigned decompress(std::vector& out, const unsigned char* in, size_t insize, + const LodePNGDecompressSettings& settings) +{ + unsigned char* buffer = 0; + size_t buffersize = 0; + unsigned error = zlib_decompress(&buffer, &buffersize, in, insize, &settings); + if(buffer) + { + out.insert(out.end(), &buffer[0], &buffer[buffersize]); + myfree(buffer); + } + return error; +} + +unsigned decompress(std::vector& out, const std::vector& in, + const LodePNGDecompressSettings& settings) +{ + return decompress(out, in.empty() ? 0 : &in[0], in.size(), settings); +} +#endif //LODEPNG_COMPILE_DECODER + +#ifdef LODEPNG_COMPILE_ENCODER +unsigned compress(std::vector& out, const unsigned char* in, size_t insize, + const LodePNGCompressSettings& settings) +{ + unsigned char* buffer = 0; + size_t buffersize = 0; + unsigned error = zlib_compress(&buffer, &buffersize, in, insize, &settings); + if(buffer) + { + out.insert(out.end(), &buffer[0], &buffer[buffersize]); + myfree(buffer); + } + return error; +} + +unsigned compress(std::vector& out, const std::vector& in, + const LodePNGCompressSettings& settings) +{ + return compress(out, in.empty() ? 0 : &in[0], in.size(), settings); +} +#endif //LODEPNG_COMPILE_ENCODER +#endif //LODEPNG_COMPILE_ZLIB + + +#ifdef LODEPNG_COMPILE_PNG + +State::State() +{ + lodepng_state_init(this); +} + +State::State(const State& other) +{ + lodepng_state_init(this); + lodepng_state_copy(this, &other); +} + +State::~State() +{ + lodepng_state_cleanup(this); +} + +State& State::operator=(const State& other) +{ + lodepng_state_copy(this, &other); + return *this; +} + +#ifdef LODEPNG_COMPILE_DECODER + +unsigned decode(std::vector& out, unsigned& w, unsigned& h, const unsigned char* in, + size_t insize, LodePNGColorType colortype, unsigned bitdepth) +{ + unsigned char* buffer; + unsigned error = lodepng_decode_memory(&buffer, &w, &h, in, insize, colortype, bitdepth); + if(buffer && !error) + { + State state; + state.info_raw.colortype = colortype; + state.info_raw.bitdepth = bitdepth; + size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw); + out.insert(out.end(), &buffer[0], &buffer[buffersize]); + myfree(buffer); + } + return error; +} + +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + const std::vector& in, LodePNGColorType colortype, unsigned bitdepth) +{ + return decode(out, w, h, in.empty() ? 0 : &in[0], (unsigned)in.size(), colortype, bitdepth); +} + +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + State& state, + const unsigned char* in, size_t insize) +{ + unsigned char* buffer; + unsigned error = lodepng_decode(&buffer, &w, &h, &state, in, insize); + if(buffer && !error) + { + size_t buffersize = lodepng_get_raw_size(w, h, &state.info_raw); + out.insert(out.end(), &buffer[0], &buffer[buffersize]); + myfree(buffer); + } + return error; +} + +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + State& state, + const std::vector& in) +{ + return decode(out, w, h, state, in.empty() ? 0 : &in[0], in.size()); +} + +#ifdef LODEPNG_COMPILE_DISK +unsigned decode(std::vector& out, unsigned& w, unsigned& h, const std::string& filename, + LodePNGColorType colortype, unsigned bitdepth) +{ + std::vector buffer; + load_file(buffer, filename); + return decode(out, w, h, buffer, colortype, bitdepth); +} +#endif //LODEPNG_COMPILE_DECODER +#endif //LODEPNG_COMPILE_DISK + +#ifdef LODEPNG_COMPILE_ENCODER +unsigned encode(std::vector& out, const unsigned char* in, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth) +{ + unsigned char* buffer; + size_t buffersize; + unsigned error = lodepng_encode_memory(&buffer, &buffersize, in, w, h, colortype, bitdepth); + if(buffer) + { + out.insert(out.end(), &buffer[0], &buffer[buffersize]); + myfree(buffer); + } + return error; +} + +unsigned encode(std::vector& out, + const std::vector& in, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth) +{ + if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84; + return encode(out, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth); +} + +unsigned encode(std::vector& out, + const unsigned char* in, unsigned w, unsigned h, + State& state) +{ + unsigned char* buffer; + size_t buffersize; + unsigned error = lodepng_encode(&buffer, &buffersize, in, w, h, &state); + if(buffer) + { + out.insert(out.end(), &buffer[0], &buffer[buffersize]); + myfree(buffer); + } + return error; +} + +unsigned encode(std::vector& out, + const std::vector& in, unsigned w, unsigned h, + State& state) +{ + if(lodepng_get_raw_size(w, h, &state.info_raw) > in.size()) return 84; + return encode(out, in.empty() ? 0 : &in[0], w, h, state); +} + +#ifdef LODEPNG_COMPILE_DISK +unsigned encode(const std::string& filename, + const unsigned char* in, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth) +{ + std::vector buffer; + unsigned error = encode(buffer, in, w, h, colortype, bitdepth); + if(!error) save_file(buffer, filename); + return error; +} + +unsigned encode(const std::string& filename, + const std::vector& in, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth) +{ + if(lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size()) return 84; + return encode(filename, in.empty() ? 0 : &in[0], w, h, colortype, bitdepth); +} +#endif //LODEPNG_COMPILE_DISK +#endif //LODEPNG_COMPILE_ENCODER +#endif //LODEPNG_COMPILE_PNG +} //namespace lodepng +#endif /*LODEPNG_COMPILE_CPP*/ diff --git a/Shadows/SampleFramework11/LodePNG/lodepng.h b/Shadows/SampleFramework11/LodePNG/lodepng.h new file mode 100644 index 0000000..b2ad200 --- /dev/null +++ b/Shadows/SampleFramework11/LodePNG/lodepng.h @@ -0,0 +1,1655 @@ +/* +LodePNG version 20121014 + +Copyright (c) 2005-2012 Lode Vandevenne + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*/ + +#ifndef LODEPNG_H +#define LODEPNG_H + +#include /*for size_t*/ + +#ifdef __cplusplus +#include +#include +#endif /*__cplusplus*/ + +/* +The following #defines are used to create code sections. They can be disabled +to disable code sections, which can give faster compile time and smaller binary. +The "NO_COMPILE" defines are designed to be used to pass as defines to the +compiler command to disable them without modifying this header, e.g. +-DLODEPNG_NO_COMPILE_ZLIB for gcc. +*/ +/*deflate & zlib. If disabled, you must specify alternative zlib functions in +the custom_zlib field of the compress and decompress settings*/ +#ifndef LODEPNG_NO_COMPILE_ZLIB +#define LODEPNG_COMPILE_ZLIB +#endif +/*png encoder and png decoder*/ +#ifndef LODEPNG_NO_COMPILE_PNG +#define LODEPNG_COMPILE_PNG +#endif +/*deflate&zlib decoder and png decoder*/ +#ifndef LODEPNG_NO_COMPILE_DECODER +#define LODEPNG_COMPILE_DECODER +#endif +/*deflate&zlib encoder and png encoder*/ +#ifndef LODEPNG_NO_COMPILE_ENCODER +#define LODEPNG_COMPILE_ENCODER +#endif +/*the optional built in harddisk file loading and saving functions*/ +#ifndef LODEPNG_NO_COMPILE_DISK +#define LODEPNG_COMPILE_DISK +#endif +/*support for chunks other than IHDR, IDAT, PLTE, tRNS, IEND: ancillary and unknown chunks*/ +#ifndef LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS +#define LODEPNG_COMPILE_ANCILLARY_CHUNKS +#endif +/*ability to convert error numerical codes to English text string*/ +#ifndef LODEPNG_NO_COMPILE_ERROR_TEXT +#define LODEPNG_COMPILE_ERROR_TEXT +#endif +/*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/ +#ifdef __cplusplus +#ifndef LODEPNG_NO_COMPILE_CPP +#define LODEPNG_COMPILE_CPP +#endif +#endif + +#ifdef LODEPNG_COMPILE_PNG +/*The PNG color types (also used for raw).*/ +typedef enum LodePNGColorType +{ + LCT_GREY = 0, /*greyscale: 1,2,4,8,16 bit*/ + LCT_RGB = 2, /*RGB: 8,16 bit*/ + LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/ + LCT_GREY_ALPHA = 4, /*greyscale with alpha: 8,16 bit*/ + LCT_RGBA = 6 /*RGB with alpha: 8,16 bit*/ +} LodePNGColorType; + +#ifdef LODEPNG_COMPILE_DECODER +/* +Converts PNG data in memory to raw pixel data. +out: Output parameter. Pointer to buffer that will contain the raw pixel data. + After decoding, its size is w * h * (bytes per pixel) bytes larger than + initially. Bytes per pixel depends on colortype and bitdepth. + Must be freed after usage with free(*out). +w: Output parameter. Pointer to width of pixel data. +h: Output parameter. Pointer to height of pixel data. +in: Memory buffer with the PNG file. +insize: size of the in buffer. +colortype: the desired color type for the raw output image. See explanation on PNG color types. +bitdepth: the desired bit depth for the raw output image. See explanation on PNG color types. +Return value: LodePNG error code (0 means no error). +*/ +unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h, + const unsigned char* in, size_t insize, + LodePNGColorType colortype, unsigned bitdepth); + +/*Same as lodepng_decode_memory, but always decodes to 32-bit RGBA raw image*/ +unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h, + const unsigned char* in, size_t insize); + +/*Same as lodepng_decode_memory, but always decodes to 24-bit RGB raw image*/ +unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h, + const unsigned char* in, size_t insize); + +#ifdef LODEPNG_COMPILE_DISK +/* +Load PNG from disk, from file with given name. +Same as the other decode functions, but instead takes a filename as input. +*/ +unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, + const char* filename, + LodePNGColorType colortype, unsigned bitdepth); + +/*Same as lodepng_decode_file, but always decodes to 32-bit RGBA raw image.*/ +unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h, + const char* filename); + +/*Same as lodepng_decode_file, but always decodes to 24-bit RGB raw image.*/ +unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, + const char* filename); +#endif /*LODEPNG_COMPILE_DISK*/ +#endif /*LODEPNG_COMPILE_DECODER*/ + + +#ifdef LODEPNG_COMPILE_ENCODER +/* +Converts raw pixel data into a PNG image in memory. The colortype and bitdepth + of the output PNG image cannot be chosen, they are automatically determined + by the colortype, bitdepth and content of the input pixel data. +out: Output parameter. Pointer to buffer that will contain the raw pixel data. + Must be freed after usage with free(*out). +outsize: Output parameter. Pointer to the size in bytes of the out buffer. +image: The raw pixel data to encode. The size of this buffer should be + w * h * (bytes per pixel), bytes per pixel depends on colortype and bitdepth. +w: width of the raw pixel data in pixels. +h: height of the raw pixel data in pixels. +colortype: the color type of the raw input image. See explanation on PNG color types. +bitdepth: the bit depth of the raw input image. See explanation on PNG color types. +Return value: LodePNG error code (0 means no error). +*/ +unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize, + const unsigned char* image, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth); + +/*Same as lodepng_encode_memory, but always encodes from 32-bit RGBA raw image.*/ +unsigned lodepng_encode32(unsigned char** out, size_t* outsize, + const unsigned char* image, unsigned w, unsigned h); + +/*Same as lodepng_encode_memory, but always encodes from 24-bit RGB raw image.*/ +unsigned lodepng_encode24(unsigned char** out, size_t* outsize, + const unsigned char* image, unsigned w, unsigned h); + +#ifdef LODEPNG_COMPILE_DISK +/* +Converts raw pixel data into a PNG file on disk. +Same as the other encode functions, but instead takes a filename as output. +NOTE: This overwrites existing files without warning! +*/ +unsigned lodepng_encode_file(const char* filename, + const unsigned char* image, unsigned w, unsigned h, + LodePNGColorType colortype, unsigned bitdepth); + +/*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image.*/ +unsigned lodepng_encode32_file(const char* filename, + const unsigned char* image, unsigned w, unsigned h); + +/*Same as lodepng_encode_file, but always encodes from 24-bit RGB raw image.*/ +unsigned lodepng_encode24_file(const char* filename, + const unsigned char* image, unsigned w, unsigned h); +#endif /*LODEPNG_COMPILE_DISK*/ +#endif /*LODEPNG_COMPILE_ENCODER*/ + + +#ifdef LODEPNG_COMPILE_CPP +namespace lodepng +{ +#ifdef LODEPNG_COMPILE_DECODER +/*Same as lodepng_decode_memory, but decodes to an std::vector.*/ +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + const unsigned char* in, size_t insize, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + const std::vector& in, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +#ifdef LODEPNG_COMPILE_DISK +/* +Converts PNG file from disk to raw pixel data in memory. +Same as the other decode functions, but instead takes a filename as input. +*/ +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + const std::string& filename, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +#endif //LODEPNG_COMPILE_DISK +#endif //LODEPNG_COMPILE_DECODER + +#ifdef LODEPNG_COMPILE_ENCODER +/*Same as lodepng_encode_memory, but encodes to an std::vector.*/ +unsigned encode(std::vector& out, + const unsigned char* in, unsigned w, unsigned h, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +unsigned encode(std::vector& out, + const std::vector& in, unsigned w, unsigned h, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +#ifdef LODEPNG_COMPILE_DISK +/* +Converts 32-bit RGBA raw pixel data into a PNG file on disk. +Same as the other encode functions, but instead takes a filename as output. +NOTE: This overwrites existing files without warning! +*/ +unsigned encode(const std::string& filename, + const unsigned char* in, unsigned w, unsigned h, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +unsigned encode(const std::string& filename, + const std::vector& in, unsigned w, unsigned h, + LodePNGColorType colortype = LCT_RGBA, unsigned bitdepth = 8); +#endif //LODEPNG_COMPILE_DISK +#endif //LODEPNG_COMPILE_ENCODER +} //namespace lodepng +#endif /*LODEPNG_COMPILE_CPP*/ +#endif /*LODEPNG_COMPILE_PNG*/ + +#ifdef LODEPNG_COMPILE_ERROR_TEXT +/*Returns an English description of the numerical error code.*/ +const char* lodepng_error_text(unsigned code); +#endif /*LODEPNG_COMPILE_ERROR_TEXT*/ + +#ifdef LODEPNG_COMPILE_DECODER +/*Settings for zlib decompression*/ +typedef struct LodePNGDecompressSettings LodePNGDecompressSettings; +struct LodePNGDecompressSettings +{ + unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/ + + /*use custom zlib decoder instead of built in one (default: null)*/ + unsigned (*custom_zlib)(unsigned char**, size_t*, + const unsigned char*, size_t, + const LodePNGDecompressSettings*); + /*use custom deflate decoder instead of built in one (default: null) + if custom_zlib is used, custom_deflate is ignored since only the built in + zlib function will call custom_deflate*/ + unsigned (*custom_inflate)(unsigned char**, size_t*, + const unsigned char*, size_t, + const LodePNGDecompressSettings*); + + void* custom_context; /*optional custom settings for custom functions*/ +}; + +extern const LodePNGDecompressSettings lodepng_default_decompress_settings; +void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings); +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_ENCODER +/* +Settings for zlib compression. Tweaking these settings tweaks the balance +between speed and compression ratio. +*/ +typedef struct LodePNGCompressSettings LodePNGCompressSettings; +struct LodePNGCompressSettings /*deflate = compress*/ +{ + /*LZ77 related settings*/ + unsigned btype; /*the block type for LZ (0, 1, 2 or 3, see zlib standard). Should be 2 for proper compression.*/ + unsigned use_lz77; /*whether or not to use LZ77. Should be 1 for proper compression.*/ + unsigned windowsize; /*the maximum is 32768, higher gives more compression but is slower. Typical value: 2048.*/ + unsigned minmatch; /*mininum lz77 length. 3 is normally best, 6 can be better for some PNGs. Default: 0*/ + unsigned nicematch; /*stop searching if >= this length found. Set to 258 for best compression. Default: 128*/ + unsigned lazymatching; /*use lazy matching: better compression but a bit slower. Default: true*/ + + /*use custom zlib encoder instead of built in one (default: null)*/ + unsigned (*custom_zlib)(unsigned char**, size_t*, + const unsigned char*, size_t, + const LodePNGCompressSettings*); + /*use custom deflate encoder instead of built in one (default: null) + if custom_zlib is used, custom_deflate is ignored since only the built in + zlib function will call custom_deflate*/ + unsigned (*custom_deflate)(unsigned char**, size_t*, + const unsigned char*, size_t, + const LodePNGCompressSettings*); + + void* custom_context; /*optional custom settings for custom functions*/ +}; + +extern const LodePNGCompressSettings lodepng_default_compress_settings; +void lodepng_compress_settings_init(LodePNGCompressSettings* settings); +#endif /*LODEPNG_COMPILE_ENCODER*/ + +#ifdef LODEPNG_COMPILE_PNG +/* +Color mode of an image. Contains all information required to decode the pixel +bits to RGBA colors. This information is the same as used in the PNG file +format, and is used both for PNG and raw image data in LodePNG. +*/ +typedef struct LodePNGColorMode +{ + /*header (IHDR)*/ + LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/ + unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/ + + /* + palette (PLTE and tRNS) + + Dynamically allocated with the colors of the palette, including alpha. + When encoding a PNG, to store your colors in the palette of the LodePNGColorMode, first use + lodepng_palette_clear, then for each color use lodepng_palette_add. + If you encode an image without alpha with palette, don't forget to put value 255 in each A byte of the palette. + + When decoding, by default you can ignore this palette, since LodePNG already + fills the palette colors in the pixels of the raw RGBA output. + + The palette is only supported for color type 3. + */ + unsigned char* palette; /*palette in RGBARGBA... order*/ + size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/ + + /* + transparent color key (tRNS) + + This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit. + For greyscale PNGs, r, g and b will all 3 be set to the same. + + When decoding, by default you can ignore this information, since LodePNG sets + pixels with this key to transparent already in the raw RGBA output. + + The color key is only supported for color types 0 and 2. + */ + unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/ + unsigned key_r; /*red/greyscale component of color key*/ + unsigned key_g; /*green component of color key*/ + unsigned key_b; /*blue component of color key*/ +} LodePNGColorMode; + +/*init, cleanup and copy functions to use with this struct*/ +void lodepng_color_mode_init(LodePNGColorMode* info); +void lodepng_color_mode_cleanup(LodePNGColorMode* info); +/*return value is error code (0 means no error)*/ +unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source); + +void lodepng_palette_clear(LodePNGColorMode* info); +/*add 1 color to the palette*/ +unsigned lodepng_palette_add(LodePNGColorMode* info, + unsigned char r, unsigned char g, unsigned char b, unsigned char a); + +/*get the total amount of bits per pixel, based on colortype and bitdepth in the struct*/ +unsigned lodepng_get_bpp(const LodePNGColorMode* info); +/*get the amount of color channels used, based on colortype in the struct. +If a palette is used, it counts as 1 channel.*/ +unsigned lodepng_get_channels(const LodePNGColorMode* info); +/*is it a greyscale type? (only colortype 0 or 4)*/ +unsigned lodepng_is_greyscale_type(const LodePNGColorMode* info); +/*has it got an alpha channel? (only colortype 2 or 6)*/ +unsigned lodepng_is_alpha_type(const LodePNGColorMode* info); +/*has it got a palette? (only colortype 3)*/ +unsigned lodepng_is_palette_type(const LodePNGColorMode* info); +/*only returns true if there is a palette and there is a value in the palette with alpha < 255. +Loops through the palette to check this.*/ +unsigned lodepng_has_palette_alpha(const LodePNGColorMode* info); +/* +Check if the given color info indicates the possibility of having non-opaque pixels in the PNG image. +Returns true if the image can have translucent or invisible pixels (it still be opaque if it doesn't use such pixels). +Returns false if the image can only have opaque pixels. +In detail, it returns true only if it's a color type with alpha, or has a palette with non-opaque values, +or if "key_defined" is true. +*/ +unsigned lodepng_can_have_alpha(const LodePNGColorMode* info); +/*Returns the byte size of a raw image buffer with given width, height and color mode*/ +size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color); + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS +/*The information of a Time chunk in PNG.*/ +typedef struct LodePNGTime +{ + unsigned year; /*2 bytes used (0-65535)*/ + unsigned month; /*1-12*/ + unsigned day; /*1-31*/ + unsigned hour; /*0-23*/ + unsigned minute; /*0-59*/ + unsigned second; /*0-60 (to allow for leap seconds)*/ +} LodePNGTime; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +/*Information about the PNG image, except pixels, width and height.*/ +typedef struct LodePNGInfo +{ + /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/ + unsigned compression_method;/*compression method of the original file. Always 0.*/ + unsigned filter_method; /*filter method of the original file*/ + unsigned interlace_method; /*interlace method of the original file*/ + LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/ + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /* + suggested background color chunk (bKGD) + This color uses the same color mode as the PNG (except alpha channel), which can be 1-bit to 16-bit. + + For greyscale PNGs, r, g and b will all 3 be set to the same. When encoding + the encoder writes the red one. For palette PNGs: When decoding, the RGB value + will be stored, not a palette index. But when encoding, specify the index of + the palette in background_r, the other two are then ignored. + + The decoder does not use this background color to edit the color of pixels. + */ + unsigned background_defined; /*is a suggested background color given?*/ + unsigned background_r; /*red component of suggested background color*/ + unsigned background_g; /*green component of suggested background color*/ + unsigned background_b; /*blue component of suggested background color*/ + + /* + non-international text chunks (tEXt and zTXt) + + The char** arrays each contain num strings. The actual messages are in + text_strings, while text_keys are keywords that give a short description what + the actual text represents, e.g. Title, Author, Description, or anything else. + + A keyword is minimum 1 character and maximum 79 characters long. It's + discouraged to use a single line length longer than 79 characters for texts. + + Don't allocate these text buffers yourself. Use the init/cleanup functions + correctly and use lodepng_add_text and lodepng_clear_text. + */ + size_t text_num; /*the amount of texts in these char** buffers (there may be more texts in itext)*/ + char** text_keys; /*the keyword of a text chunk (e.g. "Comment")*/ + char** text_strings; /*the actual text*/ + + /* + international text chunks (iTXt) + Similar to the non-international text chunks, but with additional strings + "langtags" and "transkeys". + */ + size_t itext_num; /*the amount of international texts in this PNG*/ + char** itext_keys; /*the English keyword of the text chunk (e.g. "Comment")*/ + char** itext_langtags; /*language tag for this text's language, ISO/IEC 646 string, e.g. ISO 639 language tag*/ + char** itext_transkeys; /*keyword translated to the international language - UTF-8 string*/ + char** itext_strings; /*the actual international text - UTF-8 string*/ + + /*time chunk (tIME)*/ + unsigned time_defined; /*set to 1 to make the encoder generate a tIME chunk*/ + LodePNGTime time; + + /*phys chunk (pHYs)*/ + unsigned phys_defined; /*if 0, there is no pHYs chunk and the values below are undefined, if 1 else there is one*/ + unsigned phys_x; /*pixels per unit in x direction*/ + unsigned phys_y; /*pixels per unit in y direction*/ + unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/ + + /* + unknown chunks + There are 3 buffers, one for each position in the PNG where unknown chunks can appear + each buffer contains all unknown chunks for that position consecutively + The 3 buffers are the unknown chunks between certain critical chunks: + 0: IHDR-PLTE, 1: PLTE-IDAT, 2: IDAT-IEND + Do not allocate or traverse this data yourself. Use the chunk traversing functions declared + later, such as lodepng_chunk_next and lodepng_chunk_append, to read/write this struct. + */ + unsigned char* unknown_chunks_data[3]; + size_t unknown_chunks_size[3]; /*size in bytes of the unknown chunks, given for protection*/ +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +} LodePNGInfo; + +/*init, cleanup and copy functions to use with this struct*/ +void lodepng_info_init(LodePNGInfo* info); +void lodepng_info_cleanup(LodePNGInfo* info); +/*return value is error code (0 means no error)*/ +unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source); + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS +void lodepng_clear_text(LodePNGInfo* info); /*use this to clear the texts again after you filled them in*/ +unsigned lodepng_add_text(LodePNGInfo* info, const char* key, const char* str); /*push back both texts at once*/ + +void lodepng_clear_itext(LodePNGInfo* info); /*use this to clear the itexts again after you filled them in*/ +unsigned lodepng_add_itext(LodePNGInfo* info, const char* key, const char* langtag, + const char* transkey, const char* str); /*push back the 4 texts of 1 chunk at once*/ +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ + +/* +Converts raw buffer from one color type to another color type, based on +LodePNGColorMode structs to describe the input and output color type. +See the reference manual at the end of this header file to see which color conversions are supported. +return value = LodePNG error code (0 if all went ok, an error if the conversion isn't supported) +The out buffer must have size (w * h * bpp + 7) / 8, where bpp is the bits per pixel +of the output color type (lodepng_get_bpp) +*/ +unsigned lodepng_convert(unsigned char* out, const unsigned char* in, + LodePNGColorMode* mode_out, LodePNGColorMode* mode_in, + unsigned w, unsigned h); + + +#ifdef LODEPNG_COMPILE_DECODER +/* +Settings for the decoder. This contains settings for the PNG and the Zlib +decoder, but not the Info settings from the Info structs. +*/ +typedef struct LodePNGDecoderSettings +{ + LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/ + + unsigned ignore_crc; /*ignore CRC checksums*/ + unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/ + +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + unsigned read_text_chunks; /*if false but remember_unknown_chunks is true, they're stored in the unknown chunks*/ + /*store all bytes from unknown chunks in the LodePNGInfo (off by default, useful for a png editor)*/ + unsigned remember_unknown_chunks; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +} LodePNGDecoderSettings; + +void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings); +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_ENCODER +/*automatically use color type with less bits per pixel if losslessly possible. Default: AUTO*/ +typedef enum LodePNGFilterStrategy +{ + /*every filter at zero*/ + LFS_ZERO, + /*Use filter that gives minumum sum, as described in the official PNG filter heuristic.*/ + LFS_MINSUM, + /*Use the filter type that gives smallest Shannon entropy for this scanline. Depending + on the image, this is better or worse than minsum.*/ + LFS_ENTROPY, + /* + Brute-force-search PNG filters by compressing each filter for each scanline. + Experimental, very slow, and only rarely gives better compression than MINSUM. + */ + LFS_BRUTE_FORCE, + /*use predefined_filters buffer: you specify the filter type for each scanline*/ + LFS_PREDEFINED +} LodePNGFilterStrategy; + +/*automatically use color type with less bits per pixel if losslessly possible. Default: LAC_AUTO*/ +typedef enum LodePNGAutoConvert +{ + LAC_NO, /*use color type user requested*/ + LAC_ALPHA, /*use color type user requested, but if only opaque pixels and RGBA or grey+alpha, use RGB or grey*/ + LAC_AUTO, /*use PNG color type that can losslessly represent the uncompressed image the smallest possible*/ + /* + like AUTO, but do not choose 1, 2 or 4 bit per pixel types. + sometimes a PNG image compresses worse if less than 8 bits per pixels. + */ + LAC_AUTO_NO_NIBBLES, + /* + like AUTO, but never choose palette color type. For small images, encoding + the palette may take more bytes than what is gained. Note that AUTO also + already prevents encoding the palette for extremely small images, but that may + not be sufficient because due to the compression it cannot predict when to + switch. + */ + LAC_AUTO_NO_PALETTE, + LAC_AUTO_NO_NIBBLES_NO_PALETTE +} LodePNGAutoConvert; + + +/*Settings for the encoder.*/ +typedef struct LodePNGEncoderSettings +{ + LodePNGCompressSettings zlibsettings; /*settings for the zlib encoder, such as window size, ...*/ + + LodePNGAutoConvert auto_convert; /*how to automatically choose output PNG color type, if at all*/ + + /*If true, follows the official PNG heuristic: if the PNG uses a palette or lower than + 8 bit depth, set all filters to zero. Otherwise use the filter_strategy. Note that to + completely follow the official PNG heuristic, filter_palette_zero must be true and + filter_strategy must be LFS_MINSUM*/ + unsigned filter_palette_zero; + /*Which filter strategy to use when not using zeroes due to filter_palette_zero. + Set filter_palette_zero to 0 to ensure always using your chosen strategy. Default: LFS_MINSUM*/ + LodePNGFilterStrategy filter_strategy; + /*used if filter_strategy is LFS_PREDEFINED. In that case, this must point to a buffer with + the same length as the amount of scanlines in the image, and each value must <= 5. You + have to cleanup this buffer, LodePNG will never free it. Don't forget that filter_palette_zero + must be set to 0 to ensure this is also used on palette or low bitdepth images.*/ + unsigned char* predefined_filters; + + /*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette). + If colortype is 3, PLTE is _always_ created.*/ + unsigned force_palette; +#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS + /*add LodePNG identifier and version as a text chunk, for debugging*/ + unsigned add_id; + /*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/ + unsigned text_compression; +#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ +} LodePNGEncoderSettings; + +void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings); +#endif /*LODEPNG_COMPILE_ENCODER*/ + + +#if defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) +/*The settings, state and information for extended encoding and decoding.*/ +typedef struct LodePNGState +{ +#ifdef LODEPNG_COMPILE_DECODER + LodePNGDecoderSettings decoder; /*the decoding settings*/ +#endif /*LODEPNG_COMPILE_DECODER*/ +#ifdef LODEPNG_COMPILE_ENCODER + LodePNGEncoderSettings encoder; /*the encoding settings*/ +#endif /*LODEPNG_COMPILE_ENCODER*/ + LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/ + LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/ + unsigned error; +#ifdef LODEPNG_COMPILE_CPP + //For the lodepng::State subclass. + virtual ~LodePNGState(){} +#endif +} LodePNGState; + +/*init, cleanup and copy functions to use with this struct*/ +void lodepng_state_init(LodePNGState* state); +void lodepng_state_cleanup(LodePNGState* state); +void lodepng_state_copy(LodePNGState* dest, const LodePNGState* source); +#endif /* defined(LODEPNG_COMPILE_DECODER) || defined(LODEPNG_COMPILE_ENCODER) */ + +#ifdef LODEPNG_COMPILE_DECODER +/* +Same as lodepng_decode_memory, but uses a LodePNGState to allow custom settings and +getting much more information about the PNG image and color mode. +*/ +unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, + LodePNGState* state, + const unsigned char* in, size_t insize); + +/* +Read the PNG header, but not the actual data. This returns only the information +that is in the header chunk of the PNG, such as width, height and color type. The +information is placed in the info_png field of the LodePNGState. +*/ +unsigned lodepng_inspect(unsigned* w, unsigned* h, + LodePNGState* state, + const unsigned char* in, size_t insize); +#endif /*LODEPNG_COMPILE_DECODER*/ + + +#ifdef LODEPNG_COMPILE_ENCODER +/*This function allocates the out buffer with standard malloc and stores the size in *outsize.*/ +unsigned lodepng_encode(unsigned char** out, size_t* outsize, + const unsigned char* image, unsigned w, unsigned h, + LodePNGState* state); +#endif /*LODEPNG_COMPILE_ENCODER*/ + +/* +The lodepng_chunk functions are normally not needed, except to traverse the +unknown chunks stored in the LodePNGInfo struct, or add new ones to it. +It also allows traversing the chunks of an encoded PNG file yourself. + +PNG standard chunk naming conventions: +First byte: uppercase = critical, lowercase = ancillary +Second byte: uppercase = public, lowercase = private +Third byte: must be uppercase +Fourth byte: uppercase = unsafe to copy, lowercase = safe to copy +*/ + +/*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/ +unsigned lodepng_chunk_length(const unsigned char* chunk); + +/*puts the 4-byte type in null terminated string*/ +void lodepng_chunk_type(char type[5], const unsigned char* chunk); + +/*check if the type is the given type*/ +unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type); + +/*0: it's one of the critical chunk types, 1: it's an ancillary chunk (see PNG standard)*/ +unsigned char lodepng_chunk_ancillary(const unsigned char* chunk); + +/*0: public, 1: private (see PNG standard)*/ +unsigned char lodepng_chunk_private(const unsigned char* chunk); + +/*0: the chunk is unsafe to copy, 1: the chunk is safe to copy (see PNG standard)*/ +unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk); + +/*get pointer to the data of the chunk, where the input points to the header of the chunk*/ +unsigned char* lodepng_chunk_data(unsigned char* chunk); +const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk); + +/*returns 0 if the crc is correct, 1 if it's incorrect (0 for OK as usual!)*/ +unsigned lodepng_chunk_check_crc(const unsigned char* chunk); + +/*generates the correct CRC from the data and puts it in the last 4 bytes of the chunk*/ +void lodepng_chunk_generate_crc(unsigned char* chunk); + +/*iterate to next chunks. don't use on IEND chunk, as there is no next chunk then*/ +unsigned char* lodepng_chunk_next(unsigned char* chunk); +const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk); + +/* +Appends chunk to the data in out. The given chunk should already have its chunk header. +The out variable and outlength are updated to reflect the new reallocated buffer. +Returns error code (0 if it went ok) +*/ +unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk); + +/* +Appends new chunk to out. The chunk to append is given by giving its length, type +and data separately. The type is a 4-letter string. +The out variable and outlength are updated to reflect the new reallocated buffer. +Returne error code (0 if it went ok) +*/ +unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, + const char* type, const unsigned char* data); + + +/*Calculate CRC32 of buffer*/ +unsigned lodepng_crc32(const unsigned char* buf, size_t len); +#endif /*LODEPNG_COMPILE_PNG*/ + + +#ifdef LODEPNG_COMPILE_ZLIB +/* +This zlib part can be used independently to zlib compress and decompress a +buffer. It cannot be used to create gzip files however, and it only supports the +part of zlib that is required for PNG, it does not support dictionaries. +*/ + +#ifdef LODEPNG_COMPILE_DECODER +/*Inflate a buffer. Inflate is the decompression step of deflate. Out buffer must be freed after use.*/ +unsigned lodepng_inflate(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings); + +/* +Decompresses Zlib data. Reallocates the out buffer and appends the data. The +data must be according to the zlib specification. +Either, *out must be NULL and *outsize must be 0, or, *out must be a valid +buffer and *outsize its size in bytes. out must be freed by user after usage. +*/ +unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGDecompressSettings* settings); +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_ENCODER +/* +Compresses data with Zlib. Reallocates the out buffer and appends the data. +Zlib adds a small header and trailer around the deflate data. +The data is output in the format of the zlib specification. +Either, *out must be NULL and *outsize must be 0, or, *out must be a valid +buffer and *outsize its size in bytes. out must be freed by user after usage. +*/ +unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGCompressSettings* settings); + +/* +Find length-limited Huffman code for given frequencies. This function is in the +public interface only for tests, it's used internally by lodepng_deflate. +*/ +unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies, + size_t numcodes, unsigned maxbitlen); + +/*Compress a buffer with deflate. See RFC 1951. Out buffer must be freed after use.*/ +unsigned lodepng_deflate(unsigned char** out, size_t* outsize, + const unsigned char* in, size_t insize, + const LodePNGCompressSettings* settings); + +#endif /*LODEPNG_COMPILE_ENCODER*/ +#endif /*LODEPNG_COMPILE_ZLIB*/ + +#ifdef LODEPNG_COMPILE_DISK +/* +Load a file from disk into buffer. The function allocates the out buffer, and +after usage you should free it. +out: output parameter, contains pointer to loaded buffer. +outsize: output parameter, size of the allocated out buffer +filename: the path to the file to load +return value: error code (0 means ok) +*/ +unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename); + +/* +Save a file from buffer to disk. Warning, if it exists, this function overwrites +the file without warning! +buffer: the buffer to write +buffersize: size of the buffer to write +filename: the path to the file to save to +return value: error code (0 means ok) +*/ +unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename); +#endif /*LODEPNG_COMPILE_DISK*/ + +#ifdef LODEPNG_COMPILE_CPP +//The LodePNG C++ wrapper uses std::vectors instead of manually allocated memory buffers. +namespace lodepng +{ +#ifdef LODEPNG_COMPILE_PNG +class State : public LodePNGState +{ + public: + State(); + State(const State& other); + virtual ~State(); + State& operator=(const State& other); +}; + +#ifdef LODEPNG_COMPILE_DECODER +//Same as other lodepng::decode, but using a State for more settings and information. +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + State& state, + const unsigned char* in, size_t insize); +unsigned decode(std::vector& out, unsigned& w, unsigned& h, + State& state, + const std::vector& in); +#endif /*LODEPNG_COMPILE_DECODER*/ + +#ifdef LODEPNG_COMPILE_ENCODER +//Same as other lodepng::encode, but using a State for more settings and information. +unsigned encode(std::vector& out, + const unsigned char* in, unsigned w, unsigned h, + State& state); +unsigned encode(std::vector& out, + const std::vector& in, unsigned w, unsigned h, + State& state); +#endif /*LODEPNG_COMPILE_ENCODER*/ + +#ifdef LODEPNG_COMPILE_DISK +/* +Load a file from disk into an std::vector. If the vector is empty, then either +the file doesn't exist or is an empty file. +*/ +void load_file(std::vector& buffer, const std::string& filename); + +/* +Save the binary data in an std::vector to a file on disk. The file is overwritten +without warning. +*/ +void save_file(const std::vector& buffer, const std::string& filename); +#endif //LODEPNG_COMPILE_DISK +#endif //LODEPNG_COMPILE_PNG + +#ifdef LODEPNG_COMPILE_ZLIB +#ifdef LODEPNG_COMPILE_DECODER +//Zlib-decompress an unsigned char buffer +unsigned decompress(std::vector& out, const unsigned char* in, size_t insize, + const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings); + +//Zlib-decompress an std::vector +unsigned decompress(std::vector& out, const std::vector& in, + const LodePNGDecompressSettings& settings = lodepng_default_decompress_settings); +#endif //LODEPNG_COMPILE_DECODER + +#ifdef LODEPNG_COMPILE_ENCODER +//Zlib-compress an unsigned char buffer +unsigned compress(std::vector& out, const unsigned char* in, size_t insize, + const LodePNGCompressSettings& settings = lodepng_default_compress_settings); + +//Zlib-compress an std::vector +unsigned compress(std::vector& out, const std::vector& in, + const LodePNGCompressSettings& settings = lodepng_default_compress_settings); +#endif //LODEPNG_COMPILE_ENCODER +#endif //LODEPNG_COMPILE_ZLIB +} //namespace lodepng +#endif /*LODEPNG_COMPILE_CPP*/ + +/* +TODO: +[.] test if there are no memory leaks or security exploits - done a lot but needs to be checked often +[.] check compatibility with vareous compilers - done but needs to be redone for every newer version +[X] converting color to 16-bit per channel types +[ ] read all public PNG chunk types (but never let the color profile and gamma ones touch RGB values) +[ ] make sure encoder generates no chunks with size > (2^31)-1 +[ ] partial decoding (stream processing) +[X] let the "isFullyOpaque" function check color keys and transparent palettes too +[X] better name for the variables "codes", "codesD", "codelengthcodes", "clcl" and "lldl" +[ ] don't stop decoding on errors like 69, 57, 58 (make warnings) +[ ] make option to choose if the raw image with non multiple of 8 bits per scanline should have padding bits or not +[ ] let the C++ wrapper catch exceptions coming from the standard library and return LodePNG error codes +*/ + +#endif /*LODEPNG_H inclusion guard*/ + +/* +LodePNG Documentation +--------------------- + +0. table of contents +-------------------- + + 1. about + 1.1. supported features + 1.2. features not supported + 2. C and C++ version + 3. security + 4. decoding + 5. encoding + 6. color conversions + 6.1. PNG color types + 6.2. color conversions + 6.3. padding bits + 7. error values + 8. chunks and PNG editing + 9. compiler support + 10. examples + 10.1. decoder C++ example + 10.2. decoder C example + 11. changes + 12. contact information + + +1. about +-------- + +PNG is a file format to store raster images losslessly with good compression, +supporting different color types and alpha channel. + +LodePNG is a PNG codec according to the Portable Network Graphics (PNG) +Specification (Second Edition) - W3C Recommendation 10 November 2003. + +The specifications used are: + +*) Portable Network Graphics (PNG) Specification (Second Edition): + http://www.w3.org/TR/2003/REC-PNG-20031110 +*) RFC 1950 ZLIB Compressed Data Format version 3.3: + http://www.gzip.org/zlib/rfc-zlib.html +*) RFC 1951 DEFLATE Compressed Data Format Specification ver 1.3: + http://www.gzip.org/zlib/rfc-deflate.html + +The most recent version of LodePNG can currently be found at +http://lodev.org/lodepng/ + +LodePNG works both in C (ISO C90) and C++, with a C++ wrapper that adds +extra functionality. + +LodePNG exists out of two files: +-lodepng.h: the header file for both C and C++ +-lodepng.c(pp): give it the name lodepng.c or lodepng.cpp (or .cc) depending on your usage + +If you want to start using LodePNG right away without reading this doc, get the +examples from the LodePNG website to see how to use it in code, or check the +smaller examples in chapter 13 here. + +LodePNG is simple but only supports the basic requirements. To achieve +simplicity, the following design choices were made: There are no dependencies +on any external library. There are functions to decode and encode a PNG with +a single function call, and extended versions of these functions taking a +LodePNGState struct allowing to specify or get more information. By default +the colors of the raw image are always RGB or RGBA, no matter what color type +the PNG file uses. To read and write files, there are simple functions to +convert the files to/from buffers in memory. + +This all makes LodePNG suitable for loading textures in games, demos and small +programs, ... It's less suitable for full fledged image editors, loading PNGs +over network (it requires all the image data to be available before decoding can +begin), life-critical systems, ... + +1.1. supported features +----------------------- + +The following features are supported by the decoder: + +*) decoding of PNGs with any color type, bit depth and interlace mode, to a 24- or 32-bit color raw image, + or the same color type as the PNG +*) encoding of PNGs, from any raw image to 24- or 32-bit color, or the same color type as the raw image +*) Adam7 interlace and deinterlace for any color type +*) loading the image from harddisk or decoding it from a buffer from other sources than harddisk +*) support for alpha channels, including RGBA color model, translucent palettes and color keying +*) zlib decompression (inflate) +*) zlib compression (deflate) +*) CRC32 and ADLER32 checksums +*) handling of unknown chunks, allowing making a PNG editor that stores custom and unknown chunks. +*) the following chunks are supported (generated/interpreted) by both encoder and decoder: + IHDR: header information + PLTE: color palette + IDAT: pixel data + IEND: the final chunk + tRNS: transparency for palettized images + tEXt: textual information + zTXt: compressed textual information + iTXt: international textual information + bKGD: suggested background color + pHYs: physical dimensions + tIME: modification time + +1.2. features not supported +--------------------------- + +The following features are _not_ supported: + +*) some features needed to make a conformant PNG-Editor might be still missing. +*) partial loading/stream processing. All data must be available and is processed in one call. +*) The following public chunks are not supported but treated as unknown chunks by LodePNG + cHRM, gAMA, iCCP, sRGB, sBIT, hIST, sPLT + Some of these are not supported on purpose: LodePNG wants to provide the RGB values + stored in the pixels, not values modified by system dependent gamma or color models. + + +2. C and C++ version +-------------------- + +The C version uses buffers allocated with alloc that you need to free() +yourself. You need to use init and cleanup functions for each struct whenever +using a struct from the C version to avoid exploits and memory leaks. + +The C++ version has extra functions with std::vectors in the interface and the +lodepng::State class which is a LodePNGState with constructor and destructor. + +These files work without modification for both C and C++ compilers because all +the additional C++ code is in "#ifdef __cplusplus" blocks that make C-compilers +ignore it, and the C code is made to compile both with strict ISO C90 and C++. + +To use the C++ version, you need to rename the source file to lodepng.cpp +(instead of lodepng.c), and compile it with a C++ compiler. + +To use the C version, you need to rename the source file to lodepng.c (instead +of lodepng.cpp), and compile it with a C compiler. + + +3. Security +----------- + +Even if carefully designed, it's always possible that LodePNG contains possible +exploits. If you discover one, please let me know, and it will be fixed. + +When using LodePNG, care has to be taken with the C version of LodePNG, as well +as the C-style structs when working with C++. The following conventions are used +for all C-style structs: + +-if a struct has a corresponding init function, always call the init function when making a new one +-if a struct has a corresponding cleanup function, call it before the struct disappears to avoid memory leaks +-if a struct has a corresponding copy function, use the copy function instead of "=". + The destination must also be inited already. + + +4. Decoding +----------- + +Decoding converts a PNG compressed image to a raw pixel buffer. + +Most documentation on using the decoder is at its declarations in the header +above. For C, simple decoding can be done with functions such as +lodepng_decode32, and more advanced decoding can be done with the struct +LodePNGState and lodepng_decode. For C++, all decoding can be done with the +various lodepng::decode functions, and lodepng::State can be used for advanced +features. + +When using the LodePNGState, it uses the following fields for decoding: +*) LodePNGInfo info_png: it stores extra information about the PNG (the input) in here +*) LodePNGColorMode info_raw: here you can say what color mode of the raw image (the output) you want to get +*) LodePNGDecoderSettings decoder: you can specify a few extra settings for the decoder to use + +LodePNGInfo info_png +-------------------- + +After decoding, this contains extra information of the PNG image, except the actual +pixels, width and height because these are already gotten directly from the decoder +functions. + +It contains for example the original color type of the PNG image, text comments, +suggested background color, etc... More details about the LodePNGInfo struct are +at its declaration documentation. + +LodePNGColorMode info_raw +------------------------- + +When decoding, here you can specify which color type you want +the resulting raw image to be. If this is different from the colortype of the +PNG, then the decoder will automatically convert the result. This conversion +always works, except if you want it to convert a color PNG to greyscale or to +a palette with missing colors. + +By default, 32-bit color is used for the result. + +LodePNGDecoderSettings decoder +------------------------------ + +The settings can be used to ignore the errors created by invalid CRC and Adler32 +chunks, and to disable the decoding of tEXt chunks. + +There's also a setting color_convert, true by default. If false, no conversion +is done, the resulting data will be as it was in the PNG (after decompression) +and you'll have to puzzle the colors of the pixels together yourself using the +color type information in the LodePNGInfo. + + +5. Encoding +----------- + +Encoding converts a raw pixel buffer to a PNG compressed image. + +Most documentation on using the encoder is at its declarations in the header +above. For C, simple encoding can be done with functions such as +lodepng_encode32, and more advanced decoding can be done with the struct +LodePNGState and lodepng_encode. For C++, all encoding can be done with the +various lodepng::encode functions, and lodepng::State can be used for advanced +features. + +Like the decoder, the encoder can also give errors. However it gives less errors +since the encoder input is trusted, the decoder input (a PNG image that could +be forged by anyone) is not trusted. + +When using the LodePNGState, it uses the following fields for encoding: +*) LodePNGInfo info_png: here you specify how you want the PNG (the output) to be. +*) LodePNGColorMode info_raw: here you say what color type of the raw image (the input) has +*) LodePNGEncoderSettings encoder: you can specify a few settings for the encoder to use + +LodePNGInfo info_png +-------------------- + +When encoding, you use this the opposite way as when decoding: for encoding, +you fill in the values you want the PNG to have before encoding. By default it's +not needed to specify a color type for the PNG since it's automatically chosen, +but it's possible to choose it yourself given the right settings. + +The encoder will not always exactly match the LodePNGInfo struct you give, +it tries as close as possible. Some things are ignored by the encoder. The +encoder uses, for example, the following settings from it when applicable: +colortype and bitdepth, text chunks, time chunk, the color key, the palette, the +background color, the interlace method, unknown chunks, ... + +When encoding to a PNG with colortype 3, the encoder will generate a PLTE chunk. +If the palette contains any colors for which the alpha channel is not 255 (so +there are translucent colors in the palette), it'll add a tRNS chunk. + +LodePNGColorMode info_raw +------------------------- + +You specify the color type of the raw image that you give to the input here, +including a possible transparent color key and palette you happen to be using in +your raw image data. + +By default, 32-bit color is assumed, meaning your input has to be in RGBA +format with 4 bytes (unsigned chars) per pixel. + +LodePNGEncoderSettings encoder +------------------------------ + +The following settings are supported (some are in sub-structs): +*) auto_convert: when this option is enabled, the encoder will +automatically choose the smallest possible color mode (including color key) that +can encode the colors of all pixels without information loss. +*) btype: the block type for LZ77. 0 = uncompressed, 1 = fixed huffman tree, + 2 = dynamic huffman tree (best compression). Should be 2 for proper + compression. +*) use_lz77: whether or not to use LZ77 for compressed block types. Should be + true for proper compression. +*) windowsize: the window size used by the LZ77 encoder (1 - 32768). Has value + 2048 by default, but can be set to 32768 for better, but slow, compression. +*) force_palette: if colortype is 2 or 6, you can make the encoder write a PLTE + chunk if force_palette is true. This can used as suggested palette to convert + to by viewers that don't support more than 256 colors (if those still exist) +*) add_id: add text chunk "Encoder: LodePNG " to the image. +*) text_compression: default 1. If 1, it'll store texts as zTXt instead of tEXt chunks. + zTXt chunks use zlib compression on the text. This gives a smaller result on + large texts but a larger result on small texts (such as a single program name). + It's all tEXt or all zTXt though, there's no separate setting per text yet. + + +6. color conversions +-------------------- + +An important thing to note about LodePNG, is that the color type of the PNG, and +the color type of the raw image, are completely independent. By default, when +you decode a PNG, you get the result as a raw image in the color type you want, +no matter whether the PNG was encoded with a palette, greyscale or RGBA color. +And if you encode an image, by default LodePNG will automatically choose the PNG +color type that gives good compression based on the values of colors and amount +of colors in the image. It can be configured to let you control it instead as +well, though. + +To be able to do this, LodePNG does conversions from one color mode to another. +It can convert from almost any color type to any other color type, except the +following conversions: RGB to greyscale is not supported, and converting to a +palette when the palette doesn't have a required color is not supported. This is +not supported on purpose: this is information loss which requires a color +reduction algorithm that is beyong the scope of a PNG encoder (yes, RGB to grey +is easy, but there are multiple ways if you want to give some channels more +weight). + +By default, when decoding, you get the raw image in 32-bit RGBA or 24-bit RGB +color, no matter what color type the PNG has. And by default when encoding, +LodePNG automatically picks the best color model for the output PNG, and expects +the input image to be 32-bit RGBA or 24-bit RGB. So, unless you want to control +the color format of the images yourself, you can skip this chapter. + +6.1. PNG color types +-------------------- + +A PNG image can have many color types, ranging from 1-bit color to 64-bit color, +as well as palettized color modes. After the zlib decompression and unfiltering +in the PNG image is done, the raw pixel data will have that color type and thus +a certain amount of bits per pixel. If you want the output raw image after +decoding to have another color type, a conversion is done by LodePNG. + +The PNG specification gives the following color types: + +0: greyscale, bit depths 1, 2, 4, 8, 16 +2: RGB, bit depths 8 and 16 +3: palette, bit depths 1, 2, 4 and 8 +4: greyscale with alpha, bit depths 8 and 16 +6: RGBA, bit depths 8 and 16 + +Bit depth is the amount of bits per pixel per color channel. So the total amount +of bits per pixel is: amount of channels * bitdepth. + +6.2. color conversions +---------------------- + +As explained in the sections about the encoder and decoder, you can specify +color types and bit depths in info_png and info_raw to change the default +behaviour. + +If, when decoding, you want the raw image to be something else than the default, +you need to set the color type and bit depth you want in the LodePNGColorMode, +or the parameters of the simple function of LodePNG you're using. + +If, when encoding, you use another color type than the default in the input +image, you need to specify its color type and bit depth in the LodePNGColorMode +of the raw image, or use the parameters of the simplefunction of LodePNG you're +using. + +If, when encoding, you don't want LodePNG to choose the output PNG color type +but control it yourself, you need to set auto_convert in the encoder settings +to LAC_NONE, and specify the color type you want in the LodePNGInfo of the +encoder. + +If you do any of the above, LodePNG may need to do a color conversion, which +follows the rules below, and may sometimes not be allowed. + +To avoid some confusion: +-the decoder converts from PNG to raw image +-the encoder converts from raw image to PNG +-the colortype and bitdepth in LodePNGColorMode info_raw, are those of the raw image +-the colortype and bitdepth in the color field of LodePNGInfo info_png, are those of the PNG +-when encoding, the color type in LodePNGInfo is ignored if auto_convert + is enabled, it is automatically generated instead +-when decoding, the color type in LodePNGInfo is set by the decoder to that of the original + PNG image, but it can be ignored since the raw image has the color type you requested instead +-if the color type of the LodePNGColorMode and PNG image aren't the same, a conversion + between the color types is done if the color types are supported. If it is not + supported, an error is returned. If the types are the same, no conversion is done. +-even though some conversions aren't supported, LodePNG supports loading PNGs from any + colortype and saving PNGs to any colortype, sometimes it just requires preparing + the raw image correctly before encoding. +-both encoder and decoder use the same color converter. + +Non supported color conversions: +-color to greyscale: no error is thrown, but the result will look ugly because +only the red channel is taken +-anything, to palette when that palette does not have that color in it: in this +case an error is thrown + +Supported color conversions: +-anything to 8-bit RGB, 8-bit RGBA, 16-bit RGB, 16-bit RGBA +-any grey or grey+alpha, to grey or grey+alpha +-anything to a palette, as long as the palette has the requested colors in it +-removing alpha channel +-higher to smaller bitdepth, and vice versa + +If you want no color conversion to be done: +-In the encoder, you can make it save a PNG with any color type by giving the +raw color mode and LodePNGInfo the same color mode, and setting auto_convert to +LAC_NO. +-In the decoder, you can make it store the pixel data in the same color type +as the PNG has, by setting the color_convert setting to false. Settings in +info_raw are then ignored. + +The function lodepng_convert does the color conversion. It is available in the +interface but normally isn't needed since the encoder and decoder already call +it. + +6.3. padding bits +----------------- + +In the PNG file format, if a less than 8-bit per pixel color type is used and the scanlines +have a bit amount that isn't a multiple of 8, then padding bits are used so that each +scanline starts at a fresh byte. But that is NOT true for the LodePNG raw input and output. +The raw input image you give to the encoder, and the raw output image you get from the decoder +will NOT have these padding bits, e.g. in the case of a 1-bit image with a width +of 7 pixels, the first pixel of the second scanline will the the 8th bit of the first byte, +not the first bit of a new byte. + + +7. error values +--------------- + +All functions in LodePNG that return an error code, return 0 if everything went +OK, or a non-zero code if there was an error. + +The meaning of the LodePNG error values can be retrieved with the function +lodepng_error_text: given the numerical error code, it returns a description +of the error in English as a string. + +Check the implementation of lodepng_error_text to see the meaning of each code. + + +8. chunks and PNG editing +------------------------- + +If you want to add extra chunks to a PNG you encode, or use LodePNG for a PNG +editor that should follow the rules about handling of unknown chunks, or if your +program is able to read other types of chunks than the ones handled by LodePNG, +then that's possible with the chunk functions of LodePNG. + +A PNG chunk has the following layout: + +4 bytes length +4 bytes type name +length bytes data +4 bytes CRC + +8.1. iterating through chunks +----------------------------- + +If you have a buffer containing the PNG image data, then the first chunk (the +IHDR chunk) starts at byte number 8 of that buffer. The first 8 bytes are the +signature of the PNG and are not part of a chunk. But if you start at byte 8 +then you have a chunk, and can check the following things of it. + +NOTE: none of these functions check for memory buffer boundaries. To avoid +exploits, always make sure the buffer contains all the data of the chunks. +When using lodepng_chunk_next, make sure the returned value is within the +allocated memory. + +unsigned lodepng_chunk_length(const unsigned char* chunk): + +Get the length of the chunk's data. The total chunk length is this length + 12. + +void lodepng_chunk_type(char type[5], const unsigned char* chunk): +unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type): + +Get the type of the chunk or compare if it's a certain type + +unsigned char lodepng_chunk_critical(const unsigned char* chunk): +unsigned char lodepng_chunk_private(const unsigned char* chunk): +unsigned char lodepng_chunk_safetocopy(const unsigned char* chunk): + +Check if the chunk is critical in the PNG standard (only IHDR, PLTE, IDAT and IEND are). +Check if the chunk is private (public chunks are part of the standard, private ones not). +Check if the chunk is safe to copy. If it's not, then, when modifying data in a critical +chunk, unsafe to copy chunks of the old image may NOT be saved in the new one if your +program doesn't handle that type of unknown chunk. + +unsigned char* lodepng_chunk_data(unsigned char* chunk): +const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk): + +Get a pointer to the start of the data of the chunk. + +unsigned lodepng_chunk_check_crc(const unsigned char* chunk): +void lodepng_chunk_generate_crc(unsigned char* chunk): + +Check if the crc is correct or generate a correct one. + +unsigned char* lodepng_chunk_next(unsigned char* chunk): +const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk): + +Iterate to the next chunk. This works if you have a buffer with consecutive chunks. Note that these +functions do no boundary checking of the allocated data whatsoever, so make sure there is enough +data available in the buffer to be able to go to the next chunk. + +unsigned lodepng_chunk_append(unsigned char** out, size_t* outlength, const unsigned char* chunk): +unsigned lodepng_chunk_create(unsigned char** out, size_t* outlength, unsigned length, + const char* type, const unsigned char* data): + +These functions are used to create new chunks that are appended to the data in *out that has +length *outlength. The append function appends an existing chunk to the new data. The create +function creates a new chunk with the given parameters and appends it. Type is the 4-letter +name of the chunk. + +8.2. chunks in info_png +----------------------- + +The LodePNGInfo struct contains fields with the unknown chunk in it. It has 3 +buffers (each with size) to contain 3 types of unknown chunks: +the ones that come before the PLTE chunk, the ones that come between the PLTE +and the IDAT chunks, and the ones that come after the IDAT chunks. +It's necessary to make the distionction between these 3 cases because the PNG +standard forces to keep the ordering of unknown chunks compared to the critical +chunks, but does not force any other ordering rules. + +info_png.unknown_chunks_data[0] is the chunks before PLTE +info_png.unknown_chunks_data[1] is the chunks after PLTE, before IDAT +info_png.unknown_chunks_data[2] is the chunks after IDAT + +The chunks in these 3 buffers can be iterated through and read by using the same +way described in the previous subchapter. + +When using the decoder to decode a PNG, you can make it store all unknown chunks +if you set the option settings.remember_unknown_chunks to 1. By default, this +option is off (0). + +The encoder will always encode unknown chunks that are stored in the info_png. +If you need it to add a particular chunk that isn't known by LodePNG, you can +use lodepng_chunk_append or lodepng_chunk_create to the chunk data in +info_png.unknown_chunks_data[x]. + +Chunks that are known by LodePNG should not be added in that way. E.g. to make +LodePNG add a bKGD chunk, set background_defined to true and add the correct +parameters there instead. + + +9. compiler support +------------------- + +No libraries other than the current standard C library are needed to compile +LodePNG. For the C++ version, only the standard C++ library is needed on top. +Add the files lodepng.c(pp) and lodepng.h to your project, include +lodepng.h where needed, and your program can read/write PNG files. + +If performance is important, use optimization when compiling! For both the +encoder and decoder, this makes a large difference. + +Make sure that LodePNG is compiled with the same compiler of the same version +and with the same settings as the rest of the program, or the interfaces with +std::vectors and std::strings in C++ can be incompatible. + +CHAR_BITS must be 8 or higher, because LodePNG uses unsigned chars for octets. + +*) gcc and g++ + +LodePNG is developed in gcc so this compiler is natively supported. It gives no +warnings with compiler options "-Wall -Wextra -pedantic -ansi", with gcc and g++ +version 4.7.1 on Linux, 32-bit and 64-bit. + +*) Mingw + +The Mingw compiler (a port of gcc) for Windows is fully supported by LodePNG. + +*) Visual Studio 2005 and up, Visual C++ Express Edition 2005 and up + +Visual Studio may give warnings about 'fopen' being deprecated. A multiplatform library +can't support the proposed Visual Studio alternative however, so LodePNG keeps using +fopen. If you don't want to see the deprecated warnings, put this on top of lodepng.h +before the inclusions: +#define _CRT_SECURE_NO_DEPRECATE + +With warning level 4 (W4), there may be a lot of warnings about possible loss of data +due to integer conversions. I'm not planning to resolve these warnings. The gcc compiler +doesn't give those even with strict warning flags. With warning level 3 in VS 2008 +Express Edition, LodePNG is, other than the fopen warnings, warning-free again since +version 20120923. + +Visual Studio may want "stdafx.h" files to be included in each source file. That +is not standard C++ and will not be added to the stock LodePNG. Try to find a +setting to disable it for this source file. + +*) Visual Studio 6.0 + +LodePNG support for Visual Studio 6.0 is not guaranteed because VS6 doesn't +follow the C++ standard correctly. + +*) Comeau C/C++ + +Vesion 20070107 compiles without problems on the Comeau C/C++ Online Test Drive +at http://www.comeaucomputing.com/tryitout in both C90 and C++ mode. + +*) Compilers on Macintosh + +LodePNG has been reported to work both with the gcc and LLVM for Macintosh, both +for C and C++. + +*) Other Compilers + +If you encounter problems on other compilers, feel free to let me know and I may +try to fix it if the compiler is modern standards complient. + + +10. examples +------------ + +This decoder example shows the most basic usage of LodePNG. More complex +examples can be found on the LodePNG website. + +10.1. decoder C++ example +------------------------- + +#include "lodepng.h" +#include + +int main(int argc, char *argv[]) +{ + const char* filename = argc > 1 ? argv[1] : "test.png"; + + //load and decode + std::vector image; + unsigned width, height; + unsigned error = lodepng::decode(image, width, height, filename); + + //if there's an error, display it + if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl; + + //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ... +} + +10.2. decoder C example +----------------------- + +#include "lodepng.h" + +int main(int argc, char *argv[]) +{ + unsigned error; + unsigned char* image; + size_t width, height; + const char* filename = argc > 1 ? argv[1] : "test.png"; + + error = lodepng_decode32_file(&image, &width, &height, filename); + + if(error) printf("decoder error %u: %s\n", error, lodepng_error_text(error)); + + / * use image here * / + + free(image); + return 0; +} + + +11. changes +----------- + +The version number of LodePNG is the date of the change given in the format +yyyymmdd. + +Some changes aren't backwards compatible. Those are indicated with a (!) +symbol. + +*) 8 okt 2012 (!): Added new filter strategy (entropy) and new auto color mode. + (no palette). Better deflate tree encoding. New compression tweak settings. + Faster color conversions while decoding. Some internal cleanups. +*) 23 sep 2012: Reduced warnings in Visual Studio a little bit. +*) 1 sep 2012 (!): Removed #define's for giving custom (de)compression functions + and made it work with function pointers instead. +*) 23 jun 2012: Added more filter strategies. Made it easier to use custom alloc + and free functions and toggle #defines from compiler flags. Small fixes. +*) 6 may 2012 (!): Made plugging in custom zlib/deflate functions more flexible. +*) 22 apr 2012 (!): Made interface more consistent, renaming a lot. Removed + redundant C++ codec classes. Reduced amount of structs. Everything changed, + but it is cleaner now imho and functionality remains the same. Also fixed + several bugs and shrinked the implementation code. Made new samples. +*) 6 nov 2011 (!): By default, the encoder now automatically chooses the best + PNG color model and bit depth, based on the amount and type of colors of the + raw image. For this, autoLeaveOutAlphaChannel replaced by auto_choose_color. +*) 9 okt 2011: simpler hash chain implementation for the encoder. +*) 8 sep 2011: lz77 encoder lazy matching instead of greedy matching. +*) 23 aug 2011: tweaked the zlib compression parameters after benchmarking. + A bug with the PNG filtertype heuristic was fixed, so that it chooses much + better ones (it's quite significant). A setting to do an experimental, slow, + brute force search for PNG filter types is added. +*) 17 aug 2011 (!): changed some C zlib related function names. +*) 16 aug 2011: made the code less wide (max 120 characters per line). +*) 17 apr 2011: code cleanup. Bugfixes. Convert low to 16-bit per sample colors. +*) 21 feb 2011: fixed compiling for C90. Fixed compiling with sections disabled. +*) 11 dec 2010: encoding is made faster, based on suggestion by Peter Eastman + to optimize long sequences of zeros. +*) 13 nov 2010: added LodePNG_InfoColor_hasPaletteAlpha and + LodePNG_InfoColor_canHaveAlpha functions for convenience. +*) 7 nov 2010: added LodePNG_error_text function to get error code description. +*) 30 okt 2010: made decoding slightly faster +*) 26 okt 2010: (!) changed some C function and struct names (more consistent). + Reorganized the documentation and the declaration order in the header. +*) 08 aug 2010: only changed some comments and external samples. +*) 05 jul 2010: fixed bug thanks to warnings in the new gcc version. +*) 14 mar 2010: fixed bug where too much memory was allocated for char buffers. +*) 02 sep 2008: fixed bug where it could create empty tree that linux apps could + read by ignoring the problem but windows apps couldn't. +*) 06 jun 2008: added more error checks for out of memory cases. +*) 26 apr 2008: added a few more checks here and there to ensure more safety. +*) 06 mar 2008: crash with encoding of strings fixed +*) 02 feb 2008: support for international text chunks added (iTXt) +*) 23 jan 2008: small cleanups, and #defines to divide code in sections +*) 20 jan 2008: support for unknown chunks allowing using LodePNG for an editor. +*) 18 jan 2008: support for tIME and pHYs chunks added to encoder and decoder. +*) 17 jan 2008: ability to encode and decode compressed zTXt chunks added + Also vareous fixes, such as in the deflate and the padding bits code. +*) 13 jan 2008: Added ability to encode Adam7-interlaced images. Improved + filtering code of encoder. +*) 07 jan 2008: (!) changed LodePNG to use ISO C90 instead of C++. A + C++ wrapper around this provides an interface almost identical to before. + Having LodePNG be pure ISO C90 makes it more portable. The C and C++ code + are together in these files but it works both for C and C++ compilers. +*) 29 dec 2007: (!) changed most integer types to unsigned int + other tweaks +*) 30 aug 2007: bug fixed which makes this Borland C++ compatible +*) 09 aug 2007: some VS2005 warnings removed again +*) 21 jul 2007: deflate code placed in new namespace separate from zlib code +*) 08 jun 2007: fixed bug with 2- and 4-bit color, and small interlaced images +*) 04 jun 2007: improved support for Visual Studio 2005: crash with accessing + invalid std::vector element [0] fixed, and level 3 and 4 warnings removed +*) 02 jun 2007: made the encoder add a tag with version by default +*) 27 may 2007: zlib and png code separated (but still in the same file), + simple encoder/decoder functions added for more simple usage cases +*) 19 may 2007: minor fixes, some code cleaning, new error added (error 69), + moved some examples from here to lodepng_examples.cpp +*) 12 may 2007: palette decoding bug fixed +*) 24 apr 2007: changed the license from BSD to the zlib license +*) 11 mar 2007: very simple addition: ability to encode bKGD chunks. +*) 04 mar 2007: (!) tEXt chunk related fixes, and support for encoding + palettized PNG images. Plus little interface change with palette and texts. +*) 03 mar 2007: Made it encode dynamic Huffman shorter with repeat codes. + Fixed a bug where the end code of a block had length 0 in the Huffman tree. +*) 26 feb 2007: Huffman compression with dynamic trees (BTYPE 2) now implemented + and supported by the encoder, resulting in smaller PNGs at the output. +*) 27 jan 2007: Made the Adler-32 test faster so that a timewaste is gone. +*) 24 jan 2007: gave encoder an error interface. Added color conversion from any + greyscale type to 8-bit greyscale with or without alpha. +*) 21 jan 2007: (!) Totally changed the interface. It allows more color types + to convert to and is more uniform. See the manual for how it works now. +*) 07 jan 2007: Some cleanup & fixes, and a few changes over the last days: + encode/decode custom tEXt chunks, separate classes for zlib & deflate, and + at last made the decoder give errors for incorrect Adler32 or Crc. +*) 01 jan 2007: Fixed bug with encoding PNGs with less than 8 bits per channel. +*) 29 dec 2006: Added support for encoding images without alpha channel, and + cleaned out code as well as making certain parts faster. +*) 28 dec 2006: Added "Settings" to the encoder. +*) 26 dec 2006: The encoder now does LZ77 encoding and produces much smaller files now. + Removed some code duplication in the decoder. Fixed little bug in an example. +*) 09 dec 2006: (!) Placed output parameters of public functions as first parameter. + Fixed a bug of the decoder with 16-bit per color. +*) 15 okt 2006: Changed documentation structure +*) 09 okt 2006: Encoder class added. It encodes a valid PNG image from the + given image buffer, however for now it's not compressed. +*) 08 sep 2006: (!) Changed to interface with a Decoder class +*) 30 jul 2006: (!) LodePNG_InfoPng , width and height are now retrieved in different + way. Renamed decodePNG to decodePNGGeneric. +*) 29 jul 2006: (!) Changed the interface: image info is now returned as a + struct of type LodePNG::LodePNG_Info, instead of a vector, which was a bit clumsy. +*) 28 jul 2006: Cleaned the code and added new error checks. + Corrected terminology "deflate" into "inflate". +*) 23 jun 2006: Added SDL example in the documentation in the header, this + example allows easy debugging by displaying the PNG and its transparency. +*) 22 jun 2006: (!) Changed way to obtain error value. Added + loadFile function for convenience. Made decodePNG32 faster. +*) 21 jun 2006: (!) Changed type of info vector to unsigned. + Changed position of palette in info vector. Fixed an important bug that + happened on PNGs with an uncompressed block. +*) 16 jun 2006: Internally changed unsigned into unsigned where + needed, and performed some optimizations. +*) 07 jun 2006: (!) Renamed functions to decodePNG and placed them + in LodePNG namespace. Changed the order of the parameters. Rewrote the + documentation in the header. Renamed files to lodepng.cpp and lodepng.h +*) 22 apr 2006: Optimized and improved some code +*) 07 sep 2005: (!) Changed to std::vector interface +*) 12 aug 2005: Initial release (C++, decoder only) + + +12. contact information +----------------------- + +Feel free to contact me with suggestions, problems, comments, ... concerning +LodePNG. If you encounter a PNG image that doesn't work properly with this +decoder, feel free to send it and I'll use it to find and fix the problem. + +My email address is (puzzle the account and domain together with an @ symbol): +Domain: gmail dot com. +Account: lode dot vandevenne. + + +Copyright (c) 2005-2012 Lode Vandevenne +*/ diff --git a/Shadows/SampleFramework11/Math.cpp b/Shadows/SampleFramework11/Math.cpp new file mode 100644 index 0000000..0d631dc --- /dev/null +++ b/Shadows/SampleFramework11/Math.cpp @@ -0,0 +1,997 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" +#include "Math.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +// == Float2 ====================================================================================== + +Float2::Float2() +{ + x = y = 0.0f; +} + +Float2::Float2(float x_) +{ + x = y = x_; +} + +Float2::Float2(float x_, float y_) +{ + x = x_; + y = y_; +} + +Float2::Float2(const XMFLOAT2& xy) +{ + x = xy.x; + y = xy.y; +} + +Float2::Float2(FXMVECTOR xy) +{ + XMStoreFloat2(this, xy); +} + +Float2& Float2::operator+=(const Float2& other) +{ + x += other.x; + y += other.y; + return *this; +} + +Float2 Float2::operator+(const Float2& other) const +{ + Float2 result; + result.x = x + other.x; + result.y = y + other.y; + return result; +} + +Float2& Float2::operator-=(const Float2& other) +{ + x -= other.x; + y -= other.y; + return *this; +} + +Float2 Float2::operator-(const Float2& other) const +{ + Float2 result; + result.x = x - other.x; + result.y = y - other.y; + return result; +} + +Float2& Float2::operator*=(const Float2& other) +{ + x *= other.x; + y *= other.y; + return *this; +} + +Float2 Float2::operator*(const Float2& other) const +{ + Float2 result; + result.x = x * other.x; + result.y = y * other.y; + return result; +} + +Float2& Float2::operator/=(const Float2& other) +{ + x /= other.x; + y /= other.y; + return *this; +} + +Float2 Float2::operator/(const Float2& other) const +{ + Float2 result; + result.x = x / other.x; + result.y = y / other.y; + return result; +} + +bool Float2::operator==(const Float2& other) const +{ + return x == other.x && y == other.y; +} + +bool Float2::operator!=(const Float2& other) const +{ + return x != other.x || y != other.y; +} + +Float2 Float2::operator-() const +{ + Float2 result; + result.x = -x; + result.y = -y; + + return result; +} + +XMVECTOR Float2::ToSIMD() const +{ + return XMLoadFloat2(this); +} + +Float2 Float2::Clamp(const Float2& val, const Float2& min, const Float2& max) +{ + Float2 retVal; + retVal.x = SampleFramework11::Clamp(val.x, min.x, max.x); + retVal.y = SampleFramework11::Clamp(val.y, min.y, max.y); + return retVal; +} + +float Float2::Length(const Float2& val) +{ + return std::sqrtf(val.x * val.x + val.y * val.y); +} + +// == Float3 ====================================================================================== + +Float3::Float3() +{ + x = y = z = 0.0f; +} + +Float3::Float3(float x_) +{ + x = y = z = x_; +} + +Float3::Float3(float x_, float y_, float z_) +{ + x = x_; + y = y_; + z = z_; +} + +Float3::Float3(const XMFLOAT3& xyz) +{ + x = xyz.x; + y = xyz.y; + z = xyz.z; +} + +Float3::Float3(FXMVECTOR xyz) +{ + XMStoreFloat3(this, xyz); +} + +Float3& Float3::operator+=(const Float3& other) +{ + x += other.x; + y += other.y; + z += other.z; + return *this; +} + +Float3 Float3::operator+(const Float3& other) const +{ + Float3 result; + result.x = x + other.x; + result.y = y + other.y; + result.z = z + other.z; + return result; +} + +Float3& Float3::operator+=(float s) +{ + x += s; + y += s; + z += s; + return *this; +} + +Float3 Float3::operator+(float s) const +{ + Float3 result; + result.x = x + s; + result.y = y + s; + result.z = z + s; + return result; +} + +Float3& Float3::operator-=(const Float3& other) +{ + x -= other.x; + y -= other.y; + z -= other.z; + return *this; +} + +Float3 Float3::operator-(const Float3& other) const +{ + Float3 result; + result.x = x - other.x; + result.y = y - other.y; + result.z = z - other.z; + return result; +} + +Float3& Float3::operator-=(float s) +{ + x -= s; + y -= s; + z -= s; + return *this; +} + +Float3 Float3::operator-(float s) const +{ + Float3 result; + result.x = x - s; + result.y = y - s; + result.z = z - s; + return result; +} + + +Float3& Float3::operator*=(const Float3& other) +{ + x *= other.x; + y *= other.y; + z *= other.z; + return *this; +} + +Float3 Float3::operator*(const Float3& other) const +{ + Float3 result; + result.x = x * other.x; + result.y = y * other.y; + result.z = z * other.z; + return result; +} + +Float3& Float3::operator*=(float s) +{ + x *= s; + y *= s; + z *= s; + return *this; +} + +Float3 Float3::operator*(float s) const +{ + Float3 result; + result.x = x * s; + result.y = y * s; + result.z = z * s; + return result; +} + +Float3& Float3::operator/=(const Float3& other) +{ + x /= other.x; + y /= other.y; + z /= other.z; + return *this; +} + +Float3 Float3::operator/(const Float3& other) const +{ + Float3 result; + result.x = x / other.x; + result.y = y / other.y; + result.z = z / other.z; + return result; +} + +Float3& Float3::operator/=(float s) +{ + x /= s; + y /= s; + z /= s; + return *this; +} + +Float3 Float3::operator/(float s) const +{ + Float3 result; + result.x = x / s; + result.y = y / s; + result.z = z / s; + return result; +} + +bool Float3::operator==(const Float3& other) const +{ + return x == other.x && y == other.y && z == other.z; +} + +bool Float3::operator!=(const Float3& other) const +{ + return x != other.x || y != other.y || z != other.z; +} + +Float3 Float3::operator-() const +{ + Float3 result; + result.x = -x; + result.y = -y; + result.z = -z; + + return result; +} + +Float3 operator*(float a, const Float3& b) +{ + return Float3(a * b.x, a * b.y, a * b.z); +} + +XMVECTOR Float3::ToSIMD() const +{ + return XMLoadFloat3(this); +} + +float Float3::Length() const +{ + return Float3::Length(*this); +} + +float Float3::Dot(const Float3& a, const Float3& b) +{ + return XMVectorGetX(XMVector3Dot(a.ToSIMD(), b.ToSIMD())); +} + +Float3 Float3::Cross(const Float3& a, const Float3& b) +{ + Float3 result; + XMStoreFloat3(&result, XMVector3Cross(a.ToSIMD(), b.ToSIMD())); + return result; +} + +Float3 Float3::Normalize(const Float3& a) +{ + Float3 result; + XMStoreFloat3(&result, XMVector3Normalize(a.ToSIMD())); + return result; +} + +Float3 Float3::Transform(const Float3& v, const Float3x3& m) +{ + XMVECTOR vec = v.ToSIMD(); + vec = XMVector3TransformCoord(vec, m.ToSIMD()); + return Float3(vec); +} + +Float3 Float3::Transform(const Float3& v, const Float4x4& m) +{ + XMVECTOR vec = v.ToSIMD(); + vec = XMVector3TransformCoord(vec, m.ToSIMD()); + return Float3(vec); +} + +Float3 Float3::TransformDirection(const Float3&v, const Float4x4& m) +{ + XMVECTOR vec = v.ToSIMD(); + vec = XMVector3TransformNormal(vec, m.ToSIMD()); + return Float3(vec); +} + +Float3 Float3::Transform(const Float3& v, const Quaternion& q) +{ + return Float3::Transform(v, q.ToFloat3x3()); +} + +Float3 Float3::Clamp(const Float3& val, const Float3& min, const Float3& max) +{ + Float3 retVal; + retVal.x = SampleFramework11::Clamp(val.x, min.x, max.x); + retVal.y = SampleFramework11::Clamp(val.y, min.y, max.y); + retVal.z = SampleFramework11::Clamp(val.z, min.z, max.z); + return retVal; +} + +Float3 Float3::Perpendicular(const Float3& vec) +{ + Assert_(vec.Length() >= 0.00001f); + + Float3 perp; + + float x = std::abs(vec.x); + float y = std::abs(vec.y); + float z = std::abs(vec.z); + float minVal = std::min(x, y); + minVal = std::min(minVal, z); + + if(minVal == x) + perp = Float3::Cross(vec, Float3(1.0f, 0.0f, 0.0f)); + else if(minVal == y) + perp = Float3::Cross(vec, Float3(0.0f, 1.0f, 0.0f)); + else + perp = Float3::Cross(vec, Float3(0.0f, 0.0f, 1.0f)); + + return Float3::Normalize(perp); +} + +float Float3::Distance(const Float3& a, const Float3& b) +{ + XMVECTOR x = XMLoadFloat3(&a); + XMVECTOR y = XMLoadFloat3(&b); + XMVECTOR length = XMVector3Length(XMVectorSubtract(x, y)); + return XMVectorGetX(length); +} + +float Float3::Length(const Float3& v) +{ + XMVECTOR x = XMLoadFloat3(&v); + XMVECTOR length = XMVector3Length(x); + return XMVectorGetX(length); +} + +// == Float4 ====================================================================================== + +Float4::Float4() +{ + x = y = z = w = 0.0f; +} + +Float4::Float4(float x_) +{ + x = y = z = w = x_; +} + +Float4::Float4(float x_, float y_, float z_, float w_) +{ + x = x_; + y = y_; + z = z_; + w = w_; +} + +Float4::Float4(const XMFLOAT3& xyz, float w_) +{ + x = xyz.x; + y = xyz.y; + z = xyz.z; + w = w_; +} + +Float4::Float4(const XMFLOAT4& xyzw) +{ + x = xyzw.x; + y = xyzw.y; + z = xyzw.z; + w = xyzw.w; +} + +Float4::Float4(FXMVECTOR xyzw) +{ + XMStoreFloat4(this, xyzw); +} + +Float4& Float4::operator+=(const Float4& other) +{ + x += other.x; + y += other.y; + z += other.z; + w += other.w; + return *this; +} + +Float4 Float4::operator+(const Float4& other) const +{ + Float4 result; + result.x = x + other.x; + result.y = y + other.y; + result.z = z + other.z; + result.w = w + other.w; + return result; +} + +Float4& Float4::operator-=(const Float4& other) +{ + x -= other.x; + y -= other.y; + z -= other.z; + w -= other.w; + return *this; +} + +Float4 Float4::operator-(const Float4& other) const +{ + Float4 result; + result.x = x - other.x; + result.y = y - other.y; + result.z = z - other.z; + result.w = w - other.w; + return result; +} + +Float4& Float4::operator*=(const Float4& other) +{ + x *= other.x; + y *= other.y; + z *= other.z; + w *= other.w; + return *this; +} + +Float4 Float4::operator*(const Float4& other) const +{ + Float4 result; + result.x = x * other.x; + result.y = y * other.y; + result.z = z * other.z; + result.w = w * other.w; + return result; +} + +Float4& Float4::operator/=(const Float4& other) +{ + x /= other.x; + y /= other.y; + z /= other.z; + w /= other.w; + return *this; +} + +Float4 Float4::operator/(const Float4& other) const +{ + Float4 result; + result.x = x / other.x; + result.y = y / other.y; + result.z = z / other.z; + result.w = w / other.w; + return result; +} + +bool Float4::operator==(const Float4& other) const +{ + return x == other.x && y == other.y && z == other.z && w == other.w; +} + +bool Float4::operator!=(const Float4& other) const +{ + return x != other.x || y != other.y || z != other.z || w != other.w; +} + + +Float4 Float4::operator-() const +{ + Float4 result; + result.x = -x; + result.y = -y; + result.z = -z; + result.w = -w; + + return result; +} + +XMVECTOR Float4::ToSIMD() const +{ + return XMLoadFloat4(this); +} + +Float3 Float4::To3D() const +{ + return Float3(x, y, z); +} + +Float4 Float4::Clamp(const Float4& val, const Float4& min, const Float4& max) +{ + Float4 retVal; + retVal.x = SampleFramework11::Clamp(val.x, min.x, max.x); + retVal.y = SampleFramework11::Clamp(val.y, min.y, max.y); + retVal.z = SampleFramework11::Clamp(val.z, min.z, max.z); + retVal.w = SampleFramework11::Clamp(val.w, min.w, max.w); + return retVal; +} + +// == Quaternion ================================================================================== + +Quaternion::Quaternion() +{ + *this = Quaternion::Identity(); +} + +Quaternion::Quaternion(float x_, float y_, float z_, float w_) +{ + x = x_; + y = y_; + z = z_; + w = w_; +} + +Quaternion::Quaternion(const Float3& axis, float angle) +{ + *this = Quaternion::FromAxisAngle(axis, angle); +} + +Quaternion::Quaternion(const Float3x3& m) +{ + *this = XMQuaternionRotationMatrix(m.ToSIMD()); +} + +Quaternion::Quaternion(const XMFLOAT4& q) +{ + x = q.x; + y = q.y; + z = q.z; + w = q.w; +} + +Quaternion::Quaternion(FXMVECTOR q) +{ + XMStoreFloat4(this, q); +} + +Quaternion& Quaternion::operator*=(const Quaternion& other) +{ + XMVECTOR q = ToSIMD(); + q = XMQuaternionMultiply(q, other.ToSIMD()); + XMStoreFloat4(this, q); + + return *this; +} + +Quaternion Quaternion::operator*(const Quaternion& other) const +{ + Quaternion q = *this; + q *= other; + return q; +} + +bool Quaternion::operator==(const Quaternion& other) const +{ + return x == other.x && y == other.y && z == other.z && w == other.w; +} + +bool Quaternion::operator!=(const Quaternion& other) const +{ + return x != other.x || y != other.y || z != other.z || w != other.w; +} + +Float3x3 Quaternion::ToFloat3x3() const +{ + return Float3x3(XMMatrixRotationQuaternion(ToSIMD())); +} + +Float4x4 Quaternion::ToFloat4x4() const +{ + return Float4x4(XMMatrixRotationQuaternion(ToSIMD())); +} + +Quaternion Quaternion::Identity() +{ + return Quaternion(0.0f, 0.0f, 0.0f, 1.0f); +} + +Quaternion Quaternion::Invert(const Quaternion& q) +{ + return Quaternion(XMQuaternionInverse(q.ToSIMD())); +} + +Quaternion Quaternion::FromAxisAngle(const Float3& axis, float angle) +{ + XMVECTOR q = XMQuaternionRotationAxis(axis.ToSIMD(), angle); + return Quaternion(q); +} + +Quaternion Quaternion::Normalize(const Quaternion& q) +{ + return Quaternion(XMQuaternionNormalize(q.ToSIMD())); +} + +Float3x3 Quaternion::ToFloat3x3(const Quaternion& q) +{ + return q.ToFloat3x3(); +} + +Float4x4 Quaternion::ToFloat4x4(const Quaternion& q) +{ + return q.ToFloat4x4(); +} + +XMVECTOR Quaternion::ToSIMD() const +{ + return XMLoadFloat4(this); +} + +// == Float3x3 ==================================================================================== + +Float3x3::Float3x3() +{ + _11 = _22 = _33 = 1.00f; + _12 = _13 = 0.0f; + _21 = _23 = 0.0f; + _31 = _32 = 0.0f; +} + +Float3x3::Float3x3(const XMFLOAT3X3& m) +{ + *static_cast(this) = m; +} + +Float3x3::Float3x3(CXMMATRIX m) +{ + XMStoreFloat3x3(this, m); +} + +Float3x3& Float3x3::operator*=(const Float3x3& other) +{ + XMMATRIX result = this->ToSIMD() * other.ToSIMD(); + XMStoreFloat3x3(this, result); + return *this; +} + +Float3x3 Float3x3::operator*(const Float3x3& other) const +{ + XMMATRIX result = this->ToSIMD() * other.ToSIMD(); + return Float3x3(result); +} + +Float3 Float3x3::Up() const +{ + return Float3(_21, _22, _23); +} + +Float3 Float3x3::Down() const +{ + return Float3(-_21, -_22, -_23); +} + +Float3 Float3x3::Left() const +{ + return Float3(-_11, -_12, -_13); +} + +Float3 Float3x3::Right() const +{ + return Float3(_11, _12, _13); +} + +Float3 Float3x3::Forward() const +{ + return Float3(_31, _32, _33); +} + +Float3 Float3x3::Back() const +{ + return Float3(-_31, -_32, -_33); +} + +void Float3x3::SetXBasis(const Float3& x) +{ + _11 = x.x; + _12 = x.y; + _13 = x.z; +} + +void Float3x3::SetYBasis(const Float3& y) +{ + _21 = y.x; + _22 = y.y; + _23 = y.z; +} + +void Float3x3::SetZBasis(const Float3& z) +{ + _31 = z.x; + _32 = z.y; + _33 = z.z; +} + +Float3x3 Float3x3::Transpose(const Float3x3& m) +{ + return XMMatrixTranspose(m.ToSIMD()); +} + +Float3x3 Float3x3::Invert(const Float3x3& m) +{ + XMVECTOR det; + return XMMatrixInverse(&det, m.ToSIMD()); +} + +Float3x3 Float3x3::ScaleMatrix(float s) +{ + Float3x3 m; + m._11 = m._22 = m._33 = s; + return m; +} + +Float3x3 Float3x3::ScaleMatrix(const Float3& s) +{ + Float3x3 m; + m._11 = s.x; + m._22 = s.y; + m._33 = s.z; + return m; +} + +XMMATRIX Float3x3::ToSIMD() const +{ + return XMLoadFloat3x3(this); +} + +// == Float4x4 ==================================================================================== + +Float4x4::Float4x4() +{ + _11 = _22 = _33 = _44 = 1.00f; + _12 = _13 = _14 = 0.0f; + _21 = _23 = _24 = 0.0f; + _31 = _32 = _34 = 0.0f; + _41 = _42 = _43 = 0.0f; +} + +Float4x4::Float4x4(const XMFLOAT4X4& m) +{ + *static_cast(this) = m; +} + +Float4x4::Float4x4(CXMMATRIX m) +{ + XMStoreFloat4x4(this, m); +} + +Float4x4& Float4x4::operator*=(const Float4x4& other) +{ + XMMATRIX result = this->ToSIMD() * other.ToSIMD(); + XMStoreFloat4x4(this, result); + return *this; +} + +Float4x4 Float4x4::operator*(const Float4x4& other) const +{ + XMMATRIX result = this->ToSIMD() * other.ToSIMD(); + return Float4x4(result); +} + +Float3 Float4x4::Up() const +{ + return Float3(_21, _22, _23); +} + +Float3 Float4x4::Down() const +{ + return Float3(-_21, -_22, -_23); +} + +Float3 Float4x4::Left() const +{ + return Float3(-_11, -_12, -_13); +} + +Float3 Float4x4::Right() const +{ + return Float3(_11, _12, _13); +} + +Float3 Float4x4::Forward() const +{ + return Float3(_31, _32, _33); +} + +Float3 Float4x4::Back() const +{ + return Float3(-_31, -_32, -_33); +} + +Float3 Float4x4::Translation() const +{ + return Float3(_41, _42, _43); +} + +void Float4x4::SetTranslation(const Float3& t) +{ + _41 = t.x; + _42 = t.y; + _43 = t.z; +} + +void Float4x4::SetXBasis(const Float3& x) +{ + _11 = x.x; + _12 = x.y; + _13 = x.z; +} + +void Float4x4::SetYBasis(const Float3& y) +{ + _21 = y.x; + _22 = y.y; + _23 = y.z; +} + +void Float4x4::SetZBasis(const Float3& z) +{ + _31 = z.x; + _32 = z.y; + _33 = z.z; +} + +Float4x4 Float4x4::Transpose(const Float4x4& m) +{ + return XMMatrixTranspose(m.ToSIMD()); +} + +Float4x4 Float4x4::Invert(const Float4x4& m) +{ + XMVECTOR det; + return XMMatrixInverse(&det, m.ToSIMD()); +} + +Float4x4 Float4x4::ScaleMatrix(float s) +{ + Float4x4 m; + m._11 = m._22 = m._33 = s; + return m; +} + +Float4x4 Float4x4::ScaleMatrix(const Float3& s) +{ + Float4x4 m; + m._11 = s.x; + m._22 = s.y; + m._33 = s.z; + return m; +} + +Float4x4 Float4x4::TranslationMatrix(const Float3& t) +{ + Float4x4 m; + m.SetTranslation(t); + return m; +} + +XMMATRIX Float4x4::ToSIMD() const +{ + return XMLoadFloat4x4(this); +} + +std::string Float4x4::Print() const +{ + std::string result = "{ { " + ToAnsiString(_11) + ToAnsiString(_12) + ToAnsiString(_13) + ToAnsiString(_14) + "}"; + result += " { " + ToAnsiString(_21) + ToAnsiString(_22) + ToAnsiString(_23) + ToAnsiString(_24) + "}"; + result += " { " + ToAnsiString(_31) + ToAnsiString(_32) + ToAnsiString(_33) + ToAnsiString(_34) + "}"; + result += " { " + ToAnsiString(_41) + ToAnsiString(_42) + ToAnsiString(_43) + ToAnsiString(_44) + "} }"; + return result; +} + +// == Uint2 ======================================================================================= + +Uint2::Uint2() : x(0), y(0) +{ +} + +Uint2::Uint2(uint32 x_, uint32 y_) : x(x_), y(y_) +{ +} + +// == Uint3 ======================================================================================= + +Uint3::Uint3() : x(0), y(0), z(0) +{ +} + +Uint3::Uint3(uint32 x_, uint32 y_, uint32 z_) : x(x_), y(y_), z(z_) +{ +} + +// == Uint4 ======================================================================================= + +Uint4::Uint4() : x(0), y(0), z(0), w(0) +{ +} + +Uint4::Uint4(uint32 x_, uint32 y_, uint32 z_, uint32 w_) : x(x_), y(y_), z(z_), w(w_) +{ +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Math.h b/Shadows/SampleFramework11/Math.h new file mode 100644 index 0000000..6fc7854 --- /dev/null +++ b/Shadows/SampleFramework11/Math.h @@ -0,0 +1,304 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "Assert.h" + +namespace SampleFramework11 +{ + +// Forward declarations +struct Quaternion; +struct Float3x3; +struct Float4x4; + +// Extension classes for XMFLOAT* classes + +struct Float2 : public XMFLOAT2 +{ + Float2(); + Float2(float x); + Float2(float x, float y); + Float2(const XMFLOAT2& xy); + Float2(FXMVECTOR xy); + + Float2& operator+=(const Float2& other); + Float2 operator+(const Float2& other) const; + + Float2& operator-=(const Float2& other); + Float2 operator-(const Float2& other) const; + + Float2& operator*=(const Float2& other); + Float2 operator*(const Float2& other) const; + + Float2& operator/=(const Float2& other); + Float2 operator/(const Float2& other) const; + + bool operator==(const Float2& other) const; + bool operator!=(const Float2& other) const; + + Float2 operator-() const; + + XMVECTOR ToSIMD() const; + + static Float2 Clamp(const Float2& val, const Float2& min, const Float2& max); + static float Length(const Float2& val); +}; + +struct Float3 : public XMFLOAT3 +{ + Float3(); + Float3(float x); + Float3(float x, float y, float z); + Float3(const XMFLOAT3& xyz); + Float3(FXMVECTOR xyz); + + Float3& operator+=(const Float3& other); + Float3 operator+(const Float3& other) const; + + Float3& operator+=(float other); + Float3 operator+(float other) const; + + Float3& operator-=(const Float3& other); + Float3 operator-(const Float3& other) const; + + Float3& operator-=(float other); + Float3 operator-(float other) const; + + Float3& operator*=(const Float3& s); + Float3 operator*(const Float3& s) const; + + Float3& operator*=(float other); + Float3 operator*(float other) const; + + Float3& operator/=(const Float3& other); + Float3 operator/(const Float3& other) const; + + Float3& operator/=(float other); + Float3 operator/(float other) const; + + bool operator==(const Float3& other) const; + bool operator!=(const Float3& other) const; + + Float3 operator-() const; + + XMVECTOR ToSIMD() const; + + float Length() const; + + static float Dot(const Float3& a, const Float3& b); + static Float3 Cross(const Float3& a, const Float3& b); + static Float3 Normalize(const Float3& a); + static Float3 Transform(const Float3& v, const Float3x3& m); + static Float3 Transform(const Float3& v, const Float4x4& m); + static Float3 TransformDirection(const Float3&v, const Float4x4& m); + static Float3 Transform(const Float3& v, const Quaternion& q); + static Float3 Clamp(const Float3& val, const Float3& min, const Float3& max); + static Float3 Perpendicular(const Float3& v); + static float Distance(const Float3& a, const Float3& b); + static float Length(const Float3& v); +}; + +// Non-member operators of Float3 +Float3 operator*(float a, const Float3& b); + +struct Float4 : public XMFLOAT4 +{ + Float4(); + Float4(float x); + Float4(float x, float y, float z, float w); + Float4(const XMFLOAT3& xyz, float w = 0.0f); + Float4(const XMFLOAT4& xyzw); + Float4(FXMVECTOR xyzw); + + Float4& operator+=(const Float4& other); + Float4 operator+(const Float4& other) const; + + Float4& operator-=(const Float4& other); + Float4 operator-(const Float4& other) const; + + Float4& operator*=(const Float4& other); + Float4 operator*(const Float4& other) const; + + Float4& operator/=(const Float4& other); + Float4 operator/(const Float4& other) const; + + bool operator==(const Float4& other) const; + bool operator!=(const Float4& other) const; + + Float4 operator-() const; + + XMVECTOR ToSIMD() const; + Float3 To3D() const; + + static Float4 Clamp(const Float4& val, const Float4& min, const Float4& max); +}; + +struct Quaternion : public XMFLOAT4 +{ + Quaternion(); + Quaternion(float x, float y, float z, float w); + Quaternion(const Float3& axis, float angle); + Quaternion(const Float3x3& m); + Quaternion(const XMFLOAT4& q); + Quaternion(FXMVECTOR q); + + Quaternion& operator*=(const Quaternion& other); + Quaternion operator*(const Quaternion& other) const; + + bool operator==(const Quaternion& other) const; + bool operator!=(const Quaternion& other) const; + + Float3x3 ToFloat3x3() const; + Float4x4 ToFloat4x4() const; + + static Quaternion Identity(); + static Quaternion Invert(const Quaternion& q); + static Quaternion FromAxisAngle(const Float3& axis, float angle); + static Quaternion Normalize(const Quaternion& q); + static Float3x3 ToFloat3x3(const Quaternion& q); + static Float4x4 ToFloat4x4(const Quaternion& q); + + XMVECTOR ToSIMD() const; +}; + +struct Float3x3 : public XMFLOAT3X3 +{ + Float3x3(); + Float3x3(const XMFLOAT3X3& m); + Float3x3(CXMMATRIX m); + + Float3x3& operator*=(const Float3x3& other); + Float3x3 operator*(const Float3x3& other) const; + + Float3 Up() const; + Float3 Down() const; + Float3 Left() const; + Float3 Right() const; + Float3 Forward() const; + Float3 Back() const; + + void SetXBasis(const Float3& x); + void SetYBasis(const Float3& y); + void SetZBasis(const Float3& z); + + static Float3x3 Transpose(const Float3x3& m); + static Float3x3 Invert(const Float3x3& m); + static Float3x3 ScaleMatrix(float s); + static Float3x3 ScaleMatrix(const Float3& s); + + XMMATRIX ToSIMD() const; +}; + +struct Float4x4 : public XMFLOAT4X4 +{ + Float4x4(); + Float4x4(const XMFLOAT4X4& m); + Float4x4(CXMMATRIX m); + + Float4x4& operator*=(const Float4x4& other); + Float4x4 operator*(const Float4x4& other) const; + + Float3 Up() const; + Float3 Down() const; + Float3 Left() const; + Float3 Right() const; + Float3 Forward() const; + Float3 Back() const; + + Float3 Translation() const; + void SetTranslation(const Float3& t); + + void SetXBasis(const Float3& x); + void SetYBasis(const Float3& y); + void SetZBasis(const Float3& z); + + static Float4x4 Transpose(const Float4x4& m); + static Float4x4 Invert(const Float4x4& m); + static Float4x4 ScaleMatrix(float s); + static Float4x4 ScaleMatrix(const Float3& s); + static Float4x4 TranslationMatrix(const Float3& t); + + XMMATRIX ToSIMD() const; + + std::string Print() const; +}; + +// Unsigned 32-bit integer vector classes +struct Uint2 +{ + uint32 x; + uint32 y; + + Uint2(); + Uint2(uint32 x, uint32 y); +}; + +struct Uint3 +{ + uint32 x; + uint32 y; + uint32 z; + + Uint3(); + Uint3(uint32 x, uint32 y, uint32 z); +}; + +struct Uint4 +{ + uint32 x; + uint32 y; + uint32 z; + uint32 w; + + Uint4(); + Uint4(uint32 x, uint32 y, uint32 z, uint32 w); +}; + +// General math functions + +// Linear interpolation +template T Lerp(const T& x, const T& y, float s) +{ + return x + (y - x) * s; +} + +// Clamps a value to the specified range +template T Clamp(T val, T min, T max) +{ + Assert_(max >= min); + + if(val < min) + val = min; + else if(val > max) + val = max; + return val; +} + +// Clamps a value to [0, 1] +template T Saturate(T val) +{ + return Clamp(val, T(0.0f), T(1.0f)); +} + +// Rounds a float +inline float Round(float r) +{ + return (r > 0.0f) ? std::floorf(r + 0.5f) : std::ceilf(r - 0.5f); +} + +// Returns a random float value between 0 and 1 +inline float RandFloat() +{ + return rand() / static_cast(RAND_MAX); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Model.cpp b/Shadows/SampleFramework11/Model.cpp new file mode 100644 index 0000000..414a2a8 --- /dev/null +++ b/Shadows/SampleFramework11/Model.cpp @@ -0,0 +1,858 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Model.h" + +#include "SDKmesh.h" +#include "Exceptions.h" +#include "Utility.h" +#include "GraphicsTypes.h" +#include "Serialization.h" +#include "FileIO.h" + +using std::string; +using std::wstring; +using std::vector; +using std::map; +using std::wifstream; + +namespace SampleFramework11 +{ + +struct Vertex +{ + Float3 Position; + Float3 Normal; + Float2 TexCoord; + Float3 Tangent; + Float3 Bitangent; + + Vertex() + { + } + + Vertex(const Float3& p, const Float3& n, const Float2& tc, const Float3& t, const Float3& b) + { + Position = p; + Normal = n; + TexCoord = tc; + Tangent = t; + Bitangent = b; + } + + void Transform(const Float3& p, const Float3& s, const Quaternion& q) + { + Position *= s; + Position = Float3::Transform(Position, q); + Position += p; + + Normal = Float3::Transform(Normal, q); + Tangent = Float3::Transform(Tangent, q); + Bitangent = Float3::Transform(Bitangent, q); + } +}; + +static const D3D11_INPUT_ELEMENT_DESC VertexInputs[5] = +{ + { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "BITANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0 }, +}; + +Mesh::Mesh() : vertexStride(0), + numVertices(0), + numIndices(0) +{ +} + +Mesh::~Mesh() +{ +} + +void Mesh::Initialize(ID3D11Device* device, SDKMesh& sdkMesh, uint32 meshIdx, bool generateTangents) +{ + const SDKMESH_MESH& sdkMeshData = *sdkMesh.GetMesh(meshIdx); + + uint32 indexSize = 2; + indexType = Mesh::Index16Bit; + if(sdkMesh.GetIndexType(meshIdx) == IT_32BIT) + { + indexSize = 4; + indexType = Mesh::Index32Bit; + } + + vertexStride = sdkMesh.GetVertexStride(meshIdx, 0); + numVertices = static_cast(sdkMesh.GetNumVertices(meshIdx, 0)); + numIndices = static_cast(sdkMesh.GetNumIndices(meshIdx)); + const uint32 vbIdx = sdkMeshData.VertexBuffers[0]; + const uint32 ibIdx = sdkMeshData.IndexBuffer; + + CreateInputElements(sdkMesh.VBElements(0)); + + vertices.resize(vertexStride * numVertices, 0); + memcpy(vertices.data(), sdkMesh.GetRawVerticesAt(vbIdx), vertexStride * numVertices); + + indices.resize(indexSize * numIndices, 0); + memcpy(indices.data(), sdkMesh.GetRawIndicesAt(ibIdx), indexSize * numIndices); + + if(generateTangents) + GenerateTangentFrame(); + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.Usage = D3D11_USAGE_IMMUTABLE; + bufferDesc.ByteWidth = vertexStride * numVertices; + bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + bufferDesc.CPUAccessFlags = 0; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = vertices.data(); + initData.SysMemPitch = 0; + initData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&bufferDesc, &initData, &vertexBuffer)); + + bufferDesc.ByteWidth = indexSize * numIndices; + bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + + initData.pSysMem = indices.data(); + DXCall(device->CreateBuffer(&bufferDesc, &initData, &indexBuffer)); + + const uint32 numSubsets = sdkMesh.GetNumSubsets(meshIdx); + meshParts.resize(numSubsets); + for(uint32 i = 0; i < numSubsets; ++i) + { + const SDKMESH_SUBSET& subset = *sdkMesh.GetSubset(meshIdx, i); + MeshPart& part = meshParts[i]; + part.IndexStart = static_cast(subset.IndexStart); + part.IndexCount = static_cast(subset.IndexCount); + part.VertexStart = static_cast(subset.VertexStart); + part.VertexCount = static_cast(subset.VertexCount); + part.MaterialIdx = subset.MaterialID; + } +} + +// Initializes the mesh as a box +void Mesh::InitBox(ID3D11Device* device, const Float3& dimensions, const Float3& position, + const Quaternion& orientation, uint32 materialIdx) +{ + const uint64 NumBoxVerts = 24; + const uint64 NumBoxIndices = 36; + + std::vector boxVerts(NumBoxVerts); + std::vector boxIndices(NumBoxIndices, 0); + + uint64 vIdx = 0; + + // Top + boxVerts[vIdx++] = Vertex(Float3(-1.0f, 1.0f, 1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(0.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, 1.0f, 1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(1.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, 1.0f, -1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(1.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, 1.0f, -1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(0.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + + // Bottom + boxVerts[vIdx++] = Vertex(Float3(-1.0f, -1.0f, -1.0f), Float3(0.0f, -1.0f, 0.0f), Float2(0.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, -1.0f, -1.0f), Float3(0.0f, -1.0f, 0.0f), Float2(1.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, -1.0f, 1.0f), Float3(0.0f, -1.0f, 0.0f), Float2(1.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, -1.0f, 1.0f), Float3(0.0f, -1.0f, 0.0f), Float2(0.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f)); + + // Front + boxVerts[vIdx++] = Vertex(Float3(-1.0f, 1.0f, -1.0f), Float3(0.0f, 0.0f, -1.0f), Float2(0.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, 1.0f, -1.0f), Float3(0.0f, 0.0f, -1.0f), Float2(1.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, -1.0f, -1.0f), Float3(0.0f, 0.0f, -1.0f), Float2(1.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, -1.0f, -1.0f), Float3(0.0f, 0.0f, -1.0f), Float2(0.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + + // Back + boxVerts[vIdx++] = Vertex(Float3(1.0f, 1.0f, 1.0f), Float3(0.0f, 0.0f, 1.0f), Float2(0.0f, 0.0f), Float3(-1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, 1.0f, 1.0f), Float3(0.0f, 0.0f, 1.0f), Float2(1.0f, 0.0f), Float3(-1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, -1.0f, 1.0f), Float3(0.0f, 0.0f, 1.0f), Float2(1.0f, 1.0f), Float3(-1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, -1.0f, 1.0f), Float3(0.0f, 0.0f, 1.0f), Float2(0.0f, 1.0f), Float3(-1.0f, 0.0f, 0.0f), Float3(0.0f, -1.0f, 0.0f)); + + // Left + boxVerts[vIdx++] = Vertex(Float3(-1.0f, 1.0f, 1.0f), Float3(-1.0f, 0.0f, 0.0f), Float2(0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, 1.0f, -1.0f), Float3(-1.0f, 0.0f, 0.0f), Float2(1.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, -1.0f, -1.0f), Float3(-1.0f, 0.0f, 0.0f), Float2(1.0f, 1.0f), Float3(0.0f, 0.0f, -1.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(-1.0f, -1.0f, 1.0f), Float3(-1.0f, 0.0f, 0.0f), Float2(0.0f, 1.0f), Float3(0.0f, 0.0f, -1.0f), Float3(0.0f, -1.0f, 0.0f)); + + // Right + boxVerts[vIdx++] = Vertex(Float3(1.0f, 1.0f, -1.0f), Float3(1.0f, 0.0f, 0.0f), Float2(0.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, 1.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float2(1.0f, 0.0f), Float3(0.0f, 0.0f, 1.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, -1.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float2(1.0f, 1.0f), Float3(0.0f, 0.0f, 1.0f), Float3(0.0f, -1.0f, 0.0f)); + boxVerts[vIdx++] = Vertex(Float3(1.0f, -1.0f, -1.0f), Float3(1.0f, 0.0f, 0.0f), Float2(0.0f, 1.0f), Float3(0.0f, 0.0f, 1.0f), Float3(0.0f, -1.0f, 0.0f)); + + for(uint64 i = 0; i < NumBoxVerts; ++i) + boxVerts[i].Transform(position, dimensions * 0.5f, orientation); + + uint64 iIdx = 0; + + // Top + boxIndices[iIdx++] = 0; + boxIndices[iIdx++] = 1; + boxIndices[iIdx++] = 2; + boxIndices[iIdx++] = 2; + boxIndices[iIdx++] = 3; + boxIndices[iIdx++] = 0; + + // Bottom + boxIndices[iIdx++] = 4 + 0; + boxIndices[iIdx++] = 4 + 1; + boxIndices[iIdx++] = 4 + 2; + boxIndices[iIdx++] = 4 + 2; + boxIndices[iIdx++] = 4 + 3; + boxIndices[iIdx++] = 4 + 0; + + // Front + boxIndices[iIdx++] = 8 + 0; + boxIndices[iIdx++] = 8 + 1; + boxIndices[iIdx++] = 8 + 2; + boxIndices[iIdx++] = 8 + 2; + boxIndices[iIdx++] = 8 + 3; + boxIndices[iIdx++] = 8 + 0; + + // Back + boxIndices[iIdx++] = 12 + 0; + boxIndices[iIdx++] = 12 + 1; + boxIndices[iIdx++] = 12 + 2; + boxIndices[iIdx++] = 12 + 2; + boxIndices[iIdx++] = 12 + 3; + boxIndices[iIdx++] = 12 + 0; + + // Left + boxIndices[iIdx++] = 16 + 0; + boxIndices[iIdx++] = 16 + 1; + boxIndices[iIdx++] = 16 + 2; + boxIndices[iIdx++] = 16 + 2; + boxIndices[iIdx++] = 16 + 3; + boxIndices[iIdx++] = 16 + 0; + + // Right + boxIndices[iIdx++] = 20 + 0; + boxIndices[iIdx++] = 20 + 1; + boxIndices[iIdx++] = 20 + 2; + boxIndices[iIdx++] = 20 + 2; + boxIndices[iIdx++] = 20 + 3; + boxIndices[iIdx++] = 20 + 0; + + const uint32 indexSize = 2; + indexType = Mesh::Index16Bit; + + vertexStride = sizeof(Vertex); + numVertices = uint32(NumBoxVerts); + numIndices = uint32(NumBoxIndices); + + inputElements.clear(); + inputElements.resize(sizeof(VertexInputs) / sizeof(D3D11_INPUT_ELEMENT_DESC)); + memcpy(inputElements.data(), VertexInputs, sizeof(VertexInputs)); + + const uint32 vbSize = vertexStride * numVertices; + vertices.resize(vbSize, 0); + memcpy(vertices.data(), boxVerts.data(), vbSize); + + const uint32 ibSize = indexSize * numIndices; + indices.resize(ibSize, 0); + memcpy(indices.data(), boxIndices.data(), ibSize); + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.Usage = D3D11_USAGE_IMMUTABLE; + bufferDesc.ByteWidth = vbSize; + bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + bufferDesc.CPUAccessFlags = 0; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = vertices.data(); + initData.SysMemPitch = 0; + initData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&bufferDesc, &initData, &vertexBuffer)); + + bufferDesc.ByteWidth = ibSize; + bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + + initData.pSysMem = indices.data(); + DXCall(device->CreateBuffer(&bufferDesc, &initData, &indexBuffer)); + + meshParts.resize(1); + + MeshPart& part = meshParts[0]; + part.IndexStart = 0; + part.IndexCount = numIndices; + part.VertexStart = 0; + part.VertexCount = numVertices; + part.MaterialIdx = materialIdx; +} + +// Initializes the mesh as a plane +void Mesh::InitPlane(ID3D11Device* device, const Float2& dimensions, const Float3& position, + const Quaternion& orientation, uint32 materialIdx) +{ + const uint64 NumPlaneVerts = 4; + const uint64 NumPlaneIndices = 6; + + std::vector planeVerts(NumPlaneVerts); + std::vector planeIndices(NumPlaneIndices, 0); + + uint64 vIdx = 0; + + planeVerts[vIdx++] = Vertex(Float3(-1.0f, 0.0f, 1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(0.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + planeVerts[vIdx++] = Vertex(Float3(1.0f, 0.0f, 1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(1.0f, 0.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + planeVerts[vIdx++] = Vertex(Float3(1.0f, 0.0f, -1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(1.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + planeVerts[vIdx++] = Vertex(Float3(-1.0f, 0.0f, -1.0f), Float3(0.0f, 1.0f, 0.0f), Float2(0.0f, 1.0f), Float3(1.0f, 0.0f, 0.0f), Float3(0.0f, 0.0f, -1.0f)); + + for(uint64 i = 0; i < NumPlaneVerts; ++i) + planeVerts[i].Transform(position, Float3(dimensions.x, 1.0f, dimensions.y) * 0.5f, orientation); + + uint64 iIdx = 0; + planeIndices[iIdx++] = 0; + planeIndices[iIdx++] = 1; + planeIndices[iIdx++] = 2; + planeIndices[iIdx++] = 2; + planeIndices[iIdx++] = 3; + planeIndices[iIdx++] = 0; + + const uint32 indexSize = 2; + indexType = Mesh::Index16Bit; + + vertexStride = sizeof(Vertex); + numVertices = uint32(NumPlaneVerts); + numIndices = uint32(NumPlaneIndices); + + inputElements.clear(); + inputElements.resize(sizeof(VertexInputs) / sizeof(D3D11_INPUT_ELEMENT_DESC)); + memcpy(inputElements.data(), VertexInputs, sizeof(VertexInputs)); + + const uint32 vbSize = vertexStride * numVertices; + vertices.resize(vbSize, 0); + memcpy(vertices.data(), planeVerts.data(), vbSize); + + const uint32 ibSize = indexSize * numIndices; + indices.resize(ibSize, 0); + memcpy(indices.data(), planeIndices.data(), ibSize); + + D3D11_BUFFER_DESC bufferDesc; + bufferDesc.Usage = D3D11_USAGE_IMMUTABLE; + bufferDesc.ByteWidth = vbSize; + bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + bufferDesc.CPUAccessFlags = 0; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = vertices.data(); + initData.SysMemPitch = 0; + initData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&bufferDesc, &initData, &vertexBuffer)); + + bufferDesc.ByteWidth = ibSize; + bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; + bufferDesc.MiscFlags = 0; + bufferDesc.StructureByteStride = 0; + + initData.pSysMem = indices.data(); + DXCall(device->CreateBuffer(&bufferDesc, &initData, &indexBuffer)); + + meshParts.resize(1); + + MeshPart& part = meshParts[0]; + part.IndexStart = 0; + part.IndexCount = numIndices; + part.VertexStart = 0; + part.VertexCount = numVertices; + part.MaterialIdx = materialIdx; +} + +void Mesh::GenerateTangentFrame() +{ + // Make sure that we have a position + texture coordinate + normal + uint32 posOffset = 0xFFFFFFFF; + uint32 nmlOffset = 0xFFFFFFFF; + uint32 tcOffset = 0xFFFFFFFF; + for(uint32 i = 0; i < inputElements.size(); ++i) + { + const std::string semantic = inputElements[i].SemanticName; + const uint32 offset = inputElements[i].AlignedByteOffset; + if(semantic == "POSITION") + posOffset = offset; + else if(semantic == "NORMAL") + nmlOffset = offset; + else if(semantic == "TEXCOORD") + tcOffset = offset; + } + + if(posOffset == 0xFFFFFFFF || nmlOffset == 0xFFFFFFFF || tcOffset == 0xFFFFFFFF) + throw Exception(L"Can't generate a tangent frame, mesh doesn't have positions, normals, and texcoords"); + + // Clone the mesh + std::vector newVertices(numVertices); + + const uint8* vtxData = vertices.data(); + for(uint32 i = 0; i < numVertices; ++i) + { + newVertices[i].Position = *reinterpret_cast(vtxData + posOffset); + newVertices[i].Normal = *reinterpret_cast(vtxData + nmlOffset); + newVertices[i].TexCoord = *reinterpret_cast(vtxData + tcOffset); + vtxData += vertexStride; + } + + // Compute the tangent frame for each vertex. The following code is based on + // "Computing Tangent Space Basis Vectors for an Arbitrary Mesh", by Eric Lengyel + // http://www.terathon.com/code/tangent.html + + // Make temporary arrays for the tangent and the bitangent + std::vector tangents(numVertices); + std::vector bitangents(numVertices); + + // Loop through each triangle + const uint32 indexSize = indexType == Index16Bit ? 2 : 4; + for (uint32 i = 0; i < numIndices; i += 3) + { + uint32 i1 = GetIndex(indices.data(), i + 0, indexSize); + uint32 i2 = GetIndex(indices.data(), i + 1, indexSize); + uint32 i3 = GetIndex(indices.data(), i + 2, indexSize); + + const Float3& v1 = newVertices[i1].Position; + const Float3& v2 = newVertices[i2].Position; + const Float3& v3 = newVertices[i3].Position; + + const Float2& w1 = newVertices[i1].TexCoord; + const Float2& w2 = newVertices[i2].TexCoord; + const Float2& w3 = newVertices[i3].TexCoord; + + float x1 = v2.x - v1.x; + float x2 = v3.x - v1.x; + float y1 = v2.y - v1.y; + float y2 = v3.y - v1.y; + float z1 = v2.z - v1.z; + float z2 = v3.z - v1.z; + + float s1 = w2.x - w1.x; + float s2 = w3.x - w1.x; + float t1 = w2.y - w1.y; + float t2 = w3.y - w1.y; + + float r = 1.0f / (s1 * t2 - s2 * t1); + Float3 sDir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r); + Float3 tDir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); + + tangents[i1] += sDir; + tangents[i2] += sDir; + tangents[i3] += sDir; + + bitangents[i1] += tDir; + bitangents[i2] += tDir; + bitangents[i3] += tDir; + } + + for (uint32 i = 0; i < numVertices; ++i) + { + Float3& n = newVertices[i].Normal; + Float3& t = tangents[i]; + + // Gram-Schmidt orthogonalize + Float3 tangent = (t - n * Float3::Dot(n, t)); + bool zeroTangent = false; + if(tangent.Length() > 0.00001f) + Float3::Normalize(tangent); + else if(n.Length() > 0.00001f) + { + tangent = Float3::Perpendicular(n); + zeroTangent = true; + } + + float sign = 1.0f; + + if(!zeroTangent) + { + Float3 b; + b = Float3::Cross(n, t); + sign = (Float3::Dot(b, bitangents[i]) < 0.0f) ? -1.0f : 1.0f; + } + + // Store the tangent + bitangent + newVertices[i].Tangent = Float3::Normalize(tangent); + + newVertices[i].Bitangent = Float3::Normalize(Float3::Cross(n, tangent)); + newVertices[i].Bitangent *= sign; + } + + inputElements.clear(); + inputElements.resize(sizeof(VertexInputs) / sizeof(D3D11_INPUT_ELEMENT_DESC)); + memcpy(inputElements.data(), VertexInputs, sizeof(VertexInputs)); + + vertexStride = sizeof(Vertex); + vertices.clear(); + vertices.resize(numVertices * vertexStride); + memcpy(vertices.data(), newVertices.data(), numVertices * vertexStride); +} + +void Mesh::CreateInputElements(const D3DVERTEXELEMENT9* declaration) +{ + map nameMap; + nameMap[D3DDECLUSAGE_POSITION] = "POSITION"; + nameMap[D3DDECLUSAGE_BLENDWEIGHT] = "BLENDWEIGHT"; + nameMap[D3DDECLUSAGE_BLENDINDICES] = "BLENDINDICES"; + nameMap[D3DDECLUSAGE_NORMAL] = "NORMAL"; + nameMap[D3DDECLUSAGE_TEXCOORD] = "TEXCOORD"; + nameMap[D3DDECLUSAGE_TANGENT] = "TANGENT"; + nameMap[D3DDECLUSAGE_BINORMAL] = "BITANGENT"; + nameMap[D3DDECLUSAGE_COLOR] = "COLOR"; + + map formatMap; + formatMap[D3DDECLTYPE_FLOAT1] = DXGI_FORMAT_R32_FLOAT; + formatMap[D3DDECLTYPE_FLOAT2] = DXGI_FORMAT_R32G32_FLOAT; + formatMap[D3DDECLTYPE_FLOAT3] = DXGI_FORMAT_R32G32B32_FLOAT; + formatMap[D3DDECLTYPE_FLOAT4] = DXGI_FORMAT_R32G32B32A32_FLOAT; + formatMap[D3DDECLTYPE_D3DCOLOR] = DXGI_FORMAT_R8G8B8A8_UNORM; + formatMap[D3DDECLTYPE_UBYTE4] = DXGI_FORMAT_R8G8B8A8_UINT; + formatMap[D3DDECLTYPE_SHORT2] = DXGI_FORMAT_R16G16_SINT; + formatMap[D3DDECLTYPE_SHORT4] = DXGI_FORMAT_R16G16B16A16_SINT; + formatMap[D3DDECLTYPE_UBYTE4N] = DXGI_FORMAT_R8G8B8A8_UNORM; + formatMap[D3DDECLTYPE_SHORT2N] = DXGI_FORMAT_R16G16_SNORM; + formatMap[D3DDECLTYPE_SHORT4N] = DXGI_FORMAT_R16G16B16A16_SNORM; + formatMap[D3DDECLTYPE_USHORT2N] = DXGI_FORMAT_R16G16_UNORM; + formatMap[D3DDECLTYPE_USHORT4N] = DXGI_FORMAT_R16G16B16A16_UNORM; + formatMap[D3DDECLTYPE_UDEC3] = DXGI_FORMAT_R10G10B10A2_UINT; + formatMap[D3DDECLTYPE_DEC3N] = DXGI_FORMAT_R10G10B10A2_UNORM; + formatMap[D3DDECLTYPE_FLOAT16_2] = DXGI_FORMAT_R16G16_FLOAT; + formatMap[D3DDECLTYPE_FLOAT16_4] = DXGI_FORMAT_R16G16B16A16_FLOAT; + + // Figure out the number of elements + uint32 numInputElements = 0; + while(declaration[numInputElements].Stream != 0xFF) + { + const D3DVERTEXELEMENT9& element9 = declaration[numInputElements]; + D3D11_INPUT_ELEMENT_DESC element11; + element11.InputSlot = 0; + element11.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + element11.InstanceDataStepRate = 0; + element11.SemanticName = nameMap[element9.Usage]; + element11.Format = formatMap[element9.Type]; + element11.AlignedByteOffset = element9.Offset; + element11.SemanticIndex = element9.UsageIndex; + inputElements.push_back(element11); + + numInputElements++; + } +} + +// Does a basic draw of all parts +void Mesh::Render(ID3D11DeviceContext* context) +{ + // Set the vertices and indices + ID3D11Buffer* vertexBuffers[1] = { vertexBuffer }; + uint32 vertexStrides[1] = { vertexStride }; + uint32 offsets[1] = { 0 }; + context->IASetVertexBuffers(0, 1, vertexBuffers, vertexStrides, offsets); + context->IASetIndexBuffer(indexBuffer, IndexBufferFormat(), 0); + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Draw each MeshPart + for(size_t i = 0; i < meshParts.size(); ++i) + { + MeshPart& meshPart = meshParts[i]; + context->DrawIndexed(meshPart.IndexCount, meshPart.IndexStart, 0); + } +} + +// == Model ======================================================================================= + +Model::Model() +{ +} + +Model::~Model() +{ +} + +void Model::CreateFromSDKMeshFile(ID3D11Device* device, LPCWSTR fileName, const wchar* normalMapSuffix, + bool generateTangentFrame, bool overrideNormalMaps) +{ + Assert_(FileExists(fileName)); + + // Use the SDKMesh class to load in the data + SDKMesh sdkMesh; + sdkMesh.Create(fileName); + + wstring directory = GetDirectoryFromFilePath(fileName); + + // Make materials + uint32 numMaterials = sdkMesh.GetNumMaterials(); + for(uint32 i = 0; i < numMaterials; ++i) + { + MeshMaterial material; + SDKMESH_MATERIAL* mat = sdkMesh.GetMaterial(i); + memcpy(&material.AmbientAlbedo, &mat->Ambient, sizeof(Float4)); + memcpy(&material.DiffuseAlbedo, &mat->Diffuse, sizeof(Float4)); + memcpy(&material.SpecularAlbedo, &mat->Specular, sizeof(Float4)); + memcpy(&material.Emissive, &mat->Emissive, sizeof(Float4)); + material.Alpha = mat->Diffuse.w; + material.SpecularPower = mat->Power; + material.DiffuseMapName = AnsiToWString(mat->DiffuseTexture); + material.NormalMapName = AnsiToWString(mat->NormalTexture); + + // Add the normal map prefix + if (normalMapSuffix && material.DiffuseMapName.length() > 0 + && (material.NormalMapName.length() == 0 || overrideNormalMaps)) + { + wstring base = GetFilePathWithoutExtension(material.DiffuseMapName.c_str()); + wstring extension = GetFileExtension(material.DiffuseMapName.c_str()); + material.NormalMapName = base + normalMapSuffix + L"." + extension; + } + + LoadMaterialResources(material, directory, device); + + meshMaterials.push_back(material); + } + + uint32 numMeshes = sdkMesh.GetNumMeshes(); + meshes.resize(numMeshes); + for(uint32 meshIdx = 0; meshIdx < numMeshes; ++meshIdx) + meshes[meshIdx].Initialize(device, sdkMesh, meshIdx, generateTangentFrame); +} + +void Model::GenerateBoxScene(ID3D11Device* device) +{ + MeshMaterial material; + material.DiffuseMapName = L"White.png"; + material.NormalMapName = L"Hex.png"; + LoadMaterialResources(material, L"..\\Content\\Textures\\", device); + meshMaterials.push_back(material); + + meshes.resize(2); + meshes[0].InitBox(device, Float3(2.0f), Float3(0.0f, 1.5f, 0.0f), Quaternion(), 0); + meshes[1].InitBox(device, Float3(10.0f, 0.25f, 10.0f), Float3(0.0f), Quaternion(), 0); +} + +void Model::GeneratePlaneScene(ID3D11Device* device, const Float2& dimensions, const Float3& position, + const Quaternion& orientation) +{ + MeshMaterial material; + material.DiffuseMapName = L"White.png"; + material.NormalMapName = L"Hex.png"; + LoadMaterialResources(material, L"..\\Content\\Textures\\", device); + meshMaterials.push_back(material); + + meshes.resize(1); + meshes[0].InitPlane(device, dimensions, position, orientation, 0); +} + +void Model::LoadMaterialResources(MeshMaterial& material, const wstring& directory, ID3D11Device* device) +{ + // Load the diffuse map + wstring diffuseMapPath = directory + material.DiffuseMapName; + if(material.DiffuseMapName.length() > 1 && FileExists(diffuseMapPath.c_str())) + material.DiffuseMap = LoadTexture(device, diffuseMapPath.c_str()); + + // Load the normal map + wstring normalMapPath = directory + material.NormalMapName; + + if(material.NormalMapName.length() > 1 && FileExists(normalMapPath.c_str())) + material.NormalMap = LoadTexture(device, normalMapPath.c_str()); +} + +void Model::WriteToFile(const wchar* path, ID3D11Device* device, ID3D11DeviceContext* context) +{ + // If the file exists, delete it + if(FileExists(path)) + Win32Call(DeleteFile(path)); + + // Create the file + HANDLE fileHandle = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + if(fileHandle == INVALID_HANDLE_VALUE) + Win32Call(false); + + // Write out the number of meshes + DWORD bytesWritten = 0; + uint32 numMeshes = static_cast(meshes.size()); + SerializeWrite(fileHandle, numMeshes); + + // Write out the meshes + for(uint32 meshIdx = 0; meshIdx < meshes.size(); ++meshIdx) + { + Mesh& mesh = meshes[meshIdx]; + + // Write the mesh vertex data + uint32 vbSize = mesh.numVertices * mesh.vertexStride; + SerializeWrite(fileHandle, vbSize); + + StagingBuffer stagingVB; + stagingVB.Initialize(device, vbSize); + context->CopyResource(stagingVB.Buffer, mesh.vertexBuffer); + const void* vertexData = stagingVB.Map(context); + Win32Call(WriteFile(fileHandle, vertexData, vbSize, &bytesWritten, NULL)); + stagingVB.Unmap(context); + + // Write the mesh index buffer data + uint32 ibSize = mesh.numIndices * (mesh.indexType == Mesh::Index16Bit ? 2 : 4); + SerializeWrite(fileHandle, ibSize); + + StagingBuffer stagingIB; + stagingIB.Initialize(device, ibSize); + context->CopyResource(stagingIB.Buffer, mesh.indexBuffer); + const void* indexData = stagingIB.Map(context); + Win32Call(WriteFile(fileHandle, indexData, stagingIB.Size, &bytesWritten, NULL)); + stagingIB.Unmap(context); + + // Write out the other mesh members + SerializeWriteVector(fileHandle, mesh.meshParts); + SerializeWriteVector(fileHandle, mesh.inputElements); + SerializeWrite(fileHandle, mesh.vertexStride); + SerializeWrite(fileHandle, mesh.numVertices); + SerializeWrite(fileHandle, mesh.numIndices); + + uint32 ibType = static_cast(mesh.indexType); + SerializeWrite(fileHandle, ibType); + + // Write out the input element names + uint32 numInputElements = static_cast(mesh.inputElementNames.size()); + SerializeWrite(fileHandle, numInputElements); + for(uint32 i = 0; i < numInputElements; ++i) + SerializeWriteString(fileHandle, mesh.inputElementNames[i]); + } + + // Write out the mesh material data + uint32 numMaterials = static_cast(meshMaterials.size()); + SerializeWrite(fileHandle, numMaterials); + + for(uint32 materialIdx = 0; materialIdx < numMaterials; ++materialIdx) + { + const MeshMaterial& material = meshMaterials[materialIdx]; + + SerializeWrite(fileHandle, material.AmbientAlbedo); + SerializeWrite(fileHandle, material.DiffuseAlbedo); + SerializeWrite(fileHandle, material.SpecularAlbedo); + SerializeWrite(fileHandle, material.Emissive); + SerializeWrite(fileHandle, material.SpecularPower); + SerializeWrite(fileHandle, material.Alpha); + SerializeWriteString(fileHandle, material.DiffuseMapName); + SerializeWriteString(fileHandle, material.NormalMapName); + } + + // Close the file + Win32Call(CloseHandle(fileHandle)); +} + +void Model::ReadFromFile(const wchar* path, ID3D11Device* device) +{ + wstring directory = GetDirectoryFromFilePath(path); + + // Open the file + HANDLE fileHandle = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if(fileHandle == INVALID_HANDLE_VALUE) + Win32Call(false); + + // Read in the number of meshes + uint32 numMeshes = 0; + SerializeRead(fileHandle, numMeshes); + + meshes.resize(numMeshes); + for(uint32 meshIdx = 0; meshIdx < numMeshes; ++meshIdx) + { + Mesh& mesh = meshes[meshIdx]; + + // Read in the vertex data + uint32 vbSize = 0; + SerializeRead(fileHandle, vbSize); + + UINT8* vbData = new UINT8[vbSize]; + DWORD bytesRead = 0; + Win32Call(ReadFile(fileHandle, vbData, vbSize, &bytesRead, NULL)); + + D3D11_BUFFER_DESC vbDesc; + vbDesc.ByteWidth = vbSize; + vbDesc.CPUAccessFlags = 0; + vbDesc.StructureByteStride = 0; + vbDesc.MiscFlags = 0; + vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + vbDesc.Usage = D3D11_USAGE_IMMUTABLE; + + D3D11_SUBRESOURCE_DATA srData; + srData.pSysMem = vbData; + srData.SysMemPitch = 0; + srData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&vbDesc, &srData, &mesh.vertexBuffer)); + + delete [] vbData; + + // Read in the index data + uint32 ibSize = 0; + SerializeRead(fileHandle, ibSize); + + UINT8* ibData = new UINT8[ibSize]; + Win32Call(ReadFile(fileHandle, ibData, ibSize, &bytesRead, NULL)); + + D3D11_BUFFER_DESC ibDesc; + ibDesc.ByteWidth = ibSize; + ibDesc.CPUAccessFlags = 0; + ibDesc.StructureByteStride = 0; + ibDesc.MiscFlags = 0; + ibDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; + ibDesc.Usage = D3D11_USAGE_IMMUTABLE; + + srData.pSysMem = ibData; + + DXCall(device->CreateBuffer(&ibDesc, &srData, &mesh.indexBuffer)); + + // Read in the other mesh members + SerializeReadVector(fileHandle, mesh.meshParts); + SerializeReadVector(fileHandle, mesh.inputElements); + SerializeRead(fileHandle, mesh.vertexStride); + SerializeRead(fileHandle, mesh.numVertices); + SerializeRead(fileHandle, mesh.numIndices); + + uint32 ibType = 0; + SerializeRead(fileHandle, ibType); + mesh.indexType = static_cast(ibType); + + // Read in the input element names + uint32 numInputElements = 0; + SerializeRead(fileHandle, numInputElements); + mesh.inputElementNames.resize(numInputElements); + for(uint32 i = 0; i < numInputElements; ++i) + { + SerializeReadString(fileHandle, mesh.inputElementNames[i]); + mesh.inputElements[i].SemanticName = mesh.inputElementNames[i].c_str(); + } + } + + // Read in the material data + uint32 numMaterials = 0; + SerializeRead(fileHandle, numMaterials); + meshMaterials.resize(numMaterials); + + for(uint32 materialIdx = 0; materialIdx < numMaterials; ++materialIdx) + { + MeshMaterial& material = meshMaterials[materialIdx]; + + SerializeRead(fileHandle, material.AmbientAlbedo); + SerializeRead(fileHandle, material.DiffuseAlbedo); + SerializeRead(fileHandle, material.SpecularAlbedo); + SerializeRead(fileHandle, material.Emissive); + SerializeRead(fileHandle, material.SpecularPower); + SerializeRead(fileHandle, material.Alpha); + SerializeReadString(fileHandle, material.DiffuseMapName); + SerializeReadString(fileHandle, material.NormalMapName); + + // Load the resources + LoadMaterialResources(material, directory, device); + } + + // Close the file + Win32Call(CloseHandle(fileHandle)); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Model.h b/Shadows/SampleFramework11/Model.h new file mode 100644 index 0000000..244145c --- /dev/null +++ b/Shadows/SampleFramework11/Model.h @@ -0,0 +1,166 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "Math.h" + +namespace SampleFramework11 +{ + +class SDKMesh; + +struct MeshMaterial +{ + Float3 AmbientAlbedo; + Float3 DiffuseAlbedo; + Float3 SpecularAlbedo; + Float3 Emissive; + float SpecularPower; + float Alpha; + std::wstring DiffuseMapName; + std::wstring NormalMapName; + ID3D11ShaderResourceViewPtr DiffuseMap; + ID3D11ShaderResourceViewPtr NormalMap; + + MeshMaterial() : SpecularPower(1.0f), Alpha(1.0f) + { + } +}; + +struct MeshPart +{ + uint32 VertexStart; + uint32 VertexCount; + uint32 IndexStart; + uint32 IndexCount; + uint32 MaterialIdx; + + MeshPart() : VertexStart(0), VertexCount(0), IndexStart(0), IndexCount(0), MaterialIdx(0) + { + } +}; + +class Mesh +{ + friend class Model; + +public: + + enum IndexType + { + Index16Bit = 0, + Index32Bit = 1 + }; + + // Lifetime + Mesh(); + ~Mesh(); + + // Init from loaded files + void Initialize(ID3D11Device* device, SDKMesh& sdkmesh, uint32 meshIdx, bool generateTangents); + + // Procedural generation + void InitBox(ID3D11Device* device, const Float3& dimensions, const Float3& position, + const Quaternion& orientation, uint32 materialIdx); + + void InitPlane(ID3D11Device* device, const Float2& dimensions, const Float3& position, + const Quaternion& orientation, uint32 materialIdx); + + // Rendering + void Render(ID3D11DeviceContext* context); + + // Accessors + ID3D11Buffer* VertexBuffer() { return vertexBuffer; } + const ID3D11Buffer* VertexBuffer() const { return vertexBuffer; } + ID3D11Buffer* IndexBuffer() { return indexBuffer; } + const ID3D11Buffer* IndexBuffer() const { return indexBuffer; } + + std::vector& MeshParts() { return meshParts; } + const std::vector& MeshParts() const { return meshParts; } + + const D3D11_INPUT_ELEMENT_DESC* InputElements() const { return &inputElements[0]; } + uint32 NumInputElements() const { return static_cast(inputElements.size()); } + + uint32 VertexStride() const { return vertexStride; } + uint32 NumVertices() const { return numVertices; } + uint32 NumIndices() const { return numIndices; } + + IndexType IndexBufferType() const { return indexType; } + DXGI_FORMAT IndexBufferFormat() const { return indexType == Index32Bit ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT; } + uint32 IndexSize() const { return indexType == Index32Bit ? 4 : 2; } + + const uint8* Vertices() const { return vertices.data(); } + const uint8* Indices() const { return indices.data(); } + +protected: + + void GenerateTangentFrame(); + void CreateInputElements(const D3DVERTEXELEMENT9* declaration); + + ID3D11BufferPtr vertexBuffer; + ID3D11BufferPtr indexBuffer; + + std::vector meshParts; + std::vector inputElements; + + uint32 vertexStride; + uint32 numVertices; + uint32 numIndices; + + IndexType indexType; + + std::vector inputElementNames; + + std::vector vertices; + std::vector indices; +}; + +class Model +{ +public: + + // Constructor/Destructor + Model(); + ~Model(); + + // Loading from file formats + void CreateFromSDKMeshFile(ID3D11Device* device, LPCWSTR fileName, + const wchar* normalMapSuffix = NULL, + bool generateTangentFrame = false, + bool overrideNormalMaps = false); + + // Procedural generation + void GenerateBoxScene(ID3D11Device* device); + void GeneratePlaneScene(ID3D11Device* device, const Float2& dimensions, const Float3& position, + const Quaternion& orientation); + + // Serialization + void WriteToFile(const wchar* path, ID3D11Device* device, ID3D11DeviceContext* context); + void ReadFromFile(const wchar* path, ID3D11Device* device); + + // Accessors + std::vector& Materials() { return meshMaterials; }; + const std::vector& Materials() const { return meshMaterials; }; + + std::vector& Meshes() { return meshes; } + const std::vector& Meshes() const { return meshes; } + +protected: + + static void LoadMaterialResources(MeshMaterial& material, const std::wstring& directory, ID3D11Device* device); + + std::vector meshes; + std::vector meshMaterials; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/MurmurHash.cpp b/Shadows/SampleFramework11/MurmurHash.cpp new file mode 100644 index 0000000..e0453b2 --- /dev/null +++ b/Shadows/SampleFramework11/MurmurHash.cpp @@ -0,0 +1,153 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" +#include "MurmurHash.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +std::wstring Hash::ToString() const +{ + return SampleFramework11::ToString(A) + L"_" + SampleFramework11::ToString(B); +} + +// Platform-specific functions and macros +#define FORCE_INLINE __forceinline + +#include + +#define ROTL32(x,y) _rotl(x,y) +#define ROTL64(x,y) _rotl64(x,y) + +#define BIG_CONSTANT(x) (x) + +//----------------------------------------------------------------------------- +// Block read - if your platform needs to do endian-swapping or can only +// handle aligned reads, do the conversion here + +FORCE_INLINE uint32_t getblock ( const uint32_t * p, int i ) +{ + return p[i]; +} + +FORCE_INLINE uint64_t getblock ( const uint64_t * p, int i ) +{ + return p[i]; +} + +//----------------------------------------------------------------------------- +// Finalization mix - force all bits of a hash block to avalanche + +FORCE_INLINE uint32_t fmix ( uint32_t h ) +{ + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + + return h; +} + +//---------- + +FORCE_INLINE uint64_t fmix ( uint64_t k ) +{ + k ^= k >> 33; + k *= BIG_CONSTANT(0xff51afd7ed558ccd); + k ^= k >> 33; + k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); + k ^= k >> 33; + + return k; +} + + +//----------------------------------------------------------------------------- + +Hash GenerateHash(const void* key, const int len, const uint32_t seed) +{ + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 16; + + uint64_t h1 = seed; + uint64_t h2 = seed; + + const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); + const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); + + //---------- + // body + + const uint64_t * blocks = (const uint64_t *)(data); + + for(int i = 0; i < nblocks; i++) + { + uint64_t k1 = getblock(blocks,i*2+0); + uint64_t k2 = getblock(blocks,i*2+1); + + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; + + h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; + + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; + + h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; + } + + //---------- + // tail + + const uint8_t * tail = (const uint8_t*)(data + nblocks*16); + + uint64_t k1 = 0; + uint64_t k2 = 0; + + switch(len & 15) + { + case 15: k2 ^= uint64_t(tail[14]) << 48; + case 14: k2 ^= uint64_t(tail[13]) << 40; + case 13: k2 ^= uint64_t(tail[12]) << 32; + case 12: k2 ^= uint64_t(tail[11]) << 24; + case 11: k2 ^= uint64_t(tail[10]) << 16; + case 10: k2 ^= uint64_t(tail[ 9]) << 8; + case 9: k2 ^= uint64_t(tail[ 8]) << 0; + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; + + case 8: k1 ^= uint64_t(tail[ 7]) << 56; + case 7: k1 ^= uint64_t(tail[ 6]) << 48; + case 6: k1 ^= uint64_t(tail[ 5]) << 40; + case 5: k1 ^= uint64_t(tail[ 4]) << 32; + case 4: k1 ^= uint64_t(tail[ 3]) << 24; + case 3: k1 ^= uint64_t(tail[ 2]) << 16; + case 2: k1 ^= uint64_t(tail[ 1]) << 8; + case 1: k1 ^= uint64_t(tail[ 0]) << 0; + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; + }; + + //---------- + // finalization + + h1 ^= len; h2 ^= len; + + h1 += h2; + h2 += h1; + + h1 = fmix(h1); + h2 = fmix(h2); + + h1 += h2; + h2 += h1; + + return Hash(h1, h2); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/MurmurHash.h b/Shadows/SampleFramework11/MurmurHash.h new file mode 100644 index 0000000..c5162cf --- /dev/null +++ b/Shadows/SampleFramework11/MurmurHash.h @@ -0,0 +1,35 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +namespace SampleFramework11 +{ + +struct Hash +{ + uint64 A; + uint64 B; + + Hash() : A(0), B(0) {} + Hash(uint64 a, uint64 b) : A(a), B(b) {} + + std::wstring ToString() const; + + bool operator==(const Hash& other) + { + return A == other.A && B == other.B; + } +}; + +Hash GenerateHash(const void* key, int len, uint32 seed = 0); + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/PCH.cpp b/Shadows/SampleFramework11/PCH.cpp new file mode 100644 index 0000000..bfb3aaa --- /dev/null +++ b/Shadows/SampleFramework11/PCH.cpp @@ -0,0 +1,11 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + diff --git a/Shadows/SampleFramework11/PCH.h b/Shadows/SampleFramework11/PCH.h new file mode 100644 index 0000000..ca9f24f --- /dev/null +++ b/Shadows/SampleFramework11/PCH.h @@ -0,0 +1,148 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +// Add common controls 6.0 DLL to the manifest +#if defined _M_IX86 +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") +#elif defined _M_IA64 +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"") +#elif defined _M_X64 +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") +#else +#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") +#endif + +// Platform SDK defines, specifies that our min version is Windows Vista +#ifndef WINVER +#define WINVER 0x0600 +#endif + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#endif + +#ifndef _WIN32_WINDOWS +#define _WIN32_WINDOWS 0x0410 +#endif + +#ifndef _WIN32_IE +#define _WIN32_IE 0x0700 +#endif + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +#define STRICT // Use strict declarations for Windows types + +// Windows Header Files: +#include +#include +#include +#include + +// MSVC COM Support +#include +#include + +// GDI+ +#include + +// DirectX Includes +#ifdef _DEBUG +#ifndef D3D_DEBUG_INFO +#define D3D_DEBUG_INFO +#endif +#endif + +#include +#include +#include +#include + +// DirectX Math +#include +#include + +using namespace DirectX; + +// Un-define min and max from the windows headers +#ifdef min + #undef min +#endif + +#ifdef max + #undef max +#endif + +// C RunTime Header Files +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma warning(push) +#pragma warning(disable : 4005) +#include +#include +#pragma warning(pop) + +// C++ Standard Library Header Files +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// AntTweakBar +#include "..\\..\\Externals\\AntTweakBar\\include\\AntTweakBar.h" + +// Static Lib Imports +#pragma comment(lib, "dxguid.lib") +#pragma comment(lib, "D3D9.lib") +#pragma comment(lib, "D3D11.lib") +#pragma comment(lib, "DXGI.lib") +#pragma comment(lib, "comctl32.lib") +#pragma comment(lib, "psapi.lib") +#pragma comment(lib, "d3dcompiler.lib") +#pragma comment(lib, "gdiplus.lib") +#pragma comment(lib, "AntTweakBar64.lib") + +#ifdef _DEBUG + #pragma comment(lib, "comsuppwd.lib") +#else + #pragma comment(lib, "comsuppw.lib") +#endif + +// Standard int typedefs +typedef int8_t int8; +typedef int16_t int16; +typedef int32_t int32; +typedef int64_t int64; +typedef uint8_t uint8; +typedef uint16_t uint16; +typedef uint32_t uint32; +typedef uint64_t uint64; + +typedef intptr_t intptr; +typedef uintptr_t uintptr; +typedef wchar_t wchar; +typedef uint32_t bool32; \ No newline at end of file diff --git a/Shadows/SampleFramework11/PostProcessorBase.cpp b/Shadows/SampleFramework11/PostProcessorBase.cpp new file mode 100644 index 0000000..abbad48 --- /dev/null +++ b/Shadows/SampleFramework11/PostProcessorBase.cpp @@ -0,0 +1,386 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "PostProcessorBase.h" + +#include "Utility.h" +#include "ShaderCompilation.h" + +namespace SampleFramework11 +{ + +PostProcessorBase::PostProcessorBase() : device(nullptr), context(nullptr) +{ +} + +PostProcessorBase::~PostProcessorBase() +{ + ClearTempRenderTargetCache(); +} + +void PostProcessorBase::Initialize(ID3D11Device* device) +{ + this->device = device; + + // Create resources for the full-screen quad + + // Load the shaders + quadVS = CompileVSFromFile(device, L"SampleFramework11\\Shaders\\Quad.hlsl", "QuadVS"); + + // Create the input layout + D3D11_INPUT_ELEMENT_DESC layout[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + }; + + DXCall(device->CreateInputLayout(layout, 2, quadVS->ByteCode->GetBufferPointer(), + quadVS->ByteCode->GetBufferSize(), &quadInputLayout)); + + // Create and initialize the vertex and index buffers + QuadVertex verts[4] = + { + { XMFLOAT4(1, 1, 1, 1), XMFLOAT2(1, 0) }, + { XMFLOAT4(1, -1, 1, 1), XMFLOAT2(1, 1) }, + { XMFLOAT4(-1, -1, 1, 1), XMFLOAT2(0, 1) }, + { XMFLOAT4(-1, 1, 1, 1), XMFLOAT2(0, 0) } + }; + + D3D11_BUFFER_DESC desc; + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.ByteWidth = sizeof(verts); + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + desc.CPUAccessFlags = 0; + desc.MiscFlags = 0; + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = verts; + initData.SysMemPitch = 0; + initData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&desc, &initData, &quadVB)); + + unsigned short indices[6] = { 0, 1, 2, 2, 3, 0 }; + + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.ByteWidth = sizeof(indices); + desc.BindFlags = D3D11_BIND_INDEX_BUFFER; + desc.CPUAccessFlags = 0; + initData.pSysMem = indices; + DXCall(device->CreateBuffer(&desc, &initData, &quadIB)); + + // Create the constant buffer + desc.Usage = D3D11_USAGE_DYNAMIC; + desc.ByteWidth = CBSize(sizeof(PSConstants)); + desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + DXCall(device->CreateBuffer(&desc, nullptr, &psConstants)); + + // Create a depth-stencil state + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = false; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + DXCall(device->CreateDepthStencilState(&dsDesc, &dsState)); + + // Create a blend state + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for (uint32 i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = false; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + DXCall(device->CreateBlendState(&blendDesc, &blendState)); + + // Create a rasterizer state + D3D11_RASTERIZER_DESC rastDesc; + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + DXCall(device->CreateRasterizerState(&rastDesc, &rastState)); + + // Create sampler states + D3D11_SAMPLER_DESC sampDesc; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.BorderColor[0] = 0; + sampDesc.BorderColor[1] = 0; + sampDesc.BorderColor[2] = 0; + sampDesc.BorderColor[3] = 0; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + sampDesc.MaxAnisotropy = 1; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + sampDesc.MinLOD = 0; + sampDesc.MipLODBias = 0; + DXCall(device->CreateSamplerState(&sampDesc, &linearSamplerState)); + + sampDesc.AddressU = sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + DXCall(device->CreateSamplerState(&sampDesc, &linearWrapSamplerState)); + + sampDesc.AddressU = sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER; + DXCall(device->CreateSamplerState(&sampDesc, &linearBorderSamplerState)); + + sampDesc.AddressU = sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + DXCall(device->CreateSamplerState(&sampDesc, &pointSamplerState)); + + sampDesc.AddressU = sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER; + DXCall(device->CreateSamplerState(&sampDesc, &pointBorderSamplerState)); +} + +void PostProcessorBase::AfterReset(uint32 width, uint32 height) +{ + // Clear all of the temp render targets + ClearTempRenderTargetCache(); + + inputWidth = width; + inputHeight = height; +} + +void PostProcessorBase::Render(ID3D11DeviceContext* deviceContext, ID3D11ShaderResourceView* input, ID3D11RenderTargetView* output) +{ + context = deviceContext; + + // Set device states + float blendFactor[4] = {1, 1, 1, 1}; + context->RSSetState(rastState); + context->OMSetBlendState(blendState, blendFactor, 0xFFFFFFFF); + context->OMSetDepthStencilState(dsState, 0); + + // Set the sampler states + ID3D11SamplerState* samplerStates[5] = { pointSamplerState, linearSamplerState, + linearWrapSamplerState, linearBorderSamplerState, + pointBorderSamplerState }; + context->PSSetSamplers(0, 5, samplerStates); + + // Set the input layout + context->IASetInputLayout(quadInputLayout); + + // Set the vertex buffer + uint32 stride = sizeof(QuadVertex); + uint32 offset = 0; + ID3D11Buffer* vertexBuffers[1] = { quadVB.GetInterfacePtr() }; + context->IASetVertexBuffers(0, 1, vertexBuffers, &stride, &offset); + + // Set the index buffer + context->IASetIndexBuffer(quadIB, DXGI_FORMAT_R16_UINT, 0); + + // Set primitive topology + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Set the shaders + context->VSSetShader(quadVS, nullptr, 0); + context->GSSetShader(nullptr, nullptr, 0); + context->DSSetShader(nullptr, nullptr, 0); + context->HSSetShader(nullptr, nullptr, 0); +} + +void PostProcessorBase::PostProcess(ID3D11ShaderResourceView* input, ID3D11RenderTargetView* output, ID3D11PixelShader* pixelShader, const wchar* name) +{ + inputs.push_back(input); + outputs.push_back(output); + PostProcess(pixelShader, name); +} + +void PostProcessorBase::PostProcess(ID3D11PixelShader* pixelShader, const wchar* name) +{ + _ASSERT(context); + + _ASSERT(inputs.size() <= MaxInputs); + + D3DPERF_BeginEvent(0xFFFFFFFF, name); + + // Set the outputs + ID3D11RenderTargetView** renderTargets = reinterpret_cast(&outputs[0]); + uint32 numRTs = static_cast(outputs.size()); + if(uaViews.size() == 0) + context->OMSetRenderTargets(numRTs, renderTargets, nullptr); + else + { + ID3D11UnorderedAccessView** uavs = reinterpret_cast(&uaViews[0]); + UINT numUAVs = static_cast(uaViews.size()); + UINT initialCounts[D3D11_PS_CS_UAV_REGISTER_COUNT] = { 0 }; + context->OMSetRenderTargetsAndUnorderedAccessViews(numRTs, renderTargets, nullptr, numRTs, numUAVs, uavs, initialCounts); + } + + // Set the input textures + ID3D11ShaderResourceView** textures = reinterpret_cast(&inputs[0]); + context->PSSetShaderResources(0, static_cast(inputs.size()), textures); + + // Set the constants + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(psConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + PSConstants* constants = reinterpret_cast(mapped.pData); + + for (size_t i = 0; i < inputs.size(); ++i) + { + if(inputs[i] == nullptr) + { + constants->InputSize[i].x = 0.0f; + constants->InputSize[i].y = 0.0f; + continue; + } + + ID3D11Resource* resource; + ID3D11Texture2DPtr texture; + D3D11_TEXTURE2D_DESC desc; + D3D11_SHADER_RESOURCE_VIEW_DESC srDesc; + inputs[i]->GetDesc(&srDesc); + uint32 mipLevel = srDesc.Texture2D.MostDetailedMip; + inputs[i]->GetResource(&resource); + texture.Attach(reinterpret_cast(resource)); + texture->GetDesc(&desc); + constants->InputSize[i].x = static_cast(std::max(desc.Width / (1 << mipLevel), 1)); + constants->InputSize[i].y = static_cast(std::max(desc.Height / (1 << mipLevel), 1)); + } + + ID3D11Resource* resource; + ID3D11Texture2DPtr texture; + D3D11_TEXTURE2D_DESC desc; + D3D11_RENDER_TARGET_VIEW_DESC rtDesc; + outputs[0]->GetResource(&resource); + outputs[0]->GetDesc(&rtDesc); + uint32 mipLevel = rtDesc.Texture2D.MipSlice; + texture.Attach(reinterpret_cast(resource)); + texture->GetDesc(&desc); + constants->OutputSize.x = static_cast(std::max(desc.Width / (1 << mipLevel), 1)); + constants->OutputSize.y = static_cast(std::max(desc.Height / (1 << mipLevel), 1)); + + context->Unmap(psConstants, 0); + + ID3D11Buffer* constantBuffers[1] = { psConstants }; + context->PSSetConstantBuffers(0, 1, constantBuffers); + + // Set the viewports + D3D11_VIEWPORT viewports[16]; + for (UINT_PTR i = 0; i < 16; ++i) + { + viewports[i].Width = static_cast(std::max(desc.Width / (1 << mipLevel), 1)); + viewports[i].Height = static_cast(std::max(desc.Height / (1 << mipLevel), 1)); + viewports[i].TopLeftX = 0; + viewports[i].TopLeftY = 0; + viewports[i].MinDepth = 0.0f; + viewports[i].MaxDepth = 1.0f; + } + context->RSSetViewports(static_cast(outputs.size()), viewports); + + // Set the pixel shader + context->PSSetShader(pixelShader, nullptr, 0); + + // Draw the quad + context->DrawIndexed(6, 0, 0); + + // Clear the SRV's and RT's + ID3D11ShaderResourceView* srViews[16] = { nullptr }; + context->PSSetShaderResources(0, static_cast(inputs.size()), srViews); + + ID3D11RenderTargetView* rtViews[16] = { nullptr }; + context->OMSetRenderTargets(static_cast(outputs.size() + uaViews.size()), rtViews, nullptr); + + inputs.clear(); + outputs.clear(); + uaViews.clear(); + + texture = nullptr; + D3DPERF_EndEvent(); +} + +TempRenderTarget* PostProcessorBase::GetTempRenderTarget(uint32 width, uint32 height, DXGI_FORMAT format, + uint32 msCount, uint32 msQuality, uint32 mipLevels, + bool generateMipMaps, bool useAsUAV) +{ + // Look through existing render targets + for (size_t i = 0; i < tempRenderTargets.size(); ++i) + { + TempRenderTarget* rt = tempRenderTargets[i]; + if (!rt->InUse && rt->Width == width && rt->Height == height && rt->Format == format + && rt->MSCount == msCount && rt->MSQuality == msQuality && (rt->UAView != nullptr) == useAsUAV) + { + rt->InUse = true; + return rt; + } + } + + // Didn't find one, have to make one + TempRenderTarget* rt = new TempRenderTarget(); + D3D11_TEXTURE2D_DESC desc; + desc.Width = width; + desc.Height = height; + desc.ArraySize = 1; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET; + if(useAsUAV) + desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS; + desc.CPUAccessFlags = 0; + desc.Format = format; + desc.MipLevels = mipLevels; + desc.MiscFlags = generateMipMaps ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0; + desc.SampleDesc.Count = msCount; + desc.SampleDesc.Quality = msQuality; + desc.Usage = D3D11_USAGE_DEFAULT; + DXCall(device->CreateTexture2D(&desc, nullptr, &rt->Texture)); + DXCall(device->CreateRenderTargetView(rt->Texture, nullptr, &rt->RTView)); + DXCall(device->CreateShaderResourceView(rt->Texture, nullptr, &rt->SRView)); + + if(useAsUAV) + device->CreateUnorderedAccessView(rt->Texture, nullptr, &rt->UAView); + else + rt->UAView = nullptr; + + rt->Width = width; + rt->Height = height; + rt->MSCount = msCount; + rt->MSQuality = msQuality; + rt->Format = format; + rt->InUse = true; + tempRenderTargets.push_back(rt); + + return tempRenderTargets[tempRenderTargets.size() - 1]; +} + +void PostProcessorBase::ClearTempRenderTargetCache() +{ + for (size_t i = 0; i < tempRenderTargets.size(); ++i) + { + tempRenderTargets[i]->SRView->Release(); + tempRenderTargets[i]->RTView->Release(); + tempRenderTargets[i]->Texture->Release(); + if(tempRenderTargets[i]->UAView) + tempRenderTargets[i]->UAView->Release(); + delete tempRenderTargets[i]; + } + + tempRenderTargets.erase(tempRenderTargets.begin(), tempRenderTargets.end()); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/PostProcessorBase.h b/Shadows/SampleFramework11/PostProcessorBase.h new file mode 100644 index 0000000..5fc816c --- /dev/null +++ b/Shadows/SampleFramework11/PostProcessorBase.h @@ -0,0 +1,106 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "ShaderCompilation.h" + +namespace SampleFramework11 +{ + +struct TempRenderTarget +{ + ID3D11Texture2D* Texture; + ID3D11ShaderResourceView* SRView; + ID3D11RenderTargetView* RTView; + ID3D11UnorderedAccessView* UAView; + UINT Width; + UINT Height; + DXGI_FORMAT Format; + UINT MSCount; + UINT MSQuality; + bool InUse; +}; + +class PostProcessorBase +{ + +public: + + PostProcessorBase(); + ~PostProcessorBase(); + + virtual void Initialize(ID3D11Device* device); + + virtual void Render(ID3D11DeviceContext* deviceContext, ID3D11ShaderResourceView* input, ID3D11RenderTargetView* output); + + virtual void AfterReset(UINT width, UINT height); + +protected: + + static const UINT_PTR MaxInputs = 4; + + struct PSConstants + { + XMFLOAT2 InputSize[MaxInputs]; + XMFLOAT2 OutputSize; + }; + + struct QuadVertex + { + XMFLOAT4 Position; + XMFLOAT2 TexCoord; + }; + + TempRenderTarget* GetTempRenderTarget(uint32 width, uint32 height, DXGI_FORMAT format, uint32 msCount = 1, + uint32 msQuality = 0, uint32 mipLevels = 1, bool generateMipMaps = false, + bool useAsUAV = false); + void ClearTempRenderTargetCache(); + + void PostProcess(ID3D11ShaderResourceView* input, ID3D11RenderTargetView* output, ID3D11PixelShader* pixelShader, const wchar* name); + virtual void PostProcess(ID3D11PixelShader* pixelShader, const wchar* name); + + std::vector tempRenderTargets; + ID3D11Device* device; + ID3D11DeviceContext* context; + + // Full screen quad + ID3D11BufferPtr quadVB; + ID3D11BufferPtr quadIB; + VertexShaderPtr quadVS; + ID3D11InputLayoutPtr quadInputLayout; + + // Sampler states + ID3D11SamplerStatePtr pointSamplerState; + ID3D11SamplerStatePtr linearSamplerState; + ID3D11SamplerStatePtr linearWrapSamplerState; + ID3D11SamplerStatePtr linearBorderSamplerState; + ID3D11SamplerStatePtr pointBorderSamplerState; + + // Device states + ID3D11DepthStencilStatePtr dsState; + ID3D11BlendStatePtr blendState; + ID3D11RasterizerStatePtr rastState; + + // Inputs and outputs + std::vector inputs; + std::vector outputs; + std::vector uaViews; + + // Constant buffer + ID3D11BufferPtr psConstants; + + uint32 inputWidth; + uint32 inputHeight; +}; + +} diff --git a/Shadows/SampleFramework11/Profiler.cpp b/Shadows/SampleFramework11/Profiler.cpp new file mode 100644 index 0000000..cceeb1a --- /dev/null +++ b/Shadows/SampleFramework11/Profiler.cpp @@ -0,0 +1,205 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" +#include "Profiler.h" +#include "Utility.h" +#include "SpriteRenderer.h" +#include "SpriteFont.h" + +using std::wstring; +using std::map; + + +namespace SampleFramework11 +{ + +// == Profiler ==================================================================================== + +Profiler Profiler::GlobalProfiler; + +void Profiler::Initialize(ID3D11Device* device, ID3D11DeviceContext* immContext) +{ + this->device = device; + this->context = immContext; +} + +void Profiler::StartProfile(const wstring& name) +{ + ProfileData& profileData = profiles[name]; + Assert_(profileData.QueryStarted == false); + Assert_(profileData.QueryFinished == false); + profileData.CPUProfile = false; + profileData.Active = true; + + if(profileData.DisjointQuery[currFrame] == NULL) + { + // Create the queries + D3D11_QUERY_DESC desc; + desc.Query = D3D11_QUERY_TIMESTAMP_DISJOINT; + desc.MiscFlags = 0; + DXCall(device->CreateQuery(&desc, &profileData.DisjointQuery[currFrame])); + + desc.Query = D3D11_QUERY_TIMESTAMP; + DXCall(device->CreateQuery(&desc, &profileData.TimestampStartQuery[currFrame])); + DXCall(device->CreateQuery(&desc, &profileData.TimestampEndQuery[currFrame])); + } + + // Start a disjoint query first + context->Begin(profileData.DisjointQuery[currFrame]); + + // Insert the start timestamp + context->End(profileData.TimestampStartQuery[currFrame]); + + profileData.QueryStarted = true; +} + +void Profiler::EndProfile(const wstring& name) +{ + ProfileData& profileData = profiles[name]; + Assert_(profileData.QueryStarted == true); + Assert_(profileData.QueryFinished == false); + + // Insert the end timestamp + context->End(profileData.TimestampEndQuery[currFrame]); + + // End the disjoint query + context->End(profileData.DisjointQuery[currFrame]); + + profileData.QueryStarted = false; + profileData.QueryFinished = true; +} + +void Profiler::StartCPUProfile(const wstring& name) +{ + ProfileData& profileData = profiles[name]; + Assert_(profileData.QueryStarted == false); + Assert_(profileData.QueryFinished == false); + profileData.CPUProfile = true; + profileData.Active = true; + + timer.Update(); + profileData.StartTime = timer.ElapsedMicroseconds(); + + profileData.QueryStarted = true; +} + +void Profiler::EndCPUProfile(const wstring& name) +{ + ProfileData& profileData = profiles[name]; + Assert_(profileData.QueryStarted == true); + Assert_(profileData.QueryFinished == false); + + timer.Update(); + profileData.EndTime = timer.ElapsedMicroseconds(); + + profileData.QueryStarted = false; + profileData.QueryFinished = true; +} + +void Profiler::EndFrame(SpriteRenderer& spriteRenderer, SpriteFont& spriteFont) +{ + // If any profile was previously active but wasn't used this frame, it could still + // have outstanding queries that we need to keep running + for(auto iter = profiles.begin(); iter != profiles.end(); iter++) + { + const std::wstring& name = (*iter).first; + ProfileData& profile = (*iter).second; + + if(!profile.CPUProfile && !profile.Active && profile.DisjointQuery[0] != nullptr) + { + StartProfile(name); + EndProfile(name); + profile.Active = false; + } + } + + currFrame = (currFrame + 1) % QueryLatency; + + Float4x4 transform; + transform.SetTranslation(Float3(25.0f, 100.0f, 0.0f)); + + // Iterate over all of the profiles + for(auto iter = profiles.begin(); iter != profiles.end(); iter++) + { + ProfileData& profile = (*iter).second; + profile.QueryFinished = false; + + float time = 0.0f; + if(profile.CPUProfile) + { + time = (profile.EndTime - profile.StartTime) / 1000.0f; + } + else + { + if(profile.DisjointQuery[currFrame] == NULL) + continue; + + // Get the query data + uint64 startTime = 0; + while(context->GetData(profile.TimestampStartQuery[currFrame], &startTime, sizeof(startTime), 0) != S_OK); + + uint64 endTime = 0; + while(context->GetData(profile.TimestampEndQuery[currFrame], &endTime, sizeof(endTime), 0) != S_OK); + + D3D11_QUERY_DATA_TIMESTAMP_DISJOINT disjointData; + while(context->GetData(profile.DisjointQuery[currFrame], &disjointData, sizeof(disjointData), 0) != S_OK); + + if(disjointData.Disjoint == false) + { + uint64 delta = endTime - startTime; + float frequency = static_cast(disjointData.Frequency); + time = (delta / frequency) * 1000.0f; + } + } + + profile.TimeSamples[profile.CurrSample] = time; + profile.CurrSample = (profile.CurrSample + 1) % ProfileData::FilterSize; + + float sum = 0.0f; + for(UINT i = 0; i < ProfileData::FilterSize; ++i) + sum += profile.TimeSamples[i]; + time = sum / ProfileData::FilterSize; + + if(profile.Active) + { + wstring output = (*iter).first + L": " + ToString(time) + L"ms"; + spriteRenderer.RenderText(spriteFont, output.c_str(), transform, Float4(1.0f, 1.0f, 0.0f, 1.0f)); + transform._42 += 25.0f; + } + + profile.Active = false; + } +} + +// == ProfileBlock ================================================================================ + +ProfileBlock::ProfileBlock(const std::wstring& name) : name(name) +{ + Profiler::GlobalProfiler.StartProfile(name); +} + +ProfileBlock::~ProfileBlock() +{ + Profiler::GlobalProfiler.EndProfile(name); +} + +// == CPUProfileBlock ============================================================================= + +CPUProfileBlock::CPUProfileBlock(const std::wstring& name) : name(name) +{ + Profiler::GlobalProfiler.StartCPUProfile(name); +} + +CPUProfileBlock::~CPUProfileBlock() +{ + Profiler::GlobalProfiler.EndCPUProfile(name); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Profiler.h b/Shadows/SampleFramework11/Profiler.h new file mode 100644 index 0000000..b16ecca --- /dev/null +++ b/Shadows/SampleFramework11/Profiler.h @@ -0,0 +1,105 @@ +//================================================================================================= +// +// Query Profiling Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "InterfacePointers.h" +#include "Timer.h" + +namespace SampleFramework11 +{ + +class SpriteRenderer; +class SpriteFont; + +class Profiler +{ + +public: + + static Profiler GlobalProfiler; + + void Initialize(ID3D11Device* device, ID3D11DeviceContext* immContext); + + void StartProfile(const std::wstring& name); + void EndProfile(const std::wstring& name); + + void StartCPUProfile(const std::wstring& name); + void EndCPUProfile(const std::wstring& name); + + void EndFrame(SpriteRenderer& spriteRenderer, SpriteFont& spriteFont); + +protected: + + // Constants + static const uint64 QueryLatency = 5; + + struct ProfileData + { + ID3D11QueryPtr DisjointQuery[QueryLatency]; + ID3D11QueryPtr TimestampStartQuery[QueryLatency]; + ID3D11QueryPtr TimestampEndQuery[QueryLatency]; + bool QueryStarted; + bool QueryFinished; + bool Active; + + bool CPUProfile; + int64 StartTime; + int64 EndTime; + + static const uint32 FilterSize = 64; + float TimeSamples[FilterSize]; + uint32 CurrSample; + + ProfileData() : QueryStarted(false), QueryFinished(false), Active(false), + CPUProfile(false), StartTime(0), EndTime(0), CurrSample(0) + { + for(uint32 i = 0; i < FilterSize; ++i) + TimeSamples[i] = 0.0f; + } + }; + + typedef std::map ProfileMap; + + ProfileMap profiles; + uint64 currFrame; + + ID3D11DevicePtr device; + ID3D11DeviceContextPtr context; + + Timer timer; +}; + +class ProfileBlock +{ +public: + + ProfileBlock(const std::wstring& name); + ~ProfileBlock(); + +protected: + + std::wstring name; +}; + +class CPUProfileBlock +{ +public: + + CPUProfileBlock(const std::wstring& name); + ~CPUProfileBlock(); + +protected: + + std::wstring name; +}; + +} diff --git a/Shadows/SampleFramework11/SDKMesh.cpp b/Shadows/SampleFramework11/SDKMesh.cpp new file mode 100644 index 0000000..8d12c86 --- /dev/null +++ b/Shadows/SampleFramework11/SDKMesh.cpp @@ -0,0 +1,380 @@ +//-------------------------------------------------------------------------------------- +// File: SDKMesh.cpp +// +// The SDK Mesh format (.sdkmesh) is not a recommended file format for games. +// It was designed to meet the specific needs of the SDK samples. Any real-world +// applications should avoid this file format in favor of a destination format that +// meets the specific needs of the application. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//-------------------------------------------------------------------------------------- +#include "PCH.h" +#include "SDKMesh.h" +#include "Utility.h" + +#ifndef SAFE_DELETE +#define SAFE_DELETE(p) { if (p) { delete (p); (p)=NULL; } } +#endif +#ifndef SAFE_DELETE_ARRAY +#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p)=NULL; } } +#endif +#ifndef SAFE_RELEASE +#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=NULL; } } +#endif + +namespace SampleFramework11 +{ + + +//-------------------------------------------------------------------------------------- +HRESULT SDKMesh::CreateFromFile( LPCWSTR szFileName, + bool bCreateAdjacencyIndices) +{ + HRESULT hr = S_OK; + + // Open the file + m_hFile = CreateFile( szFileName, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, + NULL ); + if( INVALID_HANDLE_VALUE == m_hFile ) + return E_FAIL; + + // Get the file size + LARGE_INTEGER FileSize; + GetFileSizeEx( m_hFile, &FileSize ); + UINT cBytes = FileSize.LowPart; + + // Allocate memory + m_pStaticMeshData = new BYTE[ cBytes ]; + if( !m_pStaticMeshData ) + { + CloseHandle( m_hFile ); + return E_OUTOFMEMORY; + } + + // Read in the file + DWORD dwBytesRead; + if( !ReadFile( m_hFile, m_pStaticMeshData, cBytes, &dwBytesRead, NULL ) ) + hr = E_FAIL; + + CloseHandle( m_hFile ); + + if( SUCCEEDED( hr ) ) + { + hr = CreateFromMemory( m_pStaticMeshData, + cBytes, + bCreateAdjacencyIndices, + false); + if( FAILED( hr ) ) + delete []m_pStaticMeshData; + } + + return hr; +} + +HRESULT SDKMesh::CreateFromMemory( BYTE* pData, + UINT DataBytes, + bool bCreateAdjacencyIndices, + bool bCopyStatic ) +{ + HRESULT hr = E_FAIL; + Float3 lower; + Float3 upper; + + + // Set outstanding resources to zero + m_NumOutstandingResources = 0; + + if( bCopyStatic ) + { + SDKMESH_HEADER* pHeader = ( SDKMESH_HEADER* )pData; + + SIZE_T StaticSize = ( SIZE_T )( pHeader->HeaderSize + pHeader->NonBufferDataSize ); + m_pHeapData = new BYTE[ StaticSize ]; + if( !m_pHeapData ) + return hr; + + m_pStaticMeshData = m_pHeapData; + + CopyMemory( m_pStaticMeshData, pData, StaticSize ); + } + else + { + m_pHeapData = pData; + m_pStaticMeshData = pData; + } + + // Pointer fixup + m_pMeshHeader = ( SDKMESH_HEADER* )m_pStaticMeshData; + m_pVertexBufferArray = ( SDKMESH_VERTEX_BUFFER_HEADER* )( m_pStaticMeshData + + m_pMeshHeader->VertexStreamHeadersOffset ); + m_pIndexBufferArray = ( SDKMESH_INDEX_BUFFER_HEADER* )( m_pStaticMeshData + + m_pMeshHeader->IndexStreamHeadersOffset ); + m_pMeshArray = ( SDKMESH_MESH* )( m_pStaticMeshData + m_pMeshHeader->MeshDataOffset ); + m_pSubsetArray = ( SDKMESH_SUBSET* )( m_pStaticMeshData + m_pMeshHeader->SubsetDataOffset ); + m_pFrameArray = ( SDKMESH_FRAME* )( m_pStaticMeshData + m_pMeshHeader->FrameDataOffset ); + m_pMaterialArray = ( SDKMESH_MATERIAL* )( m_pStaticMeshData + m_pMeshHeader->MaterialDataOffset ); + + // Setup subsets + for( UINT i = 0; i < m_pMeshHeader->NumMeshes; i++ ) + { + m_pMeshArray[i].pSubsets = ( UINT* )( m_pStaticMeshData + m_pMeshArray[i].SubsetOffset ); + m_pMeshArray[i].pFrameInfluences = ( UINT* )( m_pStaticMeshData + m_pMeshArray[i].FrameInfluenceOffset ); + } + + // error condition + if( m_pMeshHeader->Version != SDKMESH_FILE_VERSION ) + { + hr = E_NOINTERFACE; + goto Error; + } + + // Setup buffer data pointer + BYTE* pBufferData = pData + m_pMeshHeader->HeaderSize + m_pMeshHeader->NonBufferDataSize; + + // Get the start of the buffer data + uint64 BufferDataStart = m_pMeshHeader->HeaderSize + m_pMeshHeader->NonBufferDataSize; + + // Create VBs + m_ppVertices = new BYTE*[m_pMeshHeader->NumVertexBuffers]; + for( UINT i = 0; i < m_pMeshHeader->NumVertexBuffers; i++ ) + { + BYTE* pVertices = NULL; + pVertices = ( BYTE* )( pBufferData + ( m_pVertexBufferArray[i].DataOffset - BufferDataStart ) ); + + m_ppVertices[i] = pVertices; + } + + // Create IBs + m_ppIndices = new BYTE*[m_pMeshHeader->NumIndexBuffers]; + for( UINT i = 0; i < m_pMeshHeader->NumIndexBuffers; i++ ) + { + BYTE* pIndices = NULL; + pIndices = ( BYTE* )( pBufferData + ( m_pIndexBufferArray[i].DataOffset - BufferDataStart ) ); + + m_ppIndices[i] = pIndices; + } + + hr = S_OK; +Error: + + return hr; +} + +#define MAX_D3D11_VERTEX_STREAMS D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT + + +//-------------------------------------------------------------------------------------- + +//-------------------------------------------------------------------------------------- +SDKMesh::SDKMesh() : m_NumOutstandingResources( 0 ), + m_bLoading( false ), + m_hFile( 0 ), + m_hFileMappingObject( 0 ), + m_pMeshHeader( NULL ), + m_pStaticMeshData( NULL ), + m_pHeapData( NULL ), + m_pAdjacencyIndexBufferArray( NULL ), + m_pAnimationData( NULL ), + m_pAnimationHeader( NULL ), + m_ppVertices( NULL ), + m_ppIndices( NULL ), + m_pBindPoseFrameMatrices( NULL ), + m_pTransformedFrameMatrices( NULL ), + m_pWorldPoseFrameMatrices( NULL ) +{ +} + + +//-------------------------------------------------------------------------------------- +SDKMesh::~SDKMesh() +{ + Destroy(); +} + +//-------------------------------------------------------------------------------------- +HRESULT SDKMesh::Create( LPCTSTR szFileName, bool bCreateAdjacencyIndices ) +{ + return CreateFromFile( szFileName, bCreateAdjacencyIndices ); +} + +//-------------------------------------------------------------------------------------- +HRESULT SDKMesh::Create( BYTE* pData, UINT DataBytes, bool bCreateAdjacencyIndices, + bool bCopyStatic) +{ + return CreateFromMemory( pData, DataBytes, bCreateAdjacencyIndices, bCopyStatic ); +} + +//-------------------------------------------------------------------------------------- +void SDKMesh::Destroy() +{ + + SAFE_DELETE_ARRAY( m_pAdjacencyIndexBufferArray ); + + SAFE_DELETE_ARRAY( m_pHeapData ); + m_pStaticMeshData = NULL; + SAFE_DELETE_ARRAY( m_pAnimationData ); + SAFE_DELETE_ARRAY( m_pBindPoseFrameMatrices ); + SAFE_DELETE_ARRAY( m_pTransformedFrameMatrices ); + SAFE_DELETE_ARRAY( m_pWorldPoseFrameMatrices ); + + SAFE_DELETE_ARRAY( m_ppVertices ); + SAFE_DELETE_ARRAY( m_ppIndices ); + + m_pMeshHeader = NULL; + m_pVertexBufferArray = NULL; + m_pIndexBufferArray = NULL; + m_pMeshArray = NULL; + m_pSubsetArray = NULL; + m_pFrameArray = NULL; + m_pMaterialArray = NULL; + + m_pAnimationHeader = NULL; + m_pAnimationFrameData = NULL; + +} + + +//-------------------------------------------------------------------------------------- +D3D11_PRIMITIVE_TOPOLOGY SDKMesh::GetPrimitiveType11( SDKMESH_PRIMITIVE_TYPE PrimType ) +{ + D3D11_PRIMITIVE_TOPOLOGY retType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST; + + switch( PrimType ) + { + case PT_TRIANGLE_LIST: + retType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST; + break; + case PT_TRIANGLE_STRIP: + retType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; + break; + case PT_LINE_LIST: + retType = D3D11_PRIMITIVE_TOPOLOGY_LINELIST; + break; + case PT_LINE_STRIP: + retType = D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP; + break; + case PT_POINT_LIST: + retType = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; + break; + case PT_TRIANGLE_LIST_ADJ: + retType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ; + break; + case PT_TRIANGLE_STRIP_ADJ: + retType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ; + break; + case PT_LINE_LIST_ADJ: + retType = D3D11_PRIMITIVE_TOPOLOGY_LINELIST_ADJ; + break; + case PT_LINE_STRIP_ADJ: + retType = D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ; + break; + }; + + return retType; +} + +//-------------------------------------------------------------------------------------- +DXGI_FORMAT SDKMesh::GetIBFormat11( UINT iMesh ) +{ + switch( m_pIndexBufferArray[ m_pMeshArray[ iMesh ].IndexBuffer ].IndexType ) + { + case IT_16BIT: + return DXGI_FORMAT_R16_UINT; + case IT_32BIT: + return DXGI_FORMAT_R32_UINT; + }; + return DXGI_FORMAT_R16_UINT; +} + +//-------------------------------------------------------------------------------------- +UINT SDKMesh::GetNumMeshes() +{ + if( !m_pMeshHeader ) + return 0; + return m_pMeshHeader->NumMeshes; +} + +//-------------------------------------------------------------------------------------- +UINT SDKMesh::GetNumMaterials() +{ + if( !m_pMeshHeader ) + return 0; + return m_pMeshHeader->NumMaterials; +} + +//-------------------------------------------------------------------------------------- +UINT SDKMesh::GetNumVBs() +{ + if( !m_pMeshHeader ) + return 0; + return m_pMeshHeader->NumVertexBuffers; +} + +//-------------------------------------------------------------------------------------- +UINT SDKMesh::GetNumIBs() +{ + if( !m_pMeshHeader ) + return 0; + return m_pMeshHeader->NumIndexBuffers; +} + +//-------------------------------------------------------------------------------------- +BYTE* SDKMesh::GetRawVerticesAt( UINT iVB ) +{ + return m_ppVertices[iVB]; +} + +//-------------------------------------------------------------------------------------- +BYTE* SDKMesh::GetRawIndicesAt( UINT iIB ) +{ + return m_ppIndices[iIB]; +} + +//-------------------------------------------------------------------------------------- +SDKMESH_MATERIAL* SDKMesh::GetMaterial( UINT iMaterial ) +{ + return &m_pMaterialArray[ iMaterial ]; +} + +//-------------------------------------------------------------------------------------- +SDKMESH_MESH* SDKMesh::GetMesh( UINT iMesh ) +{ + return &m_pMeshArray[ iMesh ]; +} + +//-------------------------------------------------------------------------------------- +UINT SDKMesh::GetNumSubsets( UINT iMesh ) +{ + return m_pMeshArray[ iMesh ].NumSubsets; +} + +//-------------------------------------------------------------------------------------- +SDKMESH_SUBSET* SDKMesh::GetSubset( UINT iMesh, UINT iSubset ) +{ + return &m_pSubsetArray[ m_pMeshArray[ iMesh ].pSubsets[iSubset] ]; +} + +//-------------------------------------------------------------------------------------- +UINT SDKMesh::GetVertexStride( UINT iMesh, UINT iVB ) +{ + return ( UINT )m_pVertexBufferArray[ m_pMeshArray[ iMesh ].VertexBuffers[iVB] ].StrideBytes; +} + +//-------------------------------------------------------------------------------------- +uint64 SDKMesh::GetNumVertices( UINT iMesh, UINT iVB ) +{ + return m_pVertexBufferArray[ m_pMeshArray[ iMesh ].VertexBuffers[iVB] ].NumVertices; +} + +//-------------------------------------------------------------------------------------- +uint64 SDKMesh::GetNumIndices( UINT iMesh ) +{ + return m_pIndexBufferArray[ m_pMeshArray[ iMesh ].IndexBuffer ].NumIndices; +} + +SDKMESH_INDEX_TYPE SDKMesh::GetIndexType( UINT iMesh ) +{ + return ( SDKMESH_INDEX_TYPE ) m_pIndexBufferArray[m_pMeshArray[ iMesh ].IndexBuffer].IndexType; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/SDKMesh.h b/Shadows/SampleFramework11/SDKMesh.h new file mode 100644 index 0000000..99e747d --- /dev/null +++ b/Shadows/SampleFramework11/SDKMesh.h @@ -0,0 +1,354 @@ +//-------------------------------------------------------------------------------------- +// File: SDKMesh.h +// +// Disclaimer: +// The SDK Mesh format (.sdkmesh) is not a recommended file format for shipping titles. +// It was designed to meet the specific needs of the SDK samples. Any real-world +// applications should avoid this file format in favor of a destination format that +// meets the specific needs of the application. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +//-------------------------------------------------------------------------------------- +#pragma once +#ifndef _SDKMESH_ +#define _SDKMESH_ + +#include "Math.h" + +namespace SampleFramework11 +{ + +//-------------------------------------------------------------------------------------- +// Hard Defines for the various structures +//-------------------------------------------------------------------------------------- +#define SDKMESH_FILE_VERSION 101 +#define MAX_VERTEX_ELEMENTS 32 +#define MAX_VERTEX_STREAMS 16 +#define MAX_FRAME_NAME 100 +#define MAX_MESH_NAME 100 +#define MAX_SUBSET_NAME 100 +#define MAX_MATERIAL_NAME 100 +#define MAX_TEXTURE_NAME MAX_PATH +#define MAX_MATERIAL_PATH MAX_PATH +#define INVALID_FRAME ((UINT)-1) +#define INVALID_MESH ((UINT)-1) +#define INVALID_MATERIAL ((UINT)-1) +#define INVALID_SUBSET ((UINT)-1) +#define INVALID_ANIMATION_DATA ((UINT)-1) +#define INVALID_SAMPLER_SLOT ((UINT)-1) +#define ERROR_RESOURCE_VALUE 1 + +template BOOL IsErrorResource( TYPE data ) +{ + if( ( TYPE )ERROR_RESOURCE_VALUE == data ) + return true; + return false; +} +//-------------------------------------------------------------------------------------- +// Enumerated Types. These will have mirrors in both D3D9 and D3D11 +//-------------------------------------------------------------------------------------- +enum SDKMESH_PRIMITIVE_TYPE +{ + PT_TRIANGLE_LIST = 0, + PT_TRIANGLE_STRIP, + PT_LINE_LIST, + PT_LINE_STRIP, + PT_POINT_LIST, + PT_TRIANGLE_LIST_ADJ, + PT_TRIANGLE_STRIP_ADJ, + PT_LINE_LIST_ADJ, + PT_LINE_STRIP_ADJ, + PT_QUAD_PATCH_LIST, + PT_TRIANGLE_PATCH_LIST, +}; + +enum SDKMESH_INDEX_TYPE +{ + IT_16BIT = 0, + IT_32BIT, +}; + +enum FRAME_TRANSFORM_TYPE +{ + FTT_RELATIVE = 0, + FTT_ABSOLUTE, //This is not currently used but is here to support absolute transformations in the future +}; + +//-------------------------------------------------------------------------------------- +// Structures. Unions with pointers are forced to 64bit. +//-------------------------------------------------------------------------------------- +struct SDKMESH_HEADER +{ + //Basic Info and sizes + UINT Version; + BYTE IsBigEndian; + uint64 HeaderSize; + uint64 NonBufferDataSize; + uint64 BufferDataSize; + + //Stats + UINT NumVertexBuffers; + UINT NumIndexBuffers; + UINT NumMeshes; + UINT NumTotalSubsets; + UINT NumFrames; + UINT NumMaterials; + + //Offsets to Data + uint64 VertexStreamHeadersOffset; + uint64 IndexStreamHeadersOffset; + uint64 MeshDataOffset; + uint64 SubsetDataOffset; + uint64 FrameDataOffset; + uint64 MaterialDataOffset; +}; + +struct SDKMESH_VERTEX_BUFFER_HEADER +{ + uint64 NumVertices; + uint64 SizeBytes; + uint64 StrideBytes; + D3DVERTEXELEMENT9 Decl[MAX_VERTEX_ELEMENTS]; + union + { + uint64 DataOffset; //(This also forces the union to 64bits) + ID3D11Buffer* pVB11; + }; +}; + +struct SDKMESH_INDEX_BUFFER_HEADER +{ + uint64 NumIndices; + uint64 SizeBytes; + UINT IndexType; + union + { + uint64 DataOffset; //(This also forces the union to 64bits) + ID3D11Buffer* pIB11; + }; +}; + +struct SDKMESH_MESH +{ + char Name[MAX_MESH_NAME]; + BYTE NumVertexBuffers; + UINT VertexBuffers[MAX_VERTEX_STREAMS]; + UINT IndexBuffer; + UINT NumSubsets; + UINT NumFrameInfluences; //aka bones + + Float3 BoundingBoxCenter; + Float3 BoundingBoxExtents; + + union + { + uint64 SubsetOffset; //Offset to list of subsets (This also forces the union to 64bits) + UINT* pSubsets; //Pointer to list of subsets + }; + union + { + uint64 FrameInfluenceOffset; //Offset to list of frame influences (This also forces the union to 64bits) + UINT* pFrameInfluences; //Pointer to list of frame influences + }; +}; + +struct SDKMESH_SUBSET +{ + char Name[MAX_SUBSET_NAME]; + UINT MaterialID; + UINT PrimitiveType; + uint64 IndexStart; + uint64 IndexCount; + uint64 VertexStart; + uint64 VertexCount; +}; + +struct SDKMESH_FRAME +{ + char Name[MAX_FRAME_NAME]; + UINT Mesh; + UINT ParentFrame; + UINT ChildFrame; + UINT SiblingFrame; + Float4x4 Matrix; + UINT AnimationDataIndex; //Used to index which set of keyframes transforms this frame +}; + +struct SDKMESH_MATERIAL +{ + char Name[MAX_MATERIAL_NAME]; + + // Use MaterialInstancePath + char MaterialInstancePath[MAX_MATERIAL_PATH]; + + // Or fall back to d3d8-type materials + char DiffuseTexture[MAX_TEXTURE_NAME]; + char NormalTexture[MAX_TEXTURE_NAME]; + char SpecularTexture[MAX_TEXTURE_NAME]; + + Float4 Diffuse; + Float4 Ambient; + Float4 Specular; + Float4 Emissive; + FLOAT Power; + + union + { + uint64 Force64_1; //Force the union to 64bits + IDirect3DTexture9* pDiffuseTexture9; + ID3D11Texture2D* pDiffuseTexture11; + }; + union + { + uint64 Force64_2; //Force the union to 64bits + IDirect3DTexture9* pNormalTexture9; + ID3D11Texture2D* pNormalTexture11; + }; + union + { + uint64 Force64_3; //Force the union to 64bits + IDirect3DTexture9* pSpecularTexture9; + ID3D11Texture2D* pSpecularTexture11; + }; + + union + { + uint64 Force64_4; //Force the union to 64bits + ID3D11ShaderResourceView* pDiffuseRV11; + }; + union + { + uint64 Force64_5; //Force the union to 64bits + ID3D11ShaderResourceView* pNormalRV11; + }; + union + { + uint64 Force64_6; //Force the union to 64bits + ID3D11ShaderResourceView* pSpecularRV11; + }; + +}; + +struct SDKANIMATION_FILE_HEADER +{ + UINT Version; + BYTE IsBigEndian; + UINT FrameTransformType; + UINT NumFrames; + UINT NumAnimationKeys; + UINT AnimationFPS; + uint64 AnimationDataSize; + uint64 AnimationDataOffset; +}; + +struct SDKANIMATION_DATA +{ + Float3 Translation; + Float4 Orientation; + Float3 Scaling; +}; + +struct SDKANIMATION_FRAME_DATA +{ + char FrameName[MAX_FRAME_NAME]; + union + { + uint64 DataOffset; + SDKANIMATION_DATA* pAnimationData; + }; +}; + +#ifndef _CONVERTER_APP_ + +//-------------------------------------------------------------------------------------- +// CDXUTSDKMesh class. This class reads the sdkmesh file format for use by the samples +//-------------------------------------------------------------------------------------- +class SDKMesh +{ +private: + UINT m_NumOutstandingResources; + bool m_bLoading; + //BYTE* m_pBufferData; + HANDLE m_hFile; + HANDLE m_hFileMappingObject; + std::vector m_MappedPointers; + +protected: + //These are the pointers to the two chunks of data loaded in from the mesh file + BYTE* m_pStaticMeshData; + BYTE* m_pHeapData; + BYTE* m_pAnimationData; + BYTE** m_ppVertices; + BYTE** m_ppIndices; + + //General mesh info + SDKMESH_HEADER* m_pMeshHeader; + SDKMESH_VERTEX_BUFFER_HEADER* m_pVertexBufferArray; + SDKMESH_INDEX_BUFFER_HEADER* m_pIndexBufferArray; + SDKMESH_MESH* m_pMeshArray; + SDKMESH_SUBSET* m_pSubsetArray; + SDKMESH_FRAME* m_pFrameArray; + SDKMESH_MATERIAL* m_pMaterialArray; + + // Adjacency information (not part of the m_pStaticMeshData, so it must be created and destroyed separately ) + SDKMESH_INDEX_BUFFER_HEADER* m_pAdjacencyIndexBufferArray; + + //Animation (TODO: Add ability to load/track multiple animation sets) + SDKANIMATION_FILE_HEADER* m_pAnimationHeader; + SDKANIMATION_FRAME_DATA* m_pAnimationFrameData; + Float4x4* m_pBindPoseFrameMatrices; + Float4x4* m_pTransformedFrameMatrices; + Float4x4* m_pWorldPoseFrameMatrices; + +protected: + virtual HRESULT CreateFromFile( LPCWSTR szFileName, + bool bCreateAdjacencyIndices); + + virtual HRESULT CreateFromMemory( BYTE* pData, + UINT DataBytes, + bool bCreateAdjacencyIndices, + bool bCopyStatic ); +public: + SDKMesh(); + virtual ~SDKMesh(); + + virtual HRESULT Create( LPCWSTR szFileName, bool bCreateAdjacencyIndices = false ); + virtual HRESULT Create( BYTE* pData, UINT DataBytes, + bool bCreateAdjacencyIndices = false, bool bCopyStatic = false ); + virtual void Destroy(); + + + //Helpers (D3D11 specific) + static D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveType11( SDKMESH_PRIMITIVE_TYPE PrimType ); + DXGI_FORMAT GetIBFormat11( UINT iMesh ); + SDKMESH_INDEX_TYPE GetIndexType( UINT iMesh ); + + //Helpers (general) + UINT GetNumMeshes(); + UINT GetNumMaterials(); + UINT GetNumVBs(); + UINT GetNumIBs(); + + BYTE* GetRawVerticesAt( UINT iVB ); + BYTE* GetRawIndicesAt( UINT iIB ); + SDKMESH_MATERIAL* GetMaterial( UINT iMaterial ); + SDKMESH_MESH* GetMesh( UINT iMesh ); + UINT GetNumSubsets( UINT iMesh ); + SDKMESH_SUBSET* GetSubset( UINT iMesh, UINT iSubset ); + UINT GetVertexStride( UINT iMesh, UINT iVB ); + UINT GetNumFrames(); + SDKMESH_FRAME* GetFrame( UINT iFrame ); + SDKMESH_FRAME* FindFrame( char* pszName ); + uint64 GetNumVertices( UINT iMesh, UINT iVB ); + uint64 GetNumIndices( UINT iMesh ); + + const D3DVERTEXELEMENT9* VBElements( UINT iVB ) { return m_pVertexBufferArray[0].Decl; } +}; + + +#endif + +} + +#endif + diff --git a/Shadows/SampleFramework11/SH.cpp b/Shadows/SampleFramework11/SH.cpp new file mode 100644 index 0000000..ffa3b77 --- /dev/null +++ b/Shadows/SampleFramework11/SH.cpp @@ -0,0 +1,162 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" +#include "SH.h" +#include "Utility.h" +#include "ShaderCompilation.h" + +namespace SampleFramework11 +{ + +SH9 ProjectOntoSH9(const Float3& dir) +{ + SH9 sh; + + // Band 0 + sh.Coefficients[0] = 0.282095f; + + // Band 1 + sh.Coefficients[1] = 0.488603f * dir.y; + sh.Coefficients[2] = 0.488603f * dir.z; + sh.Coefficients[3] = 0.488603f * dir.x; + + // Band 2 + sh.Coefficients[4] = 1.092548f * dir.x * dir.y; + sh.Coefficients[5] = 1.092548f * dir.y * dir.z; + sh.Coefficients[6] = 0.315392f * (3.0f * dir.z * dir.z - 1.0f); + sh.Coefficients[7] = 1.092548f * dir.x * dir.z; + sh.Coefficients[8] = 0.546274f * (dir.x * dir.x - dir.y * dir.y); + + return sh; +} + +SH9Color ProjectOntoSH9Color(const Float3& dir, const Float3& color) +{ + SH9 sh = ProjectOntoSH9(dir); + SH9Color shColor; + for(uint64 i = 0; i < 9; ++i) + shColor.Coefficients[i] = color * sh.Coefficients[i]; + return shColor; +} + +H4 ProjectOntoH4(const Float3& dir) +{ + H4 result; + + result[0] = (1.0f / std::sqrt(2.0f * 3.14159f)); + + // Band 1 + result[1] = std::sqrt(1.5f / 3.14159f) * dir.y; + result[2] = std::sqrt(1.5f / 3.14159f) * (2 * dir.z - 1.0f); + result[3] = std::sqrt(1.5f / 3.14159f) * dir.x; + + return result; +} + +float EvalH4(const H4& h, const Float3& dir) +{ + H4 b = ProjectOntoH4(dir); + return H4::Dot(h, b); +} + +H4 ConvertToH4(const SH9& sh) +{ + const float rt2 = sqrt(2.0f); + const float rt32 = sqrt(3.0f / 2.0f); + const float rt52 = sqrt(5.0f / 2.0f); + const float rt152 = sqrt(15.0f / 2.0f); + const float convMatrix[4][9] = + { + { 1.0f / rt2, 0, 0.5f * rt32, 0, 0, 0, 0, 0, 0 }, + { 0, 1.0f / rt2, 0, 0, 0, (3.0f / 8.0f) * rt52, 0, 0, 0 }, + { 0, 0, 1.0f / (2.0f * rt2), 0, 0, 0, 0.25f * rt152, 0, 0 }, + { 0, 0, 0, 1.0f / rt2, 0, 0, 0, (3.0f / 8.0f) * rt52, 0 } + }; + + H4 hBasis; + + for(uint64 row = 0; row < 4; ++row) + { + hBasis.Coefficients[row] = 0.0f; + + for(uint64 col = 0; col < 9; ++col) + hBasis.Coefficients[row] += convMatrix[row][col] * sh.Coefficients[col]; + } + + return hBasis; +} + +SH9Color ProjectCubemapToSH(ID3D11DeviceContext* context, ID3D11ShaderResourceView* cubeMap) +{ + ID3D11Texture2DPtr srcTexture; + cubeMap->GetResource(reinterpret_cast(&srcTexture)); + + D3D11_TEXTURE2D_DESC srcDesc; + srcTexture->GetDesc(&srcDesc); + + ID3D11DevicePtr device; + context->GetDevice(&device); + + ID3D11Texture2DPtr tempTexture; + D3D11_TEXTURE2D_DESC tempDesc = srcDesc; + tempDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; + tempDesc.MipLevels = 1; + tempDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; + tempDesc.Usage = D3D11_USAGE_DEFAULT; + DXCall(device->CreateTexture2D(&tempDesc, nullptr, &tempTexture)); + + ID3D11UnorderedAccessViewPtr tempUAV; + DXCall(device->CreateUnorderedAccessView(tempTexture, nullptr, &tempUAV)); + + ID3D11ShaderResourceViewPtr tempSRV; + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + srvDesc.Format = srcDesc.Format; + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + srvDesc.Texture2DArray.MostDetailedMip = 0; + srvDesc.Texture2DArray.MipLevels = srcDesc.MipLevels; + srvDesc.Texture2DArray.FirstArraySlice = 0; + srvDesc.Texture2DArray.ArraySize = 6; + DXCall(device->CreateShaderResourceView(srcTexture, &srvDesc, &tempSRV)); + + static const uint32 TGSize = 1024; + static ComputeShaderPtr decodeShader; + if(decodeShader.Valid() == false) + { + CompileOptions opts; + opts.Add("TGSize_", TGSize); + decodeShader = CompileCSFromFile(device, L"SampleFramework11\\Shaders\\DecodeTextureCS.hlsl", + "DecodeTextureCS", "cs_5_0", opts); + } + + ID3D11ShaderResourceView* srvs[1] = { tempSRV }; + context->CSSetShaderResources(0, 1, srvs); + + ID3D11UnorderedAccessView* uavs[1] = { tempUAV }; + context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); + + context->CSSetShader(decodeShader, nullptr, 0); + + context->Dispatch(DispatchSize(TGSize, srcDesc.Width), srcDesc.Height, 6); + + float red[9] = { 0.0f }; + float green[9] = { 0.0f }; + float blue[9] = { 0.0f }; + + //$$$TODO + // DXCall(D3DX11SHProjectCubeMap(context, 3, tempTexture, red, green, blue)); + + SH9Color sh; + for(uint64 i = 0; i < 9; ++i) + sh.Coefficients[i] = Float3(red[i], green[i], blue[i]); + + return sh; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/SH.h b/Shadows/SampleFramework11/SH.h new file mode 100644 index 0000000..10c808e --- /dev/null +++ b/Shadows/SampleFramework11/SH.h @@ -0,0 +1,226 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "GraphicsTypes.h" +#include "Math.h" + +namespace SampleFramework11 +{ + +// Constants +static const float CosineA0 = 1.0f; +static const float CosineA1 = 2.0f / 3.0f; +static const float CosineA2 = 0.25f; + +template class SH +{ + +protected: + + void Assign(const SH& other) + { + for(uint64 i = 0; i < N; ++i) + Coefficients[i] = other.Coefficients[i]; + } + +public: + + T Coefficients[N]; + + SH() + { + for(uint64 i = 0; i < N; ++i) + Coefficients[i] = 0.0f; + } + + // Operator overloads + T& operator[](uint64 idx) + { + return Coefficients[idx]; + } + + T operator[](uint64 idx) const + { + return Coefficients[idx]; + } + + SH& operator+=(const SH& other) + { + for(uint64 i = 0; i < N; ++i) + Coefficients[i] += other.Coefficients[i]; + return *this; + } + + SH operator+(const SH& other) const + { + SH result; + for(uint64 i = 0; i < N; ++i) + result.Coefficients[i] = Coefficients[i] + other.Coefficients[i]; + return result; + } + + SH& operator-=(const SH& other) + { + for(uint64 i = 0; i < N; ++i) + Coefficients[i] -= other.Coefficients[i]; + return *this; + } + + SH operator-(const SH& other) const + { + SH result; + for(uint64 i = 0; i < N; ++i) + result.Coefficients[i] = Coefficients[i] - other.Coefficients[i]; + return result; + } + + + SH& operator*=(const T& scale) + { + for(uint64 i = 0; i < N; ++i) + Coefficients[i] *= scale; + return *this; + } + + + SH operator*(const SH& other) const + { + SH result; + for(uint64 i = 0; i < N; ++i) + result.Coefficients[i] = Coefficients[i] * other.Coefficients[i]; + return result; + } + + SH& operator*=(const SH& other) + { + for(uint64 i = 0; i < N; ++i) + Coefficients[i] *= other.Coefficients[i]; + return *this; + } + + + SH operator*(const T& scale) const + { + SH result; + for(uint64 i = 0; i < N; ++i) + result.Coefficients[i] = Coefficients[i] * scale; + return result; + } + + SH& operator/=(const T& scale) + { + for(uint32 i = 0; i < N; ++i) + Coefficients[i] /= scale; + return *this; + } + + SH operator/(const T& scale) const + { + SH result; + for(uint64 i = 0; i < N; ++i) + result.Coefficients[i] = Coefficients[i] / scale; + return result; + } + + // Dot products + T Dot(const SH& other) const + { + T result = 0.0f; + for(uint64 i = 0; i < N; ++i) + result += Coefficients[i] * other.Coefficients[i]; + return result; + } + + static T Dot(const SH& a, const SH& b) + { + T result = 0.0f; + for(uint64 i = 0; i < N; ++i) + result += a.Coefficients[i] * b.Coefficients[i]; + return result; + } + + // Convolution with cosine kernel + void ConvolveWithCosineKernel() + { + Coefficients[0] *= CosineA0; + + for(uint64 i = 1; i < N; ++i) + if(i < 4) + Coefficients[i] *= CosineA1; + else if(i < 9) + Coefficients[i] *= CosineA2; + } +}; + +// Spherical Harmonics +typedef SH SH4; +typedef SH SH4Color; +typedef SH SH9; +typedef SH SH9Color; + +// H-basis +class H4 : public SH +{ + +public: + + H4() : SH() + { + } + + H4(float h0, float h1, float h2, float h3) : SH() + { + Coefficients[0] = h0; + Coefficients[1] = h1; + Coefficients[2] = h2; + Coefficients[3] = h3; + } + + H4(const SH& sh) + { + Assign(sh); + } +}; + +typedef SH H4Color; + +// For proper alignment with shader constant buffers +struct ShaderSH9Color +{ + Float4 Coefficients[9]; + + ShaderSH9Color() + { + } + + ShaderSH9Color(const SH9Color& sh9Clr) + { + for(uint32 i = 0; i < 9; ++i) + Coefficients[i] = Float4(sh9Clr.Coefficients[i], 0.0f); + } +}; + +SH9 ProjectOntoSH9(const Float3& dir); +SH9Color ProjectOntoSH9Color(const Float3& dir, const Float3& color); + +// H-basis functions +H4 ProjectOntoH4(const Float3& dir); +float EvalH4(const H4& h, const Float3& dir); +H4 ConvertToH4(const SH9& sh); + +// Lighting environment generation functions +SH9Color ProjectCubemapToSH(ID3D11DeviceContext* context, ID3D11ShaderResourceView* cubeMap); + +// Constants +static const H4 H4Identity = H4(std::sqrt(2.0f * 3.14159f), 0.0f, 0.0f, 0.0f); + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Serialization.h b/Shadows/SampleFramework11/Serialization.h new file mode 100644 index 0000000..b573983 --- /dev/null +++ b/Shadows/SampleFramework11/Serialization.h @@ -0,0 +1,81 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Exceptions.h" + +namespace SampleFramework11 +{ + +// Serialization helpers +template void SerializeWrite(HANDLE fileHandle, const T& val) +{ + DWORD bytesWritten = 0; + Win32Call(WriteFile(fileHandle, &val, sizeof(T), &bytesWritten, NULL)); +} + +template void SerializeWriteVector(HANDLE fileHandle, const std::vector& vec) +{ + DWORD bytesWritten = 0; + uint32 numElements = static_cast(vec.size()); + uint32 vectorSize = numElements * sizeof(T); + Win32Call(WriteFile(fileHandle, &numElements, sizeof(uint32), &bytesWritten, NULL)); + if(numElements > 0) + Win32Call(WriteFile(fileHandle, &vec[0], vectorSize, &bytesWritten, NULL)); +} + +template void SerializeWriteString(HANDLE fileHandle, const std::basic_string& str) +{ + DWORD bytesWritten = 0; + uint32 numChars = static_cast(str.length()); + uint32 strSize = numChars * sizeof(T); + Win32Call(WriteFile(fileHandle, &numChars, sizeof(uint32), &bytesWritten, NULL)); + if(numChars > 0) + Win32Call(WriteFile(fileHandle, str.c_str(), strSize, &bytesWritten, NULL)); + +} + +template void SerializeRead(HANDLE fileHandle, T& val) +{ + DWORD bytesRead = 0; + Win32Call(ReadFile(fileHandle, &val, sizeof(T), &bytesRead, NULL)); +} + +template void SerializeReadVector(HANDLE fileHandle, std::vector& vec) +{ + DWORD bytesRead = 0; + uint32 numElements = 0; + Win32Call(ReadFile(fileHandle, &numElements, sizeof(uint32), &bytesRead, NULL)); + + vec.clear(); + if(numElements > 0) + { + vec.resize(numElements); + Win32Call(ReadFile(fileHandle, &vec[0], sizeof(T) * numElements, &bytesRead, NULL)); + } +} + +template void SerializeReadString(HANDLE fileHandle, std::basic_string& str) +{ + DWORD bytesRead = 0; + uint32 numChars = 0; + Win32Call(ReadFile(fileHandle, &numChars, sizeof(uint32), &bytesRead, NULL)); + + str.clear(); + if(numChars > 0) + { + str.resize(numChars + 1, 0); + Win32Call(ReadFile(fileHandle, &str[0], sizeof(T) * numChars, &bytesRead, NULL)); + } +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Settings.cpp b/Shadows/SampleFramework11/Settings.cpp new file mode 100644 index 0000000..fcb1518 --- /dev/null +++ b/Shadows/SampleFramework11/Settings.cpp @@ -0,0 +1,567 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" +#include "Settings.h" +#include "Exceptions.h" +#include "Utility.h" +#include "App.h" +#include "TwHelper.h" + +namespace SampleFramework11 +{ + +// == Setting ===================================================================================== + +Setting::Setting() : tweakBar(NULL), type(SettingType::Invalid), data(NULL), changed(false) +{ +} + +void Setting::Initialize(TwBar* tweakBar_, SettingType type_, void* data_, const char* name_, + const char* group_, const char* label_, const char* helpText_, + ETwType twType_) +{ + tweakBar = tweakBar_; + type = type_; + data = data_; + name = name_; + group = group_; + label = label_; + helpText = helpText_; + changed = false; + + static const ETwType twTypes[] = + { + TW_TYPE_FLOAT, + TW_TYPE_INT32, + TW_TYPE_BOOL32, + TW_TYPE_UINT32, // not used + TW_TYPE_DIR3F, + TW_TYPE_QUAT4F, + TW_TYPE_COLOR3F, + }; + + StaticAssert_(_countof(twTypes) == uint64(SettingType::NumTypes)); + + const ETwType twType = twType_ == TW_TYPE_UNDEF ? twTypes[uint64(type)] : twType_; + TwCall(TwAddVarRW(tweakBar, name.c_str(), twType, data, nullptr)); + TwHelper::SetLabel(tweakBar, name.c_str(), label.c_str()); + TwHelper::SetHelpText(tweakBar, name.c_str(), helpText.c_str()); + TwHelper::SetGroup(tweakBar, name.c_str(), group.c_str()); +} + +void Setting::SetReadOnly(bool readOnly) +{ + Assert_(tweakBar != NULL); + TwHelper::SetReadOnly(tweakBar, name.c_str(), readOnly); +} + +void Setting::SetEditable(bool editable) +{ + SetReadOnly(!editable); +} + +FloatSetting& Setting::AsFloat() +{ + Assert_(type == SettingType::Float); + return *(static_cast(this)); +} + +IntSetting& Setting::AsInt() +{ + Assert_(type == SettingType::Int); + return *(static_cast(this)); +} + +BoolSetting& Setting::AsBool() +{ + Assert_(type == SettingType::Bool); + return *(static_cast(this)); +} + +EnumSetting& Setting::AsEnum() +{ + Assert_(type == SettingType::Enum); + return *(static_cast(this)); +} + +DirectionSetting& Setting::AsDirection() +{ + Assert_(type == SettingType::Direction); + return *(static_cast(this)); +} + +OrientationSetting& Setting::AsOrientation() +{ + Assert_(type == SettingType::Orientation); + return *(static_cast(this)); +} + +ColorSetting& Setting::AsColor() +{ + Assert_(type == SettingType::Color); + return *(static_cast(this)); +} + +bool Setting::Changed() const +{ + return changed; +} + +const std::string& Setting::Name() const +{ + return name; +} + +// == FloatSetting ================================================================================ + +FloatSetting::FloatSetting() : val(0.0f), oldVal(0.0f), minVal(0.0f), maxVal(0.0f), step(0.0) +{ +} + +void FloatSetting::Initialize(TwBar* tweakBar_, const char* name_, const char* group_, + const char* label_, const char* helpText_, float initialVal, + float minVal_, float maxVal_, float step_) +{ + val = initialVal; + oldVal = initialVal; + minVal = minVal_; + maxVal = maxVal_; + step = step_; + Assert_(minVal < maxVal); + Assert_(minVal <= val && val <= maxVal); + Setting::Initialize(tweakBar_, SettingType::Float, &val, name_, group_, label_, helpText_); + TwHelper::SetMinMax(tweakBar, name.c_str(), minVal, maxVal); + TwHelper::SetStep(tweakBar, name.c_str(), step); +} + +void FloatSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +float FloatSetting::Value() const +{ + return val; +} + +void FloatSetting::SetValue(float newVal) +{ + val = Clamp(newVal, minVal, maxVal); +} + +FloatSetting::operator float() +{ + return val; +} + +// == IntSetting ================================================================================== + +IntSetting::IntSetting() : val(0), oldVal(0), minVal(0), maxVal(0) +{ +} + +void IntSetting::Initialize(TwBar* tweakBar_, const char* name_, const char* group_, + const char* label_, const char* helpText_, int32 initialVal, + int32 minVal_, int32 maxVal_) +{ + val = initialVal; + oldVal = initialVal; + minVal = minVal_; + maxVal = maxVal_; + Assert_(minVal < maxVal); + Assert_(minVal <= val && val <= maxVal); + Setting::Initialize(tweakBar_, SettingType::Int, &val, name_, group_, label_, helpText_); + TwHelper::SetMinMax(tweakBar, name.c_str(), minVal, maxVal); +} + +void IntSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +int32 IntSetting::Value() const +{ + return val; +} + +void IntSetting::SetValue(int32 newVal) +{ + val = Clamp(newVal, minVal, maxVal); +} + +IntSetting::operator int32() +{ + return val; +} + +// == BoolSetting ================================================================================= + +BoolSetting::BoolSetting() : val(false), oldVal(0) +{ +} + +void BoolSetting::Initialize(TwBar* tweakBar_, const char* name_, + const char* group_, const char* label_, + const char* helpText_, bool32 initialVal) +{ + val = initialVal ? true : false; + oldVal = val; + Setting::Initialize(tweakBar_, SettingType::Bool, &val, name_, group_, label_, helpText_); +} + +void BoolSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +bool32 BoolSetting::Value() const +{ + return val; +} + +void BoolSetting::SetValue(bool32 newVal) +{ + val = newVal ? true : false; +} + +BoolSetting::operator bool32() +{ + return val; +} + +// == EnumSetting ================================================================================= + +EnumSetting::EnumSetting() : val(0), oldVal(0), numValues(0) +{ +} + +void EnumSetting::Initialize(TwBar* tweakBar_, const char* name_, + const char* group_, const char* label_, + const char* helpText_, uint32 initialVal, + uint32 numValues_, const char* const* valueLabels) +{ + val = std::min(initialVal, numValues - 1); + numValues = numValues_; + + // Register an enum type + std::vector enumValues(numValues); + for(uint32 i = 0; i < numValues; ++i) + { + enumValues[i].Value = i; + enumValues[i].Label = valueLabels[i]; + } + TwType twType = TwDefineEnum(name_, enumValues.data(), numValues); + TwCall(twType); + + Setting::Initialize(tweakBar_, SettingType::Enum, &val, name_, group_, label_, helpText_, twType); +} + +void EnumSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +bool32 EnumSetting::Value() const +{ + return val; +} + +void EnumSetting::SetValue(uint32 newVal) +{ + val = std::min(newVal, numValues - 1); +} + +EnumSetting::operator uint32() +{ + return val; +} + +// == DirectionSetting ============================================================================ + +DirectionSetting::DirectionSetting() +{ +} + +void DirectionSetting::Initialize(TwBar* tweakBar_, const char* name_, + const char* group_, const char* label_, + const char* helpText_, Float3 initialVal) +{ + val = Float3::Normalize(initialVal); + oldVal = val; + Setting::Initialize(tweakBar_, SettingType::Direction, &val, name_, group_, label_, helpText_); +} + +void DirectionSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +Float3 DirectionSetting::Value() const +{ + return val; +} + +void DirectionSetting::SetValue(Float3 newVal) +{ + val = Float3::Normalize(newVal); +} + +DirectionSetting::operator Float3() +{ + return val; +} + +// == OrientationSetting ========================================================================== + +OrientationSetting::OrientationSetting() +{ +} + +void OrientationSetting::Initialize(TwBar* tweakBar_, + const char* name_, + const char* group_, + const char* label_, + const char* helpText_, + Quaternion initialVal) +{ + val = Quaternion::Normalize(initialVal); + oldVal = val; + + Setting::Initialize(tweakBar_, SettingType::Orientation, &val, name_, group_, label_, helpText_); + + TwHelper::SetAxisMapping(tweakBar, name_, TwHelper::Axis::PositiveX, TwHelper::Axis::PositiveY, + TwHelper::Axis::NegativeZ); +} + +void OrientationSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +Quaternion OrientationSetting::Value() const +{ + return val; +} + +void OrientationSetting::SetValue(Quaternion newVal) +{ + val = Quaternion::Normalize(newVal); +} + +OrientationSetting::operator Quaternion() +{ + return val; +} + +// == ColorSetting ================================================================================ + +ColorSetting::ColorSetting() : hdr(false) +{ +} + +void ColorSetting::Initialize(TwBar* tweakBar_, const char* name_, + const char* group_, const char* label_, + const char* helpText_, Float3 initialVal, + bool hdr_, float minIntensity_, float maxIntensity_, + float step_) +{ + hdr = hdr_; + float initialIntensity = 1.0f; + if(hdr) + { + val = Float3::Clamp(initialVal, 0.0f, FLT_MAX); + initialIntensity = std::max(std::max(val.x, val.y), val.z); + val /= initialIntensity; + initialIntensity = Clamp(initialIntensity, minIntensity_, maxIntensity_); + } + else + val = Float3::Clamp(initialVal, 0.0f, 1.0f); + + oldVal = val; + Setting::Initialize(tweakBar_, SettingType::Color, &val, name_, group_, label_, helpText_); + + if(hdr) + { + std::string intensityName = name_; + intensityName += "Intensity"; + std::string intensityLabel = label_; + intensityLabel += " Intensity"; + std::string intensityHelp = "The intensity of the "; + intensityHelp += name_; + intensityHelp += " setting"; + intensity.Initialize(tweakBar_, intensityName.c_str(), group_, + intensityLabel.c_str(), intensityHelp.c_str(), + initialIntensity, minIntensity_, maxIntensity_, step_); + } +} + +void ColorSetting::Update() +{ + changed = oldVal != val; + oldVal = val; +} + +Float3 ColorSetting::Value() const +{ + return hdr ? val * intensity.Value() : val; +} + +void ColorSetting::SetValue(Float3 newVal) +{ + if(hdr) + { + newVal = Float3::Clamp(newVal, 0.0f, FLT_MAX); + float newIntensity = std::max(std::max(val.x, val.y), val.z); + newVal /= newIntensity; + intensity.SetValue(newIntensity); + } + + val = Float3::Clamp(newVal, 0.0f, 1.0f); +} + +ColorSetting::operator Float3() +{ + return hdr ? val * intensity.Value() : val; +} + +// == SettingsContainer =========================================================================== + +SettingsContainer::SettingsContainer() : tweakBar(nullptr) +{ +} + +SettingsContainer::~SettingsContainer() +{ + settings.clear(); + for(uint64 i = 0; i < allocatedSettings.size(); ++i) + delete allocatedSettings[i]; +} + +void SettingsContainer::Initialize(TwBar* tweakBar_) +{ + Assert_(tweakBar_ != nullptr); + tweakBar = tweakBar_; +} + +void SettingsContainer::Update() +{ + for(auto iter = settings.begin(); iter != settings.end(); iter++) + iter->second->Update(); +} + +void SettingsContainer::SetGroupOpened(const char* groupName, bool opened) +{ + TwHelper::SetOpened(tweakBar, groupName, opened); +} + +Setting& SettingsContainer::operator[](const std::string& name) +{ + Assert_(settings.find(name) != settings.end()); + return *settings[name]; +} + +void SettingsContainer::AddFloatSetting(const char* name, const char* label, const char* group, + float initialVal, float minVal, float maxVal, float step, + const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + FloatSetting* setting = new FloatSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal, minVal, maxVal, step); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddIntSetting(const char* name, const char* label, const char* group, + int32 initialVal, int32 minVal, int32 maxVal, + const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + IntSetting* setting = new IntSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal, minVal, maxVal); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddBoolSetting(const char* name, const char* label, const char* group, + bool32 initialVal, const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + BoolSetting* setting = new BoolSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddEnumSetting(const char* name, const char* label, const char* group, + uint32 initialVal, uint32 numValues, + const char* const* valueLabels, const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + EnumSetting* setting = new EnumSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal, numValues, valueLabels); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddDirectionSetting(const char* name, const char* label, const char* group, + Float3 initialVal, const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + DirectionSetting* setting = new DirectionSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddOrientationSetting(const char* name, const char* label, const char* group, + Quaternion initialVal, const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + OrientationSetting* setting = new OrientationSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddColorSetting(const char* name, const char* label, const char* group, + Float3 initialVal, bool hdr, float minIntensity, + float maxIntensity, float step, const char* helpText) +{ + Assert_(settings.find(name) == settings.end()); + Assert_(tweakBar != nullptr); + ColorSetting* setting = new ColorSetting(); + setting->Initialize(tweakBar, name, group, label, helpText, initialVal, + hdr, minIntensity, maxIntensity, step); + settings[name] = setting; + allocatedSettings.push_back(setting); +} + +void SettingsContainer::AddSetting(Setting* setting) +{ + Assert_(setting != NULL); + settings[setting->Name()] = setting; +} + +// Global definition +SettingsContainer Settings; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Settings.h b/Shadows/SampleFramework11/Settings.h new file mode 100644 index 0000000..da307c7 --- /dev/null +++ b/Shadows/SampleFramework11/Settings.h @@ -0,0 +1,347 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "Math.h" + +namespace SampleFramework11 +{ + +class FloatSetting; +class IntSetting; +class BoolSetting; +class EnumSetting; +class DirectionSetting; +class OrientationSetting; +class ColorSetting; + +enum class SettingType +{ + Float = 0, + Int = 1, + Bool = 2, + Enum = 3, + Direction = 4, + Orientation = 5, + Color = 6, + + Invalid, + NumTypes = Invalid +}; + +// Base class for all setting types +class Setting +{ + +protected: + + TwBar* tweakBar; + SettingType type; + void* data; + std::string name; + std::string group; + std::string label; + std::string helpText; + bool changed; + + void Initialize(TwBar* tweakBar, SettingType type, void* data, const char* name, + const char* group, const char* label, const char* helpText, + ETwType twType = TW_TYPE_UNDEF); + +public: + + Setting(); + + virtual void Update() = 0; + + void SetReadOnly(bool readOnly); + void SetEditable(bool editable); + + FloatSetting& AsFloat(); + IntSetting& AsInt(); + BoolSetting& AsBool(); + EnumSetting& AsEnum(); + DirectionSetting& AsDirection(); + OrientationSetting& AsOrientation(); + ColorSetting& AsColor(); + + bool Changed() const; + const std::string& Name() const; +}; + +// 32-bit float setting +class FloatSetting : public Setting +{ + +private: + + float val; + float oldVal; + float minVal; + float maxVal; + float step; + +public: + + FloatSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, float initialVal, + float minVal, float maxVal, float step); + + virtual void Update() override; + + float Value() const; + void SetValue(float newVal); + operator float(); + + float MinValue() const { return minVal; }; + float MaxValue() const { return maxVal; }; +}; + +// 32-bit integer setting +class IntSetting : public Setting +{ + +private: + + int32 val; + int32 oldVal; + int32 minVal; + int32 maxVal; + +public: + + IntSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, int32 initialVal, + int32 minVal, int32 maxVal); + + virtual void Update() override; + + int32 Value() const; + void SetValue(int32 newVal); + operator int32(); +}; + +// Boolean setting +class BoolSetting : public Setting +{ + +private: + + bool32 val; + int32 oldVal; + +public: + + BoolSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, bool32 initialVal); + + virtual void Update() override; + + bool32 Value() const; + void SetValue(bool32 newVal); + operator bool32(); +}; + +// Enumeration setting +class EnumSetting : public Setting +{ + +protected: + + uint32 val; + uint32 oldVal; + uint32 numValues; + +public: + + EnumSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, uint32 initialVal, + uint32 numValues, const char* const* valueLabels); + + virtual void Update() override; + + uint32 Value() const; + void SetValue(uint32 newVal); + + operator uint32(); +}; + +// Templated enumeration setting +template class EnumSettingT : public EnumSetting +{ + +public: + + EnumSettingT() + { + } + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, T initialVal, + uint32 numValues, const char* const* valueLabels) + { + EnumSetting::Initialize(tweakBar, name, group, label, helpText, + uint32(initialVal), numValues, valueLabels); + } + + operator T() + { + return T(val); + } + + void SetValue(T newVal) + { + SetValue(uint32(newVal)); + } +}; + +// 3D direction setting +class DirectionSetting : public Setting +{ + +private: + + Float3 val; + Float3 oldVal; + +public: + + DirectionSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, Float3 initialVal); + + virtual void Update() override; + + Float3 Value() const; + void SetValue(Float3 newVal); + operator Float3(); +}; + +// Quaternion orientation setting +class OrientationSetting : public Setting +{ + +private: + + Quaternion val; + Quaternion oldVal; + +public: + + OrientationSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, Quaternion initialVal); + + virtual void Update() override; + + Quaternion Value() const; + void SetValue(Quaternion newVal); + operator Quaternion(); +}; + +// RGB color setting +class ColorSetting : public Setting +{ + +private: + + Float3 val; + Float3 oldVal; + + FloatSetting intensity; + bool hdr; + +public: + + ColorSetting(); + + void Initialize(TwBar* tweakBar, const char* name, const char* group, + const char* label, const char* helpText, Float3 initialVal, + bool hdr, float minIntensity, float maxIntensity, float step); + + virtual void Update() override; + + Float3 Value() const; + void SetValue(Float3 newVal); + operator Float3(); +}; + +// Container for settings (essentially wraps a tweak bar) +class SettingsContainer +{ + +private: + + TwBar* tweakBar; + std::map settings; + std::vector allocatedSettings; + +public: + + SettingsContainer(); + ~SettingsContainer(); + + void Initialize(TwBar* tweakBar); + + void Update(); + + // Access settings by name + Setting& operator[](const std::string& name); + + // Set container properties + void SetGroupOpened(const char* groupName, bool opened); + + // Add new settings + void AddFloatSetting(const char* name, const char* label, const char* group, + float initialVal, float minVal = 0.0f, float maxVal = 1.0f, + float step = 0.01f, const char* helpText = ""); + + void AddIntSetting(const char* name, const char* label, const char* group, + int32 initialVal, int32 minVal = INT_MIN, int32 maxVal = INT_MAX, + const char* helpText = ""); + + void AddBoolSetting(const char* name, const char* label, const char* group, + bool32 initialVal, const char* helpText = ""); + + void AddDirectionSetting(const char* name, const char* label, const char* group, + Float3 initialVal, const char* helpText = ""); + + void AddOrientationSetting(const char* name, const char* label, const char* group, + Quaternion initialVal, const char* helpText = ""); + + void AddEnumSetting(const char* name, const char* label, const char* group, + uint32 initialVal, uint32 numValues, const char* const* valueLabels, + const char* helpText = ""); + + void AddColorSetting(const char* name, const char* label, const char* group, + Float3 initialVal, bool hdr = false, float minIntensity = 0.0f, + float maxIntensity = 1.0f, float step = 0.01f, + const char* helpText = ""); + + // Add existing setting + void AddSetting(Setting* setting); + + TwBar* TweakBar() { return tweakBar; } +}; + +extern SettingsContainer Settings; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/ShaderCompilation.cpp b/Shadows/SampleFramework11/ShaderCompilation.cpp new file mode 100644 index 0000000..e01d79b --- /dev/null +++ b/Shadows/SampleFramework11/ShaderCompilation.cpp @@ -0,0 +1,516 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "ShaderCompilation.h" + +#include "Utility.h" +#include "Exceptions.h" +#include "InterfacePointers.h" +#include "FileIO.h" +#include "MurmurHash.h" + +using std::vector; +using std::wstring; +using std::string; +using std::map; + +namespace SampleFramework11 +{ + +static string GetExpandedShaderCode(const wchar* path, vector& filePaths) +{ + for(uint64 i = 0; i < filePaths.size(); ++i) + if(filePaths[i] == path) + throw Exception(L"File \"" + wstring(path) + L" is recursively included"); + + filePaths.push_back(path); + + string fileContents = ReadFileAsString(path); + wstring fileDirectory = GetDirectoryFromFilePath(path); + + // Look for includes + size_t lineStart = 0; + size_t lineEnd = std::string::npos; + while(true) + { + size_t lineEnd = fileContents.find('\n', lineStart); + size_t lineLength = 0; + if(lineEnd == string::npos) + lineLength = string::npos; + else + lineLength = lineEnd - lineStart; + + string line = fileContents.substr(lineStart, lineLength); + if(line.find("#include") == 0) + { + size_t startQuote = line.find('\"'); + size_t endQuote = line.find('\"', startQuote + 1); + string includePath = line.substr(startQuote + 1, endQuote - startQuote - 1); + wstring fullIncludePath = fileDirectory + AnsiToWString(includePath.c_str()); + if(FileExists(fullIncludePath.c_str()) == false) + throw Exception(L"Couldn't find #included file \"" + fullIncludePath + L"\""); + + string includeCode = GetExpandedShaderCode(fullIncludePath.c_str(), filePaths); + fileContents.insert(lineEnd + 1, includeCode); + lineEnd += includeCode.length(); + } + + if(lineEnd == string::npos) + break; + + lineStart = lineEnd + 1; + } + + return fileContents; +} + +static const wstring baseCacheDir = L"ShaderCache\\"; + +#if _DEBUG + static const wstring cacheSubDir = L"Debug\\"; +#else + static const std::wstring cacheSubDir = L"Release\\"; +#endif + +static const wstring cacheDir = baseCacheDir + cacheSubDir; + +static string MakeDefinesString(const D3D_SHADER_MACRO* defines) +{ + string definesString; + while(defines && defines->Name != nullptr && defines != nullptr) + { + if(definesString.length() > 0) + definesString += "|"; + definesString += defines->Name; + definesString += "="; + definesString += defines->Definition; + ++defines; + } + + return definesString; +} + +static wstring MakeShaderCacheName(const std::string& shaderCode, const char* functionName, + const char* profile, const D3D_SHADER_MACRO* defines) +{ + string hashString = shaderCode; + hashString += "\n"; + hashString += functionName; + hashString += "\n"; + hashString += profile; + hashString += "\n"; + + hashString += MakeDefinesString(defines); + + Hash codeHash = GenerateHash(hashString.data(), int(hashString.length()), 0); + + return cacheDir + codeHash.ToString() + L".cache"; +} + +static ID3DBlob* CompileShader(const wchar* path, const char* functionName, const char* profile, + const D3D_SHADER_MACRO* defines, bool forceOptimization, + vector& filePaths) +{ + // Make a hash off the expanded shader code + string shaderCode = GetExpandedShaderCode(path, filePaths); + wstring cacheName = MakeShaderCacheName(shaderCode, functionName, profile, defines); + + if(FileExists(cacheName.c_str())) + { + File cacheFile(cacheName.c_str(), File::OpenRead); + + const uint64 shaderSize = cacheFile.Size(); + vector compressedShader; + compressedShader.resize(shaderSize); + cacheFile.Read(shaderSize, compressedShader.data()); + + ID3DBlob* decompressedShader[1] = { nullptr }; + uint32 indices[1] = { 0 }; + DXCall(D3DDecompressShaders(compressedShader.data(), shaderSize, 1, 0, + indices, 0, decompressedShader, nullptr)); + + return decompressedShader[0]; + } + + std::printf("Compiling shader %s %s %s\n", WStringToAnsi(GetFileName(path).c_str()).c_str(), + profile, MakeDefinesString(defines).c_str()); + + // Loop until we succeed, or an exception is thrown + while(true) + { + UINT flags = D3DCOMPILE_WARNINGS_ARE_ERRORS; + #ifdef _DEBUG + flags |= D3DCOMPILE_DEBUG; + if(forceOptimization == false) + flags |= D3DCOMPILE_SKIP_OPTIMIZATION; + #endif + + ID3DBlob* compiledShader; + ID3DBlobPtr errorMessages; + HRESULT hr = D3DCompileFromFile(path, defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, functionName, + profile, flags, 0, &compiledShader, &errorMessages); + + if(FAILED(hr)) + { + if(errorMessages) + { + wchar message[1024] = { 0 }; + char* blobdata = reinterpret_cast(errorMessages->GetBufferPointer()); + + MultiByteToWideChar(CP_ACP, 0, blobdata, static_cast(errorMessages->GetBufferSize()), message, 1024); + std::wstring fullMessage = L"Error compiling shader file \""; + fullMessage += path; + fullMessage += L"\" - "; + fullMessage += message; + + // Pop up a message box allowing user to retry compilation + int retVal = MessageBoxW(nullptr, fullMessage.c_str(), L"Shader Compilation Error", MB_RETRYCANCEL); + if(retVal != IDRETRY) + throw DXException(hr, fullMessage.c_str()); + + #if EnableShaderCaching_ + shaderCode = GetExpandedShaderCode(path); + cacheName = MakeShaderCacheName(shaderCode, functionName, profile, defines); + #endif + } + else + { + _ASSERT(false); + throw DXException(hr); + } + } + else + { + // Compress the shader + D3D_SHADER_DATA shaderData; + shaderData.pBytecode = compiledShader->GetBufferPointer(); + shaderData.BytecodeLength = compiledShader->GetBufferSize(); + ID3DBlobPtr compressedShader; + DXCall(D3DCompressShaders(1, &shaderData, D3D_COMPRESS_SHADER_KEEP_ALL_PARTS, &compressedShader)); + + // Create the cache directory if it doesn't exist + if(DirectoryExists(baseCacheDir.c_str()) == false) + Win32Call(CreateDirectory(baseCacheDir.c_str(), nullptr)); + + if(DirectoryExists(cacheDir.c_str()) == false) + Win32Call(CreateDirectory(cacheDir.c_str(), nullptr)); + + File cacheFile(cacheName.c_str(), File::OpenWrite); + + // Write the compiled shader to disk + uint64 shaderSize = compressedShader->GetBufferSize(); + cacheFile.Write(shaderSize, compressedShader->GetBufferPointer()); + + return compiledShader; + } + } +} + +struct ShaderFile +{ + wstring FilePath; + uint64 TimeStamp; + vector Shaders; + + ShaderFile(const wstring& filePath) : TimeStamp(0), FilePath(filePath) + { + } +}; + +vector ShaderFiles; +vector CompiledShaders; + +template ID3D11DeviceChild* CreateShader(ID3D11Device* device, ID3DBlob* byteCode) +{ + if(typeid(T) == typeid(ID3D11VertexShader)) + { + ID3D11VertexShader* shader = nullptr; + DXCall(device->CreateVertexShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), + nullptr, &shader)); + return shader; + } + else if(typeid(T) == typeid(ID3D11HullShader)) + { + ID3D11HullShader* shader = nullptr; + DXCall(device->CreateHullShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), + nullptr, &shader)); + return shader; + } + else if(typeid(T) == typeid(ID3D11DomainShader)) + { + ID3D11DomainShader* shader = nullptr; + DXCall(device->CreateDomainShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), + nullptr, &shader)); + return shader; + } + else if(typeid(T) == typeid(ID3D11GeometryShader)) + { + ID3D11GeometryShader* shader = nullptr; + DXCall(device->CreateGeometryShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), + nullptr, &shader)); + return shader; + } + else if(typeid(T) == typeid(ID3D11PixelShader)) + { + ID3D11PixelShader* shader = nullptr; + DXCall(device->CreatePixelShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), + nullptr, &shader)); + return shader; + } + else if(typeid(T) == typeid(ID3D11ComputeShader)) + { + ID3D11ComputeShader* shader = nullptr; + DXCall(device->CreateComputeShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), + nullptr, &shader)); + return shader; + } + + AssertFail_("Invalid type passed to CreateShader"); + return nullptr; +} + +template void CompileShader(ID3D11Device* device, CompiledShader* shader) +{ + Assert_(shader != nullptr); + Assert_(*shader->Type == typeid(T)); + + vector filePaths; + D3D_SHADER_MACRO defines[CompileOptions::MaxDefines + 1]; + shader->CompileOpts.MakeDefines(defines); + shader->ByteCode = CompileShader(shader->FilePath.c_str(), shader->FunctionName.c_str(), + shader->Profile.c_str(), defines, + shader->ForceOptimization, filePaths); + + shader->ShaderPtr.Attach(CreateShader(device, shader->ByteCode)); + + for(uint64 fileIdx = 0; fileIdx < filePaths.size(); ++ fileIdx) + { + const wstring& filePath = filePaths[fileIdx]; + ShaderFile* shaderFile = nullptr; + for(uint64 shaderFileIdx = 0; shaderFileIdx < ShaderFiles.size(); ++shaderFileIdx) + { + if(ShaderFiles[shaderFileIdx]->FilePath == filePath) + { + shaderFile = ShaderFiles[shaderFileIdx]; + break; + } + } + if(shaderFile == nullptr) + { + shaderFile = new ShaderFile(filePath); + ShaderFiles.push_back(shaderFile); + } + + bool containsShader = false; + for(uint64 shaderIdx = 0; shaderIdx < shaderFile->Shaders.size(); ++shaderIdx) + { + if(shaderFile->Shaders[shaderIdx] == shader) + { + containsShader = true; + break; + } + } + + if(containsShader == false) + shaderFile->Shaders.push_back(shader); + } +} + +VertexShaderPtr CompileVSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName, + const char* profile, + const CompileOptions& compileOptions, + bool forceOptimization) +{ + CompiledVertexShader* compiledShader = new CompiledVertexShader(path, functionName, profile, compileOptions, forceOptimization); + CompileShader(device, compiledShader); + CompiledShaders.push_back(compiledShader); + + return compiledShader; +} + +PixelShaderPtr CompilePSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName, + const char* profile, + const CompileOptions& compileOptions, + bool forceOptimization) +{ + CompiledPixelShader* compiledShader = new CompiledPixelShader(path, functionName, profile, compileOptions, forceOptimization); + CompileShader(device, compiledShader); + CompiledShaders.push_back(compiledShader); + + return compiledShader; +} + +GeometryShaderPtr CompileGSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName, + const char* profile, + const CompileOptions& compileOptions, + bool forceOptimization) +{ + CompiledGeometryShader* compiledShader = new CompiledGeometryShader(path, functionName, profile, compileOptions, forceOptimization); + CompileShader(device, compiledShader); + CompiledShaders.push_back(compiledShader); + + return compiledShader; +} + +HullShaderPtr CompileHSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName, + const char* profile, + const CompileOptions& compileOptions, + bool forceOptimization) +{ + CompiledHullShader* compiledShader = new CompiledHullShader(path, functionName, profile, compileOptions, forceOptimization); + CompileShader(device, compiledShader); + CompiledShaders.push_back(compiledShader); + + return compiledShader; +} + +DomainShaderPtr CompileDSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName, + const char* profile, + const CompileOptions& compileOptions, + bool forceOptimization) +{ + CompiledDomainShader* compiledShader = new CompiledDomainShader(path, functionName, profile, compileOptions, forceOptimization); + CompileShader(device, compiledShader); + CompiledShaders.push_back(compiledShader); + + return compiledShader; +} + +ComputeShaderPtr CompileCSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName, + const char* profile, + const CompileOptions& compileOptions, + bool forceOptimization) +{ + CompiledComputeShader* compiledShader = new CompiledComputeShader(path, functionName, profile, compileOptions, forceOptimization); + CompileShader(device, compiledShader); + CompiledShaders.push_back(compiledShader); + + return compiledShader; +} + +void UpdateShaders(ID3D11Device* device) +{ + if(ShaderFiles.size() == 0) + return; + + static uint64 currFrame = 0; + currFrame = (currFrame + 1) % 10; + if(currFrame != 0) + return; + + static uint64 currFile = 0; + currFile = (currFile + 1) % uint64(ShaderFiles.size()); + + ShaderFile* file = ShaderFiles[currFile]; + const uint64 newTimeStamp = GetFileTimestamp(file->FilePath.c_str()); + if(file->TimeStamp == 0) + { + file->TimeStamp = newTimeStamp; + return; + } + + if(file->TimeStamp < newTimeStamp) + { + file->TimeStamp = newTimeStamp; + for(uint64 i = 0; i < file->Shaders.size(); ++i) + { + CompiledShader* shader = file->Shaders[i]; + if(*shader->Type == typeid(ID3D11VertexShader)) + CompileShader(device, shader); + else if(*shader->Type == typeid(ID3D11HullShader)) + CompileShader(device, shader); + else if(*shader->Type == typeid(ID3D11DomainShader)) + CompileShader(device, shader); + else if(*shader->Type == typeid(ID3D11GeometryShader)) + CompileShader(device, shader); + else if(*shader->Type == typeid(ID3D11PixelShader)) + CompileShader(device, shader); + else if(*shader->Type == typeid(ID3D11ComputeShader)) + CompileShader(device, shader); + else + AssertFail_("Invalid shader type"); + } + } +} + +void ShutdownShaders() +{ + for(uint64 i = 0; i < ShaderFiles.size(); ++i) + delete ShaderFiles[i]; + + for(uint64 i = 0; i < CompiledShaders.size(); ++i) + delete CompiledShaders[i]; +} + +// == CompileOptions ============================================================================== + +CompileOptions::CompileOptions() +{ + Reset(); +} + +void CompileOptions::Add(const std::string& name, uint32 value) +{ + Assert_(numDefines < MaxDefines); + + nameOffsets[numDefines] = bufferIdx; + for(uint32 i = 0; i < name.length(); ++i) + buffer[bufferIdx++] = name[i]; + ++bufferIdx; + + std::string stringVal = ToAnsiString(value); + defineOffsets[numDefines] = bufferIdx; + for(uint32 i = 0; i < stringVal.length(); ++i) + buffer[bufferIdx++] = stringVal[i]; + ++bufferIdx; + + ++numDefines; +} + +void CompileOptions::Reset() +{ + numDefines = 0; + bufferIdx = 0; + + for(uint32 i = 0; i < MaxDefines; ++i) + { + nameOffsets[i] = 0xFFFFFFFF; + defineOffsets[i] = 0xFFFFFFFF; + } + + ZeroMemory(buffer, BufferSize); +} + +void CompileOptions::MakeDefines(D3D_SHADER_MACRO defines[MaxDefines + 1]) const +{ + for(uint32 i = 0; i < numDefines; ++i) + { + defines[i].Name = buffer + nameOffsets[i]; + defines[i].Definition = buffer + defineOffsets[i]; + } + + defines[numDefines].Name = nullptr; + defines[numDefines].Definition = nullptr; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/ShaderCompilation.h b/Shadows/SampleFramework11/ShaderCompilation.h new file mode 100644 index 0000000..391dac1 --- /dev/null +++ b/Shadows/SampleFramework11/ShaderCompilation.h @@ -0,0 +1,191 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "Assert.h" + +namespace SampleFramework11 +{ + +class CompileOptions +{ +public: + + // constants + static const uint32 MaxDefines = 16; + static const uint32 BufferSize = 1024; + + CompileOptions(); + + void Add(const std::string& name, uint32 value); + void Reset(); + + void MakeDefines(D3D_SHADER_MACRO defines[MaxDefines + 1]) const; + +private: + + uint32 nameOffsets[MaxDefines]; + uint32 defineOffsets[MaxDefines]; + char buffer[BufferSize]; + uint32 numDefines; + uint32 bufferIdx; +}; + +class CompiledShader +{ + +public: + + ID3D11DeviceChildPtr ShaderPtr; + std::wstring FilePath; + std::string FunctionName; + std::string Profile; + CompileOptions CompileOpts; + bool ForceOptimization; + ID3DBlobPtr ByteCode; + const type_info* Type; + + CompiledShader(const wchar* filePath, const char* functionName, + const char* profile, const CompileOptions& compileOptions, + bool forceOptimization, const type_info& type) : FilePath(filePath), + FunctionName(functionName), + Profile(profile), + CompileOpts(compileOptions), + ForceOptimization(forceOptimization), + Type(&type) + { + } +}; + +template class CompiledShaderT : public CompiledShader +{ + +public: + + CompiledShaderT(const wchar* filePath, const char* functionName, + const char* profile, const CompileOptions& compileOptions, + bool forceOptimization) : CompiledShader(filePath, functionName, profile, + compileOptions, forceOptimization, typeid(T)) + { + } + + T* Shader() const + { + Assert_(ShaderPtr != nullptr); + Assert_(*Type == typeid(T)); + return reinterpret_cast(ShaderPtr.GetInterfacePtr()); + } +}; + +typedef CompiledShaderT CompiledVertexShader; +typedef CompiledShaderT CompiledHullShader; +typedef CompiledShaderT CompiledDomainShader; +typedef CompiledShaderT CompiledGeometryShader; +typedef CompiledShaderT CompiledPixelShader; +typedef CompiledShaderT CompiledComputeShader; + +template class CompiledShaderPtr +{ +public: + + CompiledShaderPtr() : ptr(nullptr) + { + } + + CompiledShaderPtr(const CompiledShaderT* ptr_) : ptr(ptr_) + { + } + + const CompiledShaderT* operator->() const + { + Assert_(ptr != nullptr); + return ptr; + } + + const CompiledShaderT& operator*() const + { + Assert_(ptr != nullptr); + return *ptr; + } + + operator T*() const + { + Assert_(ptr != nullptr); + return ptr->Shader(); + } + + bool Valid() const + { + return ptr != nullptr; + } + +private: + + const CompiledShaderT* ptr; +}; + +typedef CompiledShaderPtr VertexShaderPtr; +typedef CompiledShaderPtr HullShaderPtr; +typedef CompiledShaderPtr DomainShaderPtr; +typedef CompiledShaderPtr GeometryShaderPtr; +typedef CompiledShaderPtr PixelShaderPtr; +typedef CompiledShaderPtr ComputeShaderPtr; + +// Compiles a shader from file and creates the appropriate shader instance +VertexShaderPtr CompileVSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName = "VS", + const char* profile = "vs_5_0", + const CompileOptions& compileOpts = CompileOptions(), + bool forceOptimization = false); + +PixelShaderPtr CompilePSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName = "PS", + const char* profile = "ps_5_0", + const CompileOptions& compileOpts = CompileOptions(), + bool forceOptimization = false); + +GeometryShaderPtr CompileGSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName = "GS", + const char* profile = "gs_5_0", + const CompileOptions& compileOpts = CompileOptions(), + bool forceOptimization = false); + +HullShaderPtr CompileHSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName = "HS", + const char* profile = "hs_5_0", + const CompileOptions& compileOpts = CompileOptions(), + bool forceOptimization = false); + +DomainShaderPtr CompileDSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName = "DS", + const char* profile = "ds_5_0", + const CompileOptions& compileOpts = CompileOptions(), + bool forceOptimization = false); + +ComputeShaderPtr CompileCSFromFile(ID3D11Device* device, + const wchar* path, + const char* functionName = "CS", + const char* profile = "cs_5_0", + const CompileOptions& compileOpts = CompileOptions(), + bool forceOptimization = false); + +void UpdateShaders(ID3D11Device* device); +void ShutdownShaders(); + +} + diff --git a/Shadows/SampleFramework11/Shaders/Conversion.hlsl b/Shadows/SampleFramework11/Shaders/Conversion.hlsl new file mode 100644 index 0000000..86bab10 --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/Conversion.hlsl @@ -0,0 +1,71 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Float/half conversion functions +//================================================================================================= +uint2 Float4ToHalf(in float4 val) +{ + uint2 result = 0; + result.x = f32tof16(val.x); + result.x |= f32tof16(val.y) << 16; + result.y = f32tof16(val.z); + result.y |= f32tof16(val.w) << 16; + + return result; +} + +uint2 Float3ToHalf(in float3 val) +{ + uint2 result = 0; + result.x = f32tof16(val.x); + result.x |= f32tof16(val.y) << 16; + result.y = f32tof16(val.z); + + return result; +} + +uint Float2ToHalf(in float2 val) +{ + uint result = 0; + result = f32tof16(val.x); + result |= f32tof16(val.y) << 16; + + return result; +} + +float4 HalfToFloat4(in uint2 val) +{ + float4 result; + result.x = f16tof32(val.x); + result.y = f16tof32(val.x >> 16); + result.z = f16tof32(val.y); + result.w = f16tof32(val.y >> 16); + + return result; +} + +float3 HalfToFloat3(in uint2 val) +{ + float3 result; + result.x = f16tof32(val.x); + result.y = f16tof32(val.x >> 16); + result.z = f16tof32(val.y); + + return result; +} + +float2 HalfToFloat2(in uint val) +{ + float2 result; + result.x = f16tof32(val); + result.y = f16tof32(val >> 16); + + return result; +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Shaders/D3DX_DXGIFormatConvert.inl b/Shadows/SampleFramework11/Shaders/D3DX_DXGIFormatConvert.inl new file mode 100644 index 0000000..1cfba72 --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/D3DX_DXGIFormatConvert.inl @@ -0,0 +1,800 @@ +//============================================================================= +// D3D11 HLSL Routines for Manual Pack/Unpack of 32-bit DXGI_FORMAT_* +//============================================================================= +// +// This file contains format conversion routines for use in the +// Compute Shader or Pixel Shader on D3D11 Hardware. +// +// Skip to the end of this comment to see a summary of the routines +// provided. The rest of the text below explains why they are needed +// and how to use them. +// +// The scenario where these can be useful is if your application +// needs to simultaneously both read and write texture - i.e. in-place +// image editing. +// +// D3D11's Unordered Access View (UAV) of a Texture1D/2D/3D resource +// allows random access reads and writes to memory from a Compute Shader +// or Pixel Shader. However, the only texture format that supports this +// is DXGI_FORMAT_R32_UINT. e.g. Other more interesting formats like +// DXGI_FORMAT_R8G8B8A8_UNORM do not support simultaneous read and +// write. You can use such formats for random access writing only +// using a UAV, or reading only using a Shader Resource View (SRV). +// But for simultaneous read+write, the format conversion hardware is +// not available. +// +// There is a workaround to this limitation, involving casting the texture +// to R32_UINT when creating a UAV, as long as the original format of the +// resource supports it (most 32 bit per element formats). This allows +// simultaneous read+write as long as the shader does manual format +// unpacking on read and packing on write. +// +// The benefit is that later on, other views such as RenderTarget Views +// or ShaderResource Views on the same texture can be used with the +// proper format (e.g. DXGI_FORMAT_R16G16_FLOAT) so the hardware can +// do the usual automatic format unpack/pack and do texture filtering etc. +// where there are no hardware limitations. +// +// The sequence of actions for an application is the following: +// +// Suppose you want to make a texture than you can use a Pixel Shader +// or Compute Shader to perform in-place editing, and that the format +// you want the data to be stored in happens to be a descendent +// of of one of these formats: +// +// DXGI_FORMAT_R10G10B10A2_TYPELESS +// DXGI_FORMAT_R8G8B8A8_TYPELESS +// DXGI_FORMAT_B8G8R8A8_TYPELESS +// DXGI_FORMAT_B8G8R8X8_TYPELESS +// DXGI_FORMAT_R16G16_TYPELESS +// +// e.g. DXGI_FORMAT_R10G10B10A2_UNORM is a descendent of +// DXGI_FORMAT_R10G10B10A2_TYPELESS, so it supports the +// usage pattern described here. +// +// (Formats descending from DXGI_FORMAT_R32_TYPELESS, such as +// DXGI_FORMAT_R32_FLOAT, are trivially supported without +// needing any of the format conversion help provided here.) +// +// Steps: +// +// (1) Create a texture with the appropriate _TYPELESS format above +// along with the needed bind flags, such as +// D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE. +// +// (2) For in-place image editing, create a UAV with the format +// DXGI_FORMAT_R32_UINT. D3D normally doesn't allow casting +// between different format "families", but the API makes +// an exception here. +// +// (3) In the Compute Shader or Pixel Shader, use the appropriate +// format pack/unpack routines provided in this file. +// For example if the DXGI_FORMAT_R32_UINT UAV really holds +// DXGI_FORMAT_R10G10B10A2_UNORM data, then, after reading a +// uint from the UAV into the shader, unpack by calling: +// +// XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput) +// +// Then to write to the UAV in the same shader, call the following +// to pack shader data into a uint that can be written out: +// +// UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +// +// (4) Other views, such as SRVs, can be created with the desired format; +// e.g. DXGI_FORMAT_R10G10B10A2_UNORM if the resource was created as +// DXGI_FORMAT_R10G10B10A2_TYPELESS. When that view is accessed by a +// shader, the hardware can do automatic type conversion as usual. +// +// Note, again, that if the shader only needs to write to a UAV, or read +// as an SRV, then none of this is needed - fully typed UAV or SRVs can +// be used. Only if simultaneous reading and writing to a UAV of a texture +// is needed are the format conversion routines provided here potentially +// useful. +// +// The following is the list of format conversion routines included in this +// file, categorized by the DXGI_FORMAT they unpack/pack. Each of the +// formats supported descends from one of the TYPELESS formats listed +// above, and supports casting to DXGI_FORMAT_R32_UINT as a UAV. +// +// DXGI_FORMAT_R10G10B10A2_UNORM: +// +// XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput) +// UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +// +// DXGI_FORMAT_R10G10B10A2_UINT: +// +// XMUINT4 D3DX_R10G10B10A2_UINT_to_UINT4(UINT packedInput) +// UINT D3DX_UINT4_to_R10G10B10A2_UINT(XMUINT4 unpackedInput) +// +// DXGI_FORMAT_R8G8B8A8_UNORM: +// +// XMFLOAT4 D3DX_R8G8B8A8_UNORM_to_FLOAT4(UINT packedInput) +// UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +// +// DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: +// +// XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput) * +// XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput) +// UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput) +// +// * The "_inexact" function above uses shader instructions that don't +// have high enough precision to give the exact answer, albeit close. +// The alternative function uses a lookup table stored in the shader +// to give an exact SRGB->float conversion. +// +// DXGI_FORMAT_R8G8B8A8_UINT: +// +// XMUINT4 D3DX_R8G8B8A8_UINT_to_UINT4(UINT packedInput) +// XMUINT D3DX_UINT4_to_R8G8B8A8_UINT(XMUINT4 unpackedInput) +// +// DXGI_FORMAT_R8G8B8A8_SNORM: +// +// XMFLOAT4 D3DX_R8G8B8A8_SNORM_to_FLOAT4(UINT packedInput) +// UINT D3DX_FLOAT4_to_R8G8B8A8_SNORM(hlsl_precise XMFLOAT4 unpackedInput) +// +// DXGI_FORMAT_R8G8B8A8_SINT: +// +// XMINT4 D3DX_R8G8B8A8_SINT_to_INT4(UINT packedInput) +// UINT D3DX_INT4_to_R8G8B8A8_SINT(XMINT4 unpackedInput) +// +// DXGI_FORMAT_B8G8R8A8_UNORM: +// +// XMFLOAT4 D3DX_B8G8R8A8_UNORM_to_FLOAT4(UINT packedInput) +// UINT D3DX_FLOAT4_to_B8G8R8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +// +// DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: +// +// XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput) * +// XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput) +// UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput) +// +// * The "_inexact" function above uses shader instructions that don't +// have high enough precision to give the exact answer, albeit close. +// The alternative function uses a lookup table stored in the shader +// to give an exact SRGB->float conversion. +// +// DXGI_FORMAT_B8G8R8X8_UNORM: +// +// XMFLOAT3 D3DX_B8G8R8X8_UNORM_to_FLOAT3(UINT packedInput) +// UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM(hlsl_precise XMFLOAT3 unpackedInput) +// +// DXGI_FORMAT_B8G8R8X8_UNORM_SRGB: +// +// XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3_inexact(UINT packedInput) * +// XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3(UINT packedInput) +// UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM_SRGB(hlsl_precise XMFLOAT3 unpackedInput) +// +// * The "_inexact" function above uses shader instructions that don't +// have high enough precision to give the exact answer, albeit close. +// The alternative function uses a lookup table stored in the shader +// to give an exact SRGB->float conversion. +// +// DXGI_FORMAT_R16G16_FLOAT: +// +// XMFLOAT2 D3DX_R16G16_FLOAT_to_FLOAT2(UINT packedInput) +// UINT D3DX_FLOAT2_to_R16G16_FLOAT(hlsl_precise XMFLOAT2 unpackedInput) +// +// DXGI_FORMAT_R16G16_UNORM: +// +// XMFLOAT2 D3DX_R16G16_UNORM_to_FLOAT2(UINT packedInput) +// UINT D3DX_FLOAT2_to_R16G16_UNORM(hlsl_precise FLOAT2 unpackedInput) +// +// DXGI_FORMAT_R16G16_UINT: +// +// XMUINT2 D3DX_R16G16_UINT_to_UINT2(UINT packedInput) +// UINT D3DX_UINT2_to_R16G16_UINT(XMUINT2 unpackedInput) +// +// DXGI_FORMAT_R16G16_SNORM: +// +// XMFLOAT2 D3DX_R16G16_SNORM_to_FLOAT2(UINT packedInput) +// UINT D3DX_FLOAT2_to_R16G16_SNORM(hlsl_precise XMFLOAT2 unpackedInput) +// +// DXGI_FORMAT_R16G16_SINT: +// +// XMINT2 D3DX_R16G16_SINT_to_INT2(UINT packedInput) +// UINT D3DX_INT2_to_R16G16_SINT(XMINT2 unpackedInput) +// +//============================================================================= + +#ifndef __D3DX_DXGI_FORMAT_CONVERT_INL___ +#define __D3DX_DXGI_FORMAT_CONVERT_INL___ + +#if HLSL_VERSION > 0 + +#define D3DX11INLINE + +typedef int INT; +typedef uint UINT; + +typedef float2 XMFLOAT2; +typedef float3 XMFLOAT3; +typedef float4 XMFLOAT4; +typedef int2 XMINT2; +typedef int4 XMINT4; +typedef uint2 XMUINT2; +typedef uint4 XMUINT4; + +#define hlsl_precise precise + +#define D3DX_Saturate_FLOAT(_V) saturate(_V) +#define D3DX_IsNan(_V) isnan(_V) +#define D3DX_Truncate_FLOAT(_V) trunc(_V) + +#else // HLSL_VERSION > 0 + +#ifndef __cplusplus +#error C++ compilation required +#endif + +#include +#include + +#define hlsl_precise + +D3DX11INLINE FLOAT D3DX_Saturate_FLOAT(FLOAT _V) +{ + return min(max(_V, 0), 1); +} +D3DX11INLINE bool D3DX_IsNan(FLOAT _V) +{ + return _V != _V; +} +D3DX11INLINE FLOAT D3DX_Truncate_FLOAT(FLOAT _V) +{ + return _V >= 0 ? floor(_V) : ceil(_V); +} + +// 2D Vector; 32 bit signed integer components +typedef struct _XMINT2 +{ + INT x; + INT y; +} XMINT2; + +// 2D Vector; 32 bit unsigned integer components +typedef struct _XMUINT2 +{ + UINT x; + UINT y; +} XMUINT2; + +// 4D Vector; 32 bit signed integer components +typedef struct _XMINT4 +{ + INT x; + INT y; + INT z; + INT w; +} XMINT4; + +// 4D Vector; 32 bit unsigned integer components +typedef struct _XMUINT4 +{ + UINT x; + UINT y; + UINT z; + UINT w; +} XMUINT4; + +#endif // HLSL_VERSION > 0 + +//============================================================================= +// SRGB Helper Functions Called By Conversions Further Below. +//============================================================================= +// SRGB_to_FLOAT_inexact is imprecise due to precision of pow implementations. +// If exact SRGB->float conversion is needed, a table lookup is provided +// further below. +D3DX11INLINE FLOAT D3DX_SRGB_to_FLOAT_inexact(hlsl_precise FLOAT val) +{ + if( val < 0.04045f ) + val /= 12.92f; + else + val = pow((val + 0.055f)/1.055f,2.4f); + return val; +} + +static const UINT D3DX_SRGBTable[] = +{ + 0x00000000,0x399f22b4,0x3a1f22b4,0x3a6eb40e,0x3a9f22b4,0x3ac6eb61,0x3aeeb40e,0x3b0b3e5d, + 0x3b1f22b4,0x3b33070b,0x3b46eb61,0x3b5b518d,0x3b70f18d,0x3b83e1c6,0x3b8fe616,0x3b9c87fd, + 0x3ba9c9b7,0x3bb7ad6f,0x3bc63549,0x3bd56361,0x3be539c1,0x3bf5ba70,0x3c0373b5,0x3c0c6152, + 0x3c15a703,0x3c1f45be,0x3c293e6b,0x3c3391f7,0x3c3e4149,0x3c494d43,0x3c54b6c7,0x3c607eb1, + 0x3c6ca5df,0x3c792d22,0x3c830aa8,0x3c89af9f,0x3c9085db,0x3c978dc5,0x3c9ec7c2,0x3ca63433, + 0x3cadd37d,0x3cb5a601,0x3cbdac20,0x3cc5e639,0x3cce54ab,0x3cd6f7d5,0x3cdfd010,0x3ce8ddb9, + 0x3cf2212c,0x3cfb9ac1,0x3d02a569,0x3d0798dc,0x3d0ca7e6,0x3d11d2af,0x3d171963,0x3d1c7c2e, + 0x3d21fb3c,0x3d2796b2,0x3d2d4ebb,0x3d332380,0x3d39152b,0x3d3f23e3,0x3d454fd1,0x3d4b991c, + 0x3d51ffef,0x3d58846a,0x3d5f26b7,0x3d65e6fe,0x3d6cc564,0x3d73c20f,0x3d7add29,0x3d810b67, + 0x3d84b795,0x3d887330,0x3d8c3e4a,0x3d9018f6,0x3d940345,0x3d97fd4a,0x3d9c0716,0x3da020bb, + 0x3da44a4b,0x3da883d7,0x3daccd70,0x3db12728,0x3db59112,0x3dba0b3b,0x3dbe95b5,0x3dc33092, + 0x3dc7dbe2,0x3dcc97b6,0x3dd1641f,0x3dd6412c,0x3ddb2eef,0x3de02d77,0x3de53cd5,0x3dea5d19, + 0x3def8e52,0x3df4d091,0x3dfa23e8,0x3dff8861,0x3e027f07,0x3e054280,0x3e080ea3,0x3e0ae378, + 0x3e0dc105,0x3e10a754,0x3e13966b,0x3e168e52,0x3e198f10,0x3e1c98ad,0x3e1fab30,0x3e22c6a3, + 0x3e25eb09,0x3e29186c,0x3e2c4ed0,0x3e2f8e41,0x3e32d6c4,0x3e362861,0x3e39831e,0x3e3ce703, + 0x3e405416,0x3e43ca5f,0x3e4749e4,0x3e4ad2ae,0x3e4e64c2,0x3e520027,0x3e55a4e6,0x3e595303, + 0x3e5d0a8b,0x3e60cb7c,0x3e6495e0,0x3e6869bf,0x3e6c4720,0x3e702e0c,0x3e741e84,0x3e781890, + 0x3e7c1c38,0x3e8014c2,0x3e82203c,0x3e84308d,0x3e8645ba,0x3e885fc5,0x3e8a7eb2,0x3e8ca283, + 0x3e8ecb3d,0x3e90f8e1,0x3e932b74,0x3e9562f8,0x3e979f71,0x3e99e0e2,0x3e9c274e,0x3e9e72b7, + 0x3ea0c322,0x3ea31892,0x3ea57308,0x3ea7d289,0x3eaa3718,0x3eaca0b7,0x3eaf0f69,0x3eb18333, + 0x3eb3fc18,0x3eb67a18,0x3eb8fd37,0x3ebb8579,0x3ebe12e1,0x3ec0a571,0x3ec33d2d,0x3ec5da17, + 0x3ec87c33,0x3ecb2383,0x3ecdd00b,0x3ed081cd,0x3ed338cc,0x3ed5f50b,0x3ed8b68d,0x3edb7d54, + 0x3ede4965,0x3ee11ac1,0x3ee3f16b,0x3ee6cd67,0x3ee9aeb6,0x3eec955d,0x3eef815d,0x3ef272ba, + 0x3ef56976,0x3ef86594,0x3efb6717,0x3efe6e02,0x3f00bd2d,0x3f02460e,0x3f03d1a7,0x3f055ff9, + 0x3f06f106,0x3f0884cf,0x3f0a1b56,0x3f0bb49b,0x3f0d50a0,0x3f0eef67,0x3f1090f1,0x3f12353e, + 0x3f13dc51,0x3f15862b,0x3f1732cd,0x3f18e239,0x3f1a946f,0x3f1c4971,0x3f1e0141,0x3f1fbbdf, + 0x3f21794e,0x3f23398e,0x3f24fca0,0x3f26c286,0x3f288b41,0x3f2a56d3,0x3f2c253d,0x3f2df680, + 0x3f2fca9e,0x3f31a197,0x3f337b6c,0x3f355820,0x3f3737b3,0x3f391a26,0x3f3aff7c,0x3f3ce7b5, + 0x3f3ed2d2,0x3f40c0d4,0x3f42b1be,0x3f44a590,0x3f469c4b,0x3f4895f1,0x3f4a9282,0x3f4c9201, + 0x3f4e946e,0x3f5099cb,0x3f52a218,0x3f54ad57,0x3f56bb8a,0x3f58ccb0,0x3f5ae0cd,0x3f5cf7e0, + 0x3f5f11ec,0x3f612eee,0x3f634eef,0x3f6571e9,0x3f6797e3,0x3f69c0d6,0x3f6beccd,0x3f6e1bbf, + 0x3f704db8,0x3f7282af,0x3f74baae,0x3f76f5ae,0x3f7933b9,0x3f7b74c6,0x3f7db8e0,0x3f800000 +}; + +D3DX11INLINE FLOAT D3DX_SRGB_to_FLOAT(UINT val) +{ +#if HLSL_VERSION > 0 + return asfloat(D3DX_SRGBTable[val]); +#else + return *(FLOAT*)&D3DX_SRGBTable[val]; +#endif +} + +D3DX11INLINE FLOAT D3DX_FLOAT_to_SRGB(hlsl_precise FLOAT val) +{ + if( val < 0.0031308f ) + val *= 12.92f; + else + val = 1.055f * pow(val,1.0f/2.4f) - 0.055f; + return val; +} + +D3DX11INLINE FLOAT D3DX_SaturateSigned_FLOAT(FLOAT _V) +{ + if (D3DX_IsNan(_V)) + { + return 0; + } + + return min(max(_V, -1), 1); +} + +D3DX11INLINE UINT D3DX_FLOAT_to_UINT(FLOAT _V, + FLOAT _Scale) +{ + return (UINT)floor(_V * _Scale + 0.5f); +} + +D3DX11INLINE FLOAT D3DX_INT_to_FLOAT(INT _V, + FLOAT _Scale) +{ + FLOAT Scaled = (FLOAT)_V / _Scale; + // The integer is a two's-complement signed + // number so the negative range is slightly + // larger than the positive range, meaning + // the scaled value can be slight less than -1. + // Clamp to keep the float range [-1, 1]. + return max(Scaled, -1.0f); +} + +D3DX11INLINE INT D3DX_FLOAT_to_INT(FLOAT _V, + FLOAT _Scale) +{ + return (INT)D3DX_Truncate_FLOAT(_V * _Scale + (_V >= 0 ? 0.5f : -0.5f)); +} + +//============================================================================= +// Conversion routines +//============================================================================= +//----------------------------------------------------------------------------- +// R10B10G10A2_UNORM <-> FLOAT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT4 D3DX_R10G10B10A2_UNORM_to_FLOAT4(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.x = (FLOAT) (packedInput & 0x000003ff) / 1023; + unpackedOutput.y = (FLOAT)(((packedInput>>10) & 0x000003ff)) / 1023; + unpackedOutput.z = (FLOAT)(((packedInput>>20) & 0x000003ff)) / 1023; + unpackedOutput.w = (FLOAT)(((packedInput>>30) & 0x00000003)) / 3; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT4_to_R10G10B10A2_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 1023)) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 1023)<<10) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 1023)<<20) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.w), 3)<<30) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R10B10G10A2_UINT <-> UINT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMUINT4 D3DX_R10G10B10A2_UINT_to_UINT4(UINT packedInput) +{ + XMUINT4 unpackedOutput; + unpackedOutput.x = packedInput & 0x000003ff; + unpackedOutput.y = (packedInput>>10) & 0x000003ff; + unpackedOutput.z = (packedInput>>20) & 0x000003ff; + unpackedOutput.w = (packedInput>>30) & 0x00000003; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_UINT4_to_R10G10B10A2_UINT(XMUINT4 unpackedInput) +{ + UINT packedOutput; + unpackedInput.x = min(unpackedInput.x, 0x000003ff); + unpackedInput.y = min(unpackedInput.y, 0x000003ff); + unpackedInput.z = min(unpackedInput.z, 0x000003ff); + unpackedInput.w = min(unpackedInput.w, 0x00000003); + packedOutput = ( (unpackedInput.x) | + ((unpackedInput.y)<<10) | + ((unpackedInput.z)<<20) | + ((unpackedInput.w)<<30) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R8G8B8A8_UNORM <-> FLOAT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_UNORM_to_FLOAT4(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.x = (FLOAT) (packedInput & 0x000000ff) / 255; + unpackedOutput.y = (FLOAT)(((packedInput>> 8) & 0x000000ff)) / 255; + unpackedOutput.z = (FLOAT)(((packedInput>>16) & 0x000000ff)) / 255; + unpackedOutput.w = (FLOAT) (packedInput>>24) / 255; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 255)) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 255)<< 8) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 255)<<16) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.w), 255)<<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R8G8B8A8_UNORM_SRGB <-> FLOAT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.x = D3DX_SRGB_to_FLOAT_inexact(((FLOAT) (packedInput & 0x000000ff) )/255); + unpackedOutput.y = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>> 8) & 0x000000ff)))/255); + unpackedOutput.z = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>>16) & 0x000000ff)))/255); + unpackedOutput.w = (FLOAT)(packedInput>>24) / 255; + return unpackedOutput; +} + +D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.x = D3DX_SRGB_to_FLOAT( (packedInput & 0x000000ff) ); + unpackedOutput.y = D3DX_SRGB_to_FLOAT((((packedInput>> 8) & 0x000000ff))); + unpackedOutput.z = D3DX_SRGB_to_FLOAT((((packedInput>>16) & 0x000000ff))); + unpackedOutput.w = (FLOAT)(packedInput>>24) / 255; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT4_to_R8G8B8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput) +{ + UINT packedOutput; + unpackedInput.x = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.x)); + unpackedInput.y = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.y)); + unpackedInput.z = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.z)); + unpackedInput.w = D3DX_Saturate_FLOAT(unpackedInput.w); + packedOutput = ( (D3DX_FLOAT_to_UINT(unpackedInput.x, 255)) | + (D3DX_FLOAT_to_UINT(unpackedInput.y, 255)<< 8) | + (D3DX_FLOAT_to_UINT(unpackedInput.z, 255)<<16) | + (D3DX_FLOAT_to_UINT(unpackedInput.w, 255)<<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R8G8B8A8_UINT <-> UINT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMUINT4 D3DX_R8G8B8A8_UINT_to_UINT4(UINT packedInput) +{ + XMUINT4 unpackedOutput; + unpackedOutput.x = packedInput & 0x000000ff; + unpackedOutput.y = (packedInput>> 8) & 0x000000ff; + unpackedOutput.z = (packedInput>>16) & 0x000000ff; + unpackedOutput.w = packedInput>>24; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_UINT4_to_R8G8B8A8_UINT(XMUINT4 unpackedInput) +{ + UINT packedOutput; + unpackedInput.x = min(unpackedInput.x, 0x000000ff); + unpackedInput.y = min(unpackedInput.y, 0x000000ff); + unpackedInput.z = min(unpackedInput.z, 0x000000ff); + unpackedInput.w = min(unpackedInput.w, 0x000000ff); + packedOutput = ( unpackedInput.x | + (unpackedInput.y<< 8) | + (unpackedInput.z<<16) | + (unpackedInput.w<<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R8G8B8A8_SNORM <-> FLOAT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT4 D3DX_R8G8B8A8_SNORM_to_FLOAT4(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + XMINT4 signExtendedBits; + signExtendedBits.x = (INT)(packedInput << 24) >> 24; + signExtendedBits.y = (INT)((packedInput << 16) & 0xff000000) >> 24; + signExtendedBits.z = (INT)((packedInput << 8) & 0xff000000) >> 24; + signExtendedBits.w = (INT)(packedInput & 0xff000000) >> 24; + unpackedOutput.x = D3DX_INT_to_FLOAT(signExtendedBits.x, 127); + unpackedOutput.y = D3DX_INT_to_FLOAT(signExtendedBits.y, 127); + unpackedOutput.z = D3DX_INT_to_FLOAT(signExtendedBits.z, 127); + unpackedOutput.w = D3DX_INT_to_FLOAT(signExtendedBits.w, 127); + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT4_to_R8G8B8A8_SNORM(hlsl_precise XMFLOAT4 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.x), 127) & 0x000000ff) | + ((D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.y), 127) & 0x000000ff)<< 8) | + ((D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.z), 127) & 0x000000ff)<<16) | + ((D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.w), 127)) <<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R8G8B8A8_SINT <-> INT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMINT4 D3DX_R8G8B8A8_SINT_to_INT4(UINT packedInput) +{ + XMINT4 unpackedOutput; + unpackedOutput.x = (INT)(packedInput << 24) >> 24; + unpackedOutput.y = (INT)((packedInput << 16) & 0xff000000) >> 24; + unpackedOutput.z = (INT)((packedInput << 8) & 0xff000000) >> 24; + unpackedOutput.w = (INT)(packedInput & 0xff000000) >> 24; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_INT4_to_R8G8B8A8_SINT(XMINT4 unpackedInput) +{ + UINT packedOutput; + unpackedInput.x = max(min(unpackedInput.x,127),-128); + unpackedInput.y = max(min(unpackedInput.y,127),-128); + unpackedInput.z = max(min(unpackedInput.z,127),-128); + unpackedInput.w = max(min(unpackedInput.w,127),-128); + packedOutput = ( (unpackedInput.x & 0x000000ff) | + ((unpackedInput.y & 0x000000ff)<< 8) | + ((unpackedInput.z & 0x000000ff)<<16) | + (unpackedInput.w <<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// B8G8R8A8_UNORM <-> FLOAT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT4 D3DX_B8G8R8A8_UNORM_to_FLOAT4(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.z = (FLOAT) (packedInput & 0x000000ff) / 255; + unpackedOutput.y = (FLOAT)(((packedInput>> 8) & 0x000000ff)) / 255; + unpackedOutput.x = (FLOAT)(((packedInput>>16) & 0x000000ff)) / 255; + unpackedOutput.w = (FLOAT) (packedInput>>24) / 255; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT4_to_B8G8R8A8_UNORM(hlsl_precise XMFLOAT4 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 255)) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 255)<< 8) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 255)<<16) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.w), 255)<<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// B8G8R8A8_UNORM_SRGB <-> FLOAT4 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4_inexact(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.z = D3DX_SRGB_to_FLOAT_inexact(((FLOAT) (packedInput & 0x000000ff) )/255); + unpackedOutput.y = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>> 8) & 0x000000ff)))/255); + unpackedOutput.x = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>>16) & 0x000000ff)))/255); + unpackedOutput.w = (FLOAT)(packedInput>>24) / 255; + return unpackedOutput; +} + +D3DX11INLINE XMFLOAT4 D3DX_B8G8R8A8_UNORM_SRGB_to_FLOAT4(UINT packedInput) +{ + hlsl_precise XMFLOAT4 unpackedOutput; + unpackedOutput.z = D3DX_SRGB_to_FLOAT( (packedInput & 0x000000ff) ); + unpackedOutput.y = D3DX_SRGB_to_FLOAT((((packedInput>> 8) & 0x000000ff))); + unpackedOutput.x = D3DX_SRGB_to_FLOAT((((packedInput>>16) & 0x000000ff))); + unpackedOutput.w = (FLOAT)(packedInput>>24) / 255; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT4_to_B8G8R8A8_UNORM_SRGB(hlsl_precise XMFLOAT4 unpackedInput) +{ + UINT packedOutput; + unpackedInput.z = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.z)); + unpackedInput.y = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.y)); + unpackedInput.x = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.x)); + unpackedInput.w = D3DX_Saturate_FLOAT(unpackedInput.w); + packedOutput = ( (D3DX_FLOAT_to_UINT(unpackedInput.z, 255)) | + (D3DX_FLOAT_to_UINT(unpackedInput.y, 255)<< 8) | + (D3DX_FLOAT_to_UINT(unpackedInput.x, 255)<<16) | + (D3DX_FLOAT_to_UINT(unpackedInput.w, 255)<<24) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// B8G8R8X8_UNORM <-> FLOAT3 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT3 D3DX_B8G8R8X8_UNORM_to_FLOAT3(UINT packedInput) +{ + hlsl_precise XMFLOAT3 unpackedOutput; + unpackedOutput.z = (FLOAT) (packedInput & 0x000000ff) / 255; + unpackedOutput.y = (FLOAT)(((packedInput>> 8) & 0x000000ff)) / 255; + unpackedOutput.x = (FLOAT)(((packedInput>>16) & 0x000000ff)) / 255; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM(hlsl_precise XMFLOAT3 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.z), 255)) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 255)<< 8) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 255)<<16) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// B8G8R8X8_UNORM_SRGB <-> FLOAT3 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3_inexact(UINT packedInput) +{ + hlsl_precise XMFLOAT3 unpackedOutput; + unpackedOutput.z = D3DX_SRGB_to_FLOAT_inexact(((FLOAT) (packedInput & 0x000000ff) )/255); + unpackedOutput.y = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>> 8) & 0x000000ff)))/255); + unpackedOutput.x = D3DX_SRGB_to_FLOAT_inexact(((FLOAT)(((packedInput>>16) & 0x000000ff)))/255); + return unpackedOutput; +} + +D3DX11INLINE XMFLOAT3 D3DX_B8G8R8X8_UNORM_SRGB_to_FLOAT3(UINT packedInput) +{ + hlsl_precise XMFLOAT3 unpackedOutput; + unpackedOutput.z = D3DX_SRGB_to_FLOAT( (packedInput & 0x000000ff) ); + unpackedOutput.y = D3DX_SRGB_to_FLOAT((((packedInput>> 8) & 0x000000ff))); + unpackedOutput.x = D3DX_SRGB_to_FLOAT((((packedInput>>16) & 0x000000ff))); + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT3_to_B8G8R8X8_UNORM_SRGB(hlsl_precise XMFLOAT3 unpackedInput) +{ + UINT packedOutput; + unpackedInput.z = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.z)); + unpackedInput.y = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.y)); + unpackedInput.x = D3DX_FLOAT_to_SRGB(D3DX_Saturate_FLOAT(unpackedInput.x)); + packedOutput = ( (D3DX_FLOAT_to_UINT(unpackedInput.z, 255)) | + (D3DX_FLOAT_to_UINT(unpackedInput.y, 255)<< 8) | + (D3DX_FLOAT_to_UINT(unpackedInput.x, 255)<<16) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R16G16_FLOAT <-> FLOAT2 +//----------------------------------------------------------------------------- + +#if HLSL_VERSION > 0 + +D3DX11INLINE XMFLOAT2 D3DX_R16G16_FLOAT_to_FLOAT2(UINT packedInput) +{ + hlsl_precise XMFLOAT2 unpackedOutput; + unpackedOutput.x = f16tof32(packedInput&0x0000ffff); + unpackedOutput.y = f16tof32(packedInput>>16); + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT2_to_R16G16_FLOAT(hlsl_precise XMFLOAT2 unpackedInput) +{ + UINT packedOutput; + packedOutput = asuint(f32tof16(unpackedInput.x)) | + (asuint(f32tof16(unpackedInput.y)) << 16); + return packedOutput; +} + +#endif // HLSL_VERSION > 0 + +//----------------------------------------------------------------------------- +// R16G16_UNORM <-> FLOAT2 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT2 D3DX_R16G16_UNORM_to_FLOAT2(UINT packedInput) +{ + hlsl_precise XMFLOAT2 unpackedOutput; + unpackedOutput.x = (FLOAT) (packedInput & 0x0000ffff) / 65535; + unpackedOutput.y = (FLOAT) (packedInput>>16) / 65535; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT2_to_R16G16_UNORM(hlsl_precise XMFLOAT2 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.x), 65535)) | + (D3DX_FLOAT_to_UINT(D3DX_Saturate_FLOAT(unpackedInput.y), 65535)<< 16) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R16G16_UINT <-> UINT2 +//----------------------------------------------------------------------------- +D3DX11INLINE XMUINT2 D3DX_R16G16_UINT_to_UINT2(UINT packedInput) +{ + XMUINT2 unpackedOutput; + unpackedOutput.x = packedInput & 0x0000ffff; + unpackedOutput.y = packedInput>>16; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_UINT2_to_R16G16_UINT(XMUINT2 unpackedInput) +{ + UINT packedOutput; + unpackedInput.x = min(unpackedInput.x,0x0000ffff); + unpackedInput.y = min(unpackedInput.y,0x0000ffff); + packedOutput = ( unpackedInput.x | + (unpackedInput.y<<16) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R16G16_SNORM <-> FLOAT2 +//----------------------------------------------------------------------------- +D3DX11INLINE XMFLOAT2 D3DX_R16G16_SNORM_to_FLOAT2(UINT packedInput) +{ + hlsl_precise XMFLOAT2 unpackedOutput; + XMINT2 signExtendedBits; + signExtendedBits.x = (INT)(packedInput << 16) >> 16; + signExtendedBits.y = (INT)(packedInput & 0xffff0000) >> 16; + unpackedOutput.x = D3DX_INT_to_FLOAT(signExtendedBits.x, 32767); + unpackedOutput.y = D3DX_INT_to_FLOAT(signExtendedBits.y, 32767); + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_FLOAT2_to_R16G16_SNORM(hlsl_precise XMFLOAT2 unpackedInput) +{ + UINT packedOutput; + packedOutput = ( (D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.x), 32767) & 0x0000ffff) | + (D3DX_FLOAT_to_INT(D3DX_SaturateSigned_FLOAT(unpackedInput.y), 32767) <<16) ); + return packedOutput; +} + +//----------------------------------------------------------------------------- +// R16G16_SINT <-> INT2 +//----------------------------------------------------------------------------- +D3DX11INLINE XMINT2 D3DX_R16G16_SINT_to_INT2(UINT packedInput) +{ + XMINT2 unpackedOutput; + unpackedOutput.x = (INT)(packedInput << 16) >> 16; + unpackedOutput.y = (INT)(packedInput & 0xffff0000) >> 16; + return unpackedOutput; +} + +D3DX11INLINE UINT D3DX_INT2_to_R16G16_SINT(XMINT2 unpackedInput) +{ + UINT packedOutput; + unpackedInput.x = max(min(unpackedInput.x,32767),-32768); + unpackedInput.y = max(min(unpackedInput.y,32767),-32768); + packedOutput = ( (unpackedInput.x & 0x0000ffff) | + (unpackedInput.y <<16) ); + return packedOutput; +} + +#endif // __D3DX_DXGI_FORMAT_CONVERT_INL___ diff --git a/Shadows/SampleFramework11/Shaders/DecodeTextureCS.hlsl b/Shadows/SampleFramework11/Shaders/DecodeTextureCS.hlsl new file mode 100644 index 0000000..8f0f35c --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/DecodeTextureCS.hlsl @@ -0,0 +1,37 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Resources +//================================================================================================= + +// Inputs +Texture2D InputTexture : register(t0); +Texture2DArray InputTextureArray : register(t0); + +// Outputs +RWTexture2D OutputTexture : register(u0); +RWTexture2DArray OutputTextureArray : register(u0); + +//================================================================================================= +// Entry points +//================================================================================================= +[numthreads(TGSize_, TGSize_, 1)] +void DecodeTextureCS(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID) +{ + const uint2 texelIdx = GroupID.xy * uint2(TGSize_, TGSize_) + GroupThreadID.xy; + OutputTexture[texelIdx] = InputTexture[texelIdx]; +} + +[numthreads(TGSize_, TGSize_, 1)] +void DecodeTextureArrayCS(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID) +{ + const uint3 texelIdx = uint3(GroupID.xy * uint2(TGSize_, TGSize_) + GroupThreadID.xy, GroupID.z); + OutputTextureArray[texelIdx] = InputTextureArray[texelIdx]; +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Shaders/PPIncludes.hlsl b/Shadows/SampleFramework11/Shaders/PPIncludes.hlsl new file mode 100644 index 0000000..ab105d3 --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/PPIncludes.hlsl @@ -0,0 +1,44 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +cbuffer PSConstants : register(b0) +{ + float2 InputSize0 : packoffset(c0.x); + float2 InputSize1 : packoffset(c0.z); + float2 InputSize2 : packoffset(c1.x); + float2 InputSize3 : packoffset(c1.z); + float2 OutputSize : packoffset(c2.x); +} + +SamplerState PointSampler : register(s0); +SamplerState LinearSampler : register(s1); +SamplerState LinearWrapSampler : register(s2); +SamplerState LinearBorderSampler : register(s3); +SamplerState PointBorderSampler : register(s4); + +Texture2D InputTexture0 : register(t0); +Texture2D InputTexture1 : register(t1); +Texture2D InputTexture2 : register(t2); +Texture2D InputTexture3 : register(t3); + +Texture2D InputTextureInt0 : register(t0); +Texture2D InputTextureInt1 : register(t1); +Texture2D InputTextureInt2 : register(t2); +Texture2D InputTextureInt3 : register(t3); + +Texture2D InputTextureUint0 : register(t0); +Texture2D InputTextureUint1 : register(t1); +Texture2D InputTextureUint2 : register(t2); +Texture2D InputTextureUint3 : register(t3); + +struct PSInput +{ + float4 PositionSS : SV_Position; + float2 TexCoord : TEXCOORD; +}; \ No newline at end of file diff --git a/Shadows/SampleFramework11/Shaders/Quad.hlsl b/Shadows/SampleFramework11/Shaders/Quad.hlsl new file mode 100644 index 0000000..c41a24f --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/Quad.hlsl @@ -0,0 +1,38 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Input/Output structs +//================================================================================================= + +struct VSInput +{ + float4 PositionCS : POSITION; + float2 TexCoord : TEXCOORD; +}; + +struct VSOutput +{ + float4 PositionCS : SV_Position; + float2 TexCoord : TEXCOORD; +}; + +//================================================================================================= +// Vertex Shader +//================================================================================================= +VSOutput QuadVS(in VSInput input) +{ + VSOutput output; + + // Just pass it along + output.PositionCS = input.PositionCS; + output.TexCoord = input.TexCoord; + + return output; +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Shaders/SH.hlsl b/Shadows/SampleFramework11/Shaders/SH.hlsl new file mode 100644 index 0000000..353b5d2 --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/SH.hlsl @@ -0,0 +1,391 @@ +struct SH4Color +{ + float3 c[4]; +}; + +struct SH9Color +{ + float3 c[9]; +}; + +typedef float4 H4; + +struct H4Color +{ + float3 c[4]; +}; + +struct H6Color +{ + float3 c[6]; +}; + +// Cosine kernel for SH +static const float CosineA0 = 1.0f; +static const float CosineA1 = 2.0f / 3.0f; +static const float CosineA2 = 0.25f; + +// == SH4 ========================================================================================= + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH and convolves with a given kernel +//------------------------------------------------------------------------------------------------- +SH4Color ProjectOntoSH4(in float3 n, in float3 color, in float A0, in float A1) +{ + SH4Color sh; + + // Band 0 + sh.c[0] = 0.282095f * A0 * color; + + // Band 1 + sh.c[1] = 0.488603f * n.y * A1 * color; + sh.c[2] = 0.488603f * n.z * A1 * color; + sh.c[3] = 0.488603f * n.x * A1 * color; + + return sh; +} + +SH4Color ProjectOntoSH4(in float3 n, in float3 color) +{ + return ProjectOntoSH4(n, color, 1.0f, 1.0f); +} + +//------------------------------------------------------------------------------------------------- +// Computes the dot project of two SH4 RGB vectors +//------------------------------------------------------------------------------------------------- +float3 SHDotProduct(in SH4Color a, in SH4Color b) +{ + float3 result = 0.0f; + + [unroll] + for(uint i = 0; i < 4; ++i) + result += a.c[i] * b.c[i]; + + return result; +} + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH4 and dots it with another SH4 vector +//------------------------------------------------------------------------------------------------- +float3 EvalSH4(in float3 dir, in SH4Color sh) +{ + SH4Color dirSH = ProjectOntoSH4(dir, float3(1.0f, 1.0f, 1.0f)); + return SHDotProduct(dirSH, sh); +} + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH4, convolves with a cosine kernel, and dots it with another +// SH4 vector +//------------------------------------------------------------------------------------------------- +float3 EvalSH4Cosine(in float3 dir, in SH4Color sh) +{ + SH4Color dirSH = ProjectOntoSH4(dir, float3(1.0f, 1.0f, 1.0f), CosineA0, CosineA1); + return SHDotProduct(dirSH, sh); +} + +//------------------------------------------------------------------------------------------------- +// Converts from 3-band to 2-band SH +//------------------------------------------------------------------------------------------------- +SH4Color ConvertToSH4(in SH9Color sh9) +{ + SH4Color sh4; + [unroll] + for(uint i = 0; i < 4; ++i) + sh4.c[i] = sh9.c[i]; + return sh4; +} + +//------------------------------------------------------------------------------------------------- +// Computes the "optimal linear direction" for a set of SH coefficients +//------------------------------------------------------------------------------------------------- +float3 OptimalLinearDirection(in SH4Color sh) +{ + float x = dot(sh.c[3], 1.0f / 3.0f); + float y = dot(sh.c[1], 1.0f / 3.0f); + float z = dot(sh.c[2], 1.0f / 3.0f); + return normalize(float3(x, y, z)); +} + +//------------------------------------------------------------------------------------------------- +// Computes the direction and color of a directional light that approximates a set of SH +// coefficients. Uses Peter Pike-Sloan's method from "Stupid SH Tricks" +//------------------------------------------------------------------------------------------------- +void ApproximateDirectionalLight(in SH4Color sh, out float3 direction, out float3 color) +{ + direction = OptimalLinearDirection(sh); + SH4Color dirSH = ProjectOntoSH4(direction, 1.0f); + dirSH.c[0] = 0.0f; + sh.c[0] = 0.0f; + color = SHDotProduct(dirSH, sh) * 867.0f / (316.0f * Pi); +} + +// == SH9 ========================================================================================= + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH and convolves with a given kernel +//------------------------------------------------------------------------------------------------- +SH9Color ProjectOntoSH9(in float3 n, in float3 color, in float A0, in float A1, in float A2) +{ + SH9Color sh; + + // Band 0 + sh.c[0] = 0.282095f * A0 * color; + + // Band 1 + sh.c[1] = 0.488603f * n.y * A1 * color; + sh.c[2] = 0.488603f * n.z * A1 * color; + sh.c[3] = 0.488603f * n.x * A1 * color; + + // Band 2 + sh.c[4] = 1.092548f * n.x * n.y * A2 * color; + sh.c[5] = 1.092548f * n.y * n.z * A2 * color; + sh.c[6] = 0.315392f * (3.0f * n.z * n.z - 1.0f) * A2 * color; + sh.c[7] = 1.092548f * n.x * n.z * A2 * color; + sh.c[8] = 0.546274f * (n.x * n.x - n.y * n.y) * A2 * color; + + return sh; +} + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH and convolves with a given kernel +//------------------------------------------------------------------------------------------------- +SH9Color ProjectOntoSH9(in float3 dir, in float3 color) +{ + return ProjectOntoSH9(dir, color, 1.0f, 1.0f, 1.0f); +} + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto H-basis +//------------------------------------------------------------------------------------------------- +H4 ProjectOntoH4(in float3 dir, in float value) +{ + H4 hBasis; + + // Band 0 + hBasis.x = value * (1.0f / sqrt(2.0f * 3.14159f)); + + // Band 1 + hBasis.y = value * -sqrt(1.5f / 3.14159f) * dir.y; + hBasis.z = value * sqrt(1.5f / 3.14159f) * (2 * dir.z - 1.0f); + hBasis.w = value * -sqrt(1.5f / 3.14159f) * dir.x; + + return hBasis; +} + +//------------------------------------------------------------------------------------------------- +// Computes the dot project of two SH9 RGB vectors +//------------------------------------------------------------------------------------------------- +float3 SHDotProduct(in SH9Color a, in SH9Color b) +{ + float3 result = 0.0f; + + [unroll] + for(uint i = 0; i < 9; ++i) + result += a.c[i] * b.c[i]; + + return result; +} + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH9 and dots it with another SH9 vector +//------------------------------------------------------------------------------------------------- +float3 EvalSH9(in float3 dir, in SH9Color sh) +{ + SH9Color dirSH = ProjectOntoSH9(dir, float3(1.0f, 1.0f, 1.0f)); + return SHDotProduct(dirSH, sh); +} + +//------------------------------------------------------------------------------------------------- +// Projects a direction onto SH9, convolves with a cosine kernel, and dots it with another +// SH9 vector +//------------------------------------------------------------------------------------------------- +float3 EvalSH9Cosine(in float3 dir, in SH9Color sh) +{ + SH9Color dirSH = ProjectOntoSH9(dir, float3(1.0f, 1.0f, 1.0f), CosineA0, CosineA1, CosineA2); + return SHDotProduct(dirSH, sh); +} + +//------------------------------------------------------------------------------------------------- +// Convolves 3-band SH coefficients with a cosine kernel +//------------------------------------------------------------------------------------------------- +SH9Color ConvolveWithCosineKernel(in SH9Color sh) +{ + sh.c[0] *= CosineA0; + + sh.c[1] *= CosineA1; + sh.c[2] *= CosineA1; + sh.c[3] *= CosineA1; + + sh.c[4] *= CosineA2; + sh.c[5] *= CosineA2; + sh.c[6] *= CosineA2; + sh.c[7] *= CosineA2; + sh.c[8] *= CosineA2; + + return sh; +} + +//------------------------------------------------------------------------------------------------- +// Computes the "optimal linear direction" for a set of SH coefficients +//------------------------------------------------------------------------------------------------- +float3 OptimalLinearDirection(in SH9Color sh) +{ + SH4Color sh4 = ConvertToSH4(sh); + return OptimalLinearDirection(sh4); +} + +//------------------------------------------------------------------------------------------------- +// Computes the direction and color of a directional light that approximates a set of SH +// coefficients. Uses Peter Pike-Sloan's method from "Stupid SH Tricks" +//------------------------------------------------------------------------------------------------- +void ApproximateDirectionalLight(in SH9Color sh, out float3 direction, out float3 color) +{ + direction = OptimalLinearDirection(sh); + SH9Color dirSH = ProjectOntoSH9(direction, 1.0f); + dirSH.c[0] = 0.0f; + sh.c[0] = 0.0f; + color = SHDotProduct(dirSH, sh) * 867.0f / (316.0f * Pi); +} + +// == H4 ========================================================================================= + +//------------------------------------------------------------------------------------------------- +// Converts from 3-band SH to 2-band H-Basis. See "Efficient Irradiance Normal Mapping" by +// Ralf Habel and Michael Wimmer for the derivations. +//------------------------------------------------------------------------------------------------- +H4Color ConvertToH4(in SH9Color sh) +{ + const float rt2 = sqrt(2.0f); + const float rt32 = sqrt(3.0f / 2.0f); + const float rt52 = sqrt(5.0f / 2.0f); + const float rt152 = sqrt(15.0f / 2.0f); + const float convMatrix[4][9] = + { + { 1.0f / rt2, 0, 0.5f * rt32, 0, 0, 0, 0, 0, 0 }, + { 0, 1.0f / rt2, 0, 0, 0, (3.0f / 8.0f) * rt52, 0, 0, 0 }, + { 0, 0, 1.0f / (2.0f * rt2), 0, 0, 0, 0.25f * rt152, 0, 0 }, + { 0, 0, 0, 1.0f / rt2, 0, 0, 0, (3.0f / 8.0f) * rt52, 0 } + }; + + H4Color hBasis; + + [unroll] + for(uint row = 0; row < 4; ++row) + { + hBasis.c[row] = 0.0f; + + [unroll] + for(uint col = 0; col < 9; ++col) + hBasis.c[row] += convMatrix[row][col] * sh.c[col]; + } + + return hBasis; +} + +//------------------------------------------------------------------------------------------------- +// Evalutes the 2-band H-Basis coefficients in the given direction +//------------------------------------------------------------------------------------------------- +float EvalH4(in float3 n, in H4 hBasis) +{ + float result = 0.0f; + + // Band 0 + result += hBasis.x * (1.0f / sqrt(2.0f * 3.14159f)); + + // Band 1 + result += hBasis.y * sqrt(1.5f / 3.14159f) * n.y; + result += hBasis.z * sqrt(1.5f / 3.14159f) * (2 * n.z - 1.0f); + result += hBasis.w * sqrt(1.5f / 3.14159f) * n.x; + + return result; +} + +//------------------------------------------------------------------------------------------------- +// Evalutes the 2-band H-Basis coefficients in the given direction +//------------------------------------------------------------------------------------------------- +float3 EvalH4(in float3 n, in H4Color hBasis) +{ + float3 color = 0.0f; + + // Band 0 + color += hBasis.c[0] * (1.0f / sqrt(2.0f * 3.14159f)); + + // Band 1 + color += hBasis.c[1] * sqrt(1.5f / 3.14159f) * n.y; + color += hBasis.c[2] * sqrt(1.5f / 3.14159f) * (2 * n.z - 1.0f); + color += hBasis.c[3] * sqrt(1.5f / 3.14159f) * n.x; + + return color; +} + +//------------------------------------------------------------------------------------------------- +// Evalutes the 2-band H-Basis coefficients in the given direction +//------------------------------------------------------------------------------------------------- +float3 EvalH4(in float3 n, in float3 H0, in float3 H1, in float3 H2, in float3 H3) +{ + H4Color h4Clr; + h4Clr.c[0] = H0; + h4Clr.c[1] = H1; + h4Clr.c[2] = H2; + h4Clr.c[3] = H3; + return EvalH4(n, h4Clr); +} + +// == H6 ========================================================================================= + +//------------------------------------------------------------------------------------------------- +// Converts from 3-band SH to 3-band H-Basis. See "Efficient Irradiance Normal Mapping" by +// Ralf Habel and Michael Wimmer for the derivations. +//------------------------------------------------------------------------------------------------- +H6Color ConvertToH6(in SH9Color sh) +{ + const float rt2 = sqrt(2.0f); + const float rt32 = sqrt(3.0f / 2.0f); + const float rt52 = sqrt(5.0f / 2.0f); + const float rt152 = sqrt(15.0f / 2.0f); + const float convMatrix[6][9] = + { + { 1.0f / rt2, 0, 0.5f * rt32, 0, 0, 0, 0, 0, 0 }, + { 0, 1.0f / rt2, 0, 0, 0, (3.0f / 8.0f) * rt52, 0, 0, 0 }, + { 0, 0, 1.0f / (2.0f * rt2), 0, 0, 0, 0.25f * rt152, 0, 0 }, + { 0, 0, 0, 1.0f / rt2, 0, 0, 0, (3.0f / 8.0f) * rt52, 0 }, + { 0, 0, 0, 0, 1.0f / rt2, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 1.0f / rt2 } + }; + + H6Color hBasis; + + [unroll] + for(uint row = 0; row < 6; ++row) + { + hBasis.c[row] = 0.0f; + + [unroll] + for(uint col = 0; col < 9; ++col) + hBasis.c[row] += convMatrix[row][col] * sh.c[col]; + } + + return hBasis; +} + +//------------------------------------------------------------------------------------------------- +// Evalutes the 3-band H-Basis coefficients in the given direction +//------------------------------------------------------------------------------------------------- +float3 EvalH6(in float3 n, in H6Color hBasis) +{ + float3 color = 0.0f; + + // Band 0 + color += hBasis.c[0] * (1.0f / sqrt(2.0f * 3.14159f)); + + // Band 1 + color += hBasis.c[1] * sqrt(1.5f / 3.14159f) * n.y; + color += hBasis.c[2] * sqrt(1.5f / 3.14159f) * (2 * n.z - 1.0f); + color += hBasis.c[3] * sqrt(1.5f / 3.14159f) * n.x; + + // Band 3 + color += hBasis.c[4] * 0.5f * sqrt(7.5f / 3.14159f) * n.x * n.y; + color += hBasis.c[5] * 0.5f * sqrt(7.5f / 3.14159f) * (n.x * n.x - n.y * n.y); + + return color; +} diff --git a/Shadows/SampleFramework11/Shaders/Skybox.hlsl b/Shadows/SampleFramework11/Shaders/Skybox.hlsl new file mode 100644 index 0000000..686b925 --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/Skybox.hlsl @@ -0,0 +1,173 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Constant buffers +//================================================================================================= + +cbuffer VSConstants : register (cb0) +{ + float4x4 View : packoffset(c0); + float4x4 Projection : packoffset(c4); + float3 Bias : packoffset(c8); +} + +cbuffer GSConstants : register(cb0) +{ + uint RTIndex : packoffset(c0); +} + +cbuffer PSConstants : register (cb0) +{ + float3 SunDirection : packoffset(c0); + uint EnableSun : packoffset(c1); +} + +//================================================================================================= +// Samplers +//================================================================================================= + +TextureCube EnvironmentMap : register(t0); +SamplerState LinearSampler : register(s0); + + +//================================================================================================= +// Input/Output structs +//================================================================================================= + +struct VSInput +{ + float3 PositionOS : POSITION; +}; + +struct VSOutput +{ + float4 PositionCS : SV_Position; + float3 TexCoord : TEXCOORD; + float3 Bias : BIAS; +}; + +struct GSOutput +{ + VSOutput VertexShaderOutput; + + uint RTIndex : SV_RenderTargetArrayIndex; + uint VPIndex : SV_ViewportArrayIndex; +}; + +//================================================================================================= +// Vertex Shader +//================================================================================================= +VSOutput SkyboxVS(in VSInput input) +{ + VSOutput output; + + // Rotate into view-space, centered on the camera + float3 positionVS = mul(input.PositionOS.xyz, (float3x3)View); + + // Transform to clip-space + output.PositionCS = mul(float4(positionVS, 1.0f), Projection); + + // Make a texture coordinate + output.TexCoord = input.PositionOS; + + // Pass along the bias + output.Bias = Bias; + + return output; +} + +//================================================================================================= +// Geometry Shader +//================================================================================================= +[maxvertexcount(3)] +void SkyboxGS(triangle VSOutput Input[3], inout TriangleStream TriStream) +{ + [unroll] + for(uint i = 0; i < 3; ++i) + { + GSOutput output; + output.VertexShaderOutput = Input[i]; + output.RTIndex = RTIndex; + output.VPIndex = RTIndex; + + TriStream.Append(output); + } + + TriStream.RestartStrip(); +} + +//================================================================================================= +// Pixel Shader +//================================================================================================= +float4 SkyboxPS(in VSOutput input) : SV_Target +{ + // Sample the skybox + float3 texColor = EnvironmentMap.Sample(LinearSampler, normalize(input.TexCoord)).rgb; + + // Apply the bias + texColor = texColor * input.Bias; + + return float4(texColor, 1.0f); +} + +//------------------------------------------------------------------------------------------------- +// Calculates the angle between two vectors +//------------------------------------------------------------------------------------------------- +float AngleBetween(in float3 dir0, in float3 dir1) +{ + return acos(dot(dir0, dir1)); +} + +//------------------------------------------------------------------------------------------------- +// Uses the CIE Clear Sky model to compute a color for a pixel, given a direction + sun direction +//------------------------------------------------------------------------------------------------- +float3 CIEClearSky(in float3 dir, in float3 sunDir) +{ + float3 skyDir = float3(dir.x, abs(dir.y), dir.z); + float gamma = AngleBetween(skyDir, sunDir); + float S = AngleBetween(sunDir, float3(0, 1, 0)); + float theta = AngleBetween(skyDir, float3(0, 1, 0)); + + float cosTheta = cos(theta); + float cosS = cos(S); + float cosGamma = cos(gamma); + + float num = (0.91f + 10 * exp(-3 * gamma) + 0.45 * cosGamma * cosGamma) * (1 - exp(-0.32f / cosTheta)); + float denom = (0.91f + 10 * exp(-3 * S) + 0.45 * cosS * cosS) * (1 - exp(-0.32f)); + + float lum = num / max(denom, 0.0001f); + + // Clear Sky model only calculates luminance, so we'll pick a strong blue color for the sky + const float3 SkyColor = float3(0.2f, 0.5f, 1.0f) * 1; + const float3 SunColor = float3(1.0f, 0.8f, 0.3f) * 1500; + const float SunWidth = 0.015f; + + float3 color = SkyColor; + + // Draw a circle for the sun + [flatten] + if (EnableSun) + { + float sunGamma = AngleBetween(dir, sunDir); + color = lerp(SunColor, SkyColor, saturate(abs(sunGamma) / SunWidth)); + } + + return max(color * lum, 0); +} + +//================================================================================================= +// Sky Model Pixel Shader +//================================================================================================= +float4 SkyModelPS(in VSOutput input) : SV_Target +{ + float3 dir = normalize(input.TexCoord); + + return float4(CIEClearSky(dir, SunDirection) * input.Bias, 1.0f); +} diff --git a/Shadows/SampleFramework11/Shaders/Sprite.hlsl b/Shadows/SampleFramework11/Shaders/Sprite.hlsl new file mode 100644 index 0000000..7829a22 --- /dev/null +++ b/Shadows/SampleFramework11/Shaders/Sprite.hlsl @@ -0,0 +1,118 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Constant buffers +//================================================================================================= +cbuffer VSPerBatch : register (b0) +{ + float2 TextureSize : packoffset(c0.x); + float2 ViewportSize : packoffset(c0.z); +} + +cbuffer VSPerInstance : register (b1) +{ + float4x4 Transform : packoffset(c0); + float4 Color : packoffset(c4); + float4 SourceRect : packoffset(c5); +} + +//================================================================================================= +// Samplers +//================================================================================================= +Texture2D SpriteTexture : register(t0); +SamplerState SpriteSampler : register(s0); + + +//================================================================================================= +// Input/Output structs +//================================================================================================= +struct VSInput +{ + float2 Position : POSITION; + float2 TexCoord : TEXCOORD; +}; + +struct VSInputInstanced +{ + float2 Position : POSITION; + float2 TexCoord : TEXCOORD; + float4x4 Transform : TRANSFORM; + float4 Color : COLOR; + float4 SourceRect : SOURCERECT; +}; + +struct VSOutput +{ + float4 Position : SV_Position; + float2 TexCoord : TEXCOORD; + float4 Color : COLOR; +}; + +//------------------------------------------------------------------------------------------------- +// Functionality common to both vertex shaders +//------------------------------------------------------------------------------------------------- +VSOutput SpriteVSCommon(float2 position, + float2 texCoord, + float4x4 transform, + float4 color, + float4 sourceRect) +{ + // Scale the quad so that it's texture-sized + float4 positionSS = float4(position * sourceRect.zw, 0.0f, 1.0f); + + // Apply transforms in screen space + positionSS = mul(positionSS, transform); + + // Scale by the viewport size, flip Y, then rescale to device coordinates + float4 positionDS = positionSS; + positionDS.xy /= ViewportSize; + positionDS = positionDS * 2 - 1; + positionDS.y *= -1; + + // Figure out the texture coordinates + float2 outTexCoord = texCoord; + outTexCoord.xy *= sourceRect.zw / TextureSize; + outTexCoord.xy += sourceRect.xy / TextureSize; + + VSOutput output; + output.Position = positionDS; + output.TexCoord = outTexCoord; + output.Color = color; + + return output; +} + +//================================================================================================= +// Vertex Shader, non-instanced +//================================================================================================= +VSOutput SpriteVS(in VSInput input) +{ + return SpriteVSCommon(input.Position, input.TexCoord, Transform, Color, SourceRect); +} + +//================================================================================================= +// Vertex Shader, instanced +//================================================================================================= +VSOutput SpriteInstancedVS(in VSInputInstanced input) +{ + return SpriteVSCommon(input.Position, input.TexCoord, + transpose(input.Transform), input.Color, input.SourceRect); +} + +//================================================================================================= +// Pixel Shader +//================================================================================================= +float4 SpritePS(in VSOutput input) : SV_Target +{ + float4 texColor = SpriteTexture.Sample(SpriteSampler, input.TexCoord); + texColor = texColor * input.Color; + texColor.rgb *= texColor.a; + return texColor; +} diff --git a/Shadows/SampleFramework11/Skybox.cpp b/Shadows/SampleFramework11/Skybox.cpp new file mode 100644 index 0000000..bb38b66 --- /dev/null +++ b/Shadows/SampleFramework11/Skybox.cpp @@ -0,0 +1,338 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Skybox.h" + +#include "Utility.h" +#include "ShaderCompilation.h" + +namespace SampleFramework11 +{ + +Skybox::Skybox() +{ +} + +Skybox::~Skybox() +{ +} + +void Skybox::Initialize(ID3D11Device* device) +{ + // Load the shaders + vertexShader = CompileVSFromFile(device, L"SampleFramework11\\Shaders\\Skybox.hlsl", "SkyboxVS", "vs_4_0"); + geometryShader = CompileGSFromFile(device, L"SampleFramework11\\Shaders\\Skybox.hlsl", "SkyboxGS", "gs_4_0"); + emPixelShader = CompilePSFromFile(device, L"SampleFramework11\\Shaders\\Skybox.hlsl", "SkyboxPS", "ps_4_0"); + skyPixelShader = CompilePSFromFile(device, L"SampleFramework11\\Shaders\\Skybox.hlsl", "SkyModelPS", "ps_4_0"); + + // Create the input layout + D3D11_INPUT_ELEMENT_DESC layout[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + }; + + ID3DBlob* byteCode = vertexShader->ByteCode; + DXCall(device->CreateInputLayout(layout, 1, byteCode->GetBufferPointer(), byteCode->GetBufferSize(), &inputLayout)); + + // Create and initialize the vertex and index buffers + Float3 verts[NumVertices] = + { + Float3(-1, 1, 1), + Float3(1, 1, 1), + Float3(1, -1, 1), + Float3(-1, -1, 1), + Float3(1, 1, -1), + Float3(-1, 1, -1), + Float3(-1, -1, -1), + Float3(1, -1,- 1), + }; + + D3D11_BUFFER_DESC desc; + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.ByteWidth = sizeof(verts); + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + desc.CPUAccessFlags = 0; + desc.MiscFlags = 0; + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = verts; + initData.SysMemPitch = 0; + initData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&desc, &initData, &vertexBuffer)); + + unsigned short indices[NumIndices] = + { + 0, 1, 2, 2, 3, 0, // Front + 1, 4, 7, 7, 2, 1, // Right + 4, 5, 6, 6, 7, 4, // Back + 5, 0, 3, 3, 6, 5, // Left + 5, 4, 1, 1, 0, 5, // Top + 3, 2, 7, 7, 6, 3 // Bottom + }; + + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.ByteWidth = sizeof(indices); + desc.BindFlags = D3D11_BIND_INDEX_BUFFER; + desc.CPUAccessFlags = 0; + initData.pSysMem = indices; + DXCall(device->CreateBuffer(&desc, &initData, &indexBuffer)); + + // Create the VS constant buffer + desc.Usage = D3D11_USAGE_DYNAMIC; + desc.ByteWidth = CBSize(sizeof(VSConstants)); + desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + DXCall(device->CreateBuffer(&desc, nullptr, &vsConstantBuffer)); + + // Create the GS constant buffer + desc.ByteWidth = CBSize(sizeof(GSConstants)); + DXCall(device->CreateBuffer(&desc, nullptr, &gsConstantBuffer)); + + // Create the PS constant buffer + desc.ByteWidth = CBSize(sizeof(PSConstants)); + DXCall(device->CreateBuffer(&desc, nullptr, &psConstantBuffer)); + + // Create a depth-stencil state + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = true; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + DXCall(device->CreateDepthStencilState(&dsDesc, &dsState)); + + // Create a blend state + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for (UINT i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = false; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + DXCall(device->CreateBlendState(&blendDesc, &blendState)); + + // Create a rasterizer state + D3D11_RASTERIZER_DESC rastDesc; + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 0.0f; + rastDesc.DepthClipEnable = true; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + DXCall(device->CreateRasterizerState(&rastDesc, &rastState)); + + D3D11_SAMPLER_DESC sampDesc; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.BorderColor[0] = 0; + sampDesc.BorderColor[1] = 0; + sampDesc.BorderColor[2] = 0; + sampDesc.BorderColor[3] = 0; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + sampDesc.MaxAnisotropy = 1; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + sampDesc.MinLOD = 0; + sampDesc.MipLODBias = 0; + DXCall(device->CreateSamplerState(&sampDesc, &samplerState)); +} + +void Skybox::RenderCommon( ID3D11DeviceContext* context, + ID3D11ShaderResourceView* environmentMap, + ID3D11PixelShader* ps, + const Float4x4& view, + const Float4x4& projection, + Float3 bias, + bool setStates, + uint32 rtIndex) +{ + if(setStates) + { + float blendFactor[4] = {1, 1, 1, 1}; + context->RSSetState(rastState); + context->OMSetBlendState(blendState, blendFactor, 0xFFFFFFFF); + context->OMSetDepthStencilState(dsState, 0); + context->PSSetSamplers(0, 1, &(samplerState.GetInterfacePtr())); + } + + // Get the viewports + UINT numViewports = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; + D3D11_VIEWPORT oldViewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; + context->RSGetViewports(&numViewports, oldViewports); + + // Set a viewport with MinZ pushed back + D3D11_VIEWPORT newViewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; + for(UINT i = 0; i < numViewports; ++i) + { + newViewports[i] = oldViewports[0]; + newViewports[i].MinDepth = 1.0f; + newViewports[i].MaxDepth = 1.0f; + } + context->RSSetViewports(numViewports, newViewports); + + // Set the input layout + context->IASetInputLayout(inputLayout); + + // Set the vertex buffer + UINT stride = sizeof(XMFLOAT3); + UINT offset = 0; + ID3D11Buffer* vertexBuffers[1] = { vertexBuffer.GetInterfacePtr() }; + context->IASetVertexBuffers(0, 1, vertexBuffers, &stride, &offset); + + // Set the index buffer + context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); + + // Set primitive topology + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Set the shaders + context->VSSetShader(vertexShader, nullptr, 0); + context->PSSetShader(ps, nullptr, 0); + context->GSSetShader(rtIndex != 0 ? geometryShader->Shader() : nullptr, nullptr, 0); + context->DSSetShader(nullptr, nullptr, 0); + context->HSSetShader(nullptr, nullptr, 0); + + // Set the constants + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(vsConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + VSConstants* constants = reinterpret_cast(mapped.pData); + constants->View = Float4x4::Transpose(view); + constants->Projection = Float4x4::Transpose(projection); + constants->Bias = bias; + context->Unmap(vsConstantBuffer, 0); + + ID3D11Buffer* buffers[1] = { vsConstantBuffer }; + context->VSSetConstantBuffers(0, 1, buffers); + + if (rtIndex != 0) + { + DXCall(context->Map(gsConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + GSConstants* gsConstants = reinterpret_cast(mapped.pData); + gsConstants->RTIndex = rtIndex; + context->Unmap(gsConstantBuffer, 0); + buffers[0] = gsConstantBuffer; + context->GSSetConstantBuffers(0, 1, buffers); + } + + // Set the texture + ID3D11ShaderResourceView* srViews[1] = { environmentMap }; + context->PSSetShaderResources(0, 1, srViews); + + // Draw + context->DrawIndexed(NumIndices, 0, 0); + + // Set the viewport back to what it was + context->RSSetViewports(numViewports, oldViewports); +} + +void Skybox::Render(ID3D11DeviceContext* context, + ID3D11ShaderResourceView* environmentMap, + const Float4x4& view, + const Float4x4& projection, + Float3 bias, + bool setStates, + uint32 rtIndex) +{ + D3DPERF_BeginEvent(0xFFFFFFFF, L"Skybox Render"); + + RenderCommon(context, environmentMap, emPixelShader, view, projection, bias, setStates, rtIndex); + + D3DPERF_EndEvent(); +} + +void Skybox::RenderSky( ID3D11DeviceContext* context, + const Float3& sunDirection, + bool enableSun, + const Float4x4& view, + const Float4x4& projection, + Float3 bias, + bool setStates, + uint32 rtIndex) +{ + D3DPERF_BeginEvent(0xFFFFFFFF, L"Skybox Render"); + + // Set the pixel shader constants + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(psConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + PSConstants* constants = reinterpret_cast(mapped.pData); + constants->SunDirection = sunDirection; + constants->EnableSun = enableSun; + context->Unmap(psConstantBuffer, 0); + + ID3D11Buffer* buffers[1] = { psConstantBuffer }; + context->PSSetConstantBuffers(0, 1, buffers); + + RenderCommon(context, nullptr, skyPixelShader, view, projection, bias, setStates, rtIndex); + + D3DPERF_EndEvent(); +} + +const float AngleBetween(const Float3& dir0, const Float3& dir1) +{ + return std::acos(Float3::Dot(dir0, dir1)); +} + +Float3 Skybox::ComputeSkyColor(const Float3& dir, const Float3& sunDir, bool enableSun) +{ + Float3 skyDir = Float3(dir.x, std::abs(dir.y), dir.z); + float gamma = AngleBetween(skyDir, sunDir); + float S = AngleBetween(sunDir, Float3(0, 1, 0)); + float theta = AngleBetween(skyDir, Float3(0, 1, 0)); + + float cosTheta = std::cos(theta); + float cosS = std::cos(S); + float cosGamma = std::cos(gamma); + + float num = (0.91f + 10 * std::exp(-3 * gamma) + 0.45f * cosGamma * cosGamma) * (1 - std::exp(-0.32f / cosTheta)); + float denom = (0.91f + 10 * std::exp(-3 * S) + 0.45f * cosS * cosS) * (1 - std::exp(-0.32f)); + + float lum = num / std::max(denom, 0.0001f); + + // Clear Sky model only calculates luminance, so we'll pick a strong blue color for the sky + const Float3 SkyColor = Float3(0.2f, 0.5f, 1.0f) * 1; + const Float3 SunColor = Float3(1.0f, 0.8f, 0.3f) * 1500; + const float SunWidth = 0.015f; + + Float3 color = SkyColor; + + // Add a circle for the sun + if(enableSun) + { + float sunGamma = AngleBetween(dir, sunDir); + color = Lerp(SunColor, SkyColor, Saturate(abs(sunGamma) / SunWidth)); + } + + color *= lum; + color.x = std::max(color.x, 0.0f); + color.y = std::max(color.y, 0.0f); + color.z = std::max(color.z, 0.0f); + + return color; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Skybox.h b/Shadows/SampleFramework11/Skybox.h new file mode 100644 index 0000000..c468f8d --- /dev/null +++ b/Shadows/SampleFramework11/Skybox.h @@ -0,0 +1,99 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "Math.h" +#include "ShaderCompilation.h" + +namespace SampleFramework11 +{ + +class Skybox +{ + +public: + + Skybox(); + ~Skybox(); + + void Initialize(ID3D11Device* device); + + void Render(ID3D11DeviceContext* context, + ID3D11ShaderResourceView* environmentMap, + const Float4x4& view, + const Float4x4& projection, + Float3 bias = Float3(1, 1, 1), + bool setStates = true, + uint32 rtIndex = 0); + + void RenderSky(ID3D11DeviceContext* context, + const Float3& sunDirection, + bool enableSun, + const Float4x4& view, + const Float4x4& projection, + Float3 bias = Float3(1, 1, 1), + bool setStates = true, + uint32 rtIndex = 0); + + static Float3 ComputeSkyColor(const Float3& dir, const Float3& sunDir, bool enableSun); + +protected: + + void RenderCommon(ID3D11DeviceContext* context, + ID3D11ShaderResourceView* environmentMap, + ID3D11PixelShader* ps, + const Float4x4& view, + const Float4x4& projection, + Float3 bias, + bool setStates, + uint32 rtIndex); + + static const uint64 NumIndices = 36; + static const uint64 NumVertices = 8; + + struct VSConstants + { + Float4x4 View; + Float4x4 Projection; + Float3 Bias; + }; + + struct GSConstants + { + uint32 RTIndex; + }; + + struct PSConstants + { + Float3 SunDirection; + float pad; + uint32 EnableSun; + }; + + VertexShaderPtr vertexShader; + GeometryShaderPtr geometryShader; + PixelShaderPtr emPixelShader; + PixelShaderPtr skyPixelShader; + ID3D11InputLayoutPtr inputLayout; + ID3D11BufferPtr vertexBuffer; + ID3D11BufferPtr indexBuffer; + ID3D11BufferPtr vsConstantBuffer; + ID3D11BufferPtr gsConstantBuffer; + ID3D11BufferPtr psConstantBuffer; + ID3D11DepthStencilStatePtr dsState; + ID3D11BlendStatePtr blendState; + ID3D11RasterizerStatePtr rastState; + ID3D11SamplerStatePtr samplerState; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Slider.cpp b/Shadows/SampleFramework11/Slider.cpp new file mode 100644 index 0000000..c082921 --- /dev/null +++ b/Shadows/SampleFramework11/Slider.cpp @@ -0,0 +1,151 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Slider.h" + +#include "Utility.h" +#include "Input.h" +#include "SpriteRenderer.h" + +namespace SampleFramework11 +{ + +Slider::Slider() : size(200.0f, 25.0f), + minVal(0.0f), + maxVal(100.0f), + numSteps(100), + value(0), + dragging(false), + hover(false), + dragValue(0), + mouseDown(false), + changed(false) +{ +} + +Slider::~Slider() +{ +} + +void Slider::Initialize(ID3D11Device* device, float minVal, float maxVal, float value, const wchar* name) +{ + enabled = true; + + this->minVal = minVal; + this->maxVal = maxVal; + this->value = value; + this->name = name; +} + +void Slider::CalcValue(float normalizedValue) +{ + float stepsValue = normalizedValue * numSteps; + stepsValue = std::floorf(stepsValue); + normalizedValue = stepsValue / numSteps; + value = minVal + ((maxVal - minVal) * normalizedValue); +} + +void Slider::Update(const KeyboardState& kbState, const MouseState& mouseState) +{ + changed = false; + + if(!enabled) + { + dragging = false; + mouseDown = false; + hover = false; + return; + } + + // Figure out the x value of the mouse relative to the slider + float mouseX = mouseState.X - position.x; + float mouseY = mouseState.Y - position.y; + + // Figure out if we're over the knob + XMFLOAT4 knobBounds = GetKnobBounds(); + float left = knobBounds.x - position.x; + float top = knobBounds.y - position.y; + float right = left + knobBounds.z * TextureHeight; + float bottom = top + knobBounds.w * TextureHeight; + bool knobHover = mouseX >= left && mouseX <= right && mouseY >= top && mouseY <= bottom; + hover = knobHover; + + float oldValue = value; + + if (mouseState.LButton.Pressed) + { + bool clicked = !mouseDown; + mouseDown = true; + + if (clicked && knobHover && !dragging) + { + dragPos = XMFLOAT2(mouseX, mouseY); + dragging = true; + dragValue = value; + } + else if (dragging) + { + float offset = (dragValue - minVal) / (maxVal - minVal); + float normalizedValue = Clamp(offset + ((mouseX - dragPos.x) / size.x), 0, 1); + CalcValue(normalizedValue); + } + } + else + { + mouseDown = false; + dragging = false; + } + + if(oldValue != value) + changed = true; +} + +Float4 Slider::GetKnobBounds() +{ + float normalizedValue = (value - minVal) / (maxVal - minVal); + float scaleX = size.y / TextureHeight; + float scaleY = size.y / TextureHeight; + float posX = position.x + (normalizedValue * size.x) - (size.y / 2.0f); + float posY = position.y - (size.y / 2.5f); + return Float4(posX, posY, scaleX, scaleY); +} + +void Slider::Render(SpriteRenderer& renderer, SpriteFont& defaultFont) +{ + value = Clamp(value, minVal, maxVal); + + float alpha = enabled ? 1.0f : 0.35f; + + // Render the bar + Float4x4 transform = Float4x4::ScaleMatrix(Float3(size.x / TextureWidth, size.y / TextureHeight, 1.0f)); + transform._41 = position.x; + transform._42 = position.y; + renderer.Render(barTexture, transform, Float4(1, 1, 1, alpha)); + + // Render the knob + Float4 knobBounds = GetKnobBounds(); + transform = Float4x4::ScaleMatrix(Float3(knobBounds.w, knobBounds.z, 1.0f)); + transform._41 = knobBounds.x; + transform._42 = knobBounds.y; + Float4 knobColor = hover ? Float4(1.5f, 1.5f, 1.5f, alpha) : Float4(1, 1, 1, alpha); + renderer.Render(knobTexture, transform, knobColor); + + transform = Float4x4::TranslationMatrix(Float3(position.x, position.y + size.y - 12.0f, 0)); + renderer.RenderText(font, name.c_str(), transform, Float4(1, 1, 1, alpha)); + + std::wstring valString = ToString(value); + transform = Float4x4::TranslationMatrix(Float3(position.x + size.x + 16.0f, position.y - 4.0f, 0)); + renderer.RenderText(font, valString.c_str(), transform, Float4(1, 1, 1, alpha)); +} + +} diff --git a/Shadows/SampleFramework11/Slider.h b/Shadows/SampleFramework11/Slider.h new file mode 100644 index 0000000..01c55c2 --- /dev/null +++ b/Shadows/SampleFramework11/Slider.h @@ -0,0 +1,75 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "GUIObject.h" +#include "SpriteFont.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +class Slider : public GUIObject +{ + +public: + + Slider(); + ~Slider(); + + void Initialize(ID3D11Device* device, float minVal, float maxVal, float value, const wchar* name); + + void Update(const KeyboardState& kbState, const MouseState& mouseState); + void Render(SpriteRenderer& renderer, SpriteFont& defaultFont); + + float& MinVal() { return minVal; } + float& MaxVal() { return maxVal; } + UINT& NumSteps() { return numSteps; } + std::wstring& Name() { return name; } + + float MinVal() const { return minVal; } + float MaxVal() const { return maxVal; } + UINT NumSteps() const { return numSteps; } + float Value() const { return value; } + std::wstring Name() const { return name; } + bool Changed() const { return changed; } + + void SetValue(float newValue) { value = Clamp(newValue, minVal, maxVal); } + + operator float() const { return Value(); } + + uint32 AsUint() const { return static_cast(Round(Value())); } + +protected: + + static const UINT TextureWidth = 512; + static const UINT TextureHeight = 64; + + Float2 size; + float minVal; + float maxVal; + UINT numSteps; + float value; + std::wstring name; + bool dragging; + float dragValue; + bool hover; + bool mouseDown; + Float2 dragPos; + bool changed; + + Float4 GetKnobBounds(); + void CalcValue(float normalizedValue); +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/SpriteFont.cpp b/Shadows/SampleFramework11/SpriteFont.cpp new file mode 100644 index 0000000..2e39ab2 --- /dev/null +++ b/Shadows/SampleFramework11/SpriteFont.cpp @@ -0,0 +1,300 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "SpriteFont.h" + +#include "Utility.h" + +using namespace Gdiplus; +using std::wstring; +using std::vector; + +namespace SampleFramework11 +{ + +SpriteFont::SpriteFont() + : size(0), + texHeight(0), + spaceWidth(0), + charHeight(0) +{ + +} + +SpriteFont::~SpriteFont() +{ + +} + +void SpriteFont::Initialize(LPCWSTR fontName, float fontSize, UINT fontStyle, bool antiAliased, ID3D11Device* device) +{ + size = fontSize; + + TextRenderingHint hint = antiAliased ? TextRenderingHintAntiAliasGridFit : TextRenderingHintSingleBitPerPixelGridFit; + + // Init GDI+ + ULONG_PTR token = NULL; + GdiplusStartupInput startupInput (NULL, true, true); + GdiplusStartupOutput startupOutput; + GdiPlusCall(GdiplusStartup(&token, &startupInput, &startupOutput)); + + try + { + // Create the font + Gdiplus::Font font(fontName, fontSize, fontStyle, UnitPixel, NULL); + + // Check for error during construction + GdiPlusCall(font.GetLastStatus()); + + // Create a temporary Bitmap and Graphics for figuring out the rough size required + // for drawing all of the characters + int size = static_cast(fontSize * NumChars * 2) + 1; + Bitmap sizeBitmap(size, size, PixelFormat32bppARGB); + GdiPlusCall(sizeBitmap.GetLastStatus()); + + Graphics sizeGraphics(&sizeBitmap); + GdiPlusCall(sizeGraphics.GetLastStatus()); + GdiPlusCall(sizeGraphics.SetTextRenderingHint(hint)); + + charHeight = font.GetHeight(&sizeGraphics) * 1.5f; + + wchar allChars[NumChars + 1]; + for(wchar i = 0; i < NumChars; ++i) + allChars[i] = i + StartChar; + allChars[NumChars] = 0; + + RectF sizeRect; + GdiPlusCall(sizeGraphics.MeasureString(allChars, NumChars, &font, PointF(0, 0), &sizeRect)); + int numRows = static_cast(sizeRect.Width / TexWidth) + 1; + int texHeight = static_cast(numRows * charHeight) + 1; + + // Create a temporary Bitmap and Graphics for drawing the characters one by one + int tempSize = static_cast(fontSize * 2); + Bitmap drawBitmap(tempSize, tempSize, PixelFormat32bppARGB); + GdiPlusCall(drawBitmap.GetLastStatus()); + + Graphics drawGraphics(&drawBitmap); + GdiPlusCall(drawGraphics.GetLastStatus()); + GdiPlusCall(drawGraphics.SetTextRenderingHint(hint)); + + // Create a temporary Bitmap + Graphics for creating a full character set + Bitmap textBitmap (TexWidth, texHeight, PixelFormat32bppARGB); + GdiPlusCall(textBitmap.GetLastStatus()); + + Graphics textGraphics (&textBitmap); + GdiPlusCall(textGraphics.GetLastStatus()); + GdiPlusCall(textGraphics.Clear(Color(0, 255, 255, 255))); + GdiPlusCall(textGraphics.SetCompositingMode(CompositingModeSourceCopy)); + + // Solid brush for text rendering + SolidBrush brush (Color(255, 255, 255, 255)); + GdiPlusCall(brush.GetLastStatus()); + + // Draw all of the characters, and copy them to the full character set + wchar charString [2]; + charString[1] = 0; + int currentX = 0; + int currentY = 0; + for(uint64 i = 0; i < NumChars; ++i) + { + charString[0] = static_cast(i + StartChar); + + // Draw the character + GdiPlusCall(drawGraphics.Clear(Color(0, 255, 255, 255))); + GdiPlusCall(drawGraphics.DrawString(charString, 1, &font, PointF(0, 0), &brush)); + + // Figure out the amount of blank space before the character + int minX = 0; + for(int x = 0; x < tempSize; ++x) + { + for(int y = 0; y < tempSize; ++y) + { + Color color; + GdiPlusCall(drawBitmap.GetPixel(x, y, &color)); + if(color.GetAlpha() > 0) + { + minX = x; + x = tempSize; + break; + } + } + } + + // Figure out the amount of blank space after the character + int maxX = tempSize - 1; + for(int x = tempSize - 1; x >= 0; --x) + { + for(int y = 0; y < tempSize; ++y) + { + Color color; + GdiPlusCall(drawBitmap.GetPixel(x, y, &color)); + if(color.GetAlpha() > 0) + { + maxX = x; + x = -1; + break; + } + } + } + + int charWidth = maxX - minX + 1; + + // Figure out if we need to move to the next row + if (currentX + charWidth >= TexWidth) + { + currentX = 0; + currentY += static_cast(charHeight) + 1; + } + + // Fill out the structure describing the character position + charDescs[i].X = static_cast(currentX); + charDescs[i].Y = static_cast(currentY); + charDescs[i].Width = static_cast(charWidth); + charDescs[i].Height = static_cast(charHeight); + + // Copy the character over + int height = static_cast(charHeight + 1); + GdiPlusCall(textGraphics.DrawImage(&drawBitmap, currentX, currentY, minX, 0, charWidth, height, UnitPixel)); + + currentX += charWidth + 1; + } + + // Figure out the width of a space character + charString[0] = ' '; + charString[1] = 0; + GdiPlusCall(drawGraphics.MeasureString(charString, 1, &font, PointF(0, 0), &sizeRect)); + spaceWidth = sizeRect.Width; + + // Lock the bitmap for direct memory access + BitmapData bmData; + GdiPlusCall(textBitmap.LockBits(&Rect(0, 0, TexWidth, texHeight), ImageLockModeRead, PixelFormat32bppARGB, &bmData)); + + // Create a D3D texture, initalized with the bitmap data + D3D11_TEXTURE2D_DESC texDesc; + texDesc.Width = TexWidth; + texDesc.Height = texHeight; + texDesc.MipLevels = 1; + texDesc.ArraySize = 1; + texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + texDesc.SampleDesc.Count = 1; + texDesc.SampleDesc.Quality = 0; + texDesc.Usage = D3D11_USAGE_IMMUTABLE; + texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + texDesc.CPUAccessFlags = 0; + texDesc.MiscFlags = 0; + + D3D11_SUBRESOURCE_DATA data; + data.pSysMem = bmData.Scan0; + data.SysMemPitch = TexWidth * 4; + data.SysMemSlicePitch = 0; + + DXCall(device->CreateTexture2D(&texDesc, &data, &texture)); + + GdiPlusCall(textBitmap.UnlockBits(&bmData)); + + // Create the shader resource view + D3D11_SHADER_RESOURCE_VIEW_DESC srDesc; + srDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + srDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + srDesc.Texture2D.MipLevels = 1; + srDesc.Texture2D.MostDetailedMip = 0; + + DXCall(device->CreateShaderResourceView(texture, &srDesc, &srView)); + } + catch (GdiPlusException e) + { + // Shutdown GDI+ + if (token != NULL) + GdiplusShutdown(token); + throw e; + } + + // Shutdown GDI+ + GdiplusShutdown(token); +} + +ID3D11ShaderResourceView* SpriteFont::SRView() const +{ + return srView; +} + +const SpriteFont::CharDesc* SpriteFont::CharDescriptors() const +{ + return charDescs; +} + +const SpriteFont::CharDesc& SpriteFont::GetCharDescriptor(wchar character) const +{ + _ASSERT(character >= StartChar && character <= EndChar); + return charDescs[character - StartChar]; +} + +float SpriteFont::Size() const +{ + return size; +} + +UINT SpriteFont::TextureWidth() const +{ + return TexWidth; +} + +UINT SpriteFont::TextureHeight() const +{ + return texHeight; +} + +float SpriteFont::SpaceWidth() const +{ + return spaceWidth; +} + +float SpriteFont::CharHeight() const +{ + return charHeight; +} + +ID3D11Texture2D* SpriteFont::Texture() const +{ + return texture; +} + +Float2 SpriteFont::MeasureText(const wchar* text) const +{ + Float2 size = Float2(0.0f, 0.0f); + Float2 curPos = Float2(0.0f, 0.0f);; + + size_t length = wcslen(text); + + for (uint64 i = 0; i < length; ++i) + { + wchar character = text[i]; + if(character == ' ') + curPos.x += SpaceWidth(); + else if(character == '\n') + { + curPos.y += CharHeight(); + curPos.x = 0; + } + else + { + SpriteFont::CharDesc desc = GetCharDescriptor(character); + curPos.x += desc.Width + 1; + } + + size.x = std::max(curPos.x, size.x); + size.y = std::max(curPos.y, size.y); + } + + return size; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/SpriteFont.h b/Shadows/SampleFramework11/SpriteFont.h new file mode 100644 index 0000000..bb8d79d --- /dev/null +++ b/Shadows/SampleFramework11/SpriteFont.h @@ -0,0 +1,78 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "InterfacePointers.h" +#include "Math.h" + +namespace SampleFramework11 +{ + +class SpriteFont +{ + +public: + + enum FontStyle + { + Regular = 0, + Bold = 1 << 0, + Italic = 1 << 1, + BoldItalic = Bold | Italic, + Underline = 1 << 2, + Strikeout = 1 << 3 + }; + + struct CharDesc + { + float X; + float Y; + float Width; + float Height; + }; + + static const wchar StartChar = '!'; + static const wchar EndChar = 127; + static const uint64 NumChars = EndChar - StartChar; + static const UINT TexWidth = 1024; + + // Lifetime + SpriteFont(); + ~SpriteFont(); + + void Initialize(LPCWSTR fontName, float fontSize, UINT fontStyle, bool antiAliased, ID3D11Device* device); + + Float2 MeasureText(const wchar* text) const; + + // Accessors + ID3D11ShaderResourceView* SRView() const; + const CharDesc* CharDescriptors() const; + const CharDesc& GetCharDescriptor(wchar character) const; + float Size() const; + ID3D11Texture2D* Texture() const; + UINT TextureWidth() const; + UINT TextureHeight() const; + float SpaceWidth() const; + float CharHeight() const; + +protected: + + ID3D11Texture2DPtr texture; + ID3D11ShaderResourceViewPtr srView; + CharDesc charDescs [NumChars]; + float size; + UINT texHeight; + float spaceWidth; + float charHeight; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/SpriteRenderer.cpp b/Shadows/SampleFramework11/SpriteRenderer.cpp new file mode 100644 index 0000000..b87b419 --- /dev/null +++ b/Shadows/SampleFramework11/SpriteRenderer.cpp @@ -0,0 +1,426 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "SpriteRenderer.h" +#include "ShaderCompilation.h" +#include "SpriteFont.h" + +namespace SampleFramework11 +{ + +SpriteRenderer::SpriteRenderer() + : initialized(false) +{ + +} + +SpriteRenderer::~SpriteRenderer() +{ + +} + +void SpriteRenderer::Initialize(ID3D11Device* device) +{ + this->device = device; + + // Load the shaders + vertexShader = CompileVSFromFile(device, L"SampleFramework11\\Shaders\\Sprite.hlsl", "SpriteVS", "vs_4_0"); + vertexShaderInstanced = CompileVSFromFile(device, L"SampleFramework11\\Shaders\\Sprite.hlsl", "SpriteInstancedVS", "vs_4_0"); + pixelShader = CompilePSFromFile(device, L"SampleFramework11\\Shaders\\Sprite.hlsl", "SpritePS", "ps_4_0"); + + // Define the input layouts + D3D11_INPUT_ELEMENT_DESC layout[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 } + }; + + DXCall(device->CreateInputLayout(layout, 2, vertexShader->ByteCode->GetBufferPointer(), + vertexShader->ByteCode->GetBufferSize(), &inputLayout)); + + D3D11_INPUT_ELEMENT_DESC layoutInstanced[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TRANSFORM", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "TRANSFORM", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "TRANSFORM", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "TRANSFORM", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 64, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "SOURCERECT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 80, D3D11_INPUT_PER_INSTANCE_DATA, 1 } + }; + + DXCall(device->CreateInputLayout(layoutInstanced, 8, vertexShaderInstanced->ByteCode->GetBufferPointer(), + vertexShaderInstanced->ByteCode->GetBufferSize(), &inputLayoutInstanced)); + + // Create the vertex buffer + SpriteVertex verts[] = + { + { Float2(0.0f, 0.0f), Float2(0.0f, 0.0f) }, + { Float2(1.0f, 0.0f), Float2(1.0f, 0.0f) }, + { Float2(1.0f, 1.0f), Float2(1.0f, 1.0f) }, + { Float2(0.0f, 1.0f), Float2(0.0f, 1.0f) } + }; + + D3D11_BUFFER_DESC desc; + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.ByteWidth = sizeof(SpriteVertex) * 4; + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + desc.CPUAccessFlags = 0; + desc.MiscFlags = 0; + + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = verts; + initData.SysMemPitch = 0; + initData.SysMemSlicePitch = 0; + DXCall(device->CreateBuffer(&desc, &initData, &vertexBuffer)); + + // Create the instance data buffer + desc.Usage = D3D11_USAGE_DYNAMIC; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + desc.ByteWidth = sizeof(SpriteDrawData) * MaxBatchSize; + DXCall(device->CreateBuffer(&desc, nullptr, &instanceDataBuffer)); + + // Create the index buffer + uint16 indices[] = { 0, 1, 2, 3, 0, 2 }; + desc.Usage = D3D11_USAGE_IMMUTABLE; + desc.ByteWidth = sizeof(uint16) * 6; + desc.BindFlags = D3D11_BIND_INDEX_BUFFER; + desc.CPUAccessFlags = 0; + initData.pSysMem = indices; + DXCall(device->CreateBuffer(&desc, &initData, &indexBuffer)); + + // Create our constant buffers + desc.Usage = D3D11_USAGE_DYNAMIC; + desc.ByteWidth = CBSize(sizeof(VSPerBatchCB)); + desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + DXCall(device->CreateBuffer(&desc, nullptr, &vsPerBatchCB)); + + desc.ByteWidth = CBSize(sizeof(SpriteDrawData)); + DXCall(device->CreateBuffer(&desc, nullptr, &vsPerInstanceCB)); + + // Create our states + D3D11_RASTERIZER_DESC rastDesc; + rastDesc.AntialiasedLineEnable = false; + rastDesc.CullMode = D3D11_CULL_NONE; + rastDesc.DepthBias = 0; + rastDesc.DepthBiasClamp = 1.0f; + rastDesc.DepthClipEnable = false; + rastDesc.FillMode = D3D11_FILL_SOLID; + rastDesc.FrontCounterClockwise = false; + rastDesc.MultisampleEnable = true; + rastDesc.ScissorEnable = false; + rastDesc.SlopeScaledDepthBias = 0; + DXCall(device->CreateRasterizerState(&rastDesc, &rastState)); + + D3D11_BLEND_DESC blendDesc; + blendDesc.AlphaToCoverageEnable = false; + blendDesc.IndependentBlendEnable = false; + for(UINT i = 0; i < 8; ++i) + { + blendDesc.RenderTarget[i].BlendEnable = true; + blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; + blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; + blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE; + blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; + } + DXCall(device->CreateBlendState(&blendDesc, &alphaBlendState)); + + D3D11_DEPTH_STENCIL_DESC dsDesc; + dsDesc.DepthEnable = false; + dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + dsDesc.DepthFunc = D3D11_COMPARISON_LESS; + dsDesc.StencilEnable = false; + dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; + dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; + dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; + dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_REPLACE; + dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; + dsDesc.BackFace = dsDesc.FrontFace; + DXCall(device->CreateDepthStencilState(&dsDesc, &dsState)); + + // linear filtering + D3D11_SAMPLER_DESC sampDesc; + sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + sampDesc.BorderColor[0] = 0; + sampDesc.BorderColor[1] = 0; + sampDesc.BorderColor[2] = 0; + sampDesc.BorderColor[3] = 0; + sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + sampDesc.MaxAnisotropy = 1; + sampDesc.MaxLOD = D3D11_FLOAT32_MAX; + sampDesc.MinLOD = 0; + sampDesc.MipLODBias = 0; + DXCall(device->CreateSamplerState(&sampDesc, &linearSamplerState)); + + // point filtering + sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + DXCall(device->CreateSamplerState(&sampDesc, &pointSamplerState)); + + initialized = true; +} + +void SpriteRenderer::Begin(ID3D11DeviceContext* deviceContext, FilterMode filterMode) +{ + _ASSERT(initialized); + _ASSERT(!context); + context = deviceContext; + + D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer Begin/End"); + + // Set the index buffer + context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); + + // Set primitive topology + context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // Set the states + float blendFactor[4] = {1, 1, 1, 1}; + context->RSSetState(rastState); + context->OMSetBlendState(alphaBlendState, blendFactor, 0xFFFFFFFF); + context->OMSetDepthStencilState(dsState, 0); + + if (filterMode == Linear) + context->PSSetSamplers(0, 1, &(linearSamplerState.GetInterfacePtr())); + else if (filterMode == Point) + context->PSSetSamplers(0, 1, &(pointSamplerState.GetInterfacePtr())); + + // Set the shaders + context->PSSetShader(pixelShader, nullptr, 0); + context->GSSetShader(nullptr, nullptr, 0); + context->DSSetShader(nullptr, nullptr, 0); + context->HSSetShader(nullptr, nullptr, 0); +} + +D3D11_TEXTURE2D_DESC SpriteRenderer::SetPerBatchData(ID3D11ShaderResourceView* texture) +{ + // Set per-batch constants + VSPerBatchCB perBatch; + + // Get the viewport dimensions + UINT numViewports = 1; + D3D11_VIEWPORT vp; + context->RSGetViewports(&numViewports, &vp); + perBatch.ViewportSize = Float2(static_cast(vp.Width), static_cast(vp.Height)); + + D3D11_TEXTURE2D_DESC desc; + + // Get the size of the texture + if(texture) + { + ID3D11Resource* resource; + ID3D11Texture2DPtr texResource; + texture->GetResource(&resource); + texResource.Attach(reinterpret_cast(resource)); + texResource->GetDesc(&desc); + perBatch.TextureSize = Float2(static_cast(desc.Width), static_cast(desc.Height)); + } + else + { + perBatch.TextureSize = Float2(1.0f, 1.0f); + desc.Width = 1; + desc.Height = 1; + } + + // Copy it into the buffer + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(vsPerBatchCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + CopyMemory(mapped.pData, &perBatch, sizeof(VSPerBatchCB)); + context->Unmap(vsPerBatchCB, 0); + + return desc; +} + +void SpriteRenderer::Render(ID3D11ShaderResourceView* texture, + const Float4x4& transform, + const Float4& color, + const Float4* drawRect) +{ + _ASSERT(context); + _ASSERT(initialized); + + D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer Render"); + + // Set the vertex shader + context->VSSetShader(vertexShader, nullptr, 0); + + // Set the input layout + context->IASetInputLayout(inputLayout); + + // Set the vertex buffer + UINT stride = sizeof(SpriteVertex); + UINT offset = 0; + ID3D11Buffer* vb = vertexBuffer.GetInterfacePtr(); + context->IASetVertexBuffers(0, 1, &vb, &stride, &offset); + + // Set per-batch constants + D3D11_TEXTURE2D_DESC desc = SetPerBatchData(texture); + + // Set per-instance data + SpriteDrawData perInstance; + perInstance.Transform = Float4x4::Transpose(transform); + perInstance.Color = color; + + // Draw rect + if(drawRect == nullptr) + perInstance.DrawRect = Float4(0, 0, static_cast(desc.Width), static_cast(desc.Height)); + else + { + _ASSERT(drawRect->x >= 0 && drawRect->x < desc.Width); + _ASSERT(drawRect->y >= 0 && drawRect->y < desc.Height); + _ASSERT(drawRect->z > 0 && drawRect->x + drawRect->z < desc.Width); + _ASSERT(drawRect->w > 0 && drawRect->y + drawRect->w < desc.Height); + perInstance.DrawRect = *drawRect; + } + + // Copy in the buffer data + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(vsPerInstanceCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + CopyMemory(mapped.pData, &perInstance, sizeof(SpriteDrawData)); + context->Unmap(vsPerInstanceCB, 0); + + ID3D11Buffer* buffers [2] = { vsPerBatchCB, vsPerInstanceCB }; + context->VSSetConstantBuffers(0, 2, buffers); + + // Set the texture + context->PSSetShaderResources(0, 1, &texture); + + context->DrawIndexed(6, 0, 0); + + D3DPERF_EndEvent(); +} + +void SpriteRenderer::RenderBatch(ID3D11ShaderResourceView* texture, + const SpriteDrawData* drawData, + uint64 numSprites) +{ + _ASSERT(context); + _ASSERT(initialized); + + D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer RenderBatch"); + + // Set the vertex shader + context->VSSetShader(vertexShaderInstanced, nullptr, 0); + + // Set the input layout + context->IASetInputLayout(inputLayoutInstanced); + + // Set per-batch constants + D3D11_TEXTURE2D_DESC desc = SetPerBatchData(texture); + + // Make sure the draw rects are all valid + for (uint64 i = 0; i < numSprites; ++i) + { + Float4 drawRect = drawData[i].DrawRect; + _ASSERT(drawRect.x >= 0 && drawRect.x < desc.Width); + _ASSERT(drawRect.y >= 0 && drawRect.y < desc.Height); + _ASSERT(drawRect.z > 0 && drawRect.x + drawRect.z <= desc.Width); + _ASSERT(drawRect.w > 0 && drawRect.y + drawRect.w <= desc.Height); + } + + uint64 numSpritesToDraw = std::min(numSprites, MaxBatchSize); + + // Copy in the instance data + D3D11_MAPPED_SUBRESOURCE mapped; + DXCall(context->Map(instanceDataBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); + CopyMemory(mapped.pData, drawData, static_cast(sizeof(SpriteDrawData) * numSpritesToDraw)); + context->Unmap(instanceDataBuffer, 0); + + // Set the constant buffer + ID3D11Buffer* constantBuffers [1] = { vsPerBatchCB }; + context->VSSetConstantBuffers(0, 1, constantBuffers); + + // Set the vertex buffers + UINT strides [2] = { sizeof(SpriteVertex), sizeof(SpriteDrawData) }; + UINT offsets [2] = { 0, 0 }; + ID3D11Buffer* vertexBuffers [2] = { vertexBuffer, instanceDataBuffer }; + context->IASetVertexBuffers(0, 2, vertexBuffers, strides, offsets); + + // Set the texture + context->PSSetShaderResources(0, 1, &texture); + + // Draw + context->DrawIndexedInstanced(6, static_cast(numSpritesToDraw), 0, 0, 0); + + D3DPERF_EndEvent(); + + // If there's any left to be rendered, do it recursively + if(numSprites > numSpritesToDraw) + RenderBatch(texture, drawData + numSpritesToDraw, numSprites - numSpritesToDraw); +} + +void SpriteRenderer::RenderText(const SpriteFont& font, + const wchar* text, + const Float4x4& transform, + const Float4& color) +{ + D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer RenderText"); + + size_t length = wcslen(text); + + Float4x4 textTransform; + + uint64 numCharsToDraw = std::min(length, MaxBatchSize); + uint64 currentDraw = 0; + for(uint64 i = 0; i < numCharsToDraw; ++i) + { + wchar character = text[i]; + if(character == ' ') + textTransform._41 += font.SpaceWidth(); + else if(character == '\n') + { + textTransform._42 += font.CharHeight(); + textTransform._41 = 0; + } + else + { + SpriteFont::CharDesc desc = font.GetCharDescriptor(character); + + textDrawData[currentDraw].Transform = textTransform * transform; + textDrawData[currentDraw].Color = color; + textDrawData[currentDraw].DrawRect.x = desc.X; + textDrawData[currentDraw].DrawRect.y = desc.Y; + textDrawData[currentDraw].DrawRect.z = desc.Width; + textDrawData[currentDraw].DrawRect.w = desc.Height; + currentDraw++; + + textTransform._41 += desc.Width + 1; + } + } + + // Submit a batch + RenderBatch(font.SRView(), textDrawData, currentDraw); + + D3DPERF_EndEvent(); + + if(length > numCharsToDraw) + RenderText(font, text + numCharsToDraw, textTransform, color); +} + +void SpriteRenderer::End() +{ + _ASSERT(context); + _ASSERT(initialized); + context = nullptr; + + D3DPERF_EndEvent(); +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/SpriteRenderer.h b/Shadows/SampleFramework11/SpriteRenderer.h new file mode 100644 index 0000000..77e0755 --- /dev/null +++ b/Shadows/SampleFramework11/SpriteRenderer.h @@ -0,0 +1,109 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Exceptions.h" +#include "Utility.h" +#include "InterfacePointers.h" +#include "Math.h" +#include "ShaderCompilation.h" + +namespace SampleFramework11 +{ + +class SpriteFont; + +class SpriteRenderer +{ + +public: + + enum FilterMode + { + DontSet = 0, + Linear = 1, + Point = 2 + }; + + static const uint64 MaxBatchSize = 1000; + + struct SpriteDrawData + { + Float4x4 Transform; + Float4 Color; + Float4 DrawRect; + }; + + SpriteRenderer(); + ~SpriteRenderer(); + + void Initialize(ID3D11Device* device); + + void Begin(ID3D11DeviceContext* deviceContext, FilterMode filterMode = DontSet); + + void Render(ID3D11ShaderResourceView* texture, + const Float4x4& transform, + const Float4& color = Float4(1, 1, 1, 1), + const Float4* drawRect = NULL); + + void RenderBatch(ID3D11ShaderResourceView* texture, + const SpriteDrawData* drawData, + uint64 numSprites); + + void RenderText(const SpriteFont& font, + const wchar* text, + const Float4x4& transform, + const Float4& color = Float4(1, 1, 1, 1)); + + void End(); + +protected: + + D3D11_TEXTURE2D_DESC SetPerBatchData(ID3D11ShaderResourceView* texture); + + ID3D11DevicePtr device; + VertexShaderPtr vertexShader; + VertexShaderPtr vertexShaderInstanced; + PixelShaderPtr pixelShader; + ID3D11BufferPtr vertexBuffer; + ID3D11BufferPtr indexBuffer; + ID3D11BufferPtr vsPerBatchCB; + ID3D11BufferPtr vsPerInstanceCB; + ID3D11BufferPtr instanceDataBuffer; + ID3D11InputLayoutPtr inputLayout; + ID3D11InputLayoutPtr inputLayoutInstanced; + ID3D11DeviceContextPtr context; + + ID3D11RasterizerStatePtr rastState; + ID3D11DepthStencilStatePtr dsState; + ID3D11BlendStatePtr alphaBlendState; + ID3D11SamplerStatePtr linearSamplerState; + ID3D11SamplerStatePtr pointSamplerState; + + bool initialized; + + SpriteDrawData textDrawData [MaxBatchSize]; + + struct SpriteVertex + { + Float2 Position; + Float2 TexCoord; + }; + + struct VSPerBatchCB + { + Float2 TextureSize; + Float2 ViewportSize; + }; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/TextGUI.cpp b/Shadows/SampleFramework11/TextGUI.cpp new file mode 100644 index 0000000..0f626f6 --- /dev/null +++ b/Shadows/SampleFramework11/TextGUI.cpp @@ -0,0 +1,87 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "TextGUI.h" + +#include "Utility.h" +#include "SpriteRenderer.h" +#include "Input.h" + +namespace SampleFramework11 +{ + +// == TextGUI ===================================================================================== + +TextGUI::TextGUI(const wchar* name, uint32 value, uint32 numValues, const wchar** valueNames, KeyboardState::Keys toggleKey) + : name(name), value(value), numValues(numValues), toggleKey(toggleKey), changed(false) +{ + _ASSERT(numValues > 0); + _ASSERT(value < numValues); + SetNames(valueNames); +} + +TextGUI::TextGUI(const wchar* name, uint32 value, uint32 numValues, KeyboardState::Keys toggleKey) + : name(name), value(value), numValues(numValues), toggleKey(toggleKey), changed(false) +{ + _ASSERT(numValues > 0); + _ASSERT(value < numValues); +} + +void TextGUI::SetNames(const wchar** valueNames) +{ + for(uint32 i = 0; i < numValues; ++i) + this->valueNames.push_back(valueNames[i]); +} + +void TextGUI::Update(const KeyboardState& kbState, const MouseState& mouseState) +{ + changed = false; + + if(kbState.RisingEdge(toggleKey)) + { + if(kbState.IsKeyDown(KeyboardState::RightShift)) + { + INT32 value_ = value; + value_ -= 1; + if(value_ == -1) + value_ = numValues - 1; + value = value_; + } + else + value = (value + 1) % numValues; + changed = true; + } +} + +void TextGUI::Render(SpriteRenderer& renderer, SpriteFont& defaultFont) +{ + XMMATRIX translation = XMMatrixTranslation(position.x, position.y, 0.0f); + std::wstring text = name; + text += L"("; + text += static_cast(toggleKey); + text += L"): "; + text += valueNames[value]; + renderer.RenderText(defaultFont, text.c_str(), translation, XMFLOAT4(1, 1, 0, 1)); +} + +// == BoolGUI ===================================================================================== + +static const wchar* BoolNames[2] = { L"Disabled", L"Enabled" }; + +BoolGUI::BoolGUI(const wchar* name, bool value, KeyboardState::Keys toggleKey) + : TextGUI(name, value, 2, BoolNames, toggleKey) +{ +} + + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/TextGUI.h b/Shadows/SampleFramework11/TextGUI.h new file mode 100644 index 0000000..9748554 --- /dev/null +++ b/Shadows/SampleFramework11/TextGUI.h @@ -0,0 +1,55 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "GUIObject.h" +#include "Input.h" + +namespace SampleFramework11 +{ + +class TextGUI : public GUIObject +{ + +public: + + void Update(const KeyboardState& kbState, const MouseState& mouseState); + void Render(SpriteRenderer& renderer, SpriteFont& defaultFont); + + operator uint32() const { return value; } + bool Changed() const { return changed; } + void SetValue(uint32 newValue) { value = std::min(newValue, numValues - 1); } + const std::wstring& ValueName() const { return valueNames[value]; } + +protected: + + TextGUI(const wchar* name, uint32 value, uint32 numValues, const wchar** valueNames, KeyboardState::Keys toggleKey); + TextGUI(const wchar* name, uint32 value, uint32 numValues, KeyboardState::Keys toggleKey); + void SetNames(const wchar** valueNames); + + std::wstring name; + uint32 value; + uint32 numValues; + std::vector valueNames; + KeyboardState::Keys toggleKey; + bool changed; +}; + +class BoolGUI : public TextGUI +{ + +public: + + BoolGUI(const wchar* name, bool value, KeyboardState::Keys toggleKey); +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Timer.cpp b/Shadows/SampleFramework11/Timer.cpp new file mode 100644 index 0000000..2e6f7db --- /dev/null +++ b/Shadows/SampleFramework11/Timer.cpp @@ -0,0 +1,174 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Timer.h" + +#include "Utility.h" + +namespace SampleFramework11 +{ + +Timer::Timer() +{ + // Query for the performance counter frequency + LARGE_INTEGER largeInt; + Win32Call(QueryPerformanceFrequency(&largeInt)); + frequency = largeInt.QuadPart; + frequencyD = static_cast(frequency); + + // Init the elapsed time + Win32Call(QueryPerformanceCounter(&largeInt)); + startTime = largeInt.QuadPart; + elapsed = largeInt.QuadPart - startTime; + elapsedF = static_cast(elapsed); + elapsedSeconds = elapsed / frequency; + elapsedSecondsD = elapsed / frequencyD; + elapsedSecondsF = static_cast(elapsedSecondsD); + elapsedMilliseconds = static_cast(elapsedSecondsD * 1000); + elapsedMillisecondsD = elapsedSecondsD * 1000; + elapsedMillisecondsF = static_cast(elapsedMillisecondsD); + elapsedMicroseconds = static_cast(elapsedMillisecondsD * 1000); + elapsedMicrosecondsD = elapsedMillisecondsD * 1000; + elapsedMicrosecondsF = static_cast(elapsedMillisecondsD); + + delta = 0; + deltaF = 0; + deltaMilliseconds = 0; + deltaMillisecondsF = 0; + deltaMicroseconds = 0; + deltaMicrosecondsF = 0; +} + +Timer::~Timer() +{ +} + +void Timer::Update() +{ + LARGE_INTEGER largeInt; + Win32Call(QueryPerformanceCounter(&largeInt)); + int64 currentTime = largeInt.QuadPart - startTime; + delta = currentTime - elapsed; + deltaF = static_cast(deltaF); + deltaSeconds = delta / frequency; + deltaSecondsD = delta / frequencyD; + deltaSecondsF = static_cast(deltaSecondsD); + deltaMillisecondsD = deltaSecondsD * 1000; + deltaMilliseconds = static_cast(deltaMillisecondsD); + deltaMillisecondsF = static_cast(deltaMillisecondsD); + deltaMicrosecondsD = deltaMillisecondsD * 1000; + deltaMicroseconds = static_cast(deltaMicrosecondsD); + deltaMicrosecondsF = static_cast(deltaMicrosecondsD); + + elapsed = currentTime; + elapsedF = static_cast(elapsed); + elapsedSeconds = elapsed / frequency; + elapsedSecondsD = elapsed / frequencyD; + elapsedSecondsF = static_cast(elapsedSecondsD); + elapsedMilliseconds = static_cast(elapsedSecondsD * 1000); + elapsedMillisecondsD = elapsedSecondsD * 1000; + elapsedMillisecondsF = static_cast(elapsedMillisecondsD); + elapsedMicroseconds = static_cast(elapsedMillisecondsD * 1000); + elapsedMicrosecondsD = elapsedMillisecondsD * 1000; + elapsedMicrosecondsF = static_cast(elapsedMillisecondsD); +} + +int64 Timer::ElapsedSeconds() const +{ + return elapsedSeconds; +} + +float Timer::ElapsedSecondsF() const +{ + return elapsedSecondsF; +} + +double Timer::ElapsedSecondsD() const +{ + return elapsedSecondsD; +} + +int64 Timer::DeltaSeconds() const +{ + return deltaSeconds; +} + +float Timer::DeltaSecondsF() const +{ + return deltaSecondsF; +} + +double Timer::DeltaSecondsD() const +{ + return deltaSecondsD; +} + +int64 Timer::ElapsedMilliseconds() const +{ + return elapsedMilliseconds; +} + +float Timer::ElapsedMillisecondsF() const +{ + return elapsedMillisecondsF; +} + +double Timer::ElapsedMillisecondsD() const +{ + return elapsedMillisecondsD; +} + +int64 Timer::DeltaMilliseconds() const +{ + return deltaMilliseconds; +} + +float Timer::DeltaMillisecondsF() const +{ + return deltaMillisecondsF; +} + +double Timer::DeltaMillisecondsD() const +{ + return deltaMillisecondsD; +} + +int64 Timer::ElapsedMicroseconds() const +{ + return elapsedMicroseconds; +} + +float Timer::ElapsedMicrosecondsF() const +{ + return elapsedMicrosecondsF; +} + +double Timer::ElapsedMicrosecondsD() const +{ + return elapsedMicrosecondsD; +} + +int64 Timer::DeltaMicroseconds() const +{ + return deltaMicroseconds; +} + +float Timer::DeltaMicrosecondsF() const +{ + return deltaMicrosecondsF; +} + +double Timer::DeltaMicrosecondsD() const +{ + return deltaMicrosecondsD; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Timer.h b/Shadows/SampleFramework11/Timer.h new file mode 100644 index 0000000..bb3a9bb --- /dev/null +++ b/Shadows/SampleFramework11/Timer.h @@ -0,0 +1,92 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +namespace SampleFramework11 +{ + +class Timer +{ + +public: + + Timer(); + ~Timer(); + + void Update(); + + int64 ElapsedSeconds() const; + float ElapsedSecondsF() const; + double ElapsedSecondsD() const; + int64 DeltaSeconds() const; + float DeltaSecondsF() const; + double DeltaSecondsD() const; + + int64 ElapsedMilliseconds() const; + float ElapsedMillisecondsF() const; + double ElapsedMillisecondsD() const; + int64 DeltaMilliseconds() const; + float DeltaMillisecondsF() const; + double DeltaMillisecondsD() const; + + int64 ElapsedMicroseconds() const; + float ElapsedMicrosecondsF() const; + double ElapsedMicrosecondsD() const; + int64 DeltaMicroseconds() const; + float DeltaMicrosecondsF() const; + double DeltaMicrosecondsD() const; + +protected: + + int64 startTime; + + int64 frequency; + double frequencyD; + + int64 elapsed; + int64 delta; + + float elapsedF; + float deltaF; + + double elapsedD; + double deltaD; + + int64 elapsedSeconds; + int64 deltaSeconds; + + float elapsedSecondsF; + float deltaSecondsF; + + double elapsedSecondsD; + double deltaSecondsD; + + int64 elapsedMilliseconds; + int64 deltaMilliseconds; + + float elapsedMillisecondsF; + float deltaMillisecondsF; + + double elapsedMillisecondsD; + double deltaMillisecondsD; + + int64 elapsedMicroseconds; + int64 deltaMicroseconds; + + float elapsedMicrosecondsF; + float deltaMicrosecondsF; + + double elapsedMicrosecondsD; + double deltaMicrosecondsD; +}; + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/TwHelper.cpp b/Shadows/SampleFramework11/TwHelper.cpp new file mode 100644 index 0000000..35e9405 --- /dev/null +++ b/Shadows/SampleFramework11/TwHelper.cpp @@ -0,0 +1,313 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" +#include "TwHelper.h" +#include "Utility.h" + +namespace SampleFramework11 +{ + +namespace TwHelper +{ + +// Variable parameters +void SetLabel(TwBar* bar, const char* varName, const char* label) +{ + TwCall(TwSetParam(bar, varName, "label", TW_PARAM_CSTRING, 1, label)); +} + +void SetHelpText(TwBar* bar, const char* varName, const char* helpText) +{ + TwCall(TwSetParam(bar, varName, "help", TW_PARAM_CSTRING, 1, helpText)); +} + +void SetGroup(TwBar* bar, const char* varName, const char* group) +{ + TwCall(TwSetParam(bar, varName, "group", TW_PARAM_CSTRING, 1, group)); +} + +void SetVisible(TwBar* bar, const char* varName, bool32 visible) +{ + TwCall(TwSetParam(bar, varName, "visible", TW_PARAM_INT32, 1, &visible)); +} + +void SetReadOnly(TwBar* bar, const char* varName, bool32 readOnly) +{ + TwCall(TwSetParam(bar, varName, "readonly", TW_PARAM_INT32, 1, &readOnly)); +} + +void SetMinMax(TwBar* bar, const char* varName, float min, float max) +{ + TwCall(TwSetParam(bar, varName, "min", TW_PARAM_FLOAT, 1, &min)); + TwCall(TwSetParam(bar, varName, "max", TW_PARAM_FLOAT, 1, &max)); +} + +void SetMinMax(TwBar* bar, const char* varName, int32 min, int32 max) +{ + TwCall(TwSetParam(bar, varName, "min", TW_PARAM_INT32, 1, &min)); + TwCall(TwSetParam(bar, varName, "max", TW_PARAM_INT32, 1, &max)); +} + +void SetStep(TwBar* bar, const char* varName, float step) +{ + TwCall(TwSetParam(bar, varName, "step", TW_PARAM_FLOAT, 1, &step)); +} + +void SetPrecision(TwBar* bar, const char* varName, int32 precision) +{ + TwCall(TwSetParam(bar, varName, "precision", TW_PARAM_INT32, 1, &precision)); +} + +void SetHexidecimal(TwBar* bar, const char* varName, bool32 hex) +{ + TwCall(TwSetParam(bar, varName, "hexa", TW_PARAM_INT32, 1, &hex)); +} + +void SetShortcutKey(TwBar* bar, const char* varName, const char* shortcutKey) +{ + TwCall(TwSetParam(bar, varName, "key", TW_PARAM_CSTRING, 1, shortcutKey)); +} + +void SetShortcutKeyIncrement(TwBar* bar, const char* varName, const char* shortcutKey) +{ + TwCall(TwSetParam(bar, varName, "keyincr", TW_PARAM_CSTRING, 1, shortcutKey)); +} + +void SetShortcutKeyDecrement(TwBar* bar, const char* varName, const char* shortcutKey) +{ + TwCall(TwSetParam(bar, varName, "keydecr", TW_PARAM_CSTRING, 1, shortcutKey)); +} + +void SetBoolLabels(TwBar* bar, const char* varName, const char* falseLabel, const char* trueLabel) +{ + TwCall(TwSetParam(bar, varName, "false", TW_PARAM_CSTRING, 1, falseLabel)); + TwCall(TwSetParam(bar, varName, "true", TW_PARAM_CSTRING, 1, trueLabel)); +} + +void SetUseAlphaChannel(TwBar* bar, const char* varName, bool32 useAlpha) +{ + TwCall(TwSetParam(bar, varName, "coloralpha", TW_PARAM_INT32, 1, &useAlpha)); +} + +void SetColorOrder(TwBar* bar, const char* varName, ColorOrder order) +{ + static const char* ColorOrderStrings[] = { "rgba", "bgra" }; + TwCall(TwSetParam(bar, varName, "colororder", TW_PARAM_CSTRING, 1, ColorOrderStrings[uint64(order)])); +} + +void SetColorMode(TwBar* bar, const char* varName, ColorMode mode) +{ + static const char* ColorModeStrings[] = { "rgb", "hls" }; + TwCall(TwSetParam(bar, varName, "colormode", TW_PARAM_CSTRING, 1, ColorModeStrings[uint64(mode)])); +} + +void SetUseArrowMode(TwBar* bar, const char* varName, bool32 useArrowMode, Float3 initialDirection) +{ + if(useArrowMode) + { + std::string dirString = "'" + ToAnsiString(initialDirection.x) + " " + ToAnsiString(initialDirection.y) + + " " + ToAnsiString(initialDirection.z) + "'"; + TwCall(TwSetParam(bar, varName, "arrow", TW_PARAM_CSTRING, 1, dirString.c_str())); + } + else + TwCall(TwSetParam(bar, varName, "arrow", TW_PARAM_CSTRING, 1, "0")); +} + +void SetArrowColor(TwBar* bar, const char* varName, Float3 color) +{ + int32 rgb[3] = { int32(Saturate(color.x) * 255.0f), + int32(Saturate(color.y) * 255.0f), + int32(Saturate(color.z) * 255.0f) }; + TwCall(TwSetParam(bar, varName, "arrowcolor", TW_PARAM_INT32, 3, rgb)); +} + +void SetAxisMapping(TwBar* bar, const char* varName, Axis xAxis, Axis yAxis, Axis zAxis) +{ + static const char* AxisStrings[] = { "x", "-x", "y", "-y", "z", "-z" }; + TwCall(TwSetParam(bar, varName, "axisx", TW_PARAM_CSTRING, 1, AxisStrings[uint64(xAxis)])); + TwCall(TwSetParam(bar, varName, "axisy", TW_PARAM_CSTRING, 1, AxisStrings[uint64(yAxis)])); + TwCall(TwSetParam(bar, varName, "axisz", TW_PARAM_CSTRING, 1, AxisStrings[uint64(zAxis)])); +} + +void SetShowNumericalValue(TwBar* bar, const char* varName, bool32 showValue) +{ + TwCall(TwSetParam(bar, varName, "showval", TW_PARAM_INT32, 1, &showValue)); +} + + +// TweakBar parameters +void SetLabel(TwBar* bar, const char* label) +{ + SetLabel(bar, nullptr, label); +} + +void SetHelpText(TwBar* bar, const char* helpText) +{ + SetHelpText(bar, nullptr, helpText); +} + +void SetColor(TwBar* bar, Float3 color) +{ + int32 rgb[3] = { int32(Saturate(color.x) * 255.0f), + int32(Saturate(color.y) * 255.0f), + int32(Saturate(color.z) * 255.0f) }; + TwCall(TwSetParam(bar, nullptr, "color", TW_PARAM_INT32, 3, rgb)); +} + +void SetAlpha(TwBar* bar, float alpha) +{ + int32 a = int32(Saturate(alpha)); + TwCall(TwSetParam(bar, nullptr, "alpha", TW_PARAM_INT32, 1, &a)); +} + +void SetTextMode(TwBar* bar, TextMode textMode) +{ + static const char* TextModeStrings[] = { "dark", "light" }; + TwCall(TwSetParam(bar, nullptr, "text", TW_PARAM_CSTRING, 1, TextModeStrings[uint64(textMode)])); +} + +void SetPosition(TwBar* bar, int32 posX, int32 posY) +{ + int32 positions[2] = { posX, posY }; + TwCall(TwSetParam(bar, nullptr, "position", TW_PARAM_INT32, 2, positions)); +} + +void SetSize(TwBar* bar, int32 sizeX, int32 sizeY) +{ + int32 sizes[2] = { sizeX, sizeY}; + TwCall(TwSetParam(bar, nullptr, "size", TW_PARAM_INT32, 2, sizes)); +} + +void SetValuesWidth(TwBar* bar, int32 width, bool32 fit) +{ + if(fit) + TwCall(TwSetParam(bar, nullptr, "valueswidth", TW_PARAM_CSTRING, 1, "fit")); + else + TwCall(TwSetParam(bar, nullptr, "valueswidth", TW_PARAM_INT32, 1, &width)); +} + +void SetRefreshRate(TwBar* bar, float rate) +{ + TwCall(TwSetParam(bar, nullptr, "refresh", TW_PARAM_FLOAT, 1, &rate)); +} + +void SetVisible(TwBar* bar, bool32 visible) +{ + SetVisible(bar, nullptr, visible); +} + +void SetIconified(TwBar* bar, bool32 iconified) +{ + TwCall(TwSetParam(bar, nullptr, "iconified", TW_PARAM_INT32, 1, &iconified)); +} + +void SetIconifiable(TwBar* bar, bool32 iconifiable) +{ + TwCall(TwSetParam(bar, nullptr, "iconifiable", TW_PARAM_INT32, 1, &iconifiable)); +} + +void SetMovable(TwBar* bar, bool32 movable) +{ + TwCall(TwSetParam(bar, nullptr, "movable", TW_PARAM_INT32, 1, &movable)); +} + +void SetResizable(TwBar* bar, bool32 resizable) +{ + TwCall(TwSetParam(bar, nullptr, "resizable", TW_PARAM_INT32, 1, &resizable)); +} + +void SetAlwaysOnTop(TwBar* bar, bool32 alwaysOnTop) +{ + TwCall(TwSetParam(bar, nullptr, "alwaystop", TW_PARAM_INT32, 1, &alwaysOnTop)); +} + +void SetAlwaysOnBottom(TwBar* bar, bool32 alwaysOnBottom) +{ + TwCall(TwSetParam(bar, nullptr, "alwaysbottom", TW_PARAM_INT32, 1, &alwaysOnBottom)); +} + +void SetContained(TwBar* bar, bool32 contained) +{ + TwCall(TwSetParam(bar, nullptr, "contained", TW_PARAM_INT32, 1, &contained)); +} + +void SetButtonAlignment(TwBar* bar, ButtonAlignment alignment) +{ + static const char* ButtonAlignmentStrings[] = { "left", "center", "right" }; + TwCall(TwSetParam(bar, nullptr, "buttonalign", TW_PARAM_CSTRING, 1, ButtonAlignmentStrings[uint64(alignment)])); +} + + +// Group parameters +void SetOpened(TwBar* bar, const char* groupName, bool32 opened) +{ + TwCall(TwSetParam(bar, groupName, "opened", TW_PARAM_INT32, 1, &opened)); +} + + +// Global parameters +void SetGlobalHelpText(const char* helpText) +{ + SetHelpText(nullptr, helpText); +} + +void SetIconPosition(IconPosition position) +{ + static const char* IconPositionStrings[] = { "topleft", "topright", "bottomleft", "bottomright" }; + TwCall(TwSetParam(nullptr, nullptr, "iconpos", TW_PARAM_CSTRING, 1, IconPositionStrings[uint64(position)])); +} + +void SetIconAlignment(IconAlignment alignment) +{ + static const char* IconAlignmentStrings[] = { "vertical", "horizontal" }; + TwCall(TwSetParam(nullptr, nullptr, "iconalign", TW_PARAM_CSTRING, 1, IconAlignmentStrings[uint64(alignment)])); +} + +void SetIconMargin(int32 marginX, int32 marginY) +{ + int32 margins[2] = { marginX, marginY }; + TwCall(TwSetParam(nullptr, nullptr, "iconmargin", TW_PARAM_INT32, 2, margins)); +} + +void SetFontSize(FontSize size) +{ + int32 fontSize[1] = { int32(size) }; + TwCall(TwSetParam(nullptr, nullptr, "fontsize", TW_PARAM_INT32, 1, fontSize)); +} + +void SetFontStyle(FontStyle style) +{ + static const char* FontStyleStrings[] = { "default", "fixed" }; + TwCall(TwSetParam(nullptr, nullptr, "fontstyle", TW_PARAM_CSTRING, 1, FontStyleStrings[uint64(style)])); +} + +void SetFontResizable(bool32 resizable) +{ + TwCall(TwSetParam(nullptr, nullptr, "fontresizable", TW_PARAM_INT32, 1, &resizable)); +} + +void SetFontScaling(float scale) +{ + TwCall(TwSetParam(nullptr, nullptr, "fontscaling", TW_PARAM_FLOAT, 1, &scale)); +} + +void SetDrawOverlappedBars(bool32 drawOverlapped) +{ + TwCall(TwSetParam(nullptr, nullptr, "overlap", TW_PARAM_INT32, 1, &drawOverlapped)); +} + +void SetButtonAlignment(ButtonAlignment alignment) +{ + SetButtonAlignment(nullptr, alignment); +} + +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/TwHelper.h b/Shadows/SampleFramework11/TwHelper.h new file mode 100644 index 0000000..ac77e68 --- /dev/null +++ b/Shadows/SampleFramework11/TwHelper.h @@ -0,0 +1,146 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" +#include "Exceptions.h" +#include "Math.h" + +namespace SampleFramework11 +{ + +namespace TwHelper +{ + +enum class ColorOrder +{ + RGBA, + ARGB, +}; + +enum class ColorMode +{ + RGB, + HLS, +}; + +enum class Axis +{ + PositiveX, + NegativeX, + PositiveY, + NegativeY, + PositiveZ, + NegativeZ, +}; + +enum class TextMode +{ + Light, + Dark, +}; + +enum class IconPosition +{ + TopLeft, + TopRight, + BottomLeft, + BottomRight, +}; + +enum class IconAlignment +{ + Vertical, + Horizontal, +}; + +enum class FontSize +{ + Small = 0, + Medium = 1, + Large = 2, +}; + +enum class FontStyle +{ + Default, + Fixed, +}; + +enum class ButtonAlignment +{ + Left, + Center, + Right, +}; + +// Helper functions for AntTweakBar + +// Variable parameters +void SetLabel(TwBar* bar, const char* varName, const char* label); +void SetHelpText(TwBar* bar, const char* varName, const char* helpText); +void SetGroup(TwBar* bar, const char* varName, const char* group); +void SetVisible(TwBar* bar, const char* varName, bool32 visible); +void SetReadOnly(TwBar* bar, const char* varName, bool32 readOnly); +void SetMinMax(TwBar* bar, const char* varName, float min, float max); +void SetMinMax(TwBar* bar, const char* varName, int32 min, int32 max); +void SetStep(TwBar* bar, const char* varName, float step); +void SetPrecision(TwBar* bar, const char* varName, int32 precision); +void SetHexidecimal(TwBar* bar, const char* varName, bool32 hex); +void SetShortcutKey(TwBar* bar, const char* varName, const char* shortcutKey); +void SetShortcutKeyIncrement(TwBar* bar, const char* varName, const char* shortcutKey); +void SetShortcutKeyDecrement(TwBar* bar, const char* varName, const char* shortcutKey); +void SetBoolLabels(TwBar* bar, const char* varName, const char* falseLabel, const char* trueLabel); +void SetUseAlphaChannel(TwBar* bar, const char* varName, bool32 useAlpha); +void SetColorOrder(TwBar* bar, const char* varName, ColorOrder order); +void SetColorMode(TwBar* bar, const char* varName, ColorMode mode); +void SetUseArrowMode(TwBar* bar, const char* varName, bool32 useArrowMode, Float3 initialDirection); +void SetArrowColor(TwBar* bar, const char* varName, Float3 color); +void SetAxisMapping(TwBar* bar, const char* varName, Axis xAxis, Axis yAxis, Axis zAxis); +void SetShowNumericalValue(TwBar* bar, const char* varName, bool32 showValue); + +// TweakBar parameters +void SetLabel(TwBar* bar, const char* label); +void SetHelpText(TwBar* bar, const char* helpText); +void SetColor(TwBar* bar, Float3 color); +void SetAlpha(TwBar* bar, float alpha); +void SetTextMode(TwBar* bar, TextMode textMode); +void SetPosition(TwBar* bar, int32 posX, int32 posY); +void SetSize(TwBar* bar, int32 sizeX, int32 sizeY); +void SetValuesWidth(TwBar* bar, int32 width, bool32 fit); +void SetRefreshRate(TwBar* bar, float rate); +void SetVisible(TwBar* bar, bool32 visible); +void SetIconified(TwBar* bar, bool32 iconified); +void SetIconifiable(TwBar* bar, bool32 iconifiable); +void SetMovable(TwBar* bar, bool32 movable); +void SetResizable(TwBar* bar, bool32 resizable); +void SetAlwaysOnTop(TwBar* bar, bool32 alwaysOnTop); +void SetAlwaysOnBottom(TwBar* bar, bool32 alwaysOnBottom); +void SetContained(TwBar* bar, bool32 contained); +void SetButtonAlignment(TwBar* bar, ButtonAlignment alignment); + +// Group parameters +void SetOpened(TwBar* bar, const char* groupName, bool32 opened); + +// Global parameters +void SetGlobalHelpText(const char* helpText); +void SetIconPosition(IconPosition position); +void SetIconAlignment(IconAlignment alignment); +void SetIconMargin(int32 marginX, int32 marginY); +void SetFontSize(FontSize size); +void SetFontStyle(FontStyle style); +void SetFontResizable(bool32 resizable); +void SetFontScaling(float scale); +void SetDrawOverlappedBars(bool32 drawOverlapped); +void SetButtonAlignment(ButtonAlignment alignment); + +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Utility.cpp b/Shadows/SampleFramework11/Utility.cpp new file mode 100644 index 0000000..3ffa505 --- /dev/null +++ b/Shadows/SampleFramework11/Utility.cpp @@ -0,0 +1,251 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Utility.h" +#include "Exceptions.h" +#include "InterfacePointers.h" +#include "DDSTextureLoader.h" +#include "WICTextureLoader.h" +#include "FileIO.h" +#include "ShaderCompilation.h" +#include "GraphicsTypes.h" + +namespace SampleFramework11 +{ + +// Converts from cartesian to barycentric coordinates +XMFLOAT3 CartesianToBarycentric(float x, float y, const XMFLOAT2& pos1, const XMFLOAT2& pos2, const XMFLOAT2& pos3) +{ + float r1 = (pos2.y - pos3.y) * (x - pos3.x) + (pos3.x - pos2.x) * (y - pos3.y); + r1 /= (pos2.y - pos3.y) * (pos1.x - pos3.x) + (pos3.x - pos2.x) * (pos1.y - pos3.y); + + float r2 = (pos3.y - pos1.y) * (x - pos3.x) + (pos1.x - pos3.x) * (y - pos3.y); + r2 /= (pos3.y - pos1.y) * (pos2.x - pos3.x) + (pos1.x - pos3.x) * (pos2.y - pos3.y); + + float r3 = 1.0f - r1 - r2; + + return XMFLOAT3(r1, r2, r3); +} + +// Converts from barycentric to cartesian coordinates +XMFLOAT2 BarycentricToCartesian(const XMFLOAT3& r, const XMFLOAT2& pos1, const XMFLOAT2& pos2, const XMFLOAT2& pos3) +{ + float x = r.x * pos1.x + r.y * pos2.x + r.z * pos3.x; + float y = r.x * pos1.y + r.y * pos2.y + r.z * pos3.y; + + return XMFLOAT2(x, y); +} + +// Converts from barycentric to cartesian coordinates +XMVECTOR BarycentricToCartesian(const XMFLOAT3& r, FXMVECTOR pos1, FXMVECTOR pos2, FXMVECTOR pos3) +{ + XMVECTOR rvec; + rvec = XMVectorScale(pos1, r.x); + rvec += XMVectorScale(pos2, r.y); + rvec += XMVectorScale(pos3, r.z); + + return rvec; +} + +// Returns true if the given barycentric coordinate is in the triangle +BOOL PointIsInTriangle(const XMFLOAT3& r, float epsilon) +{ + float minr = 0.0f - epsilon; + float maxr = 1.0f + epsilon; + + if(r.x < minr || r.x > maxr) + return false; + + if(r.y < minr || r.y > maxr) + return false; + + if(r.z < minr || r.z > maxr) + return false; + + float rsum = r.x + r.y + r.z; + return rsum >= minr && rsum <= maxr; +} + +// Computes a compute shader dispatch size given a thread group size, and number of elements to process +uint32 DispatchSize(uint32 tgSize, uint32 numElements) +{ + uint32 dispatchSize = numElements / tgSize; + dispatchSize += numElements % tgSize > 0 ? 1 : 0; + return dispatchSize; +} + +void SetCSInputs(ID3D11DeviceContext* context, ID3D11ShaderResourceView* srv0, ID3D11ShaderResourceView* srv1, + ID3D11ShaderResourceView* srv2, ID3D11ShaderResourceView* srv3) +{ + ID3D11ShaderResourceView* srvs[4] = { srv0, srv1, srv2, srv3 }; + context->CSSetShaderResources(0, 4, srvs); +} + +void ClearCSInputs(ID3D11DeviceContext* context) +{ + SetCSInputs(context, nullptr, nullptr, nullptr, nullptr); +} + +void SetCSOutputs(ID3D11DeviceContext* context, ID3D11UnorderedAccessView* uav0, ID3D11UnorderedAccessView* uav1, + ID3D11UnorderedAccessView* uav2, ID3D11UnorderedAccessView* uav3, ID3D11UnorderedAccessView* uav4, + ID3D11UnorderedAccessView* uav5) +{ + ID3D11UnorderedAccessView* uavs[6] = { uav0, uav1, uav2, uav3, uav4, uav5 }; + context->CSSetUnorderedAccessViews(0, 6, uavs, nullptr); +} + +void ClearCSOutputs(ID3D11DeviceContext* context) +{ + SetCSOutputs(context, nullptr, nullptr, nullptr, nullptr); +} + +void SetCSSamplers(ID3D11DeviceContext* context, ID3D11SamplerState* sampler0, ID3D11SamplerState* sampler1, + ID3D11SamplerState* sampler2, ID3D11SamplerState* sampler3) +{ + ID3D11SamplerState* samplers[4] = { sampler0, sampler1, sampler2, sampler3 }; + context->CSSetSamplers(0, 4, samplers); +} + +void SetCSShader(ID3D11DeviceContext* context, ID3D11ComputeShader* shader) +{ + context->CSSetShader(shader, nullptr, 0); +} + +void SetCSConstants(ID3D11DeviceContext* context, ID3D11Buffer* constantBuffer, uint32 slot) +{ + ID3D11Buffer* constants[1] = { constantBuffer }; + context->CSSetConstantBuffers(slot, 1, constants); +} + +// Loads a texture, using either the DDS loader or the WIC loader +ID3D11ShaderResourceViewPtr LoadTexture(ID3D11Device* device, const wchar* filePath) +{ + ID3D11DeviceContextPtr context; + device->GetImmediateContext(&context); + + ID3D11ResourcePtr resource; + ID3D11ShaderResourceViewPtr srv; + + const std::wstring extension = GetFileExtension(filePath); + if(extension == L"DDS" || extension == L"dds") + { + DXCall(CreateDDSTextureFromFile(device, filePath, &resource, &srv, 0)); + return srv; + } + else + { + DXCall(CreateWICTextureFromFile(device, context, filePath, &resource, &srv, 0)); + return srv; + } +} + + +// Decode a texture into 32-bit floats and copies it to the CPU +void GetTextureData(ID3D11Device* device, ID3D11ShaderResourceView* textureSRV, + std::vector& textureData) +{ + static ComputeShaderPtr decodeTextureCS; + static ComputeShaderPtr decodeTextureArrayCS; + + static const uint32 TGSize = 16; + + if(decodeTextureCS.Valid() == false) + { + CompileOptions opts; + opts.Add("TGSize_", TGSize); + decodeTextureCS = CompileCSFromFile(device, L"SampleFramework11\\Shaders\\DecodeTextureCS.hlsl", + "DecodeTextureCS", "cs_5_0", opts); + + decodeTextureArrayCS = CompileCSFromFile(device, L"SampleFramework11\\Shaders\\DecodeTextureCS.hlsl", + "DecodeTextureArrayCS", "cs_5_0", opts); + } + + ID3D11Texture2DPtr texture; + textureSRV->GetResource(reinterpret_cast(&texture)); + + D3D11_TEXTURE2D_DESC texDesc; + texture->GetDesc(&texDesc); + + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + textureSRV->GetDesc(&srvDesc); + + ID3D11ShaderResourceViewPtr sourceSRV = textureSRV; + uint32 arraySize = texDesc.ArraySize; + if(srvDesc.ViewDimension == D3D11_SRV_DIMENSION_TEXTURECUBE + || srvDesc.ViewDimension == D3D11_SRV_DIMENSION_TEXTURECUBEARRAY) + { + arraySize *= 6; + + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; + srvDesc.Texture2DArray.ArraySize = arraySize; + srvDesc.Texture2DArray.FirstArraySlice = 0; + srvDesc.Texture2DArray.MostDetailedMip = 0; + srvDesc.Texture2DArray.MipLevels = -1; + DXCall(device->CreateShaderResourceView(texture, &srvDesc, &sourceSRV)); + } + + D3D11_TEXTURE2D_DESC decodeTextureDesc; + decodeTextureDesc.Width = texDesc.Width; + decodeTextureDesc.Height = texDesc.Height; + decodeTextureDesc.ArraySize = arraySize; + decodeTextureDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; + decodeTextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; + decodeTextureDesc.MipLevels = 1; + decodeTextureDesc.MiscFlags = 0; + decodeTextureDesc.SampleDesc.Count = 1; + decodeTextureDesc.SampleDesc.Quality = 0; + decodeTextureDesc.Usage = D3D11_USAGE_DEFAULT; + decodeTextureDesc.CPUAccessFlags = 0; + + ID3D11Texture2DPtr decodeTexture; + DXCall(device->CreateTexture2D(&decodeTextureDesc, nullptr, &decodeTexture)); + + ID3D11UnorderedAccessViewPtr decodeTextureUAV; + DXCall(device->CreateUnorderedAccessView(decodeTexture, nullptr, &decodeTextureUAV)); + + ID3D11DeviceContextPtr context; + device->GetImmediateContext(&context); + + SetCSInputs(context, sourceSRV); + SetCSOutputs(context, decodeTextureUAV); + SetCSShader(context, arraySize > 1 ? decodeTextureArrayCS : decodeTextureCS); + + context->Dispatch(DispatchSize(TGSize, texDesc.Width), DispatchSize(TGSize, texDesc.Height), arraySize); + + ClearCSInputs(context); + ClearCSOutputs(context); + + StagingTexture2D stagingTexture; + stagingTexture.Initialize(device, texDesc.Width, texDesc.Height, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 1, 0, arraySize); + context->CopyResource(stagingTexture.Texture, decodeTexture); + + textureData.resize(texDesc.Width * texDesc.Height * arraySize); + + for(uint32 slice = 0; slice < arraySize; ++slice) + { + uint32 pitch = 0; + const uint8* srcData = reinterpret_cast(stagingTexture.Map(context, slice, pitch)); + + const uint32 sliceOffset = texDesc.Width * texDesc.Height * slice; + + for(uint32 y = 0; y < texDesc.Height; ++y) + { + const Float4* rowData = reinterpret_cast(srcData); + + for(uint32 x = 0; x < texDesc.Width; ++x) + textureData[y * texDesc.Width + x + sliceOffset] = rowData[x]; + + srcData += pitch; + } + } +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Utility.h b/Shadows/SampleFramework11/Utility.h new file mode 100644 index 0000000..2bc3e0d --- /dev/null +++ b/Shadows/SampleFramework11/Utility.h @@ -0,0 +1,188 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Exceptions.h" +#include "InterfacePointers.h" +#include "Math.h" +#include "Assert.h" + +namespace SampleFramework11 +{ + +// Returns a size suitable for creating a constant buffer, by rounding up +// to the next multiple of 16 +inline UINT CBSize(UINT size) +{ + return ((size + 15) / 16) * 16; +} + +// Converts an ANSI string to a std::wstring +inline std::wstring AnsiToWString(const char* ansiString) +{ + wchar buffer[512]; + Win32Call(MultiByteToWideChar(CP_ACP, 0, ansiString, -1, buffer, 512)); + return std::wstring(buffer); +} + +inline std::string WStringToAnsi(const wchar* wideString) +{ + char buffer[512]; + Win32Call(WideCharToMultiByte(CP_ACP, 0, wideString, -1, buffer, 612, NULL, NULL)); + return std::string(buffer); +} + +// Splits up a string using a delimiter +inline void Split(const std::wstring& str, std::vector& parts, const std::wstring& delimiters = L" ") +{ + // Skip delimiters at beginning + std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); + + // Find first "non-delimiter" + std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); + + while (std::wstring::npos != pos || std::wstring::npos != lastPos) + { + // Found a token, add it to the vector + parts.push_back(str.substr(lastPos, pos - lastPos)); + + // Skip delimiters. Note the "not_of" + lastPos = str.find_first_not_of(delimiters, pos); + + // Find next "non-delimiter" + pos = str.find_first_of(delimiters, lastPos); + } +} + +// Splits up a string using a delimiter +inline std::vector Split(const std::wstring& str, const std::wstring& delimiters = L" ") +{ + std::vector parts; + Split(str, parts, delimiters); + return parts; +} + +// Parses a string into a number +template inline T Parse(const std::wstring& str) +{ + std::wistringstream stream(str); + wchar_t c; + T x; + if (!(str >> x) || stream.get(c)) + throw Exception(L"Can't parse string \"" + str + L"\""); + return x; +} + +// Converts a number to a string +template inline std::wstring ToString(const T& val) +{ + std::wostringstream stream; + if (!(stream << val)) + throw Exception(L"Error converting value to string"); + return stream.str(); +} + +// Converts a number to an ansi string +template inline std::string ToAnsiString(const T& val) +{ + std::ostringstream stream; + if (!(stream << val)) + throw Exception(L"Error converting value to string"); + return stream.str(); +} + +// Outputs a string to the debugger output +inline void DebugPrint(const std::wstring& str) +{ + std::wstring output = str + L"\n"; + OutputDebugStringW(output.c_str()); +} + +// Returns the number of mip levels given a texture size +inline UINT NumMipLevels(UINT width, UINT height) +{ + UINT numMips = 0; + UINT size = std::max(width, height); + while (1U << numMips <= size) + ++numMips; + + if (1U << numMips < size) + ++numMips; + + return numMips; +} + +// Gets an index from an index buffer +inline uint32 GetIndex(const void* indices, uint32 idx, uint32 indexSize) +{ + if(indexSize == 2) + return reinterpret_cast(indices)[idx]; + else + return reinterpret_cast(indices)[idx]; +} + +// Sets the viewport for a given render target size +inline void SetViewport(ID3D11DeviceContext* context, UINT rtWidth, UINT rtHeight) +{ + D3D11_VIEWPORT viewport; + viewport.Width = static_cast(rtWidth); + viewport.Height = static_cast(rtHeight); + viewport.MinDepth = 0.0f; + viewport.MaxDepth = 1.0f; + viewport.TopLeftX = 0.0f; + viewport.TopLeftY = 0.0f; + + context->RSSetViewports(1, &viewport); +} + +// Copies a portion of a buffer to another buffer +inline void CopyBufferRegion(ID3D11DeviceContext* context, ID3D11Buffer* dstBuffer, ID3D11Buffer* srcBuffer, + uint32 dstOffset, uint32 srcOffset, uint32 srcSize) +{ + D3D11_BOX srcBox; + srcBox.left = srcOffset; + srcBox.right = srcOffset + srcSize; + srcBox.top = 0; + srcBox.bottom = 1; + srcBox.front = 0; + srcBox.back = 1; + context->CopySubresourceRegion(dstBuffer, 0, dstOffset, 0, 0, srcBuffer, 0, &srcBox); +} + +// Barycentric coordinate functions +XMFLOAT3 CartesianToBarycentric(float x, float y, const XMFLOAT2& pos1, const XMFLOAT2& pos2, const XMFLOAT2& pos3); +XMFLOAT2 BarycentricToCartesian(const XMFLOAT3& r, const XMFLOAT2& pos1, const XMFLOAT2& pos2, const XMFLOAT2& pos3); +XMVECTOR BarycentricToCartesian(const XMFLOAT3& r, FXMVECTOR pos1, FXMVECTOR pos2, FXMVECTOR pos3); +BOOL PointIsInTriangle(const XMFLOAT3& r, float epsilon = 0.0f); + +// Compute shader helpers +uint32 DispatchSize(uint32 tgSize, uint32 numElements); +void SetCSInputs(ID3D11DeviceContext* context, ID3D11ShaderResourceView* srv0, ID3D11ShaderResourceView* srv1 = NULL, + ID3D11ShaderResourceView* srv2 = NULL, ID3D11ShaderResourceView* srv3 = NULL); +void ClearCSInputs(ID3D11DeviceContext* context); +void SetCSOutputs(ID3D11DeviceContext* context, ID3D11UnorderedAccessView* uav0, ID3D11UnorderedAccessView* uav1 = NULL, + ID3D11UnorderedAccessView* uav2 = NULL, ID3D11UnorderedAccessView* uav3 = NULL, + ID3D11UnorderedAccessView* uav4 = NULL, ID3D11UnorderedAccessView* uav5 = NULL); +void ClearCSOutputs(ID3D11DeviceContext* context); +void SetCSSamplers(ID3D11DeviceContext* context, ID3D11SamplerState* sampler0, ID3D11SamplerState* sampler1 = NULL, + ID3D11SamplerState* sampler2 = NULL, ID3D11SamplerState* sampler3 = NULL); +void SetCSShader(ID3D11DeviceContext* context, ID3D11ComputeShader* shader); +void SetCSConstants(ID3D11DeviceContext* context, ID3D11Buffer* constantBuffer, uint32 slot); + +// Texture loading +ID3D11ShaderResourceViewPtr LoadTexture(ID3D11Device* device, const wchar* filePath); + +// Decode a texture into 32-bit floats and copies it to the CPU +void GetTextureData(ID3D11Device* device, ID3D11ShaderResourceView* texture, + std::vector& textureData); + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/WICTextureLoader.cpp b/Shadows/SampleFramework11/WICTextureLoader.cpp new file mode 100644 index 0000000..f606c4b --- /dev/null +++ b/Shadows/SampleFramework11/WICTextureLoader.cpp @@ -0,0 +1,705 @@ +//-------------------------------------------------------------------------------------- +// File: WICTextureLoader.cpp +// +// Function for loading a WIC image and creating a Direct3D 11 runtime texture for it +// (auto-generating mipmaps if possible) +// +// Note: Assumes application has already called CoInitializeEx +// +// Warning: CreateWICTexture* functions are not thread-safe if given a d3dContext instance for +// auto-gen mipmap support. +// +// Note these functions are useful for images created as simple 2D textures. For +// more complex resources, DDSTextureLoader is an excellent light-weight runtime loader. +// For a full-featured DDS file reader, writer, and texture processing pipeline see +// the 'Texconv' sample and the 'DirectXTex' library. +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// http://go.microsoft.com/fwlink/?LinkId=248926 +// http://go.microsoft.com/fwlink/?LinkId=248929 +//-------------------------------------------------------------------------------------- + +// We could load multi-frame images (TIFF/GIF) into a texture array. +// For now, we just load the first frame (note: DirectXTex supports multi-frame images) + +#include "PCH.h" +#include "WICTextureLoader.h" + +namespace SampleFramework11 +{ + +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) && !defined(DXGI_1_2_FORMATS) +#define DXGI_1_2_FORMATS +#endif + +//--------------------------------------------------------------------------------- +template class ScopedObject +{ +public: + explicit ScopedObject( T *p = 0 ) : _pointer(p) {} + ~ScopedObject() + { + if ( _pointer ) + { + _pointer->Release(); + _pointer = nullptr; + } + } + + bool IsNull() const { return (!_pointer); } + + T& operator*() { return *_pointer; } + T* operator->() { return _pointer; } + T** operator&() { return &_pointer; } + + void Reset(T *p = 0) { if ( _pointer ) { _pointer->Release(); } _pointer = p; } + + T* Get() const { return _pointer; } + +private: + ScopedObject(const ScopedObject&); + ScopedObject& operator=(const ScopedObject&); + + T* _pointer; +}; + +//------------------------------------------------------------------------------------- +// WIC Pixel Format Translation Data +//------------------------------------------------------------------------------------- +struct WICTranslate +{ + GUID wic; + DXGI_FORMAT format; +}; + +static WICTranslate g_WICFormats[] = +{ + { GUID_WICPixelFormat128bppRGBAFloat, DXGI_FORMAT_R32G32B32A32_FLOAT }, + + { GUID_WICPixelFormat64bppRGBAHalf, DXGI_FORMAT_R16G16B16A16_FLOAT }, + { GUID_WICPixelFormat64bppRGBA, DXGI_FORMAT_R16G16B16A16_UNORM }, + + { GUID_WICPixelFormat32bppRGBA, DXGI_FORMAT_R8G8B8A8_UNORM }, + { GUID_WICPixelFormat32bppBGRA, DXGI_FORMAT_B8G8R8A8_UNORM }, // DXGI 1.1 + { GUID_WICPixelFormat32bppBGR, DXGI_FORMAT_B8G8R8X8_UNORM }, // DXGI 1.1 + + { GUID_WICPixelFormat32bppRGBA1010102XR, DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM }, // DXGI 1.1 + { GUID_WICPixelFormat32bppRGBA1010102, DXGI_FORMAT_R10G10B10A2_UNORM }, + { GUID_WICPixelFormat32bppRGBE, DXGI_FORMAT_R9G9B9E5_SHAREDEXP }, + +#ifdef DXGI_1_2_FORMATS + + { GUID_WICPixelFormat16bppBGRA5551, DXGI_FORMAT_B5G5R5A1_UNORM }, + { GUID_WICPixelFormat16bppBGR565, DXGI_FORMAT_B5G6R5_UNORM }, + +#endif // DXGI_1_2_FORMATS + + { GUID_WICPixelFormat32bppGrayFloat, DXGI_FORMAT_R32_FLOAT }, + { GUID_WICPixelFormat16bppGrayHalf, DXGI_FORMAT_R16_FLOAT }, + { GUID_WICPixelFormat16bppGray, DXGI_FORMAT_R16_UNORM }, + { GUID_WICPixelFormat8bppGray, DXGI_FORMAT_R8_UNORM }, + + { GUID_WICPixelFormat8bppAlpha, DXGI_FORMAT_A8_UNORM }, + +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) + { GUID_WICPixelFormat96bppRGBFloat, DXGI_FORMAT_R32G32B32_FLOAT }, +#endif +}; + +//------------------------------------------------------------------------------------- +// WIC Pixel Format nearest conversion table +//------------------------------------------------------------------------------------- + +struct WICConvert +{ + GUID source; + GUID target; +}; + +static WICConvert g_WICConvert[] = +{ + // Note target GUID in this conversion table must be one of those directly supported formats (above). + + { GUID_WICPixelFormatBlackWhite, GUID_WICPixelFormat8bppGray }, // DXGI_FORMAT_R8_UNORM + + { GUID_WICPixelFormat1bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat2bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat4bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat8bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + + { GUID_WICPixelFormat2bppGray, GUID_WICPixelFormat8bppGray }, // DXGI_FORMAT_R8_UNORM + { GUID_WICPixelFormat4bppGray, GUID_WICPixelFormat8bppGray }, // DXGI_FORMAT_R8_UNORM + + { GUID_WICPixelFormat16bppGrayFixedPoint, GUID_WICPixelFormat16bppGrayHalf }, // DXGI_FORMAT_R16_FLOAT + { GUID_WICPixelFormat32bppGrayFixedPoint, GUID_WICPixelFormat32bppGrayFloat }, // DXGI_FORMAT_R32_FLOAT + +#ifdef DXGI_1_2_FORMATS + + { GUID_WICPixelFormat16bppBGR555, GUID_WICPixelFormat16bppBGRA5551 }, // DXGI_FORMAT_B5G5R5A1_UNORM + +#else + + { GUID_WICPixelFormat16bppBGR555, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat16bppBGRA5551, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat16bppBGR565, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + +#endif // DXGI_1_2_FORMATS + + { GUID_WICPixelFormat32bppBGR101010, GUID_WICPixelFormat32bppRGBA1010102 }, // DXGI_FORMAT_R10G10B10A2_UNORM + + { GUID_WICPixelFormat24bppBGR, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat24bppRGB, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat32bppPBGRA, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat32bppPRGBA, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + + { GUID_WICPixelFormat48bppRGB, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat48bppBGR, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat64bppBGRA, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat64bppPRGBA, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat64bppPBGRA, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + + { GUID_WICPixelFormat48bppRGBFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat48bppBGRFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat64bppRGBAFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat64bppBGRAFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat64bppRGBFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat64bppRGBHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat48bppRGBHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + + { GUID_WICPixelFormat128bppPRGBAFloat, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT + { GUID_WICPixelFormat128bppRGBFloat, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT + { GUID_WICPixelFormat128bppRGBAFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT + { GUID_WICPixelFormat128bppRGBFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT + + { GUID_WICPixelFormat32bppCMYK, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat64bppCMYK, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat40bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat80bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) + { GUID_WICPixelFormat32bppRGB, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM + { GUID_WICPixelFormat64bppRGB, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM + { GUID_WICPixelFormat64bppPRGBAHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT + { GUID_WICPixelFormat96bppRGBFixedPoint, GUID_WICPixelFormat96bppRGBFloat }, // DXGI_FORMAT_R32G32B32_FLOAT +#else + { GUID_WICPixelFormat96bppRGBFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT +#endif + + // We don't support n-channel formats +}; + +//-------------------------------------------------------------------------------------- +static IWICImagingFactory* _GetWIC() +{ + static IWICImagingFactory* s_Factory = nullptr; + + if ( s_Factory ) + return s_Factory; + + HRESULT hr = CoCreateInstance( + CLSID_WICImagingFactory, + nullptr, + CLSCTX_INPROC_SERVER, + __uuidof(IWICImagingFactory), + (LPVOID*)&s_Factory + ); + + if ( FAILED(hr) ) + { + s_Factory = nullptr; + return nullptr; + } + + return s_Factory; +} + +//--------------------------------------------------------------------------------- +static DXGI_FORMAT _WICToDXGI( const GUID& guid ) +{ + for( size_t i=0; i < _countof(g_WICFormats); ++i ) + { + if ( memcmp( &g_WICFormats[i].wic, &guid, sizeof(GUID) ) == 0 ) + return g_WICFormats[i].format; + } + + return DXGI_FORMAT_UNKNOWN; +} + +//--------------------------------------------------------------------------------- +static size_t _WICBitsPerPixel( REFGUID targetGuid ) +{ + IWICImagingFactory* pWIC = _GetWIC(); + if ( !pWIC ) + return 0; + + ScopedObject cinfo; + if ( FAILED( pWIC->CreateComponentInfo( targetGuid, &cinfo ) ) ) + return 0; + + WICComponentType type; + if ( FAILED( cinfo->GetComponentType( &type ) ) ) + return 0; + + if ( type != WICPixelFormat ) + return 0; + + ScopedObject pfinfo; + if ( FAILED( cinfo->QueryInterface( __uuidof(IWICPixelFormatInfo), reinterpret_cast( &pfinfo ) ) ) ) + return 0; + + UINT bpp; + if ( FAILED( pfinfo->GetBitsPerPixel( &bpp ) ) ) + return 0; + + return bpp; +} + +//--------------------------------------------------------------------------------- +static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice, + _In_opt_ ID3D11DeviceContext* d3dContext, + _In_ IWICBitmapFrameDecode *frame, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize ) +{ + UINT width, height; + HRESULT hr = frame->GetSize( &width, &height ); + if ( FAILED(hr) ) + return hr; + + assert( width > 0 && height > 0 ); + + if ( !maxsize ) + { + // This is a bit conservative because the hardware could support larger textures than + // the Feature Level defined minimums, but doing it this way is much easier and more + // performant for WIC than the 'fail and retry' model used by DDSTextureLoader + + switch( d3dDevice->GetFeatureLevel() ) + { + case D3D_FEATURE_LEVEL_9_1: + case D3D_FEATURE_LEVEL_9_2: + maxsize = 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; + + case D3D_FEATURE_LEVEL_9_3: + maxsize = 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; + + case D3D_FEATURE_LEVEL_10_0: + case D3D_FEATURE_LEVEL_10_1: + maxsize = 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/; + break; + + default: + maxsize = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; + break; + } + } + + assert( maxsize > 0 ); + + UINT twidth, theight; + if ( width > maxsize || height > maxsize ) + { + float ar = static_cast(height) / static_cast(width); + if ( width > height ) + { + twidth = static_cast( maxsize ); + theight = static_cast( static_cast(maxsize) * ar ); + } + else + { + theight = static_cast( maxsize ); + twidth = static_cast( static_cast(maxsize) / ar ); + } + assert( twidth <= maxsize && theight <= maxsize ); + } + else + { + twidth = width; + theight = height; + } + + // Determine format + WICPixelFormatGUID pixelFormat; + hr = frame->GetPixelFormat( &pixelFormat ); + if ( FAILED(hr) ) + return hr; + + WICPixelFormatGUID convertGUID; + memcpy( &convertGUID, &pixelFormat, sizeof(WICPixelFormatGUID) ); + + size_t bpp = 0; + + DXGI_FORMAT format = _WICToDXGI( pixelFormat ); + if ( format == DXGI_FORMAT_UNKNOWN ) + { + for( size_t i=0; i < _countof(g_WICConvert); ++i ) + { + if ( memcmp( &g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 ) + { + memcpy( &convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID) ); + + format = _WICToDXGI( g_WICConvert[i].target ); + assert( format != DXGI_FORMAT_UNKNOWN ); + bpp = _WICBitsPerPixel( convertGUID ); + break; + } + } + + if ( format == DXGI_FORMAT_UNKNOWN ) + return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED ); + } + else + { + bpp = _WICBitsPerPixel( pixelFormat ); + } + +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) + if ( (format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0 ) + { + // Special case test for optional device support for autogen mipchains for R32G32B32_FLOAT + UINT fmtSupport = 0; + hr = d3dDevice->CheckFormatSupport( DXGI_FORMAT_R32G32B32_FLOAT, &fmtSupport ); + if ( FAILED(hr) || !( fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) ) + { + // Use R32G32B32A32_FLOAT instead which is required for Feature Level 10.0 and up + memcpy( &convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID) ); + format = DXGI_FORMAT_R32G32B32A32_FLOAT; + bpp = 128; + } + } +#endif + + if ( !bpp ) + return E_FAIL; + + // Verify our target format is supported by the current device + // (handles WDDM 1.0 or WDDM 1.1 device driver cases as well as DirectX 11.0 Runtime without 16bpp format support) + UINT support = 0; + hr = d3dDevice->CheckFormatSupport( format, &support ); + if ( FAILED(hr) || !(support & D3D11_FORMAT_SUPPORT_TEXTURE2D) ) + { + // Fallback to RGBA 32-bit format which is supported by all devices + memcpy( &convertGUID, &GUID_WICPixelFormat32bppRGBA, sizeof(WICPixelFormatGUID) ); + format = DXGI_FORMAT_R8G8B8A8_UNORM; + bpp = 32; + } + + // Allocate temporary memory for image + size_t rowPitch = ( twidth * bpp + 7 ) / 8; + size_t imageSize = rowPitch * theight; + + std::unique_ptr temp( new uint8_t[ imageSize ] ); + + // Load image data + if ( memcmp( &convertGUID, &pixelFormat, sizeof(GUID) ) == 0 + && twidth == width + && theight == height ) + { + // No format conversion or resize needed + hr = frame->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); + if ( FAILED(hr) ) + return hr; + } + else if ( twidth != width || theight != height ) + { + // Resize + IWICImagingFactory* pWIC = _GetWIC(); + if ( !pWIC ) + return E_NOINTERFACE; + + ScopedObject scaler; + hr = pWIC->CreateBitmapScaler( &scaler ); + if ( FAILED(hr) ) + return hr; + + hr = scaler->Initialize( frame, twidth, theight, WICBitmapInterpolationModeFant ); + if ( FAILED(hr) ) + return hr; + + WICPixelFormatGUID pfScaler; + hr = scaler->GetPixelFormat( &pfScaler ); + if ( FAILED(hr) ) + return hr; + + if ( memcmp( &convertGUID, &pfScaler, sizeof(GUID) ) == 0 ) + { + // No format conversion needed + hr = scaler->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); + if ( FAILED(hr) ) + return hr; + } + else + { + ScopedObject FC; + hr = pWIC->CreateFormatConverter( &FC ); + if ( FAILED(hr) ) + return hr; + + hr = FC->Initialize( scaler.Get(), convertGUID, WICBitmapDitherTypeErrorDiffusion, 0, 0, WICBitmapPaletteTypeCustom ); + if ( FAILED(hr) ) + return hr; + + hr = FC->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); + if ( FAILED(hr) ) + return hr; + } + } + else + { + // Format conversion but no resize + IWICImagingFactory* pWIC = _GetWIC(); + if ( !pWIC ) + return E_NOINTERFACE; + + ScopedObject FC; + hr = pWIC->CreateFormatConverter( &FC ); + if ( FAILED(hr) ) + return hr; + + hr = FC->Initialize( frame, convertGUID, WICBitmapDitherTypeErrorDiffusion, 0, 0, WICBitmapPaletteTypeCustom ); + if ( FAILED(hr) ) + return hr; + + hr = FC->CopyPixels( 0, static_cast( rowPitch ), static_cast( imageSize ), temp.get() ); + if ( FAILED(hr) ) + return hr; + } + + // See if format is supported for auto-gen mipmaps (varies by feature level) + bool autogen = false; + if ( d3dContext != 0 && textureView != 0 ) // Must have context and shader-view to auto generate mipmaps + { + UINT fmtSupport = 0; + hr = d3dDevice->CheckFormatSupport( format, &fmtSupport ); + if ( SUCCEEDED(hr) && ( fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) ) + { + autogen = true; + } + } + + // Create texture + D3D11_TEXTURE2D_DESC desc; + desc.Width = twidth; + desc.Height = theight; + desc.MipLevels = (autogen) ? 0 : 1; + desc.ArraySize = 1; + desc.Format = format; + desc.SampleDesc.Count = 1; + desc.SampleDesc.Quality = 0; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = (autogen) ? (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) : (D3D11_BIND_SHADER_RESOURCE); + desc.CPUAccessFlags = 0; + desc.MiscFlags = (autogen) ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0; + + D3D11_SUBRESOURCE_DATA initData; + initData.pSysMem = temp.get(); + initData.SysMemPitch = static_cast( rowPitch ); + initData.SysMemSlicePitch = static_cast( imageSize ); + + ID3D11Texture2D* tex = nullptr; + hr = d3dDevice->CreateTexture2D( &desc, (autogen) ? nullptr : &initData, &tex ); + if ( SUCCEEDED(hr) && tex != 0 ) + { + if (textureView != 0) + { + D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; + memset( &SRVDesc, 0, sizeof( SRVDesc ) ); + SRVDesc.Format = format; + SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1; + + hr = d3dDevice->CreateShaderResourceView( tex, &SRVDesc, textureView ); + if ( FAILED(hr) ) + { + tex->Release(); + return hr; + } + + if ( autogen ) + { + assert( d3dContext != 0 ); + d3dContext->UpdateSubresource( tex, 0, nullptr, temp.get(), static_cast(rowPitch), static_cast(imageSize) ); + d3dContext->GenerateMips( *textureView ); + } + } + + if (texture != 0) + { + *texture = tex; + } + else + { +#if defined(_DEBUG) || defined(PROFILE) + tex->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("WICTextureLoader")-1, + "WICTextureLoader" + ); +#endif + tex->Release(); + } + } + + return hr; +} + +//-------------------------------------------------------------------------------------- +HRESULT CreateWICTextureFromMemory( _In_ ID3D11Device* d3dDevice, + _In_opt_ ID3D11DeviceContext* d3dContext, + _In_bytecount_(wicDataSize) const uint8_t* wicData, + _In_ size_t wicDataSize, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize + ) +{ + if (!d3dDevice || !wicData || (!texture && !textureView)) + { + return E_INVALIDARG; + } + + if ( !wicDataSize ) + { + return E_FAIL; + } + +#ifdef _M_AMD64 + if ( wicDataSize > 0xFFFFFFFF ) + return HRESULT_FROM_WIN32( ERROR_FILE_TOO_LARGE ); +#endif + + IWICImagingFactory* pWIC = _GetWIC(); + if ( !pWIC ) + return E_NOINTERFACE; + + // Create input stream for memory + ScopedObject stream; + HRESULT hr = pWIC->CreateStream( &stream ); + if ( FAILED(hr) ) + return hr; + + hr = stream->InitializeFromMemory( const_cast( wicData ), static_cast( wicDataSize ) ); + if ( FAILED(hr) ) + return hr; + + // Initialize WIC + ScopedObject decoder; + hr = pWIC->CreateDecoderFromStream( stream.Get(), 0, WICDecodeMetadataCacheOnDemand, &decoder ); + if ( FAILED(hr) ) + return hr; + + ScopedObject frame; + hr = decoder->GetFrame( 0, &frame ); + if ( FAILED(hr) ) + return hr; + + hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), texture, textureView, maxsize ); + if ( FAILED(hr)) + return hr; + +#if defined(_DEBUG) || defined(PROFILE) + if (texture != 0 && *texture != 0) + { + (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("WICTextureLoader")-1, + "WICTextureLoader" + ); + } + + if (textureView != 0 && *textureView != 0) + { + (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName, + sizeof("WICTextureLoader")-1, + "WICTextureLoader" + ); + } +#endif + + return hr; +} + +//-------------------------------------------------------------------------------------- +HRESULT CreateWICTextureFromFile( _In_ ID3D11Device* d3dDevice, + _In_opt_ ID3D11DeviceContext* d3dContext, + _In_z_ const wchar_t* fileName, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize ) +{ + if (!d3dDevice || !fileName || (!texture && !textureView)) + { + return E_INVALIDARG; + } + + IWICImagingFactory* pWIC = _GetWIC(); + if ( !pWIC ) + return E_NOINTERFACE; + + // Initialize WIC + ScopedObject decoder; + HRESULT hr = pWIC->CreateDecoderFromFilename( fileName, 0, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &decoder ); + if ( FAILED(hr) ) + return hr; + + ScopedObject frame; + hr = decoder->GetFrame( 0, &frame ); + if ( FAILED(hr) ) + return hr; + + hr = CreateTextureFromWIC( d3dDevice, d3dContext, frame.Get(), texture, textureView, maxsize ); + if ( FAILED(hr)) + return hr; + +#if defined(_DEBUG) || defined(PROFILE) + if (texture != 0 || textureView != 0) + { + CHAR strFileA[MAX_PATH]; + WideCharToMultiByte( CP_ACP, + WC_NO_BEST_FIT_CHARS, + fileName, + -1, + strFileA, + MAX_PATH, + nullptr, + FALSE + ); + const CHAR* pstrName = strrchr( strFileA, '\\' ); + if (!pstrName) + { + pstrName = strFileA; + } + else + { + pstrName++; + } + + if (texture != 0 && *texture != 0) + { + (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName, + static_cast( strnlen_s(pstrName, MAX_PATH) ), + pstrName + ); + } + + if (textureView != 0 && *textureView != 0 ) + { + (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName, + static_cast( strnlen_s(pstrName, MAX_PATH) ), + pstrName + ); + } + } +#endif + + return hr; +} + +} diff --git a/Shadows/SampleFramework11/WICTextureLoader.h b/Shadows/SampleFramework11/WICTextureLoader.h new file mode 100644 index 0000000..55b9447 --- /dev/null +++ b/Shadows/SampleFramework11/WICTextureLoader.h @@ -0,0 +1,52 @@ +//-------------------------------------------------------------------------------------- +// File: WICTextureLoader.h +// +// Function for loading a WIC image and creating a Direct3D 11 runtime texture for it +// (auto-generating mipmaps if possible) +// +// Note: Assumes application has already called CoInitializeEx +// +// Warning: CreateWICTexture* functions are not thread-safe if given a d3dContext instance for +// auto-gen mipmap support. +// +// Note these functions are useful for images created as simple 2D textures. For +// more complex resources, DDSTextureLoader is an excellent light-weight runtime loader. +// For a full-featured DDS file reader, writer, and texture processing pipeline see +// the 'Texconv' sample and the 'DirectXTex' library. +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// http://go.microsoft.com/fwlink/?LinkId=248926 +// http://go.microsoft.com/fwlink/?LinkId=248929 +//-------------------------------------------------------------------------------------- + +#pragma once + +#include "PCH.h" + +namespace SampleFramework11 +{ + +HRESULT CreateWICTextureFromMemory( _In_ ID3D11Device* d3dDevice, + _In_opt_ ID3D11DeviceContext* d3dContext, + _In_bytecount_(wicDataSize) const uint8_t* wicData, + _In_ size_t wicDataSize, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0 + ); + +HRESULT CreateWICTextureFromFile( _In_ ID3D11Device* d3dDevice, + _In_opt_ ID3D11DeviceContext* d3dContext, + _In_z_ const wchar_t* szFileName, + _Out_opt_ ID3D11Resource** texture, + _Out_opt_ ID3D11ShaderResourceView** textureView, + _In_ size_t maxsize = 0 + ); + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Window.cpp b/Shadows/SampleFramework11/Window.cpp new file mode 100644 index 0000000..acccd02 --- /dev/null +++ b/Shadows/SampleFramework11/Window.cpp @@ -0,0 +1,351 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Window.h" + +using std::wstring; +using std::vector; +using std::map; +using std::tr1::function; +using std::tr1::bind; +using std::tr1::mem_fn; + +namespace SampleFramework11 +{ + +Window::Window( HINSTANCE hinstance, + LPCWSTR name, + DWORD style, + DWORD exStyle, + DWORD clientWidth, + DWORD clientHeight, + LPCWSTR iconResource, + LPCWSTR smallIconResource, + LPCWSTR menuResource, + LPCWSTR accelResource) : style(style), + exStyle(exStyle), + appName(name), + hinstance(hinstance), + hwnd(NULL) +{ + if (hinstance == NULL) + this->hinstance = GetModuleHandle(NULL); + + INITCOMMONCONTROLSEX cce; + cce.dwSize = sizeof(INITCOMMONCONTROLSEX); + cce.dwICC = ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_STANDARD_CLASSES|ICC_STANDARD_CLASSES; + ::InitCommonControlsEx ( &cce ); + + MakeWindow(iconResource, smallIconResource, menuResource); + SetClientArea(clientWidth, clientHeight); + + if (accelResource) + { + accelTable = ::LoadAccelerators(hinstance, accelResource); + if (!accelTable) + throw Win32Exception(::GetLastError()); + } +} + +Window::~Window() +{ + ::DestroyWindow(hwnd); + ::UnregisterClass(appName.c_str(), hinstance); +} + +HWND Window::GetHwnd() const +{ + return hwnd; +} + +HMENU Window::GetMenu() const +{ + return ::GetMenu(hwnd); +} + +HINSTANCE Window::GetHinstance() const +{ + return hinstance; +} + +BOOL Window::IsAlive() const +{ + return ::IsWindow(hwnd); +} + +BOOL Window::IsMinimized() const +{ + return ::IsIconic(hwnd); +} + +void Window::SetWindowStyle(DWORD newStyle) +{ + if (!::SetWindowLongPtr(hwnd, GWL_STYLE, newStyle)) + throw Win32Exception(::GetLastError()); + + style = newStyle; +} + +void Window::SetExtendedStyle(DWORD newExStyle) +{ + if (!::SetWindowLongPtr(hwnd, GWL_EXSTYLE, newExStyle)) + throw Win32Exception(::GetLastError()); + + exStyle = newExStyle; +} + +LONG_PTR Window::GetWindowStyle() const +{ + return GetWindowLongPtr(hwnd, GWL_STYLE); +} + +LONG_PTR Window::GetExtendedStyle() const +{ + return GetWindowLongPtr(hwnd, GWL_EXSTYLE); +} + +void Window::MakeWindow(LPCWSTR sIconResource, LPCWSTR sSmallIconResource, LPCWSTR sMenuResource) +{ + + HICON hIcon = NULL; + if(sIconResource) + { + hIcon = reinterpret_cast(::LoadImage(hinstance, + sIconResource, + IMAGE_ICON, + 0, + 0, + LR_DEFAULTCOLOR)); + } + + HICON hSmallIcon = NULL; + if(sSmallIconResource) + { + hIcon = reinterpret_cast(::LoadImage(hinstance, + sSmallIconResource, + IMAGE_ICON, + 0, + 0, + LR_DEFAULTCOLOR)); + } + + HCURSOR hCursor = ::LoadCursorW(NULL, IDC_ARROW); + + // Register the window class + WNDCLASSEX wc = { sizeof(WNDCLASSEX), + CS_DBLCLKS, + WndProc, + 0, + 0, + hinstance, + hIcon, + hCursor, + NULL, + sMenuResource, + appName.c_str(), + hSmallIcon + }; + + if (!::RegisterClassEx(&wc)) + throw Win32Exception(::GetLastError()); + + // Create the application's window + hwnd = ::CreateWindowEx(exStyle, + appName.c_str(), + appName.c_str(), + style, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + NULL, + NULL, + hinstance, + (void*)this + ); + + if(!hwnd) + throw Win32Exception(::GetLastError()); + } + +void Window::SetWindowPos(INT posX, INT posY) +{ + if (!::SetWindowPos(hwnd, HWND_NOTOPMOST, posX, posY, 0, 0, SWP_NOSIZE)) + throw Win32Exception(::GetLastError()); +} + +void Window::GetWindowPos(INT& posX, INT& posY) const +{ + RECT windowRect; + if (!::GetWindowRect(hwnd, &windowRect)) + throw Win32Exception(::GetLastError()); + posX = windowRect.left; + posY = windowRect.top; +} + +void Window::ShowWindow(bool show) +{ + INT cmdShow = show ? SW_SHOW : SW_HIDE; + + ::ShowWindow(hwnd, cmdShow); +} + +void Window::SetClientArea(INT clientX, INT clientY) +{ + RECT windowRect; + ::SetRect( &windowRect, 0, 0, clientX, clientY ); + + BOOL bIsMenu = (::GetMenu(hwnd) != NULL); + if ( !::AdjustWindowRectEx(&windowRect, style, bIsMenu, exStyle)) + throw Win32Exception(::GetLastError()); + + if (!::SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, SWP_NOMOVE)) + throw Win32Exception(::GetLastError()); +} + +void Window::GetClientArea(INT& clientX, INT& clientY) const +{ + RECT clientRect; + if (!::GetClientRect(hwnd, &clientRect)) + throw Win32Exception(::GetLastError()); + + clientX = clientRect.right; + clientY = clientRect.bottom; +} + +void Window::SetWindowTitle(LPCWSTR title) +{ + if (!::SetWindowText(hwnd, title)) + throw Win32Exception(::GetLastError()); +} + +void Window::SetScrollRanges( INT scrollRangeX, + INT scrollRangeY, + INT posX, + INT posY ) +{ + INT clientX, clientY; + GetClientArea(clientX, clientY); + + // Horizontal first + SCROLLINFO scrollInfo; + scrollInfo.cbSize = sizeof( SCROLLINFO ); + scrollInfo.fMask = SIF_PAGE|SIF_POS|SIF_RANGE; + scrollInfo.nMin = 0; + scrollInfo.nMax = scrollRangeX; + scrollInfo.nPos = posX; + scrollInfo.nTrackPos = 0; + scrollInfo.nPage = static_cast(((FLOAT) clientX / scrollRangeX) * clientX); + ::SetScrollInfo(hwnd, SB_HORZ, &scrollInfo, true); + + // Then vertical + scrollInfo.nMax = scrollRangeX; + scrollInfo.nPos = posY; + scrollInfo.nPage = static_cast(((FLOAT) clientY / scrollRangeX) * clientY); + ::SetScrollInfo(hwnd, SB_VERT, &scrollInfo, true); +} + +INT Window::CreateMessageBox(LPCWSTR message, LPCWSTR title, UINT type) +{ + if (title == NULL) + return ::MessageBox(hwnd, message, appName.c_str(), type); + else + return ::MessageBox(hwnd, message, title, type); +} + +void Window::Maximize() +{ + ::ShowWindow( hwnd, SW_MAXIMIZE ); +} + +void Window::Destroy() +{ + ::DestroyWindow(hwnd); + ::UnregisterClass(appName.c_str(), hinstance); +} + +LRESULT Window::MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + if(TwEventWin(hWnd, uMsg, wParam, lParam)) + return 0; + + if(messageCallbacks.find(uMsg) != messageCallbacks.end()) + { + Callback callback = messageCallbacks[uMsg]; + MsgFunction msgFunction = callback.Function; + return msgFunction(callback.Context, hWnd, uMsg, wParam, lParam); + } + else + { + switch (uMsg) + { + // Window is being destroyed + case WM_DESTROY: + ::PostQuitMessage(0); + return 0; + + // Window is being closed + case WM_CLOSE: + { + DestroyWindow(hwnd); + return 0; + } + } + } + + return ::DefWindowProc(hwnd, uMsg, wParam, lParam); +} + +LRESULT WINAPI Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + switch(uMsg) + { + case WM_NCCREATE: + { + LPCREATESTRUCT pCreateStruct = reinterpret_cast(lParam); + Window* pObj = reinterpret_cast( pCreateStruct->lpCreateParams); + ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast(pCreateStruct->lpCreateParams)); + return ::DefWindowProc(hWnd, uMsg, wParam, lParam); + } + } + + Window* pObj = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA)); + if (pObj) + return pObj->MessageHandler(hWnd, uMsg, wParam, lParam); + else + return ::DefWindowProc(hWnd, uMsg, wParam, lParam); +} + + +void Window::MessageLoop() +{ + // Main message loop: + MSG msg; + + while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + if(!accelTable || !TranslateAccelerator(msg.hwnd, accelTable, &msg)) + { + ::TranslateMessage( &msg ); + ::DispatchMessage( &msg ); + } + } +} + +void Window::RegisterMessageCallback(UINT uMessage, MsgFunction msgFunction, void* context) +{ + Callback callback; + callback.Function = msgFunction; + callback.Context = context; + messageCallbacks[uMessage] = callback; +} + +} \ No newline at end of file diff --git a/Shadows/SampleFramework11/Window.h b/Shadows/SampleFramework11/Window.h new file mode 100644 index 0000000..8724aae --- /dev/null +++ b/Shadows/SampleFramework11/Window.h @@ -0,0 +1,104 @@ +//================================================================================================= +// +// MJP's DX11 Sample Framework +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "PCH.h" + +#include "Exceptions.h" + +namespace SampleFramework11 +{ + +class Window +{ + +// Public types +public: + + typedef LRESULT (*MsgFunction)(void*, HWND, UINT, WPARAM, LPARAM); + +// Constructor and destructor +public: + + Window(HINSTANCE hinstance, + LPCWSTR name = L"SampleCommon Window", + DWORD style = WS_CAPTION|WS_OVERLAPPED|WS_SYSMENU, + DWORD exStyle = WS_EX_APPWINDOW, + DWORD clientWidth = 1280, + DWORD clientHeight = 720, + LPCWSTR iconResource = NULL, + LPCWSTR smallIconResource = NULL, + LPCWSTR menuResource = NULL, + LPCWSTR accelResource = NULL ); + ~Window(); + + + +// Public methods +public: + + HWND GetHwnd() const; + HMENU GetMenu() const; + HINSTANCE GetHinstance() const; + void MessageLoop(); + + BOOL IsAlive() const; + BOOL IsMinimized() const; + LONG_PTR GetWindowStyle() const; + LONG_PTR GetExtendedStyle() const; + void SetWindowStyle(DWORD newStyle ); + void SetExtendedStyle(DWORD newExStyle ); + void Maximize(); + void SetWindowPos(INT posX, INT posY); + void GetWindowPos(INT& posX, INT& posY) const; + void ShowWindow(bool show = true); + void SetClientArea(INT clientX, INT clientY); + void GetClientArea(INT& clientX, INT& clientY) const; + void SetWindowTitle(LPCWSTR title); + void SetScrollRanges(INT scrollRangeX, + INT scrollRangeY, + INT posX, + INT posY); + void Destroy(); + + INT CreateMessageBox(LPCWSTR message, LPCWSTR title = NULL, UINT type = MB_OK); + + void RegisterMessageCallback(UINT message, MsgFunction msgFunction, void* context); + + operator HWND() { return hwnd; } //conversion operator + +// Private methods +private: + void MakeWindow(LPCWSTR iconResource, LPCWSTR smallIconResource, LPCWSTR menuResource); + + LRESULT MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + static LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + +// Private members +private: + + // Window properties + HWND hwnd; // The window handle + HINSTANCE hinstance; // The HINSTANCE of the application + std::wstring appName; // The name of the application + DWORD style; // The current window style + DWORD exStyle; // The extended window style + HACCEL accelTable; // Accelerator table handle + + struct Callback + { + MsgFunction Function; + void* Context; + }; + + std::map messageCallbacks; // Message callback map +}; + +} \ No newline at end of file diff --git a/Shadows/SettingsCompiler/App.config b/Shadows/SettingsCompiler/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Shadows/SettingsCompiler/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Shadows/SettingsCompiler/Properties/AssemblyInfo.cs b/Shadows/SettingsCompiler/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9b6ae17 --- /dev/null +++ b/Shadows/SettingsCompiler/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SettingsCompiler")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SettingsCompiler")] +[assembly: AssemblyCopyright("Copyright © 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("82e71a1c-e522-48b7-80f5-8b6d46fa51fb")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Shadows/SettingsCompiler/SettingTypes.cs b/Shadows/SettingsCompiler/SettingTypes.cs new file mode 100644 index 0000000..71082b0 --- /dev/null +++ b/Shadows/SettingsCompiler/SettingTypes.cs @@ -0,0 +1,463 @@ +using System; +using System.Reflection; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; + +namespace SettingsCompiler +{ + public enum SettingType + { + Float, + Int, + Bool, + Enum, + Direction, + Orientation, + Color, + } + + public struct EnumValue + { + public string Name; + public string Label; + + public EnumValue(string name, string label) + { + this.Name = name; + this.Label = label; + } + } + + public abstract class Setting + { + public string Name; + public string DisplayName = ""; + public SettingType Type; + public string Group = ""; + public String HelpText = ""; + public bool UseAsShaderConstant = true; + + public Setting(FieldInfo field, SettingType type, string group) + { + Type = type; + Name = field.Name; + Group = group; + DisplayNameAttribute.GetDisplayName(field, ref DisplayName); + HelpTextAttribute.GetHelpText(field, ref HelpText); + GroupAttribute.GetGroup(field, ref Group); + UseAsShaderConstant = UseAsShaderConstantAttribute.UseFieldAsShaderConstant(field); + } + + public abstract void WriteDeclaration(List lines); + public abstract void WriteDefinition(List lines); + public abstract void WriteInitialization(List lines); + + public void WriteHLSL(List lines) + { + if(UseAsShaderConstant == false) + return; + + string typeString = ""; + switch(Type) + { + case SettingType.Enum: + case SettingType.Int: + typeString = "int"; + break; + case SettingType.Bool: + typeString = "bool"; + break; + case SettingType.Float: + typeString = "float"; + break; + case SettingType.Direction: + case SettingType.Color: + typeString = "float3"; + break; + case SettingType.Orientation: + typeString = "float4"; + break; + default: + Debug.Assert(false); + break; + } + + lines.Add(" " + typeString + " " + Name + ";"); + } + + public void WriteCBufferStruct(List lines, ref uint cbSize) + { + if(UseAsShaderConstant == false) + return; + + string typeString = ""; + switch(Type) + { + case SettingType.Enum: + case SettingType.Int: + typeString = "int32"; + cbSize += 1; + break; + case SettingType.Bool: + typeString = "bool32"; + cbSize += 1; + break; + case SettingType.Float: + typeString = "float"; + cbSize += 1; + break; + case SettingType.Direction: + case SettingType.Color: + typeString = "Float3"; + if(cbSize % 4 > 1) + typeString = "Float4Align " + typeString; + cbSize += 3; + break; + case SettingType.Orientation: + typeString = "Float4Align Quaternion"; + cbSize += 4; + break; + default: + Debug.Assert(false); + break; + } + + lines.Add(" " + typeString + " " + Name + ";"); + } + + public void WriteCBufferUpdate(List lines) + { + if(UseAsShaderConstant == false) + return; + + lines.Add(string.Format(" CBuffer.Data." + Name + " = {0};", Name)); + } + + public static string FloatString(float num) + { + return num.ToString("F4", CultureInfo.InvariantCulture) + "f"; + } + + public static string MakeParameter(float parameter) + { + return ", " + FloatString(parameter); + } + + public static string MakeParameter(string parameter) + { + return ", \"" + parameter + "\""; + } + + public static string MakeParameter(int parameter) + { + return ", " + parameter; + } + + public static string MakeParameter(bool parameter) + { + return ", " + parameter.ToString().ToLower(); + } + + public static string MakeParameter(Direction parameter) + { + return ", Float3(" + FloatString(parameter.X) + ", " + + FloatString(parameter.Y) + ", " + + FloatString(parameter.Z) + ")"; + } + + public static string MakeParameter(Orientation parameter) + { + return ", Quaternion(" + FloatString(parameter.X) + ", " + + FloatString(parameter.Y) + ", " + + FloatString(parameter.Z) + ", " + + FloatString(parameter.W) + ")"; + } + + public static string MakeParameter(Color parameter) + { + return ", Float3(" + FloatString(parameter.R) + ", " + + FloatString(parameter.G) + ", " + + FloatString(parameter.B) + ")"; + } + } + + public class FloatSetting : Setting + { + public float Value = 0.0f; + public float MinValue = float.MinValue; + public float MaxValue = float.MaxValue; + public float StepSize = 0.01f; + + public FloatSetting(float value, FieldInfo field, string group) + : base(field, SettingType.Float, group) + { + Value = value; + MinValueAttribute.GetMinValue(field, ref MinValue); + MaxValueAttribute.GetMaxValue(field, ref MaxValue); + StepSizeAttribute.GetStepSize(field, ref StepSize); + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern FloatSetting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" FloatSetting " + Name + ";"); + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeParameter(Value); + paramString += MakeParameter(MinValue); + paramString += MakeParameter(MaxValue); + paramString += MakeParameter(StepSize); + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } + + public class IntSetting : Setting + { + public int Value = 0; + public int MinValue = int.MinValue; + public int MaxValue = int.MaxValue; + + public IntSetting(int value, FieldInfo field, string group) + : base(field, SettingType.Int, group) + { + Value = value; + MinValueAttribute.GetMinValue(field, ref MinValue); + MaxValueAttribute.GetMaxValue(field, ref MaxValue); + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern IntSetting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" IntSetting " + Name + ";"); + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeParameter(Value); + paramString += MakeParameter(MinValue); + paramString += MakeParameter(MaxValue); + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } + + public class BoolSetting : Setting + { + public bool Value = false; + + public BoolSetting(bool value, FieldInfo field, string group) + : base(field, SettingType.Bool, group) + { + Value = value; + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern BoolSetting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" BoolSetting " + Name + ";"); + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeParameter(Value); + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } + + public class EnumSetting : Setting + { + public object Value; + public Type EnumType; + public string EnumTypeName; + public int NumEnumValues = 0; + + public EnumSetting(object value, FieldInfo field, Type enumType, string group) + : base(field, SettingType.Enum, group) + { + Value = value; + EnumType = enumType; + NumEnumValues = EnumType.GetEnumValues().Length; + EnumTypeName = EnumType.Name; + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern " + EnumTypeName + "Setting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" " + EnumTypeName + "Setting " + Name + ";"); + } + + private string MakeEnumParameter(object value) + { + string parameter = EnumTypeName + "::" + EnumType.GetEnumName(value); + return ", " + parameter; + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeEnumParameter(Value); + paramString += MakeParameter(NumEnumValues); + paramString += ", " + EnumTypeName + "Labels"; + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } + + public class DirectionSetting : Setting + { + public Direction Value = new Direction(0.0f, 0.0f, 1.0f); + + public DirectionSetting(Direction value, FieldInfo field, string group) + : base(field, SettingType.Direction, group) + { + Value = value; + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern DirectionSetting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" DirectionSetting " + Name + ";"); + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeParameter(Value); + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } + + public class OrientationSetting : Setting + { + public Orientation Value = Orientation.Identity; + + public OrientationSetting(Orientation value, FieldInfo field, string group) + : base(field, SettingType.Orientation, group) + { + Value = value; + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern OrientationSetting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" OrientationSetting " + Name + ";"); + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeParameter(Value); + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } + + public class ColorSetting : Setting + { + public Color Value = new Color(1.0f, 1.0f, 1.0f); + public bool HDR = false; + public float MinValue = float.MinValue; + public float MaxValue = float.MaxValue; + public float StepSize = 0.01f; + + public ColorSetting(Color value, FieldInfo field, string group) + : base(field, SettingType.Color, group) + { + Value = value; + HDR = HDRAttribute.HDRColor(field); + MinValueAttribute.GetMinValue(field, ref MinValue); + MaxValueAttribute.GetMaxValue(field, ref MaxValue); + StepSizeAttribute.GetStepSize(field, ref StepSize); + } + + public override void WriteDeclaration(List lines) + { + lines.Add(" extern ColorSetting " + Name + ";"); + } + + public override void WriteDefinition(List lines) + { + lines.Add(" ColorSetting " + Name + ";"); + } + + public override void WriteInitialization(List lines) + { + string paramString = "tweakBar"; + paramString += MakeParameter(Name); + paramString += MakeParameter(Group); + paramString += MakeParameter(DisplayName); + paramString += MakeParameter(HelpText); + paramString += MakeParameter(Value); + paramString += MakeParameter(HDR); + paramString += MakeParameter(MinValue); + paramString += MakeParameter(MaxValue); + paramString += MakeParameter(StepSize); + lines.Add(" " + Name + ".Initialize(" + paramString + ");"); + lines.Add(" Settings.AddSetting(&" + Name + ");"); + lines.Add(""); + } + } +} \ No newline at end of file diff --git a/Shadows/SettingsCompiler/SettingsCompiler.cs b/Shadows/SettingsCompiler/SettingsCompiler.cs new file mode 100644 index 0000000..07e45af --- /dev/null +++ b/Shadows/SettingsCompiler/SettingsCompiler.cs @@ -0,0 +1,381 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Reflection; +using System.CodeDom.Compiler; +using Microsoft.CSharp; + +namespace SettingsCompiler +{ + class SettingsCompiler + { + static Assembly CompileSettings(string inputFilePath) + { + string fileName = Path.GetFileNameWithoutExtension(inputFilePath); + + string code = File.ReadAllText(inputFilePath); + code = "using SettingsCompiler;\r\n\r\n" + "namespace " + fileName + "\r\n{\r\n" + code; + code += "\r\n}"; + + Dictionary compilerOpts = new Dictionary { { "CompilerVersion", "v4.0" } }; + CSharpCodeProvider compiler = new CSharpCodeProvider(compilerOpts); + + string[] sources = { code }; + CompilerParameters compilerParams = new CompilerParameters(); + compilerParams.GenerateInMemory = true; + compilerParams.ReferencedAssemblies.Add("System.dll"); + compilerParams.ReferencedAssemblies.Add("SettingsCompilerAttributes.dll"); + CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, sources); + if(results.Errors.HasErrors) + { + string errMsg = "Errors were returned from the C# compiler:\r\n\r\n"; + foreach(CompilerError compilerError in results.Errors) + { + int lineNum = compilerError.Line - 4; + errMsg += inputFilePath + "(" + lineNum + "): " + compilerError.ErrorText + "\r\n"; + } + throw new Exception(errMsg); + } + + return results.CompiledAssembly; + } + + static void ReflectType(Type settingsType, List settings, List enumTypes, + string group) + { + object settingsInstance = Activator.CreateInstance(settingsType); + + BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; + FieldInfo[] fields = settingsType.GetFields(flags); + foreach(FieldInfo field in fields) + { + foreach(Setting setting in settings) + if(setting.Name == field.Name) + throw new Exception(string.Format("Duplicate setting \"{0}\" detected", setting.Name)); + + Type fieldType = field.FieldType; + object fieldValue = field.GetValue(settingsInstance); + if(fieldType == typeof(float)) + settings.Add(new FloatSetting((float)fieldValue, field, group)); + else if(fieldType == typeof(int)) + settings.Add(new IntSetting((int)fieldValue, field, group)); + else if(fieldType == typeof(bool)) + settings.Add(new BoolSetting((bool)fieldValue, field, group)); + else if(fieldType.IsEnum) + { + if(enumTypes.Contains(fieldType) == false) + enumTypes.Add(fieldType); + settings.Add(new EnumSetting(fieldValue, field, fieldType, group)); + } + else if(fieldType == typeof(Direction)) + settings.Add(new DirectionSetting((Direction)fieldValue, field, group)); + else if(fieldType == typeof(Orientation)) + settings.Add(new OrientationSetting((Orientation)fieldValue, field, group)); + else if(fieldType == typeof(Color)) + settings.Add(new ColorSetting((Color)fieldValue, field, group)); + else + throw new Exception("Invalid type for setting " + field.Name); + } + } + + static void ReflectSettings(Assembly assembly, string inputFilePath, List settings, List enumTypes) + { + string filePath = Path.GetFileNameWithoutExtension(inputFilePath); + Type settingsType = assembly.GetType(filePath + ".Settings", false); + if(settingsType == null) + throw new Exception("Settings file " + inputFilePath + " doesn't define a \"Settings\" class"); + + ReflectType(settingsType, settings, enumTypes, ""); + + Type[] nestedTypes = settingsType.GetNestedTypes(); + foreach(Type nestedType in nestedTypes) + ReflectType(nestedType, settings, enumTypes, nestedType.Name); + } + + static void WriteIfChanged(List lines, string outputPath) + { + string outputText = ""; + foreach(string line in lines) + outputText += line + "\r\n"; + + string fileText = ""; + if(File.Exists(outputPath)) + fileText = File.ReadAllText(outputPath); + + int idx = fileText.IndexOf("// ================================================================================================"); + if(idx >= 0) + outputText += "\r\n" + fileText.Substring(idx); + + if(fileText != outputText) + File.WriteAllText(outputPath, outputText); + } + + public static void WriteEnumTypes(List lines, List enumTypes) + { + foreach(Type enumType in enumTypes) + { + if(enumType.GetEnumUnderlyingType() != typeof(int)) + throw new Exception("Invalid underlying type for enum " + enumType.Name + ", must be int"); + string[] enumNames = enumType.GetEnumNames(); + int numEnumValues = enumNames.Length; + + Array values = enumType.GetEnumValues(); + int[] enumValues = new int[numEnumValues]; + for(int i = 0; i < numEnumValues; ++i) + enumValues[i] = (int)values.GetValue(i); + + lines.Add("enum class " + enumType.Name); + lines.Add("{"); + for(int i = 0; i < values.Length; ++i) + lines.Add(" " + enumNames[i] + " = " + enumValues[i] + ","); + lines.Add("\r\n NumValues"); + + lines.Add("};\r\n"); + + lines.Add("typedef EnumSettingT<" + enumType.Name + "> " + enumType.Name + "Setting;\r\n"); + } + } + + public static void WriteEnumLabels(List lines, List enumTypes) + { + foreach(Type enumType in enumTypes) + { + string[] enumNames = enumType.GetEnumNames(); + int numEnumValues = enumNames.Length; + string[] enumLabels = new string[numEnumValues]; + + for(int i = 0; i < numEnumValues; ++i) + { + FieldInfo enumField = enumType.GetField(enumNames[i]); + EnumLabelAttribute attr = enumField.GetCustomAttribute(); + enumLabels[i] = attr != null ? attr.Label : enumNames[i]; + } + + lines.Add("static const char* " + enumType.Name + "Labels[" + numEnumValues + "] ="); + lines.Add("{"); + foreach(string label in enumLabels) + lines.Add(" \"" + label + "\","); + + lines.Add("};\r\n"); + } + } + + static void GenerateHeader(List settings, string outputName, string outputPath, + List enumTypes) + { + List lines = new List(); + + lines.Add("#pragma once"); + lines.Add(""); + lines.Add("#include \"SampleFramework11/PCH.h\""); + lines.Add("#include \"SampleFramework11/Settings.h\""); + lines.Add("#include \"SampleFramework11/GraphicsTypes.h\""); + lines.Add(""); + lines.Add("using namespace SampleFramework11;"); + lines.Add(""); + + WriteEnumTypes(lines, enumTypes); + + lines.Add("namespace " + outputName); + lines.Add("{"); + + uint numCBSettings = 0; + foreach(Setting setting in settings) + { + setting.WriteDeclaration(lines); + if(setting.UseAsShaderConstant) + ++numCBSettings; + } + + if(numCBSettings > 0) + { + lines.Add(""); + lines.Add(string.Format(" struct {0}CBuffer", outputName)); + lines.Add(" {"); + + uint cbSize = 0; + foreach(Setting setting in settings) + setting.WriteCBufferStruct(lines, ref cbSize); + + lines.Add(" };"); + lines.Add(""); + lines.Add(string.Format(" extern ConstantBuffer<{0}CBuffer> CBuffer;", outputName)); + } + + lines.Add(""); + lines.Add(" void Initialize(ID3D11Device* device);"); + lines.Add(" void UpdateCBuffer(ID3D11DeviceContext* context);"); + + lines.Add("};"); + + WriteIfChanged(lines, outputPath); + } + + static void GenerateCPP(List settings, string outputName, string outputPath, + List enumTypes) + { + List lines = new List(); + + lines.Add("#include \"PCH.h\""); + lines.Add("#include \"" + outputName + ".h\""); + lines.Add(""); + lines.Add("using namespace SampleFramework11;"); + lines.Add(""); + + WriteEnumLabels(lines, enumTypes); + + lines.Add("namespace " + outputName); + lines.Add("{"); + + uint numCBSettings = 0; + foreach(Setting setting in settings) + { + setting.WriteDefinition(lines); + if(setting.UseAsShaderConstant) + ++numCBSettings; + } + + if(numCBSettings > 0) + { + lines.Add(""); + lines.Add(string.Format(" ConstantBuffer<{0}CBuffer> CBuffer;", outputName)); + } + + lines.Add(""); + lines.Add(" void Initialize(ID3D11Device* device)"); + lines.Add(" {"); + lines.Add(" TwBar* tweakBar = Settings.TweakBar();"); + lines.Add(""); + + foreach(Setting setting in settings) + setting.WriteInitialization(lines); + + if(numCBSettings > 0) + lines.Add(" CBuffer.Initialize(device);"); + + lines.Add(" }"); + + lines.Add(""); + lines.Add(" void UpdateCBuffer(ID3D11DeviceContext* context)"); + lines.Add(" {"); + + foreach(Setting setting in settings) + setting.WriteCBufferUpdate(lines); + + if(numCBSettings > 0) + { + lines.Add(""); + lines.Add(" CBuffer.ApplyChanges(context);"); + lines.Add(" CBuffer.SetVS(context, 7);"); + lines.Add(" CBuffer.SetHS(context, 7);"); + lines.Add(" CBuffer.SetDS(context, 7);"); + lines.Add(" CBuffer.SetGS(context, 7);"); + lines.Add(" CBuffer.SetPS(context, 7);"); + lines.Add(" CBuffer.SetCS(context, 7);"); + } + + lines.Add(" }"); + + lines.Add("}"); + + WriteIfChanged(lines, outputPath); + } + + static void GenerateHLSL(List settings, string outputName, string outputPath, + List enumTypes) + { + uint numCBSettings = 0; + foreach(Setting setting in settings) + { + if(setting.UseAsShaderConstant) + ++numCBSettings; + } + + List lines = new List(); + + if(numCBSettings == 0) + WriteIfChanged(lines, outputPath); + + lines.Add(string.Format("cbuffer {0} : register(b7)", outputName)); + lines.Add("{"); + + foreach(Setting setting in settings) + setting.WriteHLSL(lines); + + lines.Add("}"); + lines.Add(""); + + foreach(Type enumType in enumTypes) + { + string[] enumNames = enumType.GetEnumNames(); + Array enumValues = enumType.GetEnumValues(); + for(int i = 0; i < enumNames.Length; ++i) + { + string line = "static const int " + enumType.Name + "_"; + line += enumNames[i] + " = " + (int)enumValues.GetValue(i) + ";"; + lines.Add(line); + } + + lines.Add(""); + } + + WriteIfChanged(lines, outputPath); + } + + static void Run(string[] args) + { + if(args.Length < 1) + throw new Exception("Invalid command-line parameters"); + + List settings = new List(); + List enumTypes = new List(); + + string filePath = args[0]; + string fileName = Path.GetFileNameWithoutExtension(filePath); + + Assembly compiledAssembly = CompileSettings(filePath); + ReflectSettings(compiledAssembly, filePath, settings, enumTypes); + + string outputDir = Path.GetDirectoryName(filePath); + string outputPath = Path.Combine(outputDir, fileName) + ".h"; + GenerateHeader(settings, fileName, outputPath, enumTypes); + + outputPath = Path.Combine(outputDir, fileName) + ".cpp"; + GenerateCPP(settings, fileName, outputPath, enumTypes); + + outputPath = Path.Combine(outputDir, fileName) + ".hlsl"; + GenerateHLSL(settings, fileName, outputPath, enumTypes); + + // Generate a dummy file that MSBuild can use to track dependencies + outputPath = Path.Combine(outputDir, fileName) + ".deps"; + File.WriteAllText(outputPath, "This file is output to allow MSBuild to track dependencies"); + } + + static int Main(string[] args) + { + + if(Debugger.IsAttached) + { + Run(args); + } + else + { + try + { + Run(args); + } + catch(Exception e) + { + Console.WriteLine("An error ocurred during settings compilation:\n\n" + e.Message); + return 1; + } + } + + return 0; + } + } +} \ No newline at end of file diff --git a/Shadows/SettingsCompiler/SettingsCompiler.csproj b/Shadows/SettingsCompiler/SettingsCompiler.csproj new file mode 100644 index 0000000..3f24724 --- /dev/null +++ b/Shadows/SettingsCompiler/SettingsCompiler.csproj @@ -0,0 +1,70 @@ + + + + + Debug + AnyCPU + {A01DD775-61A4-42F1-9B2A-772E50456A68} + Exe + Properties + SettingsCompiler + SettingsCompiler + v4.5 + 512 + + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + true + false + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + + + + + + + + + + + + + + + + + + + {c61f717f-ff92-4d36-930e-1f49c198cbee} + SettingsCompilerAttributes + + + + + copy $(TargetPath) $(SolutionDir)$(TargetFileName) + + + \ No newline at end of file diff --git a/Shadows/SettingsCompilerAttributes/Attributes.cs b/Shadows/SettingsCompilerAttributes/Attributes.cs new file mode 100644 index 0000000..e12b4c8 --- /dev/null +++ b/Shadows/SettingsCompilerAttributes/Attributes.cs @@ -0,0 +1,254 @@ +using System; +using System.Reflection; + +namespace SettingsCompiler +{ + public struct Direction + { + public float X; + public float Y; + public float Z; + + public Direction(float x, float y, float z) + { + X = x; + Y = y; + Z = z; + } + } + + public struct Color + { + public float R; + public float G; + public float B; + + public Color(float r, float g, float b) + { + R = r; + G = g; + B = b; + } + } + + public struct Orientation + { + public float X; + public float Y; + public float Z; + public float W; + + public static Orientation Identity = new Orientation(0.0f, 0.0f, 0.0f, 1.0f); + + public Orientation(float x, float y, float z, float w) + { + X = x; + Y = y; + Z = z; + W = w; + } + } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public class DisplayNameAttribute : Attribute + { + public readonly string DisplayName; + + public DisplayNameAttribute(string displayName) + { + this.DisplayName = displayName; + } + + public static void GetDisplayName(FieldInfo field, ref string displayName) + { + DisplayNameAttribute attr = field.GetCustomAttribute(); + if(attr != null) + displayName = attr.DisplayName; + } + } + + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = false)] + public class HelpTextAttribute : Attribute + { + public readonly string HelpText; + + public HelpTextAttribute(string helpText) + { + this.HelpText = helpText; + } + + public static void GetHelpText(FieldInfo field, ref string helpText) + { + HelpTextAttribute attr = field.GetCustomAttribute(); + if(attr != null) + helpText = attr.HelpText; + } + } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public class GroupAttribute : Attribute + { + public readonly string Group; + + public GroupAttribute(string group) + { + this.Group = group; + } + + public static void GetGroup(FieldInfo field, ref string group) + { + GroupAttribute attr = field.GetCustomAttribute(); + if(attr != null) + group = attr.Group; + } + } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public class MinValueAttribute : Attribute + { + public readonly float MinValueFloat; + public readonly int MinValueInt; + + public MinValueAttribute(float minValue) + { + this.MinValueFloat = minValue; + this.MinValueInt = (int)minValue; + } + + public MinValueAttribute(int minValue) + { + this.MinValueFloat = (float)minValue; + this.MinValueInt = minValue; + } + + public static void GetMinValue(FieldInfo field, ref float minValue) + { + MinValueAttribute attr = field.GetCustomAttribute(); + if(attr != null) + minValue = attr.MinValueFloat; + } + + public static void GetMinValue(FieldInfo field, ref int minValue) + { + MinValueAttribute attr = field.GetCustomAttribute(); + if(attr != null) + minValue = attr.MinValueInt; + } + } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public class MaxValueAttribute : Attribute + { + public readonly float MaxValueFloat; + public readonly int MaxValueInt; + + public MaxValueAttribute(float maxValue) + { + this.MaxValueFloat = maxValue; + this.MaxValueInt = (int)maxValue; + } + + public MaxValueAttribute(int maxValue) + { + this.MaxValueFloat = (float)maxValue; + this.MaxValueInt = maxValue; + } + + public static void GetMaxValue(FieldInfo field, ref float maxValue) + { + MaxValueAttribute attr = field.GetCustomAttribute(); + if(attr != null) + maxValue = attr.MaxValueFloat; + } + + public static void GetMaxValue(FieldInfo field, ref int maxValue) + { + MaxValueAttribute attr = field.GetCustomAttribute(); + if(attr != null) + maxValue = attr.MaxValueInt; + } + } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public class StepSizeAttribute : System.Attribute + { + public readonly float StepSizeFloat; + public readonly int StepSizeInt; + + public StepSizeAttribute(float stepSize) + { + this.StepSizeFloat = stepSize; + this.StepSizeInt = (int)stepSize; + } + + public StepSizeAttribute(int stepSize) + { + this.StepSizeFloat = (float)stepSize; + this.StepSizeInt = stepSize; + } + + public static void GetStepSize(FieldInfo field, ref float stepSize) + { + StepSizeAttribute attr = field.GetCustomAttribute(); + if(attr != null) + stepSize = attr.StepSizeFloat; + } + + public static void GetStepSize(FieldInfo field, ref int stepSize) + { + StepSizeAttribute attr = field.GetCustomAttribute(); + if(attr != null) + stepSize = attr.StepSizeInt; + } + } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public class EnumLabelAttribute : System.Attribute + { + public readonly string Label; + public EnumLabelAttribute(string label) + { + Label = label; + } + } + + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = false)] + public class UseAsShaderConstantAttribute : Attribute + { + public readonly bool UseAsShaderConstant = true; + + public UseAsShaderConstantAttribute(bool useAsShaderConstant) + { + this.UseAsShaderConstant = useAsShaderConstant; + } + + public static bool UseFieldAsShaderConstant(FieldInfo field) + { + UseAsShaderConstantAttribute attr = field.GetCustomAttribute(); + if(attr != null) + return attr.UseAsShaderConstant; + else + return true; + } + } + + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = false)] + public class HDRAttribute : Attribute + { + public readonly bool HDR = false; + + public HDRAttribute(bool hdr) + { + this.HDR = hdr; + } + + public static bool HDRColor(FieldInfo field) + { + HDRAttribute attr = field.GetCustomAttribute(); + if(attr != null) + return attr.HDR; + else + return false; + } + } +} \ No newline at end of file diff --git a/Shadows/SettingsCompilerAttributes/Properties/AssemblyInfo.cs b/Shadows/SettingsCompilerAttributes/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..91f9e29 --- /dev/null +++ b/Shadows/SettingsCompilerAttributes/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SettingsCompilerAttributes")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SettingsCompilerAttributes")] +[assembly: AssemblyCopyright("Copyright © 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a4917329-37f4-41b3-bedd-b7a7ce76bf2d")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Shadows/SettingsCompilerAttributes/SettingsCompilerAttributes.csproj b/Shadows/SettingsCompilerAttributes/SettingsCompilerAttributes.csproj new file mode 100644 index 0000000..8a9aad5 --- /dev/null +++ b/Shadows/SettingsCompilerAttributes/SettingsCompilerAttributes.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {C61F717F-FF92-4D36-930E-1F49C198CBEE} + Library + Properties + SettingsCompilerAttributes + SettingsCompilerAttributes + v4.5 + 512 + + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + false + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + + + + + + + + + + + + + + + + + copy $(TargetPath) $(SolutionDir)$(TargetFileName) + + + \ No newline at end of file diff --git a/Shadows/SetupShadows.hlsl b/Shadows/SetupShadows.hlsl new file mode 100644 index 0000000..f63883a --- /dev/null +++ b/Shadows/SetupShadows.hlsl @@ -0,0 +1,339 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Includes +//================================================================================================= +#include "SharedConstants.h" +#include "AppSettings.hlsl" + +//================================================================================================= +// Constant buffers +//================================================================================================= +cbuffer SetupConstants : register(b0) +{ + float4x4 GlobalShadowMatrix; + float4x4 ViewProjInv; + float3 CameraRight; + float CameraNearClip; + float CameraFarClip; +} + +//================================================================================================= +// Resources +//================================================================================================= +Texture2D DepthReductionOutput : register(t0); +RWStructuredBuffer ShadowRenderingMatrices : register(u0); +RWStructuredBuffer CascadeSplits : register(u1); +RWStructuredBuffer CascadeOffsets : register(u2); +RWStructuredBuffer CascadeScales : register(u3); +RWStructuredBuffer CascadePlanes : register(u4); + +float4x4 OrthographicProjection(float l, float b, float r, + float t, float zn, float zf) +{ + return float4x4(float4(2.0f / (r - l), 0, 0, 0), + float4(0, 2.0f / (t - b), 0, 0), + float4(0, 0, 1 / (zf - zn), 0), + float4((l + r) / (l - r), (t + b)/(b - t), zn / (zn - zf), 1)); +} + +float4x4 InverseRotationTranslation(in float3x3 r, in float3 t) +{ + float4x4 inv = float4x4(float4(r._11_21_31, 0.0f), + float4(r._12_22_32, 0.0f), + float4(r._13_23_33, 0.0f), + float4(0.0f, 0.0f, 0.0f, 1.0f)); + inv[3][0] = -dot(t, r[0]); + inv[3][1] = -dot(t, r[1]); + inv[3][2] = -dot(t, r[2]); + return inv; +} + +float4x4 InverseScaleTranslation(in float4x4 m) +{ + float4x4 inv = float4x4(float4(1.0f, 0.0f, 0.0f, 0.0f), + float4(0.0f, 1.0f, 0.0f, 0.0f), + float4(0.0f, 0.0f, 1.0f, 0.0f), + float4(0.0f, 0.0f, 0.0f, 1.0f)); + + inv[0][0] = 1.0f / m[0][0]; + inv[1][1] = 1.0f / m[1][1]; + inv[2][2] = 1.0f / m[2][2]; + inv[3][0] = -m[3][0] * inv[0][0]; + inv[3][1] = -m[3][1] * inv[1][1]; + inv[3][2] = -m[3][2] * inv[2][2]; + + return inv; +} + +float4 PlaneFromPoints(in float3 point1, in float3 point2, in float3 point3) +{ + float3 v21 = point1 - point2; + float3 v31 = point1 - point3; + + float3 n = normalize(cross(v21, v31)); + float d = -dot(n, point1); + + return float4(n, d); +} + +[numthreads(NumCascades, 1, 1)] +void SetupCascades(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID, + uint ThreadIndex : SV_GroupIndex) +{ + float sMapSize = 512.0f; + if(ShadowMapSize == ShadowMapSize_SMSize1024) + sMapSize = 1024.0f; + else if(ShadowMapSize == ShadowMapSize_SMSize2048) + sMapSize = 2048.0f; + + const float2 ReductionDepth = DepthReductionOutput[uint2(0, 0)]; + const float MinDistance = AutoComputeDepthBounds ? ReductionDepth.x : MinCascadeDistance; + const float MaxDistance = AutoComputeDepthBounds ? ReductionDepth.y : MaxCascadeDistance; + + // Compute the split distances based on the partitioning mode + float cascadeSplits[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + + if(PartitionMode == PartitionMode_Manual) + { + cascadeSplits[0] = MinDistance + SplitDistance0 * MaxDistance; + cascadeSplits[1] = MinDistance + SplitDistance1 * MaxDistance; + cascadeSplits[2] = MinDistance + SplitDistance2 * MaxDistance; + cascadeSplits[3] = MinDistance + SplitDistance3 * MaxDistance; + } + else if(PartitionMode == PartitionMode_Logarithmic + || PartitionMode == PartitionMode_PSSM) + { + float lambda = 1.0f; + if(PartitionMode == PartitionMode_PSSM) + lambda = PSSMLambda; + + float nearClip = CameraNearClip; + float farClip = CameraFarClip; + float clipRange = farClip - nearClip; + + float minZ = nearClip + MinDistance * clipRange; + float maxZ = nearClip + MaxDistance * clipRange; + + float range = maxZ - minZ; + float ratio = maxZ / minZ; + + for(uint i = 0; i < NumCascades; ++i) + { + float p = (i + 1) / float(NumCascades); + float logScale = minZ * pow(abs(ratio), p); + float uniformScale = minZ + range * p; + float d = lambda * (logScale - uniformScale) + uniformScale; + cascadeSplits[i] = (d - nearClip) / clipRange; + } + } + + const uint cascadeIdx = ThreadIndex; + + // Get the 8 points of the view frustum in world space + float3 frustumCornersWS[8] = + { + float3(-1.0f, 1.0f, 0.0f), + float3( 1.0f, 1.0f, 0.0f), + float3( 1.0f, -1.0f, 0.0f), + float3(-1.0f, -1.0f, 0.0f), + float3(-1.0f, 1.0f, 1.0f), + float3( 1.0f, 1.0f, 1.0f), + float3( 1.0f, -1.0f, 1.0f), + float3(-1.0f, -1.0f, 1.0f), + }; + + float prevSplitDist = cascadeIdx == 0 ? MinDistance : cascadeSplits[cascadeIdx - 1]; + float splitDist = cascadeSplits[cascadeIdx]; + + [unroll] + for(uint i = 0; i < 8; ++i) + { + float4 corner = mul(float4(frustumCornersWS[i], 1.0f), ViewProjInv); + frustumCornersWS[i] = corner.xyz / corner.w; + } + + // Get the corners of the current cascade slice of the view frustum + [unroll] + for(i = 0; i < 4; ++i) + { + float3 cornerRay = frustumCornersWS[i + 4] - frustumCornersWS[i]; + float3 nearCornerRay = cornerRay * prevSplitDist; + float3 farCornerRay = cornerRay * splitDist; + frustumCornersWS[i + 4] = frustumCornersWS[i] + farCornerRay; + frustumCornersWS[i] = frustumCornersWS[i] + nearCornerRay; + } + + // Calculate the centroid of the view frustum slice + float3 frustumCenter = 0.0f; + [unroll] + for(i = 0; i < 8; ++i) + frustumCenter += frustumCornersWS[i]; + frustumCenter /= 8.0f; + + // Pick the up vector to use for the light camera + float3 upDir = CameraRight; + + // This needs to be constant it to be stable + if(StabilizeCascades) + upDir = float3(0.0f, 1.0f, 0.0f); + + // Create a temporary view matrix for the light + float3 lightCameraPos = frustumCenter; + float3x3 lightCameraRot; + lightCameraRot[2] = -LightDirection; + lightCameraRot[0] = normalize(cross(upDir, lightCameraRot[2])); + lightCameraRot[1] = cross(lightCameraRot[2], lightCameraRot[0]); + float4x4 lightView = InverseRotationTranslation(lightCameraRot, lightCameraPos); + + float3 minExtents; + float3 maxExtents; + if(StabilizeCascades) + { + // Calculate the radius of a bounding sphere surrounding the frustum corners + float sphereRadius = 0.0f; + [unroll] + for(i = 0; i < 8; ++i) + { + float dist = length(frustumCornersWS[i] - frustumCenter); + sphereRadius = max(sphereRadius, dist); + } + + sphereRadius = ceil(sphereRadius * 16.0f) / 16.0f; + + maxExtents = sphereRadius; + minExtents = -maxExtents; + } + else + { + // Calculate an AABB around the frustum corners + const float MaxFloat = 3.402823466e+38F; + float3 mins = float3(MaxFloat, MaxFloat, MaxFloat); + float3 maxes = float3(-MaxFloat, -MaxFloat, -MaxFloat); + for(i = 0; i < 8; ++i) + { + float3 corner = mul(float4(frustumCornersWS[i], 1.0f), lightView).xyz; + mins = min(mins, corner); + maxes = max(maxes, corner); + } + + minExtents = mins; + maxExtents = maxes; + + // Adjust the min/max to accommodate the filtering size + float scale = (sMapSize + 9.0f) / float(sMapSize); + minExtents.x *= scale; + minExtents.y *= scale; + maxExtents.x *= scale; + maxExtents.y *= scale; + } + + float3 cascadeExtents = maxExtents - minExtents; + + // Get position of the shadow camera + float3 shadowCameraPos = frustumCenter + LightDirection * -minExtents.z; + + // Come up with a new orthographic camera for the shadow caster + float4x4 shadowView = InverseRotationTranslation(lightCameraRot, shadowCameraPos); + float4x4 shadowProj = OrthographicProjection(minExtents.x, minExtents.y, maxExtents.x, + maxExtents.y, 0.0f, cascadeExtents.z); + + if(StabilizeCascades) + { + // Create the rounding matrix, by projecting the world-space origin and determining + // the fractional offset in texel space + float4x4 shadowMatrix = mul(shadowView, shadowProj); + float3 shadowOrigin = 0.0f; + shadowOrigin = mul(float4(shadowOrigin, 1.0f), shadowMatrix).xyz; + shadowOrigin *= (sMapSize / 2.0f); + + float3 roundedOrigin = round(shadowOrigin); + float3 roundOffset = roundedOrigin - shadowOrigin; + roundOffset = roundOffset * (2.0f / sMapSize); + roundOffset.z = 0.0f; + + shadowProj[3][0] += roundOffset.x; + shadowProj[3][1] += roundOffset.y; + } + + float4x4 shadowRenderingMatrix = mul(shadowView, shadowProj); + ShadowRenderingMatrices[cascadeIdx * 4 + 0] = shadowRenderingMatrix._11_21_31_41; + ShadowRenderingMatrices[cascadeIdx * 4 + 1] = shadowRenderingMatrix._12_22_32_42; + ShadowRenderingMatrices[cascadeIdx * 4 + 2] = shadowRenderingMatrix._13_23_33_43; + ShadowRenderingMatrices[cascadeIdx * 4 + 3] = shadowRenderingMatrix._14_24_34_44; + + float4x4 invView = float4x4(float4(lightCameraRot[0], 0.0f), + float4(lightCameraRot[1], 0.0f), + float4(lightCameraRot[2], 0.0f), + float4(shadowCameraPos, 1.0f)); + float4x4 invProj = InverseScaleTranslation(shadowProj); + + float4x4 shadowRenderingInv = mul(invProj, invView); + + // Create frustum planes for the shadow rendering matrix + float3 corners[8] = + { + float3( 1.0f, -1.0f, 0.0f), + float3(-1.0f, -1.0f, 0.0f), + float3( 1.0f, 1.0f, 0.0f), + float3(-1.0f, 1.0f, 0.0f), + float3( 1.0f, -1.0f, 1.0f), + float3(-1.0f, -1.0f, 1.0f), + float3( 1.0f, 1.0f, 1.0f), + float3(-1.0f, 1.0f, 1.0f), + }; + + [unroll] + for(i = 0; i < 8; ++i) + { + float4 corner = mul(float4(corners[i], 1.0f), shadowRenderingInv); + corners[i] = corner.xyz / corner.w; + } + + float4 frustumPlanes[6]; + frustumPlanes[0] = PlaneFromPoints(corners[0], corners[4], corners[2]); + frustumPlanes[1] = PlaneFromPoints(corners[1], corners[3], corners[5]); + frustumPlanes[2] = PlaneFromPoints(corners[3], corners[2], corners[7]); + frustumPlanes[3] = PlaneFromPoints(corners[1], corners[5], corners[0]); + frustumPlanes[4] = PlaneFromPoints(corners[5], corners[7], corners[4]); + frustumPlanes[5] = PlaneFromPoints(corners[1], corners[0], corners[3]); + + [unroll] + for(i = 0; i < 6; ++i) + CascadePlanes[i + cascadeIdx * 6] = frustumPlanes[i]; + + // Apply the scale/offset matrix, which transforms from [-1,1] + // post-projection space to [0,1] UV space + float4x4 texScaleBias = float4x4(float4(0.5f, 0.0f, 0.0f, 0.0f), + float4(0.0f, -0.5f, 0.0f, 0.0f), + float4(0.0f, 0.0f, 1.0f, 0.0f), + float4(0.5f, 0.5f, 0.0f, 1.0f)); + float4x4 texScaleBiasInv = InverseScaleTranslation(texScaleBias); + + // Store the split distance in terms of view space depth + const float clipDist = CameraFarClip - CameraNearClip; + CascadeSplits[cascadeIdx] = CameraNearClip + splitDist * clipDist; + + // Calculate the position of the lower corner of the cascade partition, in the UV space + // of the first cascade partition + float4x4 invCascadeMat = mul(mul(texScaleBiasInv, invProj), invView); + float3 cascadeCorner = mul(float4(0.0f, 0.0f, 0.0f, 1.0f), invCascadeMat).xyz; + cascadeCorner = mul(float4(cascadeCorner, 1.0f), GlobalShadowMatrix).xyz; + + // Do the same for the upper corner + float3 otherCorner = mul(float4(1.0f, 1.0f, 1.0f, 1.0f), invCascadeMat).xyz; + otherCorner = mul(float4(otherCorner, 1.0f), GlobalShadowMatrix).xyz; + + // Calculate the scale and offset + float3 cascadeScale = 1.0f / (otherCorner - cascadeCorner); + CascadeOffsets[cascadeIdx] = float4(-cascadeCorner, 0.0f); + CascadeScales[cascadeIdx] = float4(cascadeScale, 1.0f); +} \ No newline at end of file diff --git a/Shadows/Shadows.cpp b/Shadows/Shadows.cpp new file mode 100644 index 0000000..f59e35a --- /dev/null +++ b/Shadows/Shadows.cpp @@ -0,0 +1,358 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#include "PCH.h" + +#include "Shadows.h" +#include "resource.h" +#include "SharedConstants.h" +#include "AppSettings.h" + +#include "SampleFramework11/InterfacePointers.h" +#include "SampleFramework11/Window.h" +#include "SampleFramework11/DeviceManager.h" +#include "SampleFramework11/Input.h" +#include "SampleFramework11/SpriteRenderer.h" +#include "SampleFramework11/Model.h" +#include "SampleFramework11/Utility.h" +#include "SampleFramework11/Camera.h" +#include "SampleFramework11/ShaderCompilation.h" +#include "SampleFramework11/Profiler.h" +#include "SampleFramework11/Settings.h" +#include "SampleFramework11/TwHelper.h" + +using namespace SampleFramework11; +using std::wstring; + +const uint32 WindowWidth = 1280; +const uint32 WindowHeight = 720; +const float WindowWidthF = static_cast(WindowWidth); +const float WindowHeightF = static_cast(WindowHeight); + +static const float NearClip = 0.25f; +static const float FarClip = 250.0f; + +// Mesh filenames +static const wstring MeshFileNames[] = +{ + L"Powerplant\\Powerplant.sdkmesh", + L"Tower\\Tower.sdkmesh", + L"Columns\\Columns.sdkmesh", +}; + +// Scale values applied to the mesh +static const float MeshScales[] = +{ + 0.5f, + 0.025f, + 0.25f, +}; + +static const float CharacterScale = 1.0f; +static const Float3 CharacterPos = Float3(25.0f, 0.0f, 3.0f); + +ShadowsApp::ShadowsApp() : App(L"Shadows", MAKEINTRESOURCEW(IDI_ICON1)), + camera(WindowWidthF / WindowHeightF, XM_PIDIV4 * 0.75f, NearClip, FarClip) +{ + deviceManager.SetBackBufferWidth(WindowWidth); + deviceManager.SetBackBufferHeight(WindowHeight); + deviceManager.SetMinFeatureLevel(D3D_FEATURE_LEVEL_11_0); + + window.SetClientArea(WindowWidth, WindowHeight); +} + +void ShadowsApp::BeforeReset() +{ + App::BeforeReset(); +} + +void ShadowsApp::AfterReset() +{ + App::AfterReset(); + + float aspect = static_cast(deviceManager.BackBufferWidth()) / deviceManager.BackBufferHeight(); + camera.SetAspectRatio(aspect); + + CreateRenderTargets(); + + postProcessor.AfterReset(deviceManager.BackBufferWidth(), deviceManager.BackBufferHeight()); +} + +void ShadowsApp::Initialize() +{ + App::Initialize(); + + AppSettings::Initialize(deviceManager.Device()); + + TwHelper::SetGlobalHelpText("Shadows Sample - implements various cascaded shadow map techniques, " + "as well as several methods for filtering shadow maps"); + + ID3D11DevicePtr device = deviceManager.Device(); + ID3D11DeviceContextPtr deviceContext = deviceManager.ImmediateContext(); + + // Camera setup + camera.SetPosition(Float3(40.0f, 5.0f, 5.0f)); + camera.SetYRotation(-XM_PIDIV2); + + // Load the meshes + for(uint32 i = 0; i < uint32(Scene::NumValues); ++i) + { + wstring path(L"..\\Content\\Models\\"); + path += MeshFileNames[i]; + models[i].CreateFromSDKMeshFile(device, path.c_str()); + } + + wstring characterPath(L"..\\Content\\Models\\Soldier\\Soldier.sdkmesh"); + characterMesh.CreateFromSDKMeshFile(device, characterPath.c_str()); + + meshRenderer.Initialize(device, deviceManager.ImmediateContext()); + + ID3D11DeviceContext* context = deviceManager.ImmediateContext(); + + Float4x4 meshWorld = Float4x4::ScaleMatrix(MeshScales[AppSettings::CurrentScene]); + meshRenderer.SetSceneMesh(context, &models[AppSettings::CurrentScene], meshWorld); + + Float4x4 characterWorld = Float4x4::ScaleMatrix(CharacterScale); + Float4x4 characterOrientation = Quaternion::ToFloat4x4(AppSettings::CharacterOrientation); + characterWorld = characterWorld * characterOrientation; + characterWorld.SetTranslation(CharacterPos); + meshRenderer.SetCharacterMesh(context, &characterMesh, characterWorld); + + skybox.Initialize(device); + + // Init the post processor + postProcessor.Initialize(device); +} + +// Creates all required render targets +void ShadowsApp::CreateRenderTargets() +{ + ID3D11Device* device = deviceManager.Device(); + uint32 width = deviceManager.BackBufferWidth(); + uint32 height = deviceManager.BackBufferHeight(); + + const uint32 MSAALevel = 4; + colorTarget.Initialize(device, width, height, DXGI_FORMAT_R16G16B16A16_FLOAT, 1, MSAALevel, 0); + depthBuffer.Initialize(device, width, height, DXGI_FORMAT_D24_UNORM_S8_UINT, true, MSAALevel, 0); + + if(MSAALevel > 1) + resolveTarget.Initialize(device, width, height, DXGI_FORMAT_R16G16B16A16_FLOAT, 1, 1, 0); + else + resolveTarget = colorTarget; + + meshRenderer.CreateReductionTargets(width, height); +} + +void ShadowsApp::Update(const Timer& timer) +{ + MouseState mouseState = MouseState::GetMouseState(window); + KeyboardState kbState = KeyboardState::GetKeyboardState(window); + + if (kbState.IsKeyDown(KeyboardState::Escape)) + window.Destroy(); + + float CamMoveSpeed = 5.0f * timer.DeltaSecondsF(); + const float CamRotSpeed = 0.180f * timer.DeltaSecondsF(); + const float MeshRotSpeed = 0.180f * timer.DeltaSecondsF(); + + // Move the camera with keyboard input + if (kbState.IsKeyDown(KeyboardState::LeftShift)) + CamMoveSpeed *= 0.25f; + + Float3 camPos = camera.Position(); + if (kbState.IsKeyDown(KeyboardState::W)) + camPos += camera.Forward() * CamMoveSpeed; + else if (kbState.IsKeyDown(KeyboardState::S)) + camPos += camera.Back() * CamMoveSpeed; + if (kbState.IsKeyDown(KeyboardState::A)) + camPos += camera.Left() * CamMoveSpeed; + else if (kbState.IsKeyDown(KeyboardState::D)) + camPos += camera.Right() * CamMoveSpeed; + if (kbState.IsKeyDown(KeyboardState::Q)) + camPos += camera.Up() * CamMoveSpeed; + else if (kbState.IsKeyDown(KeyboardState::E)) + camPos += camera.Down() * CamMoveSpeed; + + camera.SetPosition(camPos); + + // Rotate the camera with the mouse + if(mouseState.RButton.Pressed && mouseState.IsOverWindow) + { + float xRot = camera.XRotation(); + float yRot = camera.YRotation(); + xRot += mouseState.DY * CamRotSpeed; + yRot += mouseState.DX * CamRotSpeed; + camera.SetXRotation(xRot); + camera.SetYRotation(yRot); + } + + if(AppSettings::AnimateLight) + { + float rotY = XMScalarModAngle(timer.DeltaSecondsF() * 0.25f); + Quaternion rotation = Quaternion::FromAxisAngle(Float3(0.0f, 1.0f, 0.0f), rotY); + Float3 lightDir = AppSettings::LightDirection; + lightDir = Float3::Transform(lightDir, rotation); + AppSettings::LightDirection.SetValue(lightDir); + } + + // Toggle VSYNC + if(kbState.RisingEdge(KeyboardState::V)) + deviceManager.SetVSYNCEnabled(!deviceManager.VSYNCEnabled()); + + if(AppSettings::CurrentScene.Changed()) + { + float scale = MeshScales[AppSettings::CurrentScene]; + meshRenderer.SetSceneMesh(deviceManager.ImmediateContext(), &models[AppSettings::CurrentScene], + XMMatrixScaling(scale, scale, scale)); + } + + AppSettings::Update(); + + meshRenderer.Update(); +} + +void ShadowsApp::Render(const Timer& timer) +{ + ID3D11DeviceContextPtr context = deviceManager.ImmediateContext(); + + AppSettings::UpdateCBuffer(context); + + RenderMainPass(); + + if(colorTarget.MultiSamples > 1) + context->ResolveSubresource(resolveTarget.Texture, 0, colorTarget.Texture, 0, colorTarget.Format); + + // Kick off post-processing + D3DPERF_BeginEvent(0xFFFFFFFF, L"Post Processing"); + PostProcessor::Constants constants; + constants.BloomThreshold = AppSettings::BloomThreshold; + constants.BloomMagnitude = AppSettings::BloomMagnitude; + constants.BloomBlurSigma = AppSettings::BloomBlurSigma; + constants.Tau = AppSettings::AdaptationRate; + constants.KeyValue = AppSettings::KeyValue; + constants.TimeDelta = timer.DeltaSecondsF(); + + postProcessor.SetConstants(constants); + postProcessor.Render(context, resolveTarget.SRView, deviceManager.BackBuffer()); + D3DPERF_EndEvent(); + + // postProcessor.DrawDepthBuffer(depthBuffer, deviceManager.BackBuffer()); + + ID3D11RenderTargetView* renderTargets[1] = { deviceManager.BackBuffer() }; + context->OMSetRenderTargets(1, renderTargets, NULL); + + D3D11_VIEWPORT vp; + vp.Width = static_cast(deviceManager.BackBufferWidth()); + vp.Height = static_cast(deviceManager.BackBufferHeight()); + vp.TopLeftX = 0; + vp.TopLeftY = 0; + vp.MinDepth = 0; + vp.MaxDepth = 1; + context->RSSetViewports(1, &vp); + + //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ + + /*spriteRenderer.Begin(context, SpriteRenderer::Linear); + + ID3D11ShaderResourceView* srv = meshRenderer.ShadowMap().SRView; + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; + srv->GetDesc(&srvDesc); + srvDesc.Texture2DArray.ArraySize = 1; + ID3D11ShaderResourceViewPtr newSrv; + deviceManager.Device()->CreateShaderResourceView(meshRenderer.ShadowMap().Texture, &srvDesc, &newSrv); + + spriteRenderer.Render(newSrv, Float4x4()); + + spriteRenderer.End();*/ + + RenderHUD(); +} + +void ShadowsApp::RenderMainPass() +{ + PIXEvent event(L"Main Pass"); + + ID3D11DeviceContextPtr context = deviceManager.ImmediateContext(); + + ID3D11RenderTargetView* renderTargets[1] = { NULL }; + ID3D11DepthStencilView* ds = depthBuffer.DSView; + + context->OMSetRenderTargets(1, renderTargets, ds); + + D3D11_VIEWPORT vp; + vp.Width = static_cast(colorTarget.Width); + vp.Height = static_cast(colorTarget.Height); + vp.TopLeftX = 0.0f; + vp.TopLeftY = 0.0f; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + context->RSSetViewports(1, &vp); + + float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + context->ClearRenderTargetView(colorTarget.RTView, clearColor); + context->ClearDepthStencilView(ds, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0); + + Float4x4 meshWorld = Float4x4::ScaleMatrix(MeshScales[AppSettings::CurrentScene]); + Float4x4 characterWorld = Float4x4::ScaleMatrix(CharacterScale); + Float4x4 characterOrientation = Quaternion::ToFloat4x4(AppSettings::CharacterOrientation); + characterWorld = characterWorld * characterOrientation; + characterWorld.SetTranslation(CharacterPos); + + { + ProfileBlock block(L"Depth Prepass"); + if(AppSettings::GPUSceneSubmission) + meshRenderer.RenderDepthGPU(context, camera, meshWorld, characterWorld, false); + else + meshRenderer.RenderDepthCPU(context, camera, meshWorld, characterWorld, false); + } + + if(AppSettings::AutoComputeDepthBounds) + meshRenderer.ReduceDepth(context, depthBuffer.SRView, camera); + + if(AppSettings::GPUSceneSubmission) + meshRenderer.RenderShadowMapGPU(context, camera, meshWorld, characterWorld); + else + meshRenderer.RenderShadowMap(context, camera, meshWorld, characterWorld); + + renderTargets[0] = colorTarget.RTView; + context->OMSetRenderTargets(1, renderTargets, ds); + + context->RSSetViewports(1, &vp); + + Float3 lightDir = AppSettings::LightDirection; + meshRenderer.Render(context, camera, meshWorld, characterWorld); + + skybox.RenderSky(context, lightDir, true, camera.ViewMatrix(), camera.ProjectionMatrix()); +} + +void ShadowsApp::RenderHUD() +{ + PIXEvent event(L"HUD Pass"); + + spriteRenderer.Begin(deviceManager.ImmediateContext(), SpriteRenderer::Point); + + Float4x4 transform = Float4x4::TranslationMatrix(Float3(25.0f, 25.0f, 0.0f)); + wstring fpsText(L"FPS: "); + fpsText += ToString(fps) + L" (" + ToString(1000.0f / fps) + L"ms)"; + spriteRenderer.RenderText(font, fpsText.c_str(), transform, XMFLOAT4(1, 1, 0, 1)); + + transform._42 += 25.0f; + wstring vsyncText(L"VSYNC (V): "); + vsyncText += deviceManager.VSYNCEnabled() ? L"Enabled" : L"Disabled"; + spriteRenderer.RenderText(font, vsyncText.c_str(), transform, XMFLOAT4(1, 1, 0, 1)); + + spriteRenderer.End(); +} + +int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) +{ + ShadowsApp app; + app.Run(); +} \ No newline at end of file diff --git a/Shadows/Shadows.h b/Shadows/Shadows.h new file mode 100644 index 0000000..0107c7a --- /dev/null +++ b/Shadows/Shadows.h @@ -0,0 +1,65 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#pragma once + +#include "SampleFramework11/PCH.h" + +#include "SampleFramework11/App.h" +#include "SampleFramework11/InterfacePointers.h" +#include "SampleFramework11/Camera.h" +#include "SampleFramework11/Model.h" +#include "SampleFramework11/SpriteFont.h" +#include "SampleFramework11/SpriteRenderer.h" +#include "SampleFramework11/Skybox.h" +#include "SampleFramework11/GraphicsTypes.h" +#include "SampleFramework11/Slider.h" +#include "SampleFramework11/SH.h" + +#include "PostProcessor.h" +#include "MeshRenderer.h" + +using namespace SampleFramework11; + +class ShadowsApp : public App +{ + +protected: + + FirstPersonCamera camera; + + Skybox skybox; + + PostProcessor postProcessor; + DepthStencilBuffer depthBuffer; + RenderTarget2D colorTarget; + RenderTarget2D resolveTarget; + + // Model + Model models[uint64(Scene::NumValues)]; + Model characterMesh; + MeshRenderer meshRenderer; + + virtual void Initialize() override; + virtual void Render(const Timer& timer) override; + virtual void Update(const Timer& timer) override; + virtual void BeforeReset() override; + virtual void AfterReset() override; + + void CreateRenderTargets(); + + void RenderMainPass(); + void RenderHUD(); + +public: + + ShadowsApp(); +}; + diff --git a/Shadows/Shadows.rc b/Shadows/Shadows.rc new file mode 100644 index 0000000..fba9c38 Binary files /dev/null and b/Shadows/Shadows.rc differ diff --git a/Shadows/Shadows.sln b/Shadows/Shadows.sln new file mode 100644 index 0000000..db67807 --- /dev/null +++ b/Shadows/Shadows.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Shadows", "Shadows.vcxproj", "{1D9C887B-E727-438B-8977-A0E7363B1FC6}" + ProjectSection(ProjectDependencies) = postProject + {A01DD775-61A4-42F1-9B2A-772E50456A68} = {A01DD775-61A4-42F1-9B2A-772E50456A68} + {C61F717F-FF92-4D36-930E-1F49C198CBEE} = {C61F717F-FF92-4D36-930E-1F49C198CBEE} + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettingsCompiler", "SettingsCompiler\SettingsCompiler.csproj", "{A01DD775-61A4-42F1-9B2A-772E50456A68}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettingsCompilerAttributes", "SettingsCompilerAttributes\SettingsCompilerAttributes.csproj", "{C61F717F-FF92-4D36-930E-1F49C198CBEE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1D9C887B-E727-438B-8977-A0E7363B1FC6}.Debug|x64.ActiveCfg = Debug|x64 + {1D9C887B-E727-438B-8977-A0E7363B1FC6}.Debug|x64.Build.0 = Debug|x64 + {1D9C887B-E727-438B-8977-A0E7363B1FC6}.Release|x64.ActiveCfg = Release|x64 + {1D9C887B-E727-438B-8977-A0E7363B1FC6}.Release|x64.Build.0 = Release|x64 + {A01DD775-61A4-42F1-9B2A-772E50456A68}.Debug|x64.ActiveCfg = Debug|x64 + {A01DD775-61A4-42F1-9B2A-772E50456A68}.Debug|x64.Build.0 = Debug|x64 + {A01DD775-61A4-42F1-9B2A-772E50456A68}.Release|x64.ActiveCfg = Release|x64 + {A01DD775-61A4-42F1-9B2A-772E50456A68}.Release|x64.Build.0 = Release|x64 + {C61F717F-FF92-4D36-930E-1F49C198CBEE}.Debug|x64.ActiveCfg = Debug|x64 + {C61F717F-FF92-4D36-930E-1F49C198CBEE}.Debug|x64.Build.0 = Debug|x64 + {C61F717F-FF92-4D36-930E-1F49C198CBEE}.Release|x64.ActiveCfg = Release|x64 + {C61F717F-FF92-4D36-930E-1F49C198CBEE}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Shadows/Shadows.vcxproj b/Shadows/Shadows.vcxproj new file mode 100644 index 0000000..308bf07 --- /dev/null +++ b/Shadows/Shadows.vcxproj @@ -0,0 +1,252 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {1D9C887B-E727-438B-8977-A0E7363B1FC6} + SampleTest + Win32Proj + Shadows + + + + Application + Unicode + true + v110 + + + Application + Unicode + v110 + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30128.1 + $(SolutionDir)$(Configuration)\$(Platform)\ + $(Configuration)\$(Platform)\ + true + $(SolutionDir)$(Configuration)\$(Platform)\ + $(Configuration)\$(Platform)\ + false + AllRules.ruleset + + + AllRules.ruleset + + + $(IncludePath) + $(IncludePath) + $(ProjectDir)..\Externals\AntTweakBar\lib;$(LibraryPath) + $(ProjectDir)..\Externals\AntTweakBar\lib;$(LibraryPath) + + + $(ExecutablePath) + + + + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebugDLL + Use + PCH.h + Level3 + ProgramDatabase + + + true + Windows + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + MaxSpeed + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + MultiThreadedDLL + true + Use + Level3 + ProgramDatabase + PCH.h + + + true + Windows + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Create + PCH.h + Create + PCH.h + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Document + copy %(FullPath) $(ProjectDir)%(Filename)%(Extension) + Copying external DLL + $(ProjectDir)%(Filename)%(Extension) + copy %(FullPath) $(ProjectDir)%(Filename)%(Extension) + Copying external DLL + $(ProjectDir)%(Filename)%(Extension) + + + Document + copy %(FullPath) $(ProjectDir)%(Filename)%(Extension) + Copying external DLL + $(ProjectDir)%(Filename)%(Extension) + copy %(FullPath) $(ProjectDir)%(Filename)%(Extension) + Copying external DLL + $(ProjectDir)%(Filename)%(Extension) + + + + + + $(ProjectDir)\SettingsCompiler.exe %(FullPath) + Compiling settings and generating C++ code + false + $(ProjectDir)\%(Filename).deps + $(ProjectDir)\SettingsCompiler.exe %(FullPath) + Compiling settings and generating C++ code + $(ProjectDir)\%(Filename).deps + false + + + + + + + + + + + + + + + + + + + + Document + + + + + false + Document + + + + + Document + + + + + + \ No newline at end of file diff --git a/Shadows/Shadows.vcxproj.filters b/Shadows/Shadows.vcxproj.filters new file mode 100644 index 0000000..c2557e3 --- /dev/null +++ b/Shadows/Shadows.vcxproj.filters @@ -0,0 +1,277 @@ + + + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\GUI + + + SampleFramework11\GUI + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + + SampleFramework11\Graphics + + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\GUI + + + SampleFramework11 + + + + SampleFramework11\LodePNG + + + SampleFramework11 + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11\Graphics + + + SampleFramework11 + + + SampleFramework11 + + + + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\GUI + + + SampleFramework11\GUI + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + + SampleFramework11\Graphics + + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11\GUI + + + SampleFramework11 + + + + SampleFramework11 + + + SampleFramework11\LodePNG + + + SampleFramework11 + + + SampleFramework11\Graphics + + + SampleFramework11\Graphics + + + SampleFramework11 + + + SampleFramework11 + + + SampleFramework11\Graphics + + + + + SampleFramework11 + + + SampleFramework11 + + + + + + + SampleFramework11\Shaders + + + SampleFramework11\Shaders + + + SampleFramework11\Shaders + + + SampleFramework11\Shaders + + + + + + SampleFramework11\Shaders + + + SampleFramework11\Shaders + + + SampleFramework11\Shaders + + + + + + + + + + + + {2f9c9f9c-5d06-4e89-b1f2-5da3a5a5e932} + + + {9da39173-e6b3-4ca3-af98-9ee3a39a9187} + + + {903f60a9-b7f2-4b72-888a-bbab0823c428} + + + {7618d8f8-2049-4e9c-ae7e-f55810a83e03} + + + {73e5c512-c3b7-46b3-8bc5-6d32387f4e8d} + + + + + + + + + + + \ No newline at end of file diff --git a/Shadows/SharedConstants.h b/Shadows/SharedConstants.h new file mode 100644 index 0000000..5000bfc --- /dev/null +++ b/Shadows/SharedConstants.h @@ -0,0 +1,55 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +#if _WINDOWS + +#pragma once + +typedef Float2 float2; +typedef Float3 float3; +typedef Float4 float4; + +typedef uint32 uint; +typedef Uint2 uint2; +typedef Uint3 uint3; +typedef Uint4 uint4; + +#endif + +// Constants +static const float Pi = 3.141592654f; +static const float Pi2 = 6.283185307f; +static const float Pi_2 = 1.570796327f; +static const float InvPi = 0.318309886f; +static const float InvPi2 = 0.159154943f; + +static const uint ReductionTGSize = 16; +static const uint CullTGSize = 128; +static const uint BatchTGSize = 256; + +static const uint NumCascades = 4; + +static const float MaxKernelSize = 9.0f; + +// Structures +struct DrawCall +{ + uint StartIndex; + uint NumIndices; + float3 SphereCenter; + float SphereRadius; +}; + +struct CulledDraw +{ + uint SrcIndexStart; + uint DstIndexStart; + uint NumIndices; +}; \ No newline at end of file diff --git a/Shadows/VSM.hlsl b/Shadows/VSM.hlsl new file mode 100644 index 0000000..a3b1f05 --- /dev/null +++ b/Shadows/VSM.hlsl @@ -0,0 +1,84 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Constants +//================================================================================================= +#define ShadowModeFixedSizePCF_ 0 +#define ShadowModeGridPCF_ 1 +#define ShadowModeRandomDiscPCF_ 2 +#define ShadowModeOptimizedPCF_ 3 +#define ShadowModeVSM_ 4 +#define ShadowModeEVSM2_ 5 +#define ShadowModeEVSM4_ 6 +#define ShadowModeMSMHamburger_ 7 +#define ShadowModeMSMHausdorff_ 8 + +#ifndef ShadowMode_ + #define ShadowMode_ 4 +#endif + +#if ShadowMode_ == ShadowModeEVSM2_ || ShadowMode_ == ShadowModeEVSM4_ + #define UseEVSM_ 1 +#else + #define UseEVSM_ 0 +#endif + +static const uint SMFormat16Bit = 0; +static const uint SMFormat32Bit = 1; + +float2 GetEVSMExponents(in float positiveExponent, in float negativeExponent, in uint vsmFormat) +{ + const float maxExponent = vsmFormat == SMFormat16Bit ? 5.54f : 42.0f; + + float2 lightSpaceExponents = float2(positiveExponent, negativeExponent); + + // Clamp to maximum range of fp32/fp16 to prevent overflow/underflow + return min(lightSpaceExponents, maxExponent); +} + +// Applies exponential warp to shadow map depth, input depth should be in [0, 1] +float2 WarpDepth(float depth, float2 exponents) +{ + // Rescale depth into [-1, 1] + depth = 2.0f * depth - 1.0f; + float pos = exp( exponents.x * depth); + float neg = -exp(-exponents.y * depth); + return float2(pos, neg); +} + +float Linstep(float a, float b, float v) +{ + return saturate((v - a) / (b - a)); +} + +// Reduces VSM light bleedning +float ReduceLightBleeding(float pMax, float amount) +{ + // Remove the [0, amount] tail and linearly rescale (amount, 1]. + return Linstep(amount, 1.0f, pMax); +} + +float ChebyshevUpperBound(float2 moments, float mean, float minVariance, + float lightBleedingReduction) +{ + // Compute variance + float variance = moments.y - (moments.x * moments.x); + variance = max(variance, minVariance); + + // Compute probabilistic upper bound + float d = mean - moments.x; + float pMax = variance / (variance + (d * d)); + + pMax = ReduceLightBleeding(pMax, lightBleedingReduction); + + // One-tailed Chebyshev + return (mean <= moments.x ? 1.0f : pMax); +} \ No newline at end of file diff --git a/Shadows/VSMConvert.hlsl b/Shadows/VSMConvert.hlsl new file mode 100644 index 0000000..026f73c --- /dev/null +++ b/Shadows/VSMConvert.hlsl @@ -0,0 +1,193 @@ +//================================================================================================= +// +// Shadows Sample +// by MJP +// http://mynameismjp.wordpress.com/ +// +// All code licensed under the MIT license +// +//================================================================================================= + +//================================================================================================= +// Includes +//================================================================================================= +#include "VSM.hlsl" +#include "MSM.hlsl" +#include "AppSettings.hlsl" +#include "SharedConstants.h" + +//================================================================================================= +// Constants +//=================================================================================================s +#ifndef MSAASamples_ + #define MSAASamples_ 1 +#endif + +#ifndef SampleRadius_ + #define SampleRadius_ 0 +#endif + +//================================================================================================= +// Resources +//================================================================================================= +#if MSAASamples_ > 1 + Texture2DMS ShadowMap : register(t0); +#else + Texture2D ShadowMap : register(t0); +#endif + +#if Vertical_ + Texture2D VSMMap : register(t0); +#else + Texture2DArray VSMMap : register(t0); +#endif + +cbuffer VSMConstants : register(b0) +{ + float4 CascadeScale; + float4 Cascade0Scale; + float2 ShadowMapDimensions; +} + +struct VSOutput +{ + float4 Position : SV_Position; + float2 TexCoord : TEXCOORD; +}; + +VSOutput FullScreenVS(in uint VertexID : SV_VertexID) +{ + VSOutput output; + + if(VertexID == 0) + { + output.Position = float4(-1.0f, 1.0f, 1.0f, 1.0f); + output.TexCoord = float2(0.0f, 0.0f); + } + else if(VertexID == 1) + { + output.Position = float4(3.0f, 1.0f, 1.0f, 1.0f); + output.TexCoord = float2(2.0f, 0.0f); + } + else + { + output.Position = float4(-1.0f, -3.0f, 1.0f, 1.0f); + output.TexCoord = float2(0.0f, 2.0f); + } + + return output; +} + +float4 ConvertToVSM(in VSOutput input) : SV_Target0 +{ + float sampleWeight = 1.0f / float(MSAASamples_); + uint2 coords = uint2(input.Position.xy); + + #if UseEVSM_ + float2 exponents = GetEVSMExponents(PositiveExponent, NegativeExponent, SMFormat); + #endif + + float4 average = float4(0.0f, 0.0f, 0.0f, 0.0f); + + // Sample indices to Load() must be literal, so force unroll + [unroll] + for(uint i = 0; i < MSAASamples_; ++i) + { + // Convert to EVSM representation + #if MSAASamples_ > 1 + float depth = ShadowMap.Load(coords, i); + #else + float depth = ShadowMap[coords]; + #endif + + #if UseMSM_ + float4 msmDepth = 0.0f; + if(SMFormat == SMFormat_SM16Bit) + msmDepth = GetOptimizedMoments(depth); + else + msmDepth = float4(depth, depth * depth, depth * depth * depth, depth * depth * depth * depth); + average += sampleWeight * msmDepth; + #else + #if UseEVSM_ + float2 vsmDepth = WarpDepth(depth, exponents); + #else + float2 vsmDepth = depth; + #endif + + average += sampleWeight * float4(vsmDepth.xy, vsmDepth.xy * vsmDepth.xy); + #endif + } + + #if ShadowMode_ == ShadowModeEVSM4_ || UseMSM_ + return average; + #else + return average.xzxz; + #endif +} + +float4 BlurSample(in float2 screenPos, in float offset, in float2 mapSize) +{ + #if Vertical_ + float2 samplePos = screenPos; + samplePos.y = clamp(screenPos.y + offset, 0, mapSize.y); + return VSMMap[uint2(samplePos)]; + #else + float2 samplePos = screenPos; + samplePos.x = clamp(screenPos.x + offset, 0, mapSize.x); + return VSMMap[uint3(samplePos, 0)]; + #endif +} + +float4 BlurVSM(in VSOutput input) : SV_Target0 +{ + #if Vertical_ + float scale = abs(CascadeScale.y); + float maxFilterSize = MaxKernelSize / abs(Cascade0Scale.y); + #else + float scale = abs(CascadeScale.x); + float maxFilterSize = MaxKernelSize / abs(Cascade0Scale.x); + #endif + + const float KernelSize = clamp(min(FilterSize, maxFilterSize) * scale, 1.0f, MaxKernelSize); + const float Radius = KernelSize / 2.0f; + + #if GPUSceneSubmission_ + [branch] + if(KernelSize > 1.0f) + { + const int SampleRadius = int(round(Radius)); + + float4 sum = 0.0f; + + [loop] + for(int i = -SampleRadius; i <= SampleRadius; ++i) + { + float4 sample = BlurSample(input.Position.xy, i, ShadowMapDimensions); + + sample *= saturate((Radius + 0.5f) - abs(i)); + + sum += sample; + } + + return sum / KernelSize; + } + else + { + return BlurSample(input.Position.xy, 0, ShadowMapDimensions); + } + #else + float4 sum = 0.0f; + + [unroll] + for(int i = -SampleRadius_; i <= SampleRadius_; ++i) + { + float4 sample = BlurSample(input.Position.xy, i, ShadowMapDimensions); + + sample *= saturate((Radius + 0.5f) - abs(i)); + + sum += sample; + } + + return sum / KernelSize; + #endif +} \ No newline at end of file diff --git a/Shadows/project.sublime-project b/Shadows/project.sublime-project new file mode 100644 index 0000000..6ca2814 --- /dev/null +++ b/Shadows/project.sublime-project @@ -0,0 +1,23 @@ +{ + "folders": + [ + { + "path": "/", + "name": "Local", + "file_exclude_patterns": ["*.sln", "*.vcxproj", "*.filters", "*.rc", "*.props", "*.user", "*.opensdf", "*.ico"], + "folder_exclude_patterns": ["Debug", "Release", "bin", "obj", "Properties", "ipch", "x64", "ShaderCache"] + }, + { + "path": "../Externals/", + "name": "Externals", + "file_exclude_patterns": ["*.sln", "*.vcxproj", "*.filters", "*.rc", "*.props", "*.user", "*.opensdf", "*.ico", "*.lib", "*.dll", "*.pdb"], + "folder_exclude_patterns": ["Debug", "Release", "x64", "obj", "Properties", "ipch", "x64", "Lib*", "bin"] + } + ], + "settings": + { + "tab_size": 4, + "translate_tabs_to_spaces": true, + "trim_trailing_white_space_on_save": true + } +} \ No newline at end of file diff --git a/Shadows/resource.h b/Shadows/resource.h new file mode 100644 index 0000000..e946855 Binary files /dev/null and b/Shadows/resource.h differ