| 1 | package net.minecraft.client.shader;
|
| 2 |
|
| 3 | import com.google.common.base.Charsets;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.google.gson.JsonArray;
|
| 7 | import com.google.gson.JsonElement;
|
| 8 | import com.google.gson.JsonObject;
|
| 9 | import com.google.gson.JsonParser;
|
| 10 | import java.io.IOException;
|
| 11 | import java.io.InputStream;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import net.minecraft.client.renderer.GlStateManager;
|
| 15 | import net.minecraft.client.renderer.OpenGlHelper;
|
| 16 | import net.minecraft.client.renderer.texture.ITextureObject;
|
| 17 | import net.minecraft.client.resources.IResourceManager;
|
| 18 | import net.minecraft.client.util.JsonBlendingMode;
|
| 19 | import net.minecraft.client.util.JsonException;
|
| 20 | import net.minecraft.util.JsonUtils;
|
| 21 | import net.minecraft.util.ResourceLocation;
|
| 22 | import org.apache.commons.io.IOUtils;
|
| 23 | import org.apache.logging.log4j.LogManager;
|
| 24 | import org.apache.logging.log4j.Logger;
|
| 25 |
|
| 26 | public class ShaderManager
|
| 27 | {
|
| 28 | private static final Logger logger = LogManager.getLogger();
|
| 29 | private static final ShaderDefault defaultShaderUniform = new ShaderDefault();
|
| 30 | private static ShaderManager staticShaderManager = null;
|
| 31 | private static int currentProgram = -1;
|
| 32 | private static boolean field_148000_e = true;
|
| 33 | private final Map<String, Object> shaderSamplers = Maps.<String, Object>newHashMap();
|
| 34 | private final List<String> samplerNames = Lists.<String>newArrayList();
|
| 35 | private final List<Integer> shaderSamplerLocations = Lists.<Integer>newArrayList();
|
| 36 | private final List<ShaderUniform> shaderUniforms = Lists.<ShaderUniform>newArrayList();
|
| 37 | private final List<Integer> shaderUniformLocations = Lists.<Integer>newArrayList();
|
| 38 | private final Map<String, ShaderUniform> mappedShaderUniforms = Maps.<String, ShaderUniform>newHashMap();
|
| 39 | private final int program;
|
| 40 | private final String programFilename;
|
| 41 | private final boolean useFaceCulling;
|
| 42 | private boolean isDirty;
|
| 43 | private final JsonBlendingMode field_148016_p;
|
| 44 | private final List<Integer> attribLocations;
|
| 45 | private final List<String> attributes;
|
| 46 | private final ShaderLoader vertexShaderLoader;
|
| 47 | private final ShaderLoader fragmentShaderLoader;
|
| 48 |
|
| 49 | public ShaderManager(IResourceManager resourceManager, String programName) throws JsonException, IOException
|
| 50 | {
|
| 51 | JsonParser jsonparser = new JsonParser();
|
| 52 | ResourceLocation resourcelocation = new ResourceLocation("shaders/program/" + programName + ".json");
|
| 53 | this.programFilename = programName;
|
| 54 | InputStream inputstream = null;
|
| 55 |
|
| 56 | try
|
| 57 | {
|
| 58 | inputstream = resourceManager.getResource(resourcelocation).getInputStream();
|
| 59 | JsonObject jsonobject = jsonparser.parse(IOUtils.toString(inputstream, Charsets.UTF_8)).getAsJsonObject();
|
| 60 | String s = JsonUtils.getString(jsonobject, "vertex");
|
| 61 | String s1 = JsonUtils.getString(jsonobject, "fragment");
|
| 62 | JsonArray jsonarray = JsonUtils.getJsonArray(jsonobject, "samplers", (JsonArray)null);
|
| 63 |
|
| 64 | if (jsonarray != null)
|
| 65 | {
|
| 66 | int i = 0;
|
| 67 |
|
| 68 | for (JsonElement jsonelement : jsonarray)
|
| 69 | {
|
| 70 | try
|
| 71 | {
|
| 72 | this.parseSampler(jsonelement);
|
| 73 | }
|
| 74 | catch (Exception exception2)
|
| 75 | {
|
| 76 | JsonException jsonexception1 = JsonException.func_151379_a(exception2);
|
| 77 | jsonexception1.func_151380_a("samplers[" + i + "]");
|
| 78 | throw jsonexception1;
|
| 79 | }
|
| 80 |
|
| 81 | ++i;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | JsonArray jsonarray1 = JsonUtils.getJsonArray(jsonobject, "attributes", (JsonArray)null);
|
| 86 |
|
| 87 | if (jsonarray1 != null)
|
| 88 | {
|
| 89 | int j = 0;
|
| 90 | this.attribLocations = Lists.<Integer>newArrayListWithCapacity(jsonarray1.size());
|
| 91 | this.attributes = Lists.<String>newArrayListWithCapacity(jsonarray1.size());
|
| 92 |
|
| 93 | for (JsonElement jsonelement1 : jsonarray1)
|
| 94 | {
|
| 95 | try
|
| 96 | {
|
| 97 | this.attributes.add(JsonUtils.getString(jsonelement1, "attribute"));
|
| 98 | }
|
| 99 | catch (Exception exception1)
|
| 100 | {
|
| 101 | JsonException jsonexception2 = JsonException.func_151379_a(exception1);
|
| 102 | jsonexception2.func_151380_a("attributes[" + j + "]");
|
| 103 | throw jsonexception2;
|
| 104 | }
|
| 105 |
|
| 106 | ++j;
|
| 107 | }
|
| 108 | }
|
| 109 | else
|
| 110 | {
|
| 111 | this.attribLocations = null;
|
| 112 | this.attributes = null;
|
| 113 | }
|
| 114 |
|
| 115 | JsonArray jsonarray2 = JsonUtils.getJsonArray(jsonobject, "uniforms", (JsonArray)null);
|
| 116 |
|
| 117 | if (jsonarray2 != null)
|
| 118 | {
|
| 119 | int k = 0;
|
| 120 |
|
| 121 | for (JsonElement jsonelement2 : jsonarray2)
|
| 122 | {
|
| 123 | try
|
| 124 | {
|
| 125 | this.parseUniform(jsonelement2);
|
| 126 | }
|
| 127 | catch (Exception exception)
|
| 128 | {
|
| 129 | JsonException jsonexception3 = JsonException.func_151379_a(exception);
|
| 130 | jsonexception3.func_151380_a("uniforms[" + k + "]");
|
| 131 | throw jsonexception3;
|
| 132 | }
|
| 133 |
|
| 134 | ++k;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | this.field_148016_p = JsonBlendingMode.func_148110_a(JsonUtils.getJsonObject(jsonobject, "blend", (JsonObject)null));
|
| 139 | this.useFaceCulling = JsonUtils.getBoolean(jsonobject, "cull", true);
|
| 140 | this.vertexShaderLoader = ShaderLoader.loadShader(resourceManager, ShaderLoader.ShaderType.VERTEX, s);
|
| 141 | this.fragmentShaderLoader = ShaderLoader.loadShader(resourceManager, ShaderLoader.ShaderType.FRAGMENT, s1);
|
| 142 | this.program = ShaderLinkHelper.getStaticShaderLinkHelper().createProgram();
|
| 143 | ShaderLinkHelper.getStaticShaderLinkHelper().linkProgram(this);
|
| 144 | this.setupUniforms();
|
| 145 |
|
| 146 | if (this.attributes != null)
|
| 147 | {
|
| 148 | for (String s2 : this.attributes)
|
| 149 | {
|
| 150 | int l = OpenGlHelper.glGetAttribLocation(this.program, s2);
|
| 151 | this.attribLocations.add(Integer.valueOf(l));
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 | catch (Exception exception3)
|
| 156 | {
|
| 157 | JsonException jsonexception = JsonException.func_151379_a(exception3);
|
| 158 | jsonexception.func_151381_b(resourcelocation.getResourcePath());
|
| 159 | throw jsonexception;
|
| 160 | }
|
| 161 | finally
|
| 162 | {
|
| 163 | IOUtils.closeQuietly(inputstream);
|
| 164 | }
|
| 165 |
|
| 166 | this.markDirty();
|
| 167 | }
|
| 168 |
|
| 169 | public void deleteShader()
|
| 170 | {
|
| 171 | ShaderLinkHelper.getStaticShaderLinkHelper().deleteShader(this);
|
| 172 | }
|
| 173 |
|
| 174 | public void endShader()
|
| 175 | {
|
| 176 | OpenGlHelper.glUseProgram(0);
|
| 177 | currentProgram = -1;
|
| 178 | staticShaderManager = null;
|
| 179 | field_148000_e = true;
|
| 180 |
|
| 181 | for (int i = 0; i < this.shaderSamplerLocations.size(); ++i)
|
| 182 | {
|
| 183 | if (this.shaderSamplers.get(this.samplerNames.get(i)) != null)
|
| 184 | {
|
| 185 | GlStateManager.setActiveTexture(OpenGlHelper.defaultTexUnit + i);
|
| 186 | GlStateManager.bindTexture(0);
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | public void useShader()
|
| 192 | {
|
| 193 | this.isDirty = false;
|
| 194 | staticShaderManager = this;
|
| 195 | this.field_148016_p.func_148109_a();
|
| 196 |
|
| 197 | if (this.program != currentProgram)
|
| 198 | {
|
| 199 | OpenGlHelper.glUseProgram(this.program);
|
| 200 | currentProgram = this.program;
|
| 201 | }
|
| 202 |
|
| 203 | if (this.useFaceCulling)
|
| 204 | {
|
| 205 | GlStateManager.enableCull();
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
| 209 | GlStateManager.disableCull();
|
| 210 | }
|
| 211 |
|
| 212 | for (int i = 0; i < this.shaderSamplerLocations.size(); ++i)
|
| 213 | {
|
| 214 | if (this.shaderSamplers.get(this.samplerNames.get(i)) != null)
|
| 215 | {
|
| 216 | GlStateManager.setActiveTexture(OpenGlHelper.defaultTexUnit + i);
|
| 217 | GlStateManager.enableTexture2D();
|
| 218 | Object object = this.shaderSamplers.get(this.samplerNames.get(i));
|
| 219 | int j = -1;
|
| 220 |
|
| 221 | if (object instanceof Framebuffer)
|
| 222 | {
|
| 223 | j = ((Framebuffer)object).framebufferTexture;
|
| 224 | }
|
| 225 | else if (object instanceof ITextureObject)
|
| 226 | {
|
| 227 | j = ((ITextureObject)object).getGlTextureId();
|
| 228 | }
|
| 229 | else if (object instanceof Integer)
|
| 230 | {
|
| 231 | j = ((Integer)object).intValue();
|
| 232 | }
|
| 233 |
|
| 234 | if (j != -1)
|
| 235 | {
|
| 236 | GlStateManager.bindTexture(j);
|
| 237 | OpenGlHelper.glUniform1i(OpenGlHelper.glGetUniformLocation(this.program, (CharSequence)this.samplerNames.get(i)), i);
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | for (ShaderUniform shaderuniform : this.shaderUniforms)
|
| 243 | {
|
| 244 | shaderuniform.upload();
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | public void markDirty()
|
| 249 | {
|
| 250 | this.isDirty = true;
|
| 251 | }
|
| 252 |
|
| 253 | /**
|
| 254 | * gets a shader uniform for the name given. null if not found.
|
| 255 | */
|
| 256 | public ShaderUniform getShaderUniform(String p_147991_1_)
|
| 257 | {
|
| 258 | return this.mappedShaderUniforms.containsKey(p_147991_1_) ? (ShaderUniform)this.mappedShaderUniforms.get(p_147991_1_) : null;
|
| 259 | }
|
| 260 |
|
| 261 | /**
|
| 262 | * gets a shader uniform for the name given. if not found, returns a default not-null value
|
| 263 | */
|
| 264 | public ShaderUniform getShaderUniformOrDefault(String p_147984_1_)
|
| 265 | {
|
| 266 | return (ShaderUniform)(this.mappedShaderUniforms.containsKey(p_147984_1_) ? (ShaderUniform)this.mappedShaderUniforms.get(p_147984_1_) : defaultShaderUniform);
|
| 267 | }
|
| 268 |
|
| 269 | /**
|
| 270 | * goes through the parsed uniforms and samplers and connects them to their GL counterparts.
|
| 271 | */
|
| 272 | private void setupUniforms()
|
| 273 | {
|
| 274 | int i = 0;
|
| 275 |
|
| 276 | for (int j = 0; i < this.samplerNames.size(); ++j)
|
| 277 | {
|
| 278 | String s = (String)this.samplerNames.get(i);
|
| 279 | int k = OpenGlHelper.glGetUniformLocation(this.program, s);
|
| 280 |
|
| 281 | if (k == -1)
|
| 282 | {
|
| 283 | logger.warn("Shader " + this.programFilename + "could not find sampler named " + s + " in the specified shader program.");
|
| 284 | this.shaderSamplers.remove(s);
|
| 285 | this.samplerNames.remove(j);
|
| 286 | --j;
|
| 287 | }
|
| 288 | else
|
| 289 | {
|
| 290 | this.shaderSamplerLocations.add(Integer.valueOf(k));
|
| 291 | }
|
| 292 |
|
| 293 | ++i;
|
| 294 | }
|
| 295 |
|
| 296 | for (ShaderUniform shaderuniform : this.shaderUniforms)
|
| 297 | {
|
| 298 | String s1 = shaderuniform.getShaderName();
|
| 299 | int l = OpenGlHelper.glGetUniformLocation(this.program, s1);
|
| 300 |
|
| 301 | if (l == -1)
|
| 302 | {
|
| 303 | logger.warn("Could not find uniform named " + s1 + " in the specified" + " shader program.");
|
| 304 | }
|
| 305 | else
|
| 306 | {
|
| 307 | this.shaderUniformLocations.add(Integer.valueOf(l));
|
| 308 | shaderuniform.setUniformLocation(l);
|
| 309 | this.mappedShaderUniforms.put(s1, shaderuniform);
|
| 310 | }
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | private void parseSampler(JsonElement p_147996_1_) throws JsonException
|
| 315 | {
|
| 316 | JsonObject jsonobject = JsonUtils.getJsonObject(p_147996_1_, "sampler");
|
| 317 | String s = JsonUtils.getString(jsonobject, "name");
|
| 318 |
|
| 319 | if (!JsonUtils.isString(jsonobject, "file"))
|
| 320 | {
|
| 321 | this.shaderSamplers.put(s, (Object)null);
|
| 322 | this.samplerNames.add(s);
|
| 323 | }
|
| 324 | else
|
| 325 | {
|
| 326 | this.samplerNames.add(s);
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | /**
|
| 331 | * adds a shader sampler texture. if it already exists, replaces it.
|
| 332 | */
|
| 333 | public void addSamplerTexture(String p_147992_1_, Object p_147992_2_)
|
| 334 | {
|
| 335 | if (this.shaderSamplers.containsKey(p_147992_1_))
|
| 336 | {
|
| 337 | this.shaderSamplers.remove(p_147992_1_);
|
| 338 | }
|
| 339 |
|
| 340 | this.shaderSamplers.put(p_147992_1_, p_147992_2_);
|
| 341 | this.markDirty();
|
| 342 | }
|
| 343 |
|
| 344 | private void parseUniform(JsonElement p_147987_1_) throws JsonException
|
| 345 | {
|
| 346 | JsonObject jsonobject = JsonUtils.getJsonObject(p_147987_1_, "uniform");
|
| 347 | String s = JsonUtils.getString(jsonobject, "name");
|
| 348 | int i = ShaderUniform.parseType(JsonUtils.getString(jsonobject, "type"));
|
| 349 | int j = JsonUtils.getInt(jsonobject, "count");
|
| 350 | float[] afloat = new float[Math.max(j, 16)];
|
| 351 | JsonArray jsonarray = JsonUtils.getJsonArray(jsonobject, "values");
|
| 352 |
|
| 353 | if (jsonarray.size() != j && jsonarray.size() > 1)
|
| 354 | {
|
| 355 | throw new JsonException("Invalid amount of values specified (expected " + j + ", found " + jsonarray.size() + ")");
|
| 356 | }
|
| 357 | else
|
| 358 | {
|
| 359 | int k = 0;
|
| 360 |
|
| 361 | for (JsonElement jsonelement : jsonarray)
|
| 362 | {
|
| 363 | try
|
| 364 | {
|
| 365 | afloat[k] = JsonUtils.getFloat(jsonelement, "value");
|
| 366 | }
|
| 367 | catch (Exception exception)
|
| 368 | {
|
| 369 | JsonException jsonexception = JsonException.func_151379_a(exception);
|
| 370 | jsonexception.func_151380_a("values[" + k + "]");
|
| 371 | throw jsonexception;
|
| 372 | }
|
| 373 |
|
| 374 | ++k;
|
| 375 | }
|
| 376 |
|
| 377 | if (j > 1 && jsonarray.size() == 1)
|
| 378 | {
|
| 379 | while (k < j)
|
| 380 | {
|
| 381 | afloat[k] = afloat[0];
|
| 382 | ++k;
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | int l = j > 1 && j <= 4 && i < 8 ? j - 1 : 0;
|
| 387 | ShaderUniform shaderuniform = new ShaderUniform(s, i + l, j, this);
|
| 388 |
|
| 389 | if (i <= 3)
|
| 390 | {
|
| 391 | shaderuniform.set((int)afloat[0], (int)afloat[1], (int)afloat[2], (int)afloat[3]);
|
| 392 | }
|
| 393 | else if (i <= 7)
|
| 394 | {
|
| 395 | shaderuniform.func_148092_b(afloat[0], afloat[1], afloat[2], afloat[3]);
|
| 396 | }
|
| 397 | else
|
| 398 | {
|
| 399 | shaderuniform.set(afloat);
|
| 400 | }
|
| 401 |
|
| 402 | this.shaderUniforms.add(shaderuniform);
|
| 403 | }
|
| 404 | }
|
| 405 |
|
| 406 | public ShaderLoader getVertexShaderLoader()
|
| 407 | {
|
| 408 | return this.vertexShaderLoader;
|
| 409 | }
|
| 410 |
|
| 411 | public ShaderLoader getFragmentShaderLoader()
|
| 412 | {
|
| 413 | return this.fragmentShaderLoader;
|
| 414 | }
|
| 415 |
|
| 416 | public int getProgram()
|
| 417 | {
|
| 418 | return this.program;
|
| 419 | }
|
| 420 | }
|
| 421 |
|