18feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams/*
28feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * Copyright (C) 2011 The Android Open Source Project
38feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams *
48feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
58feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * you may not use this file except in compliance with the License.
68feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * You may obtain a copy of the License at
78feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams *
88feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
98feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams *
108feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * Unless required by applicable law or agreed to in writing, software
118feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
128feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * See the License for the specific language governing permissions and
148feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams * limitations under the License.
158feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams */
168feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
178feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
188feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams#include "rsdCore.h"
198feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams#include "rsdProgramStore.h"
208feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
218feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams#include "rsContext.h"
228feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams#include "rsProgramStore.h"
238feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
248feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams#include <GLES/gl.h>
258feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams#include <GLES/glext.h>
268feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
278feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
288feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsusing namespace android;
298feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsusing namespace android::renderscript;
308feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
318feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsstruct DrvProgramStore {
328feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    GLenum blendSrc;
338feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    GLenum blendDst;
348feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    bool blendEnable;
358feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
368feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    GLenum depthFunc;
378feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    bool depthTestEnable;
388feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams};
398feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
408feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsbool rsdProgramStoreInit(const Context *rsc, const ProgramStore *ps) {
418feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    DrvProgramStore *drv = (DrvProgramStore *)calloc(1, sizeof(DrvProgramStore));
428feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    if (drv == NULL) {
438feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        return false;
448feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
458feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
468feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    ps->mHal.drv = drv;
478feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    drv->depthTestEnable = true;
488feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
498feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    switch (ps->mHal.state.depthFunc) {
508feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_ALWAYS:
518feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthTestEnable = false;
528feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_ALWAYS;
538feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
548feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_LESS:
558feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_LESS;
568feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
578feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_LEQUAL:
588feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_LEQUAL;
598feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
608feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_GREATER:
618feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_GREATER;
628feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
638feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_GEQUAL:
648feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_GEQUAL;
658feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
668feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_EQUAL:
678feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_EQUAL;
688feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
698feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_DEPTH_FUNC_NOTEQUAL:
708feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->depthFunc = GL_NOTEQUAL;
718feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
728feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    default:
73af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Unknown depth function.");
748feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        goto error;
758feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
768feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
778feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
788feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
798feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    drv->blendEnable = true;
808feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    if ((ps->mHal.state.blendSrc == RS_BLEND_SRC_ONE) &&
818feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        (ps->mHal.state.blendDst == RS_BLEND_DST_ZERO)) {
828feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendEnable = false;
838feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
848feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
858feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    switch (ps->mHal.state.blendSrc) {
868feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_ZERO:
878feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_ZERO;
888feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
898feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_ONE:
908feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_ONE;
918feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
928feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_DST_COLOR:
938feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_DST_COLOR;
948feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
958feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
968feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_ONE_MINUS_DST_COLOR;
978feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
988feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_SRC_ALPHA:
998feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_SRC_ALPHA;
1008feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1018feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
1028feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_ONE_MINUS_SRC_ALPHA;
1038feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1048feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_DST_ALPHA:
1058feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_DST_ALPHA;
1068feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1078feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
1088feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_ONE_MINUS_DST_ALPHA;
1098feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1108feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
1118feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendSrc = GL_SRC_ALPHA_SATURATE;
1128feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1138feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    default:
114c0a65425f8eb3573b1abd2a48292ca953c0a8590Alex Sakhartchouk        rsc->setError(RS_ERROR_FATAL_DRIVER, "Unknown blend src mode.");
1158feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        goto error;
1168feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
1178feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1188feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    switch (ps->mHal.state.blendDst) {
1198feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_ZERO:
1208feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_ZERO;
1218feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1228feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_ONE:
1238feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_ONE;
1248feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1258feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_SRC_COLOR:
1268feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_SRC_COLOR;
1278feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1288feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
1298feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_ONE_MINUS_SRC_COLOR;
1308feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1318feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_SRC_ALPHA:
1328feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_SRC_ALPHA;
1338feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1348feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
1358feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_ONE_MINUS_SRC_ALPHA;
1368feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1378feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_DST_ALPHA:
1388feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_DST_ALPHA;
1398feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1408feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
1418feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        drv->blendDst = GL_ONE_MINUS_DST_ALPHA;
1428feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        break;
1438feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    default:
144c0a65425f8eb3573b1abd2a48292ca953c0a8590Alex Sakhartchouk        rsc->setError(RS_ERROR_FATAL_DRIVER, "Unknown blend dst mode.");
1458feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        goto error;
1468feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
1478feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1488feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    return true;
1498feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1508feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samserror:
1518feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    free(drv);
1528feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    ps->mHal.drv = NULL;
1538feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    return false;
1548feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams}
1558feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1568feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsvoid rsdProgramStoreSetActive(const Context *rsc, const ProgramStore *ps) {
1578feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    DrvProgramStore *drv = (DrvProgramStore *)ps->mHal.drv;
1588feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1592382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glColorMask, ps->mHal.state.colorRWriteEnable,
1608feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams                ps->mHal.state.colorGWriteEnable,
1618feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams                ps->mHal.state.colorBWriteEnable,
1628feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams                ps->mHal.state.colorAWriteEnable);
1638feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1648feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    if (drv->blendEnable) {
1652382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glEnable, GL_BLEND);
1662382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glBlendFunc, drv->blendSrc, drv->blendDst);
1678feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    } else {
1682382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glDisable, GL_BLEND);
1698feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
1708feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1718feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    if (rsc->mUserSurfaceConfig.depthMin > 0) {
1722382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glDepthMask, ps->mHal.state.depthWriteEnable);
1738feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        if (drv->depthTestEnable || ps->mHal.state.depthWriteEnable) {
1742382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            RSD_CALL_GL(glEnable, GL_DEPTH_TEST);
1752382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            RSD_CALL_GL(glDepthFunc, drv->depthFunc);
1768feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        } else {
1772382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
1788feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        }
1798feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    } else {
1802382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glDepthMask, false);
1812382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
1828feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
1838feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1848feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    /*
1858feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    if (rsc->mUserSurfaceConfig.stencilMin > 0) {
1868feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    } else {
1878feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        glStencilMask(0);
1888feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams        glDisable(GL_STENCIL_TEST);
1898feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
1908feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    */
1918feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1928feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    if (ps->mHal.state.ditherEnable) {
1932382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glEnable, GL_DITHER);
1948feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    } else {
1952382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glDisable, GL_DITHER);
1968feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    }
1978feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams}
1988feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
1998feea4e0dec48ea03bd6d32706d058b86dddc5baJason Samsvoid rsdProgramStoreDestroy(const Context *rsc, const ProgramStore *ps) {
2008feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    free(ps->mHal.drv);
2018feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams    ps->mHal.drv = NULL;
2028feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams}
2038feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
2048feea4e0dec48ea03bd6d32706d058b86dddc5baJason Sams
205