89 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			89 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								package pass
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"github.com/vkngwrapper/core/v2/core1_0"
							 | 
						||
| 
								 | 
							
									"zworld/engine/render/uniform"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/cache"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/descriptor"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/material"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/renderpass"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/shader"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/vertex"
							 | 
						||
| 
								 | 
							
									"zworld/engine/renderapi/vulkan"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								type ShadowMatCache struct {
							 | 
						||
| 
								 | 
							
									app    vulkan.App
							 | 
						||
| 
								 | 
							
									pass   renderpass.T
							 | 
						||
| 
								 | 
							
									frames int
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func NewShadowMaterialMaker(app vulkan.App, pass renderpass.T, frames int) MaterialCache {
							 | 
						||
| 
								 | 
							
									return cache.New[*material.Def, []Material](&ShadowMatCache{
							 | 
						||
| 
								 | 
							
										app:    app,
							 | 
						||
| 
								 | 
							
										pass:   pass,
							 | 
						||
| 
								 | 
							
										frames: frames,
							 | 
						||
| 
								 | 
							
									})
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (m *ShadowMatCache) Name() string { return "ShadowMaterials" }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (m *ShadowMatCache) Instantiate(def *material.Def, callback func([]Material)) {
							 | 
						||
| 
								 | 
							
									if def == nil {
							 | 
						||
| 
								 | 
							
										def = &material.Def{}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									desc := &BasicDescriptors{
							 | 
						||
| 
								 | 
							
										Camera: &descriptor.Uniform[uniform.Camera]{
							 | 
						||
| 
								 | 
							
											Stages: core1_0.StageAll,
							 | 
						||
| 
								 | 
							
										},
							 | 
						||
| 
								 | 
							
										Objects: &descriptor.Storage[uniform.Object]{
							 | 
						||
| 
								 | 
							
											Stages: core1_0.StageAll,
							 | 
						||
| 
								 | 
							
											Size:   2000,
							 | 
						||
| 
								 | 
							
										},
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// read vertex pointers from vertex format
							 | 
						||
| 
								 | 
							
									pointers := vertex.ParsePointers(def.VertexFormat)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// fetch shader from cache
							 | 
						||
| 
								 | 
							
									shader := m.app.Shaders().Fetch(shader.NewRef("shadow"))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// create material
							 | 
						||
| 
								 | 
							
									mat := material.New(
							 | 
						||
| 
								 | 
							
										m.app.Device(),
							 | 
						||
| 
								 | 
							
										material.Args{
							 | 
						||
| 
								 | 
							
											Shader:     shader,
							 | 
						||
| 
								 | 
							
											Pass:       m.pass,
							 | 
						||
| 
								 | 
							
											Subpass:    MainSubpass,
							 | 
						||
| 
								 | 
							
											Pointers:   pointers,
							 | 
						||
| 
								 | 
							
											CullMode:   vertex.CullFront,
							 | 
						||
| 
								 | 
							
											DepthTest:  true,
							 | 
						||
| 
								 | 
							
											DepthWrite: true,
							 | 
						||
| 
								 | 
							
											DepthFunc:  core1_0.CompareOpLess,
							 | 
						||
| 
								 | 
							
											DepthClamp: true,
							 | 
						||
| 
								 | 
							
											Primitive:  def.Primitive,
							 | 
						||
| 
								 | 
							
										},
							 | 
						||
| 
								 | 
							
										desc)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									instances := make([]Material, m.frames)
							 | 
						||
| 
								 | 
							
									for i := range instances {
							 | 
						||
| 
								 | 
							
										instance := mat.Instantiate(m.app.Pool())
							 | 
						||
| 
								 | 
							
										instances[i] = &BasicMaterial{
							 | 
						||
| 
								 | 
							
											id:       def.Hash(),
							 | 
						||
| 
								 | 
							
											Instance: instance,
							 | 
						||
| 
								 | 
							
											Objects:  NewObjectBuffer(desc.Objects.Size),
							 | 
						||
| 
								 | 
							
											Meshes:   m.app.Meshes(),
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									callback(instances)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (m *ShadowMatCache) Destroy() {
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func (m *ShadowMatCache) Delete(mat []Material) {
							 | 
						||
| 
								 | 
							
									mat[0].Destroy()
							 | 
						||
| 
								 | 
							
								}
							 |