| 1 | package net.minecraft.client.shader;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.io.BufferedInputStream;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStream;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import java.util.Map;
|
| 9 | import net.minecraft.client.renderer.OpenGlHelper;
|
| 10 | import net.minecraft.client.resources.IResourceManager;
|
| 11 | import net.minecraft.client.util.JsonException;
|
| 12 | import net.minecraft.util.ResourceLocation;
|
| 13 | import org.apache.commons.io.IOUtils;
|
| 14 | import org.apache.commons.lang3.StringUtils;
|
| 15 | import org.lwjgl.BufferUtils;
|
| 16 |
|
| 17 | public class ShaderLoader
|
| 18 | {
|
| 19 | private final ShaderLoader.ShaderType shaderType;
|
| 20 | private final String shaderFilename;
|
| 21 | private int shader;
|
| 22 | private int shaderAttachCount = 0;
|
| 23 |
|
| 24 | private ShaderLoader(ShaderLoader.ShaderType type, int shaderId, String filename)
|
| 25 | {
|
| 26 | this.shaderType = type;
|
| 27 | this.shader = shaderId;
|
| 28 | this.shaderFilename = filename;
|
| 29 | }
|
| 30 |
|
| 31 | public void attachShader(ShaderManager manager)
|
| 32 | {
|
| 33 | ++this.shaderAttachCount;
|
| 34 | OpenGlHelper.glAttachShader(manager.getProgram(), this.shader);
|
| 35 | }
|
| 36 |
|
| 37 | public void deleteShader(ShaderManager manager)
|
| 38 | {
|
| 39 | --this.shaderAttachCount;
|
| 40 |
|
| 41 | if (this.shaderAttachCount <= 0)
|
| 42 | {
|
| 43 | OpenGlHelper.glDeleteShader(this.shader);
|
| 44 | this.shaderType.getLoadedShaders().remove(this.shaderFilename);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public String getShaderFilename()
|
| 49 | {
|
| 50 | return this.shaderFilename;
|
| 51 | }
|
| 52 |
|
| 53 | public static ShaderLoader loadShader(IResourceManager resourceManager, ShaderLoader.ShaderType type, String filename) throws IOException
|
| 54 | {
|
| 55 | ShaderLoader shaderloader = (ShaderLoader)type.getLoadedShaders().get(filename);
|
| 56 |
|
| 57 | if (shaderloader == null)
|
| 58 | {
|
| 59 | ResourceLocation resourcelocation = new ResourceLocation("shaders/program/" + filename + type.getShaderExtension());
|
| 60 | BufferedInputStream bufferedinputstream = new BufferedInputStream(resourceManager.getResource(resourcelocation).getInputStream());
|
| 61 | byte[] abyte = toByteArray(bufferedinputstream);
|
| 62 | ByteBuffer bytebuffer = BufferUtils.createByteBuffer(abyte.length);
|
| 63 | bytebuffer.put(abyte);
|
| 64 | bytebuffer.position(0);
|
| 65 | int i = OpenGlHelper.glCreateShader(type.getShaderMode());
|
| 66 | OpenGlHelper.glShaderSource(i, bytebuffer);
|
| 67 | OpenGlHelper.glCompileShader(i);
|
| 68 |
|
| 69 | if (OpenGlHelper.glGetShaderi(i, OpenGlHelper.GL_COMPILE_STATUS) == 0)
|
| 70 | {
|
| 71 | String s = StringUtils.trim(OpenGlHelper.glGetShaderInfoLog(i, 32768));
|
| 72 | JsonException jsonexception = new JsonException("Couldn\'t compile " + type.getShaderName() + " program: " + s);
|
| 73 | jsonexception.func_151381_b(resourcelocation.getResourcePath());
|
| 74 | throw jsonexception;
|
| 75 | }
|
| 76 |
|
| 77 | shaderloader = new ShaderLoader(type, i, filename);
|
| 78 | type.getLoadedShaders().put(filename, shaderloader);
|
| 79 | }
|
| 80 |
|
| 81 | return shaderloader;
|
| 82 | }
|
| 83 |
|
| 84 | protected static byte[] toByteArray(BufferedInputStream p_177064_0_) throws IOException
|
| 85 | {
|
| 86 | byte[] abyte;
|
| 87 |
|
| 88 | try
|
| 89 | {
|
| 90 | abyte = IOUtils.toByteArray((InputStream)p_177064_0_);
|
| 91 | }
|
| 92 | finally
|
| 93 | {
|
| 94 | p_177064_0_.close();
|
| 95 | }
|
| 96 |
|
| 97 | return abyte;
|
| 98 | }
|
| 99 |
|
| 100 | public static enum ShaderType
|
| 101 | {
|
| 102 | VERTEX("vertex", ".vsh", OpenGlHelper.GL_VERTEX_SHADER),
|
| 103 | FRAGMENT("fragment", ".fsh", OpenGlHelper.GL_FRAGMENT_SHADER);
|
| 104 |
|
| 105 | private final String shaderName;
|
| 106 | private final String shaderExtension;
|
| 107 | private final int shaderMode;
|
| 108 | private final Map<String, ShaderLoader> loadedShaders = Maps.<String, ShaderLoader>newHashMap();
|
| 109 |
|
| 110 | private ShaderType(String p_i45090_3_, String p_i45090_4_, int p_i45090_5_)
|
| 111 | {
|
| 112 | this.shaderName = p_i45090_3_;
|
| 113 | this.shaderExtension = p_i45090_4_;
|
| 114 | this.shaderMode = p_i45090_5_;
|
| 115 | }
|
| 116 |
|
| 117 | public String getShaderName()
|
| 118 | {
|
| 119 | return this.shaderName;
|
| 120 | }
|
| 121 |
|
| 122 | protected String getShaderExtension()
|
| 123 | {
|
| 124 | return this.shaderExtension;
|
| 125 | }
|
| 126 |
|
| 127 | protected int getShaderMode()
|
| 128 | {
|
| 129 | return this.shaderMode;
|
| 130 | }
|
| 131 |
|
| 132 | protected Map<String, ShaderLoader> getLoadedShaders()
|
| 133 | {
|
| 134 | return this.loadedShaders;
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|