summaryrefslogtreecommitdiff
path: root/src/3d/renderer_skybox.cc
blob: 203cc07a78d46c16c05d196a73651a0360367f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// This file is part of the 64k demo project.
// It implements the skybox pipeline for Renderer3D.

#include "3d/renderer.h"
#include "generated/assets.h"
#include "gpu/effects/shader_composer.h"
#include "util/asset_manager.h"

void Renderer3D::create_skybox_pipeline() {
  WGPUBindGroupLayoutEntry entries[3] = {};
  entries[0].binding = 0;
  entries[0].visibility = WGPUShaderStage_Fragment;
  entries[0].texture.sampleType = WGPUTextureSampleType_Float;
  entries[0].texture.viewDimension = WGPUTextureViewDimension_2D;

  entries[1].binding = 1;
  entries[1].visibility = WGPUShaderStage_Fragment;
  entries[1].sampler.type = WGPUSamplerBindingType_Filtering;

  entries[2].binding = 2;
  entries[2].visibility = WGPUShaderStage_Fragment;
  entries[2].buffer.type = WGPUBufferBindingType_Uniform;
  entries[2].buffer.minBindingSize = sizeof(GlobalUniforms);

  WGPUBindGroupLayoutDescriptor bgl_desc = {};
  bgl_desc.entryCount = 3;
  bgl_desc.entries = entries;
  WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device_, &bgl_desc);

  WGPUPipelineLayoutDescriptor pl_desc = {};
  pl_desc.bindGroupLayoutCount = 1;
  pl_desc.bindGroupLayouts = &bgl;
  WGPUPipelineLayout pipeline_layout =
      wgpuDeviceCreatePipelineLayout(device_, &pl_desc);

  const uint8_t* shader_code_asset =
      GetAsset(AssetId::ASSET_SHADER_SKYBOX, nullptr);
  std::string shader_source =
      ShaderComposer::Get().Compose({}, (const char*)shader_code_asset);

#if defined(DEMO_CROSS_COMPILE_WIN32)
  WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
  wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
  wgsl_desc.code = shader_source.c_str();
  WGPUShaderModuleDescriptor shader_desc = {};
  shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#else
  WGPUShaderSourceWGSL wgsl_desc = {};
  wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
  wgsl_desc.code = str_view(shader_source.c_str());
  WGPUShaderModuleDescriptor shader_desc = {};
  shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#endif
  WGPUShaderModule shader_module =
      wgpuDeviceCreateShaderModule(device_, &shader_desc);

  WGPURenderPipelineDescriptor desc = {};
  desc.layout = pipeline_layout;
  desc.vertex.module = shader_module;
#if defined(DEMO_CROSS_COMPILE_WIN32)
  desc.vertex.entryPoint = "vs_main";
#else
  desc.vertex.entryPoint = {"vs_main", 7};
#endif
  WGPUColorTargetState color_target = {};
  color_target.format = format_;
  color_target.writeMask = WGPUColorWriteMask_All;
  WGPUFragmentState fragment = {};
  fragment.module = shader_module;
#if defined(DEMO_CROSS_COMPILE_WIN32)
  fragment.entryPoint = "fs_main";
#else
  fragment.entryPoint = {"fs_main", 7};
#endif
  fragment.targetCount = 1;
  fragment.targets = &color_target;
  desc.fragment = &fragment;
  desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
  desc.primitive.cullMode = WGPUCullMode_Back;
  desc.primitive.frontFace = WGPUFrontFace_CCW;
  desc.multisample.count = 1;
  desc.multisample.mask = 0xFFFFFFFF;

  WGPUDepthStencilState depth_stencil = {};
  depth_stencil.format = WGPUTextureFormat_Depth24Plus;
  depth_stencil.depthWriteEnabled = WGPUOptionalBool_False;
  depth_stencil.depthCompare = WGPUCompareFunction_Always;
  desc.depthStencil = &depth_stencil;

  skybox_pipeline_ = wgpuDeviceCreateRenderPipeline(device_, &desc);
  wgpuBindGroupLayoutRelease(bgl);
  wgpuPipelineLayoutRelease(pipeline_layout);
  wgpuShaderModuleRelease(shader_module);
}