Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nv30/nv30_state.c
4574 views
/*1* Copyright 2012 Red Hat Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21* Authors: Ben Skeggs22*23*/2425#include "util/format/u_format.h"26#include "util/u_helpers.h"27#include "util/u_inlines.h"2829#include "nouveau_gldefs.h"30#include "nv_object.xml.h"31#include "nv30/nv30-40_3d.xml.h"32#include "nv30/nv30_context.h"33#include "nv30/nv30_winsys.h"3435#define NV40_3D_MRT_BLEND_ENABLE 0x0000036c3637static void *38nv30_blend_state_create(struct pipe_context *pipe,39const struct pipe_blend_state *cso)40{41struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;42struct nv30_blend_stateobj *so;43uint32_t blend[2], cmask[2];44int i;4546so = CALLOC_STRUCT(nv30_blend_stateobj);47if (!so)48return NULL;49so->pipe = *cso;5051if (cso->logicop_enable) {52SB_MTHD30(so, COLOR_LOGIC_OP_ENABLE, 2);53SB_DATA (so, 1);54SB_DATA (so, nvgl_logicop_func(cso->logicop_func));55} else {56SB_MTHD30(so, COLOR_LOGIC_OP_ENABLE, 1);57SB_DATA (so, 0);58}5960SB_MTHD30(so, DITHER_ENABLE, 1);61SB_DATA (so, cso->dither);6263blend[0] = cso->rt[0].blend_enable;64cmask[0] = !!(cso->rt[0].colormask & PIPE_MASK_A) << 24 |65!!(cso->rt[0].colormask & PIPE_MASK_R) << 16 |66!!(cso->rt[0].colormask & PIPE_MASK_G) << 8 |67!!(cso->rt[0].colormask & PIPE_MASK_B);68if (cso->independent_blend_enable) {69blend[1] = 0;70cmask[1] = 0;71for (i = 1; i < 4; i++) {72blend[1] |= cso->rt[i].blend_enable << i;73cmask[1] |= !!(cso->rt[i].colormask & PIPE_MASK_A) << (0 + (i * 4)) |74!!(cso->rt[i].colormask & PIPE_MASK_R) << (1 + (i * 4)) |75!!(cso->rt[i].colormask & PIPE_MASK_G) << (2 + (i * 4)) |76!!(cso->rt[i].colormask & PIPE_MASK_B) << (3 + (i * 4));77}78} else {79blend[1] = 0x0000000e * (blend[0] & 0x00000001);80cmask[1] = 0x00001110 * !!(cmask[0] & 0x01000000);81cmask[1] |= 0x00002220 * !!(cmask[0] & 0x00010000);82cmask[1] |= 0x00004440 * !!(cmask[0] & 0x00000100);83cmask[1] |= 0x00008880 * !!(cmask[0] & 0x00000001);84}8586if (eng3d->oclass >= NV40_3D_CLASS) {87SB_MTHD40(so, MRT_BLEND_ENABLE, 2);88SB_DATA (so, blend[1]);89SB_DATA (so, cmask[1]);90}9192if (blend[0] || blend[1]) {93SB_MTHD30(so, BLEND_FUNC_ENABLE, 3);94SB_DATA (so, blend[0]);95SB_DATA (so, (nvgl_blend_func(cso->rt[0].alpha_src_factor) << 16) |96nvgl_blend_func(cso->rt[0].rgb_src_factor));97SB_DATA (so, (nvgl_blend_func(cso->rt[0].alpha_dst_factor) << 16) |98nvgl_blend_func(cso->rt[0].rgb_dst_factor));99if (eng3d->oclass < NV40_3D_CLASS) {100SB_MTHD30(so, BLEND_EQUATION, 1);101SB_DATA (so, nvgl_blend_eqn(cso->rt[0].rgb_func));102} else {103SB_MTHD40(so, BLEND_EQUATION, 1);104SB_DATA (so, (nvgl_blend_eqn(cso->rt[0].alpha_func) << 16) |105nvgl_blend_eqn(cso->rt[0].rgb_func));106}107} else {108SB_MTHD30(so, BLEND_FUNC_ENABLE, 1);109SB_DATA (so, blend[0]);110}111112SB_MTHD30(so, COLOR_MASK, 1);113SB_DATA (so, cmask[0]);114return so;115}116117static void118nv30_blend_state_bind(struct pipe_context *pipe, void *hwcso)119{120struct nv30_context *nv30 = nv30_context(pipe);121122nv30->blend = hwcso;123nv30->dirty |= NV30_NEW_BLEND;124}125126static void127nv30_blend_state_delete(struct pipe_context *pipe, void *hwcso)128{129FREE(hwcso);130}131132static void *133nv30_rasterizer_state_create(struct pipe_context *pipe,134const struct pipe_rasterizer_state *cso)135{136struct nv30_rasterizer_stateobj *so;137138so = CALLOC_STRUCT(nv30_rasterizer_stateobj);139if (!so)140return NULL;141so->pipe = *cso;142143SB_MTHD30(so, SHADE_MODEL, 1);144SB_DATA (so, cso->flatshade ? NV30_3D_SHADE_MODEL_FLAT :145NV30_3D_SHADE_MODEL_SMOOTH);146147SB_MTHD30(so, POLYGON_MODE_FRONT, 6);148SB_DATA (so, nvgl_polygon_mode(cso->fill_front));149SB_DATA (so, nvgl_polygon_mode(cso->fill_back));150if (cso->cull_face == PIPE_FACE_FRONT_AND_BACK)151SB_DATA (so, NV30_3D_CULL_FACE_FRONT_AND_BACK);152else153if (cso->cull_face == PIPE_FACE_FRONT)154SB_DATA (so, NV30_3D_CULL_FACE_FRONT);155else156SB_DATA (so, NV30_3D_CULL_FACE_BACK);157SB_DATA (so, cso->front_ccw ? NV30_3D_FRONT_FACE_CCW :158NV30_3D_FRONT_FACE_CW);159SB_DATA (so, cso->poly_smooth);160SB_DATA (so, cso->cull_face != PIPE_FACE_NONE);161162SB_MTHD30(so, POLYGON_OFFSET_POINT_ENABLE, 3);163SB_DATA (so, cso->offset_point);164SB_DATA (so, cso->offset_line);165SB_DATA (so, cso->offset_tri);166if (cso->offset_point || cso->offset_line || cso->offset_tri) {167SB_MTHD30(so, POLYGON_OFFSET_FACTOR, 2);168SB_DATA (so, fui(cso->offset_scale));169SB_DATA (so, fui(cso->offset_units * 2.0));170}171172SB_MTHD30(so, LINE_WIDTH, 2);173SB_DATA (so, (unsigned char)(cso->line_width * 8.0) & 0xff);174SB_DATA (so, cso->line_smooth);175SB_MTHD30(so, LINE_STIPPLE_ENABLE, 2);176SB_DATA (so, cso->line_stipple_enable);177SB_DATA (so, (cso->line_stipple_pattern << 16) |178cso->line_stipple_factor);179180SB_MTHD30(so, VERTEX_TWO_SIDE_ENABLE, 1);181SB_DATA (so, cso->light_twoside);182SB_MTHD30(so, POLYGON_STIPPLE_ENABLE, 1);183SB_DATA (so, cso->poly_stipple_enable);184SB_MTHD30(so, POINT_SIZE, 1);185SB_DATA (so, fui(cso->point_size));186SB_MTHD30(so, FLATSHADE_FIRST, 1);187SB_DATA (so, cso->flatshade_first);188189SB_MTHD30(so, DEPTH_CONTROL, 1);190SB_DATA (so, cso->depth_clip_near ? 0x00000001 : 0x00000010);191return so;192}193194static void195nv30_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)196{197struct nv30_context *nv30 = nv30_context(pipe);198199nv30->rast = hwcso;200nv30->dirty |= NV30_NEW_RASTERIZER;201}202203static void204nv30_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)205{206FREE(hwcso);207}208209static void *210nv30_zsa_state_create(struct pipe_context *pipe,211const struct pipe_depth_stencil_alpha_state *cso)212{213struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;214struct nv30_zsa_stateobj *so;215216so = CALLOC_STRUCT(nv30_zsa_stateobj);217if (!so)218return NULL;219so->pipe = *cso;220221SB_MTHD30(so, DEPTH_FUNC, 3);222SB_DATA (so, nvgl_comparison_op(cso->depth_func));223SB_DATA (so, cso->depth_writemask);224SB_DATA (so, cso->depth_enabled);225226if (eng3d->oclass == NV35_3D_CLASS || eng3d->oclass >= NV40_3D_CLASS) {227SB_MTHD35(so, DEPTH_BOUNDS_TEST_ENABLE, 3);228SB_DATA (so, cso->depth_bounds_test);229SB_DATA (so, fui(cso->depth_bounds_min));230SB_DATA (so, fui(cso->depth_bounds_max));231}232233if (cso->stencil[0].enabled) {234SB_MTHD30(so, STENCIL_ENABLE(0), 3);235SB_DATA (so, 1);236SB_DATA (so, cso->stencil[0].writemask);237SB_DATA (so, nvgl_comparison_op(cso->stencil[0].func));238SB_MTHD30(so, STENCIL_FUNC_MASK(0), 4);239SB_DATA (so, cso->stencil[0].valuemask);240SB_DATA (so, nvgl_stencil_op(cso->stencil[0].fail_op));241SB_DATA (so, nvgl_stencil_op(cso->stencil[0].zfail_op));242SB_DATA (so, nvgl_stencil_op(cso->stencil[0].zpass_op));243} else {244SB_MTHD30(so, STENCIL_ENABLE(0), 2);245SB_DATA (so, 0);246SB_DATA (so, 0x000000ff);247}248249if (cso->stencil[1].enabled) {250SB_MTHD30(so, STENCIL_ENABLE(1), 3);251SB_DATA (so, 1);252SB_DATA (so, cso->stencil[1].writemask);253SB_DATA (so, nvgl_comparison_op(cso->stencil[1].func));254SB_MTHD30(so, STENCIL_FUNC_MASK(1), 4);255SB_DATA (so, cso->stencil[1].valuemask);256SB_DATA (so, nvgl_stencil_op(cso->stencil[1].fail_op));257SB_DATA (so, nvgl_stencil_op(cso->stencil[1].zfail_op));258SB_DATA (so, nvgl_stencil_op(cso->stencil[1].zpass_op));259} else {260SB_MTHD30(so, STENCIL_ENABLE(1), 1);261SB_DATA (so, 0);262}263264SB_MTHD30(so, ALPHA_FUNC_ENABLE, 3);265SB_DATA (so, cso->alpha_enabled ? 1 : 0);266SB_DATA (so, nvgl_comparison_op(cso->alpha_func));267SB_DATA (so, float_to_ubyte(cso->alpha_ref_value));268269return so;270}271272static void273nv30_zsa_state_bind(struct pipe_context *pipe, void *hwcso)274{275struct nv30_context *nv30 = nv30_context(pipe);276277nv30->zsa = hwcso;278nv30->dirty |= NV30_NEW_ZSA;279}280281static void282nv30_zsa_state_delete(struct pipe_context *pipe, void *hwcso)283{284FREE(hwcso);285}286287static void288nv30_set_blend_color(struct pipe_context *pipe,289const struct pipe_blend_color *bcol)290{291struct nv30_context *nv30 = nv30_context(pipe);292293nv30->blend_colour = *bcol;294nv30->dirty |= NV30_NEW_BLEND_COLOUR;295}296297static void298nv30_set_stencil_ref(struct pipe_context *pipe,299const struct pipe_stencil_ref sr)300{301struct nv30_context *nv30 = nv30_context(pipe);302303nv30->stencil_ref = sr;304nv30->dirty |= NV30_NEW_STENCIL_REF;305}306307static void308nv30_set_clip_state(struct pipe_context *pipe,309const struct pipe_clip_state *clip)310{311struct nv30_context *nv30 = nv30_context(pipe);312313memcpy(nv30->clip.ucp, clip->ucp, sizeof(clip->ucp));314315nv30->dirty |= NV30_NEW_CLIP;316}317318static void319nv30_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)320{321struct nv30_context *nv30 = nv30_context(pipe);322323nv30->sample_mask = sample_mask;324nv30->dirty |= NV30_NEW_SAMPLE_MASK;325}326327static void328nv30_set_constant_buffer(struct pipe_context *pipe,329enum pipe_shader_type shader, uint index,330bool pass_reference,331const struct pipe_constant_buffer *cb)332{333struct nv30_context *nv30 = nv30_context(pipe);334struct pipe_resource *buf = cb ? cb->buffer : NULL;335unsigned size;336337if (cb && cb->user_buffer) {338buf = nouveau_user_buffer_create(pipe->screen, (void*)cb->user_buffer,339cb->buffer_size,340PIPE_BIND_CONSTANT_BUFFER);341}342343size = 0;344if (buf)345size = buf->width0 / (4 * sizeof(float));346347if (shader == PIPE_SHADER_VERTEX) {348if (pass_reference) {349pipe_resource_reference(&nv30->vertprog.constbuf, NULL);350nv30->vertprog.constbuf = buf;351} else {352pipe_resource_reference(&nv30->vertprog.constbuf, buf);353}354nv30->vertprog.constbuf_nr = size;355nv30->dirty |= NV30_NEW_VERTCONST;356} else357if (shader == PIPE_SHADER_FRAGMENT) {358if (pass_reference) {359pipe_resource_reference(&nv30->fragprog.constbuf, NULL);360nv30->fragprog.constbuf = buf;361} else {362pipe_resource_reference(&nv30->fragprog.constbuf, buf);363}364nv30->fragprog.constbuf_nr = size;365nv30->dirty |= NV30_NEW_FRAGCONST;366}367368if (cb && cb->user_buffer) {369pipe_resource_reference(&buf, NULL);370}371}372373static void374nv30_set_framebuffer_state(struct pipe_context *pipe,375const struct pipe_framebuffer_state *fb)376{377struct nv30_context *nv30 = nv30_context(pipe);378379nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FB);380381nv30->framebuffer = *fb;382nv30->dirty |= NV30_NEW_FRAMEBUFFER;383384/* Hardware can't handle different swizzled-ness or different blocksizes385* for zs and cbufs. If both are supplied and something doesn't match,386* blank out the zs for now so that at least *some* rendering can occur.387*/388if (fb->nr_cbufs > 0 && fb->zsbuf) {389struct nv30_miptree *color_mt = nv30_miptree(fb->cbufs[0]->texture);390struct nv30_miptree *zeta_mt = nv30_miptree(fb->zsbuf->texture);391392if (color_mt->swizzled != zeta_mt->swizzled ||393(color_mt->swizzled &&394(util_format_get_blocksize(fb->zsbuf->format) > 2) !=395(util_format_get_blocksize(fb->cbufs[0]->format) > 2))) {396nv30->framebuffer.zsbuf = NULL;397debug_printf("Mismatched color and zeta formats, ignoring zeta.\n");398}399}400}401402static void403nv30_set_polygon_stipple(struct pipe_context *pipe,404const struct pipe_poly_stipple *stipple)405{406struct nv30_context *nv30 = nv30_context(pipe);407408nv30->stipple = *stipple;409nv30->dirty |= NV30_NEW_STIPPLE;410}411412static void413nv30_set_scissor_states(struct pipe_context *pipe,414unsigned start_slot,415unsigned num_viewports,416const struct pipe_scissor_state *scissor)417{418struct nv30_context *nv30 = nv30_context(pipe);419420nv30->scissor = *scissor;421nv30->dirty |= NV30_NEW_SCISSOR;422}423424static void425nv30_set_viewport_states(struct pipe_context *pipe,426unsigned start_slot,427unsigned num_viewports,428const struct pipe_viewport_state *vpt)429{430struct nv30_context *nv30 = nv30_context(pipe);431432nv30->viewport = *vpt;433nv30->dirty |= NV30_NEW_VIEWPORT;434}435436static void437nv30_set_vertex_buffers(struct pipe_context *pipe,438unsigned start_slot, unsigned count,439unsigned unbind_num_trailing_slots,440bool take_ownership,441const struct pipe_vertex_buffer *vb)442{443struct nv30_context *nv30 = nv30_context(pipe);444445nouveau_bufctx_reset(nv30->bufctx, BUFCTX_VTXBUF);446447util_set_vertex_buffers_count(nv30->vtxbuf, &nv30->num_vtxbufs,448vb, start_slot, count,449unbind_num_trailing_slots,450take_ownership);451452nv30->dirty |= NV30_NEW_ARRAYS;453}454455void456nv30_state_init(struct pipe_context *pipe)457{458pipe->create_blend_state = nv30_blend_state_create;459pipe->bind_blend_state = nv30_blend_state_bind;460pipe->delete_blend_state = nv30_blend_state_delete;461462pipe->create_rasterizer_state = nv30_rasterizer_state_create;463pipe->bind_rasterizer_state = nv30_rasterizer_state_bind;464pipe->delete_rasterizer_state = nv30_rasterizer_state_delete;465466pipe->create_depth_stencil_alpha_state = nv30_zsa_state_create;467pipe->bind_depth_stencil_alpha_state = nv30_zsa_state_bind;468pipe->delete_depth_stencil_alpha_state = nv30_zsa_state_delete;469470pipe->set_blend_color = nv30_set_blend_color;471pipe->set_stencil_ref = nv30_set_stencil_ref;472pipe->set_clip_state = nv30_set_clip_state;473pipe->set_sample_mask = nv30_set_sample_mask;474pipe->set_constant_buffer = nv30_set_constant_buffer;475pipe->set_framebuffer_state = nv30_set_framebuffer_state;476pipe->set_polygon_stipple = nv30_set_polygon_stipple;477pipe->set_scissor_states = nv30_set_scissor_states;478pipe->set_viewport_states = nv30_set_viewport_states;479480pipe->set_vertex_buffers = nv30_set_vertex_buffers;481}482483484