rsSampler.cpp revision e0158410a2fedef43f5a2848c28393ad611e8cad
149fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert/*
249fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * Copyright (C) 2009 The Android Open Source Project
349fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert *
449fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
549fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * you may not use this file except in compliance with the License.
649fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * You may obtain a copy of the License at
749fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert *
849fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
949fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert *
1049fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
1149fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
1249fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1349fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * See the License for the specific language governing permissions and
1449fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert * limitations under the License.
1549fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert */
1649fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert
1749fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert#include <GLES/gl.h>
18848fa7a19abedc372452073abaf52780c7b6d78dAmith Yamasani#include <GLES/glext.h>
1949fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert
2049fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert#include "rsContext.h"
2149fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert#include "rsSampler.h"
2249fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert
23848fa7a19abedc372452073abaf52780c7b6d78dAmith Yamasani
2449fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringertusing namespace android;
25848fa7a19abedc372452073abaf52780c7b6d78dAmith Yamasaniusing namespace android::renderscript;
2693bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert
27848fa7a19abedc372452073abaf52780c7b6d78dAmith Yamasani
2869494b842a3f907164a457852c385f86dbe71d15Bjorn BringertSampler::Sampler()
2969494b842a3f907164a457852c385f86dbe71d15Bjorn Bringert{
3069494b842a3f907164a457852c385f86dbe71d15Bjorn Bringert    // Should not get called.
3169494b842a3f907164a457852c385f86dbe71d15Bjorn Bringert    rsAssert(0);
32848fa7a19abedc372452073abaf52780c7b6d78dAmith Yamasani}
3369494b842a3f907164a457852c385f86dbe71d15Bjorn Bringert
3469494b842a3f907164a457852c385f86dbe71d15Bjorn BringertSampler::Sampler(RsSamplerValue magFilter,
3569494b842a3f907164a457852c385f86dbe71d15Bjorn Bringert                 RsSamplerValue minFilter,
3669494b842a3f907164a457852c385f86dbe71d15Bjorn Bringert                 RsSamplerValue wrapS,
37848fa7a19abedc372452073abaf52780c7b6d78dAmith Yamasani                 RsSamplerValue wrapT,
3849fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert                 RsSamplerValue wrapR)
3949fd8e0994577badc6194c2c3b5f771f2b793fe4Bjorn Bringert{
40    mMagFilter = magFilter;
41    mMinFilter = minFilter;
42    mWrapS = wrapS;
43    mWrapT = wrapT;
44    mWrapR = wrapR;
45}
46
47Sampler::~Sampler()
48{
49}
50
51void Sampler::setupGL()
52{
53    GLenum trans[] = {
54        GL_NEAREST, //RS_SAMPLER_NEAREST,
55        GL_LINEAR, //RS_SAMPLER_LINEAR,
56        GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
57        GL_REPEAT, //RS_SAMPLER_WRAP,
58        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
59
60    };
61
62
63    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
64    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
65    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
66    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
67
68}
69
70void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
71{
72    ss->mSamplers[slot].set(this);
73    mBoundSlot = slot;
74}
75
76void Sampler::unbindFromContext(SamplerState *ss)
77{
78    int32_t slot = mBoundSlot;
79    mBoundSlot = -1;
80    ss->mSamplers[slot].clear();
81}
82
83void SamplerState::setupGL()
84{
85    for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
86        Sampler *s = mSamplers[ct].get();
87        if (s) {
88            s->setupGL();
89        } else {
90            glBindTexture(GL_TEXTURE_2D, 0);
91        }
92    }
93}
94
95////////////////////////////////
96
97namespace android {
98namespace renderscript {
99
100
101void rsi_SamplerBegin(Context *rsc)
102{
103    SamplerState * ss = &rsc->mStateSampler;
104
105    ss->mMagFilter = RS_SAMPLER_LINEAR;
106    ss->mMinFilter = RS_SAMPLER_LINEAR;
107    ss->mWrapS = RS_SAMPLER_WRAP;
108    ss->mWrapT = RS_SAMPLER_WRAP;
109    ss->mWrapR = RS_SAMPLER_WRAP;
110}
111
112void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
113{
114    SamplerState * ss = &rsc->mStateSampler;
115
116    switch(param) {
117    case RS_SAMPLER_MAG_FILTER:
118        ss->mMagFilter = value;
119        break;
120    case RS_SAMPLER_MIN_FILTER:
121        ss->mMinFilter = value;
122        break;
123    case RS_SAMPLER_WRAP_S:
124        ss->mWrapS = value;
125        break;
126    case RS_SAMPLER_WRAP_T:
127        ss->mWrapT = value;
128        break;
129    case RS_SAMPLER_WRAP_R:
130        ss->mWrapR = value;
131        break;
132    }
133
134}
135
136RsSampler rsi_SamplerCreate(Context *rsc)
137{
138    SamplerState * ss = &rsc->mStateSampler;
139
140
141    Sampler * s = new Sampler(ss->mMagFilter,
142                              ss->mMinFilter,
143                              ss->mWrapS,
144                              ss->mWrapT,
145                              ss->mWrapR);
146    s->incRef();
147    return s;
148}
149
150
151}}
152