create instance
This commit is contained in:
parent
6ba3276c84
commit
2296cda4b5
14
cmd/main.go
14
cmd/main.go
@ -5,6 +5,7 @@ import (
|
||||
"zworld/engine/core/zlog"
|
||||
"zworld/engine/render/vulkan/instance"
|
||||
"zworld/engine/rule"
|
||||
"zworld/engine/window"
|
||||
"zworld/plugin/objloader"
|
||||
)
|
||||
|
||||
@ -13,11 +14,16 @@ func main() {
|
||||
engine.FTimeRuleOption(&rule.FTimeRule{}),
|
||||
engine.FActorRuleOption(&rule.FActorRule{}),
|
||||
engine.FRenderRuleOption(&rule.FRenderRule{}))
|
||||
err, mesh := objloader.LoadObj("asset/model/sphere.obj")
|
||||
zlog.Infof("err", err, len(mesh.Positions))
|
||||
win := window.NewSDLWindow(640, 720)
|
||||
err = win.CreateWindow()
|
||||
if err != nil {
|
||||
zlog.Infof("err", err)
|
||||
}
|
||||
vulkan := instance.New("test", win, window.CreateLoaderFromSDL)
|
||||
vulkan.EnumeratePhysicalDevices()
|
||||
for i := 1; i < 100; i++ {
|
||||
w.Tick()
|
||||
}
|
||||
err, mesh := objloader.LoadObj("asset/model/sphere.obj")
|
||||
zlog.Infof("err", err, len(mesh.Positions))
|
||||
vulkan := instance.New("test")
|
||||
vulkan.EnumeratePhysicalDevices()
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"github.com/vkngwrapper/extensions/v2/khr_surface"
|
||||
)
|
||||
|
||||
var validationLayers = []string{"VK_LAYER_KHRONOS_validation"}
|
||||
var extensions = []string{
|
||||
khr_surface.ExtensionName,
|
||||
ext_debug_utils.ExtensionName,
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
package instance
|
||||
|
||||
//#cgo LDFLAGS: -LF:/Coding/GoModule/cgo -lvulkan
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/vkngwrapper/core/v2"
|
||||
"github.com/vkngwrapper/core/v2/common"
|
||||
"github.com/vkngwrapper/core/v2/core1_0"
|
||||
"zworld/engine/window"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -20,25 +23,23 @@ type T interface {
|
||||
Destroy()
|
||||
Ptr() core1_0.Instance
|
||||
}
|
||||
|
||||
type FLoaderCreate func() *core.VulkanLoader
|
||||
type FInstance struct {
|
||||
ptr core1_0.Instance
|
||||
loader *core.VulkanLoader
|
||||
ptr core1_0.Instance
|
||||
}
|
||||
|
||||
func New(appName string) T {
|
||||
loader, err := core.CreateLoaderFromProcAddr(glfw.GetVulkanGetInstanceProcAddress())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
func New(appName string, win *window.FSDLWindow, LoaderCreate FLoaderCreate) T {
|
||||
loader := LoaderCreate()
|
||||
instanceOptions := &core1_0.InstanceCreateInfo{
|
||||
APIVersion: common.APIVersion(common.CreateVersion(1, 1, 0)),
|
||||
ApplicationName: appName,
|
||||
ApplicationVersion: common.CreateVersion(0, 1, 0),
|
||||
EngineName: "goworld",
|
||||
EngineVersion: common.CreateVersion(0, 2, 1),
|
||||
}
|
||||
handle, _, err := loader.CreateInstance(nil, core1_0.InstanceCreateInfo{
|
||||
APIVersion: common.APIVersion(common.CreateVersion(1, 1, 0)),
|
||||
ApplicationName: appName,
|
||||
ApplicationVersion: common.CreateVersion(0, 1, 0),
|
||||
EngineName: "goworld",
|
||||
EngineVersion: common.CreateVersion(0, 2, 1),
|
||||
EnabledLayerNames: layers,
|
||||
EnabledExtensionNames: extensions,
|
||||
})
|
||||
help.getCreateInstanceExtension(instanceOptions, loader, win)
|
||||
handle, _, err := loader.CreateInstance(nil, *instanceOptions)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
5
engine/render/vulkan/instance/type.go
Normal file
5
engine/render/vulkan/instance/type.go
Normal file
@ -0,0 +1,5 @@
|
||||
package instance
|
||||
|
||||
const enableValidationLayers = true
|
||||
|
||||
var help FVulkanHelp
|
||||
58
engine/render/vulkan/instance/vulkan_help.go
Normal file
58
engine/render/vulkan/instance/vulkan_help.go
Normal file
@ -0,0 +1,58 @@
|
||||
package instance
|
||||
|
||||
import (
|
||||
"github.com/vkngwrapper/core/v2"
|
||||
"github.com/vkngwrapper/core/v2/core1_0"
|
||||
"github.com/vkngwrapper/extensions/v2/ext_debug_utils"
|
||||
"github.com/vkngwrapper/extensions/v2/khr_portability_enumeration"
|
||||
"zworld/engine/window"
|
||||
)
|
||||
|
||||
type FVulkanHelp struct{}
|
||||
|
||||
func (h *FVulkanHelp) getCreateInstanceExtension(instanceOptions *core1_0.InstanceCreateInfo, loader *core.VulkanLoader, win *window.FSDLWindow) error {
|
||||
// Add extensions
|
||||
sdlExtensions := win.GetInstance().VulkanGetInstanceExtensions()
|
||||
extensions, _, err := loader.AvailableExtensions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ext := range sdlExtensions {
|
||||
_, hasExt := extensions[ext]
|
||||
if !hasExt {
|
||||
return nil
|
||||
}
|
||||
instanceOptions.EnabledExtensionNames = append(instanceOptions.EnabledExtensionNames, ext)
|
||||
}
|
||||
|
||||
if enableValidationLayers {
|
||||
instanceOptions.EnabledExtensionNames = append(instanceOptions.EnabledExtensionNames, ext_debug_utils.ExtensionName)
|
||||
}
|
||||
|
||||
_, enumerationSupported := extensions[khr_portability_enumeration.ExtensionName]
|
||||
if enumerationSupported {
|
||||
instanceOptions.EnabledExtensionNames = append(instanceOptions.EnabledExtensionNames, khr_portability_enumeration.ExtensionName)
|
||||
instanceOptions.Flags |= khr_portability_enumeration.InstanceCreateEnumeratePortability
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *FVulkanHelp) getCreateInstanceLayer(instanceOptions *core1_0.InstanceCreateInfo, loader *core.VulkanLoader, win *window.FSDLWindow) error {
|
||||
// Add extensions
|
||||
layers, _, err := loader.AvailableLayers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if enableValidationLayers {
|
||||
for _, layer := range validationLayers {
|
||||
_, hasValidation := layers[layer]
|
||||
if !hasValidation {
|
||||
return nil
|
||||
}
|
||||
instanceOptions.EnabledLayerNames = append(instanceOptions.EnabledLayerNames, layer)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -1,5 +1,9 @@
|
||||
package window
|
||||
|
||||
//#cgo CFLAGS: -IF:/Coding/GoModule/cgo
|
||||
//#cgo LDFLAGS: -LF:/Coding/GoModule/cgo -lsdl2
|
||||
//
|
||||
import "C"
|
||||
import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/vkngwrapper/core/v2"
|
||||
@ -12,6 +16,19 @@ type FSDLWindow struct {
|
||||
height int32
|
||||
}
|
||||
|
||||
func NewSDLWindow(width, height int32) *FSDLWindow {
|
||||
return &FSDLWindow{
|
||||
width: width,
|
||||
height: height,
|
||||
}
|
||||
}
|
||||
func CreateLoaderFromSDL() *core.VulkanLoader {
|
||||
loader, err := core.CreateLoaderFromProcAddr(sdl.VulkanGetVkGetInstanceProcAddr())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return loader
|
||||
}
|
||||
func (w *FSDLWindow) CreateWindow() error {
|
||||
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
|
||||
return err
|
||||
@ -23,3 +40,6 @@ func (w *FSDLWindow) CreateWindow() error {
|
||||
w.window = window
|
||||
return nil
|
||||
}
|
||||
func (w *FSDLWindow) GetInstance() *sdl.Window {
|
||||
return w.window
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user