-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_triangle.py
More file actions
160 lines (128 loc) · 5.78 KB
/
Copy pathbasic_triangle.py
File metadata and controls
160 lines (128 loc) · 5.78 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# /******************************************************************************
# * Copyright (C) 1991-2026 ASDAlexander77.
# *
# * Permission is hereby granted, free of charge, to any person obtaining
# * a copy of this software and associated documentation files (the
# * "Software"), to deal in the Software without restriction, including
# * without limitation the rights to use, copy, modify, merge, publish,
# * distribute, sublicense, and/or sell copies of the Software, and to
# * permit persons to whom the Software is furnished to do so, subject to
# * the following conditions:
# *
# * The above copyright notice and this permission notice shall be
# * included in all copies or substantial portions of the Software.
# *
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ******************************************************************************/
if __name__ == "__main__":
import sys
from pathlib import Path
from src import pydonut as pyd
WINDOW_TITLE = "PyDonut Basic Triangle"
folder = Path(__file__).resolve().parent
class BasicTriangle(pyd.IRenderPass):
def __init__(self: BasicTriangle, deviceManager: pyd.DeviceManager) -> None:
super().__init__(deviceManager)
self.vertexShader: pyd.Shader | None = None
self.pixelShader: pyd.Shader | None = None
self.pipeline: pyd.GraphicsPipeline | None = None
self.commandList: pyd.CommandList | None = None
def Init(self: BasicTriangle) -> bool:
device = self.GetDevice()
api = device.getGraphicsAPI()
shaderPath = folder / "shaders" / "basic_triangle" / "shaders.hlsl"
source = shaderPath.read_text(encoding="utf-8")
try:
assert pyd.CompileShader is not None
vsBytecode = pyd.CompileShader(
source,
"main_vs",
pyd.ShaderType.Vertex,
api,
sourceName=shaderPath.name,
)
psBytecode = pyd.CompileShader(
source,
"main_ps",
pyd.ShaderType.Pixel,
api,
sourceName=shaderPath.name,
)
except RuntimeError as e:
pyd.log.fatal(f"Shader compilation failed: {e}")
return False
self.vertexShader = device.createShader(
vsBytecode, "main_vs", pyd.ShaderType.Vertex
)
self.pixelShader = device.createShader(
psBytecode, "main_ps", pyd.ShaderType.Pixel
)
if not self.vertexShader or not self.pixelShader:
return False
self.commandList = device.createCommandList()
return True
def BackBufferResizing(self: BasicTriangle):
self.pipeline = None
def Animate(self: BasicTriangle, elapsedTimeSeconds: float):
self.GetDeviceManager().SetInformativeWindowTitle(WINDOW_TITLE)
def Render(self: BasicTriangle, framebuffer: pyd.Framebuffer):
device = self.GetDevice()
assert self.commandList is not None
if not self.pipeline:
psoDesc = pyd.GraphicsPipelineDesc()
psoDesc.VS = self.vertexShader
psoDesc.PS = self.pixelShader
psoDesc.primType = pyd.PrimitiveType.TriangleList
psoDesc.renderState.depthStencilState.depthTestEnable = False
self.pipeline = device.createGraphicsPipeline(
psoDesc, framebuffer.getFramebufferInfo()
)
self.commandList.open()
pyd.ClearColorAttachment(self.commandList, framebuffer, 0, pyd.Color(0.0))
state = pyd.GraphicsState()
state.pipeline = self.pipeline
state.framebuffer = framebuffer
state.viewport.addViewportAndScissorRect(
framebuffer.getFramebufferInfo().getViewport()
)
self.commandList.setGraphicsState(state)
args = pyd.DrawArguments()
args.vertexCount = 3
self.commandList.draw(args)
self.commandList.close()
device.executeCommandList(self.commandList)
is_debug = "-debug" in sys.argv
api = pyd.GetGraphicsAPIFromCommandLine(sys.argv)
print(f"Selected Graphics API: {api}")
deviceManager = pyd.DeviceManager.Create(api)
if not deviceManager:
pyd.log.fatal("Failed to create DeviceManager.")
sys.exit(1)
else:
print("DeviceManager created successfully.")
deviceParams = pyd.DeviceCreationParameters()
if is_debug:
print("Debug mode is enabled.")
deviceParams.enableDebugRuntime = True
deviceParams.enableNvrhiValidationLayer = True
if not deviceManager.CreateWindowDeviceAndSwapChain(deviceParams, "PyDonut Window"):
pyd.log.fatal(
"Cannot initialize a graphics device with the requested parameters"
)
sys.exit(1)
example = BasicTriangle(deviceManager)
if example.Init():
deviceManager.AddRenderPassToBack(example)
deviceManager.RunMessageLoop()
deviceManager.RemoveRenderPass(example)
deviceManager.Shutdown()
if is_debug:
deviceManager.ReportLiveObjects()
del deviceManager
print("Done.")