Program.h revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
15cbbce535744b89df5ecea95de21ee3733298260Romain Guy/*
25cbbce535744b89df5ecea95de21ee3733298260Romain Guy * Copyright (C) 2010 The Android Open Source Project
35cbbce535744b89df5ecea95de21ee3733298260Romain Guy *
45cbbce535744b89df5ecea95de21ee3733298260Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
55cbbce535744b89df5ecea95de21ee3733298260Romain Guy * you may not use this file except in compliance with the License.
65cbbce535744b89df5ecea95de21ee3733298260Romain Guy * You may obtain a copy of the License at
75cbbce535744b89df5ecea95de21ee3733298260Romain Guy *
85cbbce535744b89df5ecea95de21ee3733298260Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
95cbbce535744b89df5ecea95de21ee3733298260Romain Guy *
105cbbce535744b89df5ecea95de21ee3733298260Romain Guy * Unless required by applicable law or agreed to in writing, software
115cbbce535744b89df5ecea95de21ee3733298260Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
125cbbce535744b89df5ecea95de21ee3733298260Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135cbbce535744b89df5ecea95de21ee3733298260Romain Guy * See the License for the specific language governing permissions and
145cbbce535744b89df5ecea95de21ee3733298260Romain Guy * limitations under the License.
155cbbce535744b89df5ecea95de21ee3733298260Romain Guy */
165cbbce535744b89df5ecea95de21ee3733298260Romain Guy
175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#ifndef ANDROID_HWUI_PROGRAM_H
185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#define ANDROID_HWUI_PROGRAM_H
195cbbce535744b89df5ecea95de21ee3733298260Romain Guy
205cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <GLES2/gl2.h>
215cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <GLES2/gl2ext.h>
225cbbce535744b89df5ecea95de21ee3733298260Romain Guy
235cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <utils/KeyedVector.h>
245cbbce535744b89df5ecea95de21ee3733298260Romain Guy
250b9db91c3dc8007b47c8fd4fb9dd85be97201a88Romain Guy#include "Matrix.h"
260b9db91c3dc8007b47c8fd4fb9dd85be97201a88Romain Guy
275cbbce535744b89df5ecea95de21ee3733298260Romain Guynamespace android {
285cbbce535744b89df5ecea95de21ee3733298260Romain Guynamespace uirenderer {
295cbbce535744b89df5ecea95de21ee3733298260Romain Guy
305cbbce535744b89df5ecea95de21ee3733298260Romain Guy/**
315cbbce535744b89df5ecea95de21ee3733298260Romain Guy * A program holds a vertex and a fragment shader. It offers several utility
325cbbce535744b89df5ecea95de21ee3733298260Romain Guy * methods to query attributes and uniforms.
335cbbce535744b89df5ecea95de21ee3733298260Romain Guy */
34889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guyclass Program {
355cbbce535744b89df5ecea95de21ee3733298260Romain Guypublic:
365cbbce535744b89df5ecea95de21ee3733298260Romain Guy    /**
375cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * Creates a new program with the specified vertex and fragment
385cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * shaders sources.
395cbbce535744b89df5ecea95de21ee3733298260Romain Guy     */
405cbbce535744b89df5ecea95de21ee3733298260Romain Guy    Program(const char* vertex, const char* fragment);
416926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    virtual ~Program();
425cbbce535744b89df5ecea95de21ee3733298260Romain Guy
435cbbce535744b89df5ecea95de21ee3733298260Romain Guy    /**
445cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * Binds this program to the GL context.
455cbbce535744b89df5ecea95de21ee3733298260Romain Guy     */
466926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    virtual void use();
475cbbce535744b89df5ecea95de21ee3733298260Romain Guy
48260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    /**
49260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy     * Marks this program as unused. This will not unbind
50260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy     * the program from the GL context.
51260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy     */
526926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    virtual void remove();
53260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
54260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    /**
55ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy     * Returns the OpenGL name of the specified attribute.
56ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy     */
57ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    int getAttrib(const char* name);
58ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy
59ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    /**
60ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy     * Returns the OpenGL name of the specified uniform.
61ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy     */
62ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    int getUniform(const char* name);
63ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy
64ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    /**
65260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy     * Indicates whether this program is currently in use with
66260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy     * the GL context.
67260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy     */
68260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    inline bool isInUse() const {
69260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy        return mUse;
70260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    }
71260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
72889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    /**
73889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     * Binds the program with the specified projection, modelView and
74889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     * transform matrices.
75889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     */
76889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
77889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy             const mat4& transformMatrix);
78889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy
79889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    /**
80707b2f78ccaa09965d7e030fda3a883ce9b75ea8Romain Guy     * Sets the color associated with this shader.
81889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     */
82707b2f78ccaa09965d7e030fda3a883ce9b75ea8Romain Guy    void setColor(const float r, const float g, const float b, const float a);
83889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy
84889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    /**
85707b2f78ccaa09965d7e030fda3a883ce9b75ea8Romain Guy     * Name of the position attribute.
86889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     */
87707b2f78ccaa09965d7e030fda3a883ce9b75ea8Romain Guy    int position;
88889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy
89889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    /**
90889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     * Name of the transform uniform.
91889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy     */
92889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    int transform;
93889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy
945cbbce535744b89df5ecea95de21ee3733298260Romain Guyprotected:
955cbbce535744b89df5ecea95de21ee3733298260Romain Guy    /**
965cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * Adds an attribute with the specified name.
975cbbce535744b89df5ecea95de21ee3733298260Romain Guy     *
985cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * @return The OpenGL name of the attribute.
995cbbce535744b89df5ecea95de21ee3733298260Romain Guy     */
1005cbbce535744b89df5ecea95de21ee3733298260Romain Guy    int addAttrib(const char* name);
1015cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1025cbbce535744b89df5ecea95de21ee3733298260Romain Guy    /**
1035cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * Adds a uniform with the specified name.
1045cbbce535744b89df5ecea95de21ee3733298260Romain Guy     *
1055cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * @return The OpenGL name of the uniform.
1065cbbce535744b89df5ecea95de21ee3733298260Romain Guy     */
1075cbbce535744b89df5ecea95de21ee3733298260Romain Guy    int addUniform(const char* name);
1085cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1095cbbce535744b89df5ecea95de21ee3733298260Romain Guyprivate:
1105cbbce535744b89df5ecea95de21ee3733298260Romain Guy    /**
1115cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * Compiles the specified shader of the specified type.
1125cbbce535744b89df5ecea95de21ee3733298260Romain Guy     *
1135cbbce535744b89df5ecea95de21ee3733298260Romain Guy     * @return The name of the compiled shader.
1145cbbce535744b89df5ecea95de21ee3733298260Romain Guy     */
1155cbbce535744b89df5ecea95de21ee3733298260Romain Guy    GLuint buildShader(const char* source, GLenum type);
1165cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1175cbbce535744b89df5ecea95de21ee3733298260Romain Guy    // Name of the OpenGL program
1185cbbce535744b89df5ecea95de21ee3733298260Romain Guy    GLuint id;
1195cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1205cbbce535744b89df5ecea95de21ee3733298260Romain Guy    // Name of the shaders
1215cbbce535744b89df5ecea95de21ee3733298260Romain Guy    GLuint vertexShader;
1225cbbce535744b89df5ecea95de21ee3733298260Romain Guy    GLuint fragmentShader;
1235cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1245cbbce535744b89df5ecea95de21ee3733298260Romain Guy    // Keeps track of attributes and uniforms slots
1255cbbce535744b89df5ecea95de21ee3733298260Romain Guy    KeyedVector<const char*, int> attributes;
1265cbbce535744b89df5ecea95de21ee3733298260Romain Guy    KeyedVector<const char*, int> uniforms;
127260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
128260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    bool mUse;
1295cbbce535744b89df5ecea95de21ee3733298260Romain Guy}; // class Program
1305cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1315cbbce535744b89df5ecea95de21ee3733298260Romain Guy}; // namespace uirenderer
1325cbbce535744b89df5ecea95de21ee3733298260Romain Guy}; // namespace android
1335cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_PROGRAM_H
135