-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathVRenderSystem.java
More file actions
300 lines (235 loc) · 10.6 KB
/
VRenderSystem.java
File metadata and controls
300 lines (235 loc) · 10.6 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package net.vulkanmod.vulkan;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.vulkanmod.vulkan.shader.PipelineState;
import net.vulkanmod.vulkan.util.ColorUtil;
import net.vulkanmod.vulkan.util.MappedBuffer;
import net.vulkanmod.vulkan.util.VUtil;
import org.joml.Matrix4f;
import org.lwjgl.system.MemoryUtil;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
public class VRenderSystem {
private static long window;
public static boolean depthTest = true;
public static boolean depthMask = true;
public static int depthFun = 515;
public static int colorMask = PipelineState.ColorMask.getColorMask(true, true, true, true);
public static boolean cull = true;
public static float clearDepth = 1.0f;
public static FloatBuffer clearColor = MemoryUtil.memAllocFloat(4);
public static MappedBuffer modelViewMatrix = new MappedBuffer(16 * 4);
public static MappedBuffer projectionMatrix = new MappedBuffer(16 * 4);
public static MappedBuffer TextureMatrix = new MappedBuffer(16 * 4);
public static MappedBuffer MVP = new MappedBuffer(16 * 4);
public static MappedBuffer ChunkOffset = new MappedBuffer(3 * 4);
public static MappedBuffer lightDirection0 = new MappedBuffer(3 * 4);
public static MappedBuffer lightDirection1 = new MappedBuffer(3 * 4);
public static MappedBuffer shaderColor = new MappedBuffer(4 * 4);
public static MappedBuffer shaderFogColor = new MappedBuffer(4 * 4);
public static MappedBuffer screenSize = new MappedBuffer(2 * 4);
public static float alphaCutout = 0.0f;
private static final float[] depthBias = new float[2];
public static void initRenderer()
{
RenderSystem.assertInInitPhase();
Vulkan.initVulkan(window);
}
public static ByteBuffer getChunkOffset() { return ChunkOffset.buffer; }
public static int maxSupportedTextureSize() {
return Device.deviceProperties.limits().maxImageDimension2D();
}
public static void renderCrosshair(int p_69348_, boolean p_69349_, boolean p_69350_, boolean p_69351_) {
RenderSystem.assertOnRenderThread();
// GlStateManager._disableTexture();
// GlStateManager._depthMask(false);
// GlStateManager._disableCull();
VRenderSystem.depthMask(false);
RenderSystem.setShader(GameRenderer::getRendertypeLinesShader);
Tesselator tesselator = RenderSystem.renderThreadTesselator();
BufferBuilder bufferbuilder = tesselator.getBuilder();
RenderSystem.lineWidth(4.0F);
bufferbuilder.begin(VertexFormat.Mode.LINES, DefaultVertexFormat.POSITION_COLOR_NORMAL);
if (p_69349_) {
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).color(0, 0, 0, 255).normal(1.0F, 0.0F, 0.0F).endVertex();
bufferbuilder.vertex((double)p_69348_, 0.0D, 0.0D).color(0, 0, 0, 255).normal(1.0F, 0.0F, 0.0F).endVertex();
}
if (p_69350_) {
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).color(0, 0, 0, 255).normal(0.0F, 1.0F, 0.0F).endVertex();
bufferbuilder.vertex(0.0D, (double)p_69348_, 0.0D).color(0, 0, 0, 255).normal(0.0F, 1.0F, 0.0F).endVertex();
}
if (p_69351_) {
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).color(0, 0, 0, 255).normal(0.0F, 0.0F, 1.0F).endVertex();
bufferbuilder.vertex(0.0D, 0.0D, (double)p_69348_).color(0, 0, 0, 255).normal(0.0F, 0.0F, 1.0F).endVertex();
}
tesselator.end();
RenderSystem.lineWidth(2.0F);
bufferbuilder.begin(VertexFormat.Mode.LINES, DefaultVertexFormat.POSITION_COLOR_NORMAL);
if (p_69349_) {
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).color(255, 0, 0, 255).normal(1.0F, 0.0F, 0.0F).endVertex();
bufferbuilder.vertex((double)p_69348_, 0.0D, 0.0D).color(255, 0, 0, 255).normal(1.0F, 0.0F, 0.0F).endVertex();
}
if (p_69350_) {
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).color(0, 255, 0, 255).normal(0.0F, 1.0F, 0.0F).endVertex();
bufferbuilder.vertex(0.0D, (double)p_69348_, 0.0D).color(0, 255, 0, 255).normal(0.0F, 1.0F, 0.0F).endVertex();
}
if (p_69351_) {
bufferbuilder.vertex(0.0D, 0.0D, 0.0D).color(127, 127, 255, 255).normal(0.0F, 0.0F, 1.0F).endVertex();
bufferbuilder.vertex(0.0D, 0.0D, (double)p_69348_).color(127, 127, 255, 255).normal(0.0F, 0.0F, 1.0F).endVertex();
}
tesselator.end();
RenderSystem.lineWidth(1.0F);
// GlStateManager._enableCull();
RenderSystem.depthMask(true);
// GlStateManager._enableTexture();
}
public static void applyMVP(Matrix4f MV, Matrix4f P) {
applyModelViewMatrix(MV);
applyProjectionMatrix(P);
calculateMVP();
}
public static void applyModelViewMatrix(Matrix4f mat) {
mat.get(modelViewMatrix.buffer.asFloatBuffer());
//MemoryUtil.memPutFloat(MemoryUtil.memAddress(modelViewMatrix), 1);
}
public static void applyProjectionMatrix(Matrix4f mat) {
Matrix4f pretransformMatrix = Vulkan.getPretransformMatrix();
FloatBuffer projMatrixBuffer = projectionMatrix.buffer.asFloatBuffer();
// This allows us to skip allocating an object
// if the matrix is known to be an identity matrix.
// Tbh idk if the jvm will just optimize out the allocation but i can't be sure
// as java is sometimes pretty pedantic about object allocations.
if((pretransformMatrix.properties() & Matrix4f.PROPERTY_IDENTITY) != 0) {
mat.get(projMatrixBuffer);
} else {
mat.mulLocal(pretransformMatrix, new Matrix4f()).get(projMatrixBuffer);
}
}
public static void calculateMVP() {
org.joml.Matrix4f MV = new org.joml.Matrix4f(modelViewMatrix.buffer.asFloatBuffer());
org.joml.Matrix4f P = new org.joml.Matrix4f(projectionMatrix.buffer.asFloatBuffer());
P.mul(MV).get(MVP.buffer);
}
public static void setTextureMatrix(Matrix4f mat) {
mat.get(TextureMatrix.buffer.asFloatBuffer());
}
public static MappedBuffer getTextureMatrix() {
return TextureMatrix;
}
public static MappedBuffer getModelViewMatrix() {
return modelViewMatrix;
}
public static MappedBuffer getProjectionMatrix() {
return projectionMatrix;
}
public static MappedBuffer getMVP() {
return MVP;
}
public static void setChunkOffset(float f1, float f2, float f3) {
long ptr = ChunkOffset.ptr;
VUtil.UNSAFE.putFloat(ptr, f1);
VUtil.UNSAFE.putFloat(ptr + 4, f2);
VUtil.UNSAFE.putFloat(ptr + 8, f3);
}
public static void setShaderColor(float f1, float f2, float f3, float f4) {
ColorUtil.setRGBA_Buffer(shaderColor, f1, f2, f3, f4);
}
public static void setShaderFogColor(float f1, float f2, float f3, float f4) {
ColorUtil.setRGBA_Buffer(shaderFogColor, f1, f2, f3, f4);
}
public static MappedBuffer getShaderColor() {
return shaderColor;
}
public static MappedBuffer getShaderFogColor() {
return shaderFogColor;
}
public static void enableColorLogicOp() {
PipelineState.currentLogicOpState = new PipelineState.LogicOpState(true, 0);
}
public static void disableColorLogicOp() {
PipelineState.currentLogicOpState = PipelineState.DEFAULT_LOGICOP_STATE;
}
public static void logicOp(GlStateManager.LogicOp p_69836_) {
PipelineState.currentLogicOpState.setLogicOp(p_69836_);
}
public static void clearColor(float f1, float f2, float f3, float f4) {
ColorUtil.setRGBA_Buffer(clearColor, f1, f2, f3, f4);
}
public static void clear(int v) {
Renderer.clearAttachments(v);
}
public static void disableDepthTest() {
depthTest = false;
}
public static void depthMask(boolean b) {
depthMask = b;
}
public static PipelineState.DepthState getDepthState() {
return new PipelineState.DepthState(depthTest, depthMask, depthFun);
}
public static void colorMask(boolean b, boolean b1, boolean b2, boolean b3) {
colorMask = PipelineState.ColorMask.getColorMask(b, b1, b2, b3);
}
public static int getColorMask() {
return colorMask;
}
public static void enableDepthTest() {
depthTest = true;
}
public static void enableCull() {
cull = true;
}
public static void disableCull() {
cull = false;
}
public static void polygonOffset(float v, float v1) {
depthBias[0] = v;
depthBias[1] = v1;
}
public static void enablePolygonOffset() {
Renderer.setDepthBias(depthBias[0], depthBias[1]);
}
public static void disablePolygonOffset() {
Renderer.setDepthBias(0.0F, 0.0F);
}
public static MappedBuffer getScreenSize() {
updateScreenSize();
return screenSize;
}
public static void updateScreenSize() {
Window window = Minecraft.getInstance().getWindow();
screenSize.putFloat(0, (float)window.getWidth());
screenSize.putFloat(4, (float)window.getHeight());
}
public static void setWindow(long window) {
VRenderSystem.window = window;
}
public static void depthFunc(int p_69457_) {
depthFun = p_69457_;
}
public static void enableBlend() {
PipelineState.blendInfo.enabled = true;
}
public static void disableBlend() {
PipelineState.blendInfo.enabled = false;
}
public static void blendFunc(GlStateManager.SourceFactor sourceFactor, GlStateManager.DestFactor destFactor) {
PipelineState.blendInfo.setBlendFunction(sourceFactor, destFactor);
}
public static void blendFunc(int srcFactor, int dstFactor) {
PipelineState.blendInfo.setBlendFunction(srcFactor, dstFactor);
}
public static void blendFuncSeparate(GlStateManager.SourceFactor p_69417_, GlStateManager.DestFactor p_69418_, GlStateManager.SourceFactor p_69419_, GlStateManager.DestFactor p_69420_) {
PipelineState.blendInfo.setBlendFuncSeparate(p_69417_, p_69418_, p_69419_, p_69420_);
}
public static void blendFuncSeparate(int srcFactorRGB, int dstFactorRGB, int srcFactorAlpha, int dstFactorAlpha) {
PipelineState.blendInfo.setBlendFuncSeparate(srcFactorRGB, dstFactorRGB, srcFactorAlpha, dstFactorAlpha);
}
}